#Force function callI
1 messages · Page 1 of 1 (latest)
Hi @grizzled summit. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To force a function call in a chat model using the Python library, you can use the tool_choice argument in the chat_completion_request function. You can specify the function you want the model to use by providing a dictionary with the type set to "function" and the function set to another dictionary with the name of the function you want to use.
Here is an example from the OpenAI Cookbook (https://cookbook.openai.com/examples/how_to_call_functions_with_chat_models#forcing-the-use-of-specific-functions-or-no-function):
messages = []
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."})
messages.append({"role": "user", "content": "Give me a weather report for Toronto, Canada."})
chat_response = chat_completion_request(
messages, tools=tools, tool_choice={"type": "function", "function": {"name": "get_n_day_weather_forecast"}}
)
chat_response.choices[0].message
In this example, the model is forced to use the get_n_day_weather_forecast function. Please replace get_n_day_weather_forecast with the function you want to use.
Please note that forcing a function call makes the model make assumptions about how to use it. If you don't want the model to use a function, you can set tool_choice to "none".
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@grizzled summit kapa.ai is still learning and improving, please let us know how it did by reacting below
@frosty tundra 1. That would force usage of the weather function, can I just force usage of any function?
2. What other tools are available / what does tools represent?
Let me check the knowledge base for you. One moment! :books:
- Yes, you can force the usage of any function that you have defined and included in the
toolsparameter. The function you want to force should be specified in thetool_choiceparameter. For example, if you have a function namedget_current_weather, you can force its usage like this:
tool_choice={"type": "function", "function": {"name": "get_current_weather"}}
Remember to replace "get_current_weather" with the name of the function you want to use.
- The
toolsparameter is used to provide function specifications to the model. Currently, only functions are supported as a tool. Each tool is an object that describes a function the model can call. It includes the function's name, description, and parameters. Here's an example from the OpenAI Cookbook (https://cookbook.openai.com/examples/how_to_build_an_agent_with_the_node_sdk#describing-our-functions-for-openai):
tools = [
{
type: "function",
function: {
name: "getCurrentWeather",
description: "Get the current weather in a given location",
parameters: {
type: "object",
properties: {
latitude: {
type: "string",
},
longitude: {
type: "string",
},
},
required: ["longitude", "latitude"],
},
}
},
{
type: "function",
function: {
name: "getLocation",
description: "Get the user's location based on their IP address",
parameters: {
type: "object",
properties: {},
},
}
},
];
In this example, two functions are defined: getCurrentWeather and getLocation. The model can generate JSON inputs for these functions based on their descriptions and parameters.
Please note that the API will not actually execute any function calls. It's up to you to execute function calls using model outputs.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@grizzled summit kapa.ai is still learning and improving, please let us know how it did by reacting below
@frosty tundra That's not what I meant. Imagine, I have multiple functions. I want the model to use ANY function, I don't care wich one. I just don't want it to answer normal text
Let me check the knowledge base for you. One moment! :books:
To force the model to use any function and not generate normal text, you can set the tool_choice parameter to "function". However, you cannot specify "any" function without specifying a particular function name. The tool_choice parameter requires a specific function name to be provided.
Here's an example:
tool_choice={"type": "function", "function": {"name": "specific_function_name"}}
In this example, replace "specific_function_name" with the name of one of the functions you have defined.
If you want the model to choose among multiple functions, you would need to make multiple requests, each time specifying a different function in the tool_choice parameter.
Please note that the model will not generate normal text when a function is specified in tool_choice. It will generate a stringified JSON object that adheres to your custom schema for the specified function. You will need to parse this string into JSON in your code and call your function with the provided arguments.
For more information, you can refer to the OpenAI documentation on function calling (https://platform.openai.com/docs/guides/function-calling).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@grizzled summit kapa.ai is still learning and improving, please let us know how it did by reacting below
That's a shame
@frosty tundra does the gpt 4 vision model support function calling?
Let me check the knowledge base for you. One moment! :books:
Based on the information provided in the extracts, the gpt-4-vision-preview model does not explicitly mention support for function calling. The documentation on function calling (https://platform.openai.com/docs/guides/function-calling) lists the models that support function calling, and gpt-4-vision-preview is not included in that list.
However, it's important to note that the gpt-4-vision-preview model is a version of GPT-4 that has been extended to understand images, in addition to all other GPT-4 Turbo capabilities. It's possible that it may support function calling if it shares the same capabilities as other GPT-4 models, but this is not explicitly stated in the provided extracts.