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 = [ |
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:
{ |
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( |
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( |
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.