Spaces:
Sleeping
Sleeping
File size: 6,435 Bytes
980dc8d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
---
title: "Function Calling"
original_url: "https://tds.s-anand.net/#/function-calling?id=function-calling-with-openai"
downloaded_at: "2025-06-08T23:26:04.973121"
---
[Function Calling with OpenAI](#/function-calling?id=function-calling-with-openai)
----------------------------------------------------------------------------------
[Function Calling](https://platform.openai.com/docs/guides/function-calling) allows Large Language Models to convert natural language into structured function calls. This is perfect for building chatbots and AI assistants that need to interact with your backend systems.
OpenAI supports [Function Calling](https://platform.openai.com/docs/guides/function-calling) – a way for LLMs to suggest what functions to call and how.
[](https://youtu.be/aqdWSYWC_LI)
Here’s a minimal example using Python and OpenAI’s function calling that identifies the weather in a given location.
```
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "httpx",
# ]
# ///
import httpx
import os
from typing import Dict, Any
def query_gpt(user_input: str, tools: list[Dict[str, Any]]) -> Dict[str, Any]:
response = httpx.post(
"https://api.openai.com/v1/chat/completions",
headers={
"Authorization": f"Bearer {os.getenv('OPENAI_API_KEY')}",
"Content-Type": "application/json",
},
json={
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": user_input}],
"tools": tools,
"tool_choice": "auto",
},
)
return response.json()["choices"][0]["message"]
WEATHER_TOOL = {
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name or coordinates"}
},
"required": ["location"],
"additionalProperties": False,
},
"strict": True,
},
}
if __name__ == "__main__":
response = query_gpt("What is the weather in San Francisco?", [WEATHER_TOOL])
print([tool_call["function"] for tool_call in response["tool_calls"]])Copy to clipboardErrorCopied
```
### [How to define functions](#/function-calling?id=how-to-define-functions)
The function definition is a [JSON schema](https://json-schema.org/) with a few OpenAI specific properties.
See the [Supported schemas](https://platform.openai.com/docs/guides/structured-outputs#supported-schemas).
Here’s an example of a function definition for scheduling a meeting:
```
MEETING_TOOL = {
"type": "function",
"function": {
"name": "schedule_meeting",
"description": "Schedule a meeting room for a specific date and time",
"parameters": {
"type": "object",
"properties": {
"date": {
"type": "string",
"description": "Meeting date in YYYY-MM-DD format"
},
"time": {
"type": "string",
"description": "Meeting time in HH:MM format"
},
"meeting_room": {
"type": "string",
"description": "Name of the meeting room"
}
},
"required": ["date", "time", "meeting_room"],
"additionalProperties": False
},
"strict": True
}
}Copy to clipboardErrorCopied
```
### [How to define multiple functions](#/function-calling?id=how-to-define-multiple-functions)
You can define multiple functions by passing a list of function definitions to the `tools` parameter.
Here’s an example of a list of function definitions for handling employee expenses and calculating performance bonuses:
```
tools = [
{
"type": "function",
"function": {
"name": "get_expense_balance",
"description": "Get expense balance for an employee",
"parameters": {
"type": "object",
"properties": {
"employee_id": {
"type": "integer",
"description": "Employee ID number"
}
},
"required": ["employee_id"],
"additionalProperties": False
},
"strict": True
}
},
{
"type": "function",
"function": {
"name": "calculate_performance_bonus",
"description": "Calculate yearly performance bonus for an employee",
"parameters": {
"type": "object",
"properties": {
"employee_id": {
"type": "integer",
"description": "Employee ID number"
},
"current_year": {
"type": "integer",
"description": "Year to calculate bonus for"
}
},
"required": ["employee_id", "current_year"],
"additionalProperties": False
},
"strict": True
}
}
]Copy to clipboardErrorCopied
```
Best Practices:
1. **Use Strict Mode**
* Always set `strict: True` to ensure valid function calls
* Define all required parameters
* Set `additionalProperties: False`
2. **Use tool choice**
* Set `tool_choice: "required"` to ensure that the model will always call one or more tools
* The default is `tool_choice: "auto"` which means the model will choose a tool only if appropriate
3. **Clear Descriptions**
* Write detailed function and parameter descriptions
* Include expected formats and units
* Mention any constraints or limitations
4. **Error Handling**
* Validate function inputs before execution
* Return clear error messages
* Handle missing or invalid parameters
[Previous
Hybrid RAG with TypeSense](#/hybrid-rag-typesense)
[Next
LLM Agents](#/llm-agents) |