#Function Calling with OpenAI Finetuned Model
1 messages · Page 1 of 1 (latest)
Hi @zenith radish. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Function calling in the OpenAI API is a feature that allows you to describe functions and have the model intelligently choose to output a JSON object containing arguments to call one or many functions. The Chat Completions API does not call the function; instead, the model generates JSON that you can use to call the function in your code [source (https://help.openai.com/en/articles/8555517-function-calling-in-the-openai-api)].
The latest models, such as gpt-3.5-turbo-0125 and gpt-4-turbo-preview, have been trained to both detect when a function should be called (depending on the input) and to respond with JSON that adheres to the function signature more closely than previous models [source (https://platform.openai.com/docs/guides/function-calling)].
To use function calling with a fine-tuned model, you would typically follow these steps:
- Call the model with the user query and a set of functions defined in the functions parameter.
- The model can choose to call one or more functions; if so, the content will be a stringified JSON object adhering to your custom schema.
- Parse the string into JSON in your code, and call your function with the provided arguments if they exist.
- Call the model again by appending the function response as a new message, and let the model summarize the results back to the user [source (https://platform.openai.com/docs/guides/function-calling)].
Here's an example of how you might define a function and its associated JSON schema:
def generate_functions(labels: dict) -> list:
return [
{
"type": "function",
"function": {
"name": "enrich_entities",
"description": "Enrich Text with Knowledge Base Links",
"parameters": {
"type": "object",
"properties": {
"r'^(?:' + '|'.join({labels}) + ')$'":
{
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": False
},
}
}
]
[source (https://cookbook.openai.com/examples/named_entity_recognition_to_enrich_text#4-chatcompletion)]