OpenAI introduced a structure specification for defining the interface between functions/plugins that an LLM can execute. This specification is becoming a standard, enabling LLM systems to provide actionable responses.
A function definition allows an LLM to interpret and format its response as a function call.
Here we are providing an example of an LLM API call that includes tools and a function definition.
Code Example: first_function.py (API call)
response = client.chat.completions.create(
model="gpt-4-1106-preview",
messages=[{"role": "system",
"content": "You are a helpful assistant."},
{"role": "user", "content": user_message}],
temperature=0.7,
tools=[ # 1
{
"type": "function", # 2
"function": {
"name": "recommend",
"description": "Provide a recommendation for a topic.", # 3
"parameters": {
"type": "object", # 4
"properties": {
"topic": {
"type": "string",
"description": "The topic to get a recommendation for.", # 5
},
"rating": {
"type": "string",
"description": "The rating to be given.", # 5
"enum": ["good", "bad", "terrible"], # 6
},
},
"required": ["topic"],
},
},
}
]
)
Explanation
To test this, open Visual Studio Code (VS Code) and set up the environment by installing dependencies from requirements.txt.
The following example demonstrates calling the function with different user inputs.
Code Example: first_function.py (exercising the API)
user = "Can you please recommend me a time travel movie?"
response = ask_chatgpt(user) # 1
print(response)
# Output
Function(arguments='{"topic":"time travel movie"}', name='recommend') # 2
user = "Can you please recommend me a good time travel movie?"
response = ask_chatgpt(user) # 3
print(response)
# Output
Function(arguments='{"topic":"time travel movie", "rating":"good"}', name='recommend') # 4Note: The actual function isn’t being executed; the LLM only suggests the function with relevant input parameters. You must extract and pass these into a function for execution.
Since an LLM does not directly execute functions or plugins, we need to integrate a separate execution layer. The following example demonstrates calling a recommendation function.
def recommend(topic, rating="good"):
    if "time travel" in topic.lower(): # 1
        return json.dumps({"topic": "time travel",
                           "recommendation": "Back to the Future",
                           "rating": rating})
    elif "recipe" in topic.lower(): # 1
        return json.dumps({"topic": "recipe",
                           "recommendation": "The best thing I ever ate.",
                           "rating": rating})
    elif "gift" in topic.lower(): # 1
        return json.dumps({"topic": "gift",
                           "recommendation": "A glorious new watch.",
                           "rating": rating})
    else: # 2
        return json.dumps({"topic": topic,
                           "recommendation": "unknown"}) # 3Explanation
Now, let’s see how to construct a request for multiple recommendations.
user = """Can you please make recommendations for the following:
Time travel movies
Recipes
Gifts"""Â # 1
messages = [{"role": "user", "content": user}]Â # 2
tools = [Â # 3
    {
        "type": "function",
        "function": {
            "name": "recommend",
            "description": "Provide a recommendation for any topic.",
            "parameters": {
                "type": "object",
                "properties": {
                    "topic": {
                        "type": "string",
                        "description": "The topic to get a recommendation for.",
                    },
                    "rating": {
                        "type": "string",
                        "description": "The rating given.",
                        "enum": ["good", "bad", "terrible"]
                    },
                },
                "required": ["topic"],
            },
        },
    }
]Explanation
After making the request, the API will return multiple function call outputs.
tool_calls = response_message.tool_calls # 1
if tool_calls:Â # 1
    available_functions = {
        "recommend": recommend,
    } # 2
    for tool_call in tool_calls: # 3
        function_name = tool_call.function.name
        function_to_call = available_functions[function_name]
        function_args = json.loads(tool_call.function.arguments)
        function_response = function_to_call(
            topic=function_args.get("topic"), # 4
            rating=function_args.get("rating"),
        )
        messages.append( # 5
            {
                "tool_call_id": tool_call.id,
                "role": "tool",
                "name": function_name,
                "content": function_response,
            }
        )
    second_response = client.chat.completions.create( # 6
        model="gpt-3.5-turbo-1106",
        messages=messages,
    )
    return second_response.choices[0].message.content # 6Explanation