#Function Calling with OpenAI Finetuned Model

1 messages · Page 1 of 1 (latest)

zenith radish
#

Can you explain the concept of function calling to me?
I have finetuned the gpt-3 model and wwould like to get the structured Json output every time.

woeful stoneBOT
#

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:

  1. Call the model with the user query and a set of functions defined in the functions parameter.
  2. 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.
  3. Parse the string into JSON in your code, and call your function with the provided arguments if they exist.
  4. 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)]

Please note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Therefore, it's important to validate the arguments in your code before calling your function [source (https://platform.openai.com/docs/api-reference)].

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: