# Function Calling — Tool Use & External Tool Integration Source: https://ofox.ai/docs/develop/guides/function-calling Function Calling allows models to automatically select and invoke your predefined tool functions based on user needs, enabling data queries, API calls, task execution, and more. ## Core Concepts The complete Function Calling flow: 1. **Define tools** — Describe available functions and parameters in the request 2. **Model decides** — The model determines whether to call a tool 3. **Return call** — The model returns the function name and arguments 4. **Execute function** — You execute the function and get the result 5. **Continue conversation** — Send the result back to the model to generate the final response ## OpenAI Protocol **Python** ```python filename="function_calling.py" showLineNumbers from openai import OpenAI import json client = OpenAI( base_url="https://api.ofox.ai/v1", api_key="" ) # 1. Define tools tools = [{ "type": "function", "function": { "name": "get_weather", "description": "Get real-time weather information for a given city", "parameters": { "type": "object", "properties": { "city": { "type": "string", "description": "City name, e.g. New York, London" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "Temperature unit" } }, "required": ["city"] } } }] # 2. Send request messages = [{"role": "user", "content": "What's the weather like in San Francisco today?"}] response = client.chat.completions.create( model="openai/gpt-6.1-sol", messages=messages, tools=tools, tool_choice="auto" ) message = response.choices[0].message # 3. Handle tool calls if message.tool_calls: for tool_call in message.tool_calls: args = json.loads(tool_call.function.arguments) # 4. Execute your function result = get_weather(args["city"]) # Your own implementation # 5. Send the result back to the model messages.append(message) messages.append({ "role": "tool", "tool_call_id": tool_call.id, "content": json.dumps(result) }) # Get the final response final = client.chat.completions.create( model="openai/gpt-6.1-sol", messages=messages, tools=tools ) print(final.choices[0].message.content) ``` **TypeScript** ```typescript filename="function_calling.ts" showLineNumbers import OpenAI from 'openai' const client = new OpenAI({ baseURL: 'https://api.ofox.ai/v1', apiKey: '' }) // 1. Define tools const tools: OpenAI.ChatCompletionTool[] = [{ type: 'function', function: { name: 'get_weather', description: 'Get real-time weather information for a given city', parameters: { type: 'object', properties: { city: { type: 'string', description: 'City name' }, unit: { type: 'string', enum: ['celsius', 'fahrenheit'] } }, required: ['city'] } } }] // 2. Send request const messages: OpenAI.ChatCompletionMessageParam[] = [ { role: 'user', content: "What's the weather like in San Francisco today?" } ] const response = await client.chat.completions.create({ model: 'openai/gpt-6.1-sol', messages, tools, tool_choice: 'auto' }) const message = response.choices[0].message // 3. Handle tool calls if (message.tool_calls) { messages.push(message) for (const toolCall of message.tool_calls) { const args = JSON.parse(toolCall.function.arguments) const result = await getWeather(args.city) // Your own implementation messages.push({ role: 'tool', tool_call_id: toolCall.id, content: JSON.stringify(result) }) } const final = await client.chat.completions.create({ model: 'openai/gpt-6.1-sol', messages, tools }) console.log(final.choices[0].message.content) } ``` ## Anthropic Protocol Anthropic uses the `tools` parameter with a slightly different format: ```python filename="anthropic_tools.py" showLineNumbers import anthropic client = anthropic.Anthropic( base_url="https://api.ofox.ai/anthropic", api_key="" ) response = client.messages.create( model="anthropic/claude-sonnet-5.5", max_tokens=1024, tools=[{ "name": "get_weather", "description": "Get real-time weather information for a given city", "input_schema": { "type": "object", "properties": { "city": {"type": "string", "description": "City name"} }, "required": ["city"] } }], messages=[{"role": "user", "content": "What's the weather like in San Francisco today?"}] ) # Handle tool_use content block for block in response.content: if block.type == "tool_use": print(f"Calling tool: {block.name}, args: {block.input}") ``` ## Parallel Function Calling The model can return multiple tool calls in a single response. You should execute them in parallel: ```python # The model may request multiple tool calls at once if message.tool_calls: # Execute all tool calls in parallel import asyncio async def execute_tools(tool_calls): tasks = [] for tc in tool_calls: args = json.loads(tc.function.arguments) tasks.append(execute_function(tc.function.name, args)) return await asyncio.gather(*tasks) ``` ## tool_choice Parameter | Value | Description | |-------|-------------| | `"auto"` | Model automatically decides whether to call tools (default) | | `"none"` | Disable tool calling | | `"required"` | Force tool calling | | `{"type": "function", "function": {"name": "xxx"}}` | Force calling a specific tool | ## Supported Models The following models support Function Calling: - OpenAI: `openai/gpt-6.1-sol`, `openai/gpt-6-luna` - Anthropic: `anthropic/claude-opus-5.5`, `anthropic/claude-sonnet-5.5`, `anthropic/claude-haiku-5.5` - Google: `google/gemini-3.8-flash`, `google/gemini-3.5-flash-lite`, `google/gemini-3.1-pro-preview` - Others: `deepseek/deepseek-v4.1-flash`, `qwen/qwen3.7-max`, `moonshotai/kimi-k3`