How do I implement function calling in Python?
Setting up with OpenAI's API
First, install the OpenAI Python library and set your API key. Then, define your functions in a list of dictionaries, each with 'name', 'description', and 'parameters' (a JSON schema).
When calling the chat completions endpoint, pass the tools parameter. The model may respond with a tool call, which you can access in the response.choices[0].message.tool_calls.
- Install openai: pip install openai
- Set API key: openai.api_key = 'your-key'
- Define tools list with function schemas.
- Call openai.chat.completions.create with tools=tools.
- Check response for tool_calls.
Executing the function and returning results
When you receive a tool call, extract the function name and arguments (as JSON). Call your actual Python function with those arguments. Then, append the function's result to the conversation as a message with role 'tool' and the tool_call_id.
Send the updated conversation back to the model. It will then generate a final answer incorporating the tool result. You may need to loop if the model calls multiple tools.
Example code snippet
Here's a simplified example: define a function get_weather(city) that returns a string. Define the tool schema. Call the API, handle the tool call, execute get_weather, and send the result back.
Remember to handle errors and validate arguments. The exact code varies by API, but the pattern is similar for Anthropic, Google, etc.
- Define function: def get_weather(city): return f'Weather in {city} is sunny.'
- Tool schema: {'type': 'function', 'function': {'name': 'get_weather', 'parameters': {...}}}
- API call: response = client.chat.completions.create(model='gpt-4', messages=messages, tools=tools)
- Parse tool call: tool_call = response.choices[0].message.tool_calls[0]
- Execute and append result: messages.append({'role': 'tool', 'tool_call_id': tool_call.id, 'content': result})
Common mistakes
- Not including the tool_call_id when returning results, causing the model to lose context.
- Forgetting to convert the arguments from JSON string to Python dictionary before calling the function.
- Assuming the model will always call a tool; it might respond directly, so handle both cases.