You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adjust the semantic conventions to support parallel tool calling like tool id
Parallel function calling
Parallel function calling is the model's ability to perform multiple function calls together, allowing the effects and results of these function calls to be resolved in parallel. This is especially useful if functions take a long time, and reduces round trips with the API. For example, the model may call functions to get the weather in 3 different locations at the same time, which will result in a message with 3 function calls in the tool_calls array, each with an id. To respond to these function calls, add 3 new messages to the conversation, each containing the result of one function call, with a tool_call_id referencing the id from tool_calls.
In this example, we define a single function get_current_weather. The model calls the function multiple times, and after sending the function response back to the model, we let it decide the next step. It responded with a user-facing message which was telling the user the temperature in San Francisco, Tokyo, and Paris. Depending on the query, it may choose to call a function again.
If you want to force the model to call a specific function you can do so by setting tool_choice with a specific function name. You can also force the model to generate a user-facing message by setting tool_choice: "none". Note that the default behavior (tool_choice: "auto") is for the model to decide on its own whether to call a function and if so which function to call.
fromopenaiimportOpenAIimportjsonclient=OpenAI()
# Example dummy function hard coded to return the same weather# In production, this could be your backend API or an external APIdefget_current_weather(location, unit="fahrenheit"):
"""Get the current weather in a given location"""if"tokyo"inlocation.lower():
returnjson.dumps({"location": "Tokyo", "temperature": "10", "unit": unit})
elif"san francisco"inlocation.lower():
returnjson.dumps({"location": "San Francisco", "temperature": "72", "unit": unit})
elif"paris"inlocation.lower():
returnjson.dumps({"location": "Paris", "temperature": "22", "unit": unit})
else:
returnjson.dumps({"location": location, "temperature": "unknown"})
defrun_conversation():
# Step 1: send the conversation and available functions to the modelmessages= [{"role": "user", "content": "What's the weather like in San Francisco, Tokyo, and Paris?"}]
tools= [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
},
}
]
response=client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=messages,
tools=tools,
tool_choice="auto", # auto is default, but we'll be explicit
)
response_message=response.choices[0].messagetool_calls=response_message.tool_calls# Step 2: check if the model wanted to call a functioniftool_calls:
# Step 3: call the function# Note: the JSON response may not always be valid; be sure to handle errorsavailable_functions= {
"get_current_weather": get_current_weather,
} # only one function in this example, but you can have multiplemessages.append(response_message) # extend conversation with assistant's reply# Step 4: send the info for each function call and function response to the modelfortool_callintool_calls:
function_name=tool_call.function.namefunction_to_call=available_functions[function_name]
function_args=json.loads(tool_call.function.arguments)
function_response=function_to_call(
location=function_args.get("location"),
unit=function_args.get("unit"),
)
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
}
) # extend conversation with function responsesecond_response=client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=messages,
) # get a new response from the model where it can see the function responsereturnsecond_responseprint(run_conversation())
The text was updated successfully, but these errors were encountered:
Adjust the semantic conventions to support parallel tool calling like tool id
The text was updated successfully, but these errors were encountered: