Choose from multiple functions using openai function calling


In a conversational interface, using OpenAI’s Chat Completions API with function calling abilities can give you a sophisticated system. Not only does it allow natural language exchanges but also provides options to interact with external APIs or databases. Let’s dive deeper into how you can use system role messages to guide the conversation and manage multiple functions effectively.

Last time, we talked about how to use function calling with openai here
Today we see how to choose from multiple functions with function calling.

Function Specifications

Start by defining function specifications that your bot can access. These might be interfaces to a weather API, a database, or any other service. Here’s a sample code snippet in Python:

functions = [
{
"name": "get_current_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"},
"format": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location", "format"]
},
},
{
"name": "get_n_day_weather_forecast",
"description": "Get an N-day weather forecast",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"},
"format": {"type": "string", "enum": ["celsius", "fahrenheit"]},
"num_days": {"type": "integer"}
},
"required": ["location", "format", "num_days"]
},
},
]

Using System Role Messages

The system role is used to guide the assistant’s behavior throughout the conversation. For example, you can specify:

messages.append({"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."})

This instructs the assistant to be cautious and avoid making assumptions, ensuring it will ask for clarifications whenever needed.

Handling Clarifications

Suppose a user asks, “What’s the weather like today?”. Given the system role message, the assistant knows it should not assume the location or temperature format. Instead, it prompts the user:

{'role': 'assistant', 'content': 'In which city and state would you like to know the current weather?'}

Selecting Among Functions

The assistant will choose the appropriate function based on the context. For example, if the user wants a weather forecast for the next few days, it may choose get_n_day_weather_forecast. The selected function and its arguments will appear in the JSON response like so:

{
'role': 'assistant',
'content': None,
'function_call': {
'name': 'get_n_day_weather_forecast',
'arguments': '{"location": "Glasgow, Scotland", "format": "celsius", "num_days": 5}'
}
}

Forcing Specific Functions

To force the assistant to use a particular function, include a function_call argument in your API request:

chat_response = chat_completion_request(
messages, functions=functions, function_call={"name": "get_n_day_weather_forecast"}
)

Be cautious when doing this, as it may lead the assistant to make assumptions.

Disabling Function Calls

If you want to disable function calls for a specific user query, set the function_call argument to “none”:

chat_response = chat_completion_request(
messages, functions=functions, function_call="none"
)

Wrapping Up

By thoughtfully using system role messages and specifying function requirements, you can design an advanced conversational interface that handles complex queries and interactions with external services.


Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !
  TOC