Function-Calling Tools: The Full Workflow
1. Function-Calling Tools: The Full Workflow
With our currency_exchange() function defined,2. Our Function
we can return to the function-calling tool workflow to chain it all together.3. Function-Calling Tools
Recall that this requires two requests to the Responses API: the first to see if a tool call is required, and what the arguments for that call are, and the second to send the function's results to the model to make it aware of the tool's output. Both of these requests require us to define what tools are available for the model, and these tool definitions are where we'll start.4. Tool Definitions: An LLM's Manual
Tool definitions are like a manual for the LLM so it can choose which tool to use and call it correctly. They are written as a list of dictionaries, and each function-calling tool in the list must have a "type" key set to "function", a name, and a description to help the model understand the tool. Next, we create a "parameters" key to specify the types and definitions of each tool parameter. Because this function takes multiple values that we'll eventually provide using a dictionary, we set this "type" key to "object". Then, under the "properties" key, we create a key for each tool parameter containing its data type and description. Like the function description, clear parameter descriptions are important for the LLM to correctly parse natural language user inputs into tool arguments. Finally, we add two more keys under the "parameters" dictionary that specifies that all three parameters are required, and not to allow additional parameters to be passed to the tool. This may seem verbose, but this degree of specificity makes tool calls much more reliable. With the function and tools list defined, we can test if the model will use this tool correctly.5. Using the Tools
Let's send a test message to the model and loop over the output items to confirm it triggers a function call. We can see an item with type "function_call" and name "convert_currency", so the model correctly requested our function. Notice also that the model correctly inferred the arguments to pass to the function from the user's input: an amount of 100 from currency USD, to currency euro. These arguments are formatted as a JSON string, which is essentially a string containing a dictionary inside it. We'll use those arguments to call the function.6. Using the Tools: The First Request
To start the tool-calling workflow, we'll create a messages list of dictionaries. As well as accepting strings, the Responses API's input parameter also accepts these more structured role-based messages. We'll send this user input to the model with our tools list and append the output items to the messages list.7. Using the Tools: The Second Request
Then, we loop over the response items, and check for function calls matching our convert_currency() function. If we find one, we use **json.loads() function from the json module, to convert the JSON string of arguments we saw earlier into a dictionary, then unpack this dictionary so the arguments can be passed to the convert_currency() function. Now that we have the tool's output, we will add it to the messages list, so the model can consider everything at once. We do this by appending to the messages dictionary with the function_call_output message type. We also convert the currency result back to a JSON string with json.dumps(). Finally, we can send the updated messages back to the model to respond to the user's input in natural language.8. Using the Tools: The Result
And there we have it! Remember, function-calling tools are only limited by your imagination. With that,9. Let's practice!
time to try this out!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.