Benefit to Keeping Function Call Request in Chat History

Function calling has been working great for months now - really happy with the results. I’m doing some refactoring, and wondering:

Is there any benefit towards keeping the original function_call in the chat history?

My current workflow just skips over the “assistant” and related { arguments }, and just adds the response from the function call in the next message, but I notice in the example here in the OpenAI docs that it the “ask_database” function call and its arguments are there in the flow of the chat.

For example, my current implementation of the example in the docs would look like this:

e[31msystem: Answer user questions by generating SQL queries against the Chinook Music Database.
e[0m
e[32muser: Hi, who are the top 5 artists by number of tracks?
e[0m
e[35mfunction (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]
e[0m
e[32muser: What is the name of the album with the most tracks?
e[0m
e[35mfunction (ask_database): [('Greatest Hits', 57)]
e[0m

Does this work just as well as having the arguments in there? Thanks!

Functions and the AI can iterate.

If you had anything more complex, that might return an error the AI could improve on, or where the AI would not know how it received the return value, than yes, a properly formatted “assistant” message showing the function emitted should be included.

That “chat history” after the user input should continue to grow until the user has been answered, and also, if it is retrieving knowledge, simple inclusion in following turns will prevent the AI from calling again upon the function to answer the similar question.

2 Likes

Thank you so much for this.

To be clear then, the request for tools part of the conversation should be an array of tools used that follows this format?

[    
        "role": "assistant",
        "content": "[
              {
               "id": "call_mKRPFMrzRHRonu7v0ra", 
                "type": "function", 
                "function": "{ 
                      "name": "find_stuff", 
                      "arguments": "{
                              "categories":["fruits"],
                              "location": "Downtown L.A."
                        }"
                 }
               }
           ],
    ],

The above works for my current implementation, but want to make sure if I’m missing anything else that should be in it.

As this topic has a selected solution, can we close it?

You should also be ready to handle and return parallel tool calls.

In another post, I wrote a demonstration of the messages used for each role. It has a tool specification, and also what the second call back to an AI needs to receive in addition. You can run that script to get the AI calling tools, to work with the object methods of python, or just see the json.

Your code for handling - constructing the message contents instead of showing - will be more impenetrable…