Assistance Needed: Storing conversation history with the new "tools" system in place

Hello team,

I’m looking for a way to store the history of a conversation that uses the new multi-function “tools” system instead of the old “function” system.
I can properly re-inject the history and answer functions with the python client but it involves adding the “ChatCompletionMessageToolCall” object from the answer directly to the messages.
If I want to stringify it or just manually add a message of the type { ‘role’ : ‘tool’, … } I don’t know what to insert here.
No problem for responses to multiple functions, only for the assistant message (with finishReason == ‘tool_calls’ and content == None).

Thank you very much if you can help!
Have a nice day.

Hi and welcome to the Developer Forum!

You can’t modify existing thread message structures, you can only build a new one and make the required modifications by selectively taking the old threads messages and introducing new ones/making edits prior to adding them.

This may get added as functionality in the future, but for now it’s a fixed structure once created.

If you just want to add new messages to the chain, I don’t see an issue though.

Thank you for your reply but that’s exactly what I do, you can add the python object directly or simply add a list of type { ‘role’ : ‘…’, ‘content’ : ‘…’ }.
For example, I add this :

                    messages.append( {
                        role' : 'tool',
                        tool_call_id' : toolCall.id,
                        content' : s
                    } )

And it works just fine in the thread and in response to GPT.

My problem comes from the previous message. The one sent by GPT as an assistant for calling one or more functions.
I add the python object, which works fine, but if I want to save this object to rebuild the history later, I don’t know how to get the corresponding “list” (to have it in JSON then in str…).

In other words, how does the python client transform the “ChatCompletionMessageToolCall” object and pass it to the API?

Ok, I’ve found :slight_smile:

                    messages.append( {
                        'role'       : 'assistant',
                        'tool_calls' : [ {
                            'id'       : toolCall.id,
                            'type'     : 'function',
                            'function' : {
                                'name'      : toolCall.function.name,
                                'arguments' : toolCall.function.arguments
                            }
                        } ]
                    } )
3 Likes

How do I get at your level :raised_hands::eyes:

1 Like

Hey there, am I understand correctly that you suggest resulting messages array should look something like this?

{'role': 'system', 'content': 'You are helpful assistant'},
{'role': 'user', 'content': 'What's the weather in Denver today?'},
{'role': 'assistant', 'tool_calls': [{'id': call_HpcW0BZsrXjkcFWyPf3xqujC, 'type': 'function', 'function': {'name': 'get_weather', 'arguments': 'location'}}]}

Trying to figure out a way to correctly store function calls in conversation myself and can’t figure it out.

OpenAI cookbook shows different example, though not sure that I understand it correctly:

messages.append({"role": "tool", "tool_call_id": assistant_message["tool_calls"][0]['id'], "name": assistant_message["tool_calls"][0]["function"]["name"], "content": results})

Yes, that’s right, except that “arguments” is a list.
(not to be confused, of course, when GPT requests a function and the response to the function).