Function Calling
Last updated
Was this helpful?
Was this helpful?
from openai import OpenAI
import json
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.tensorx.ai/v1"
)
# Define your tools
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name, e.g. London, UK"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit"
}
},
"required": ["location"]
}
}
}
]
# Make the request
response = client.chat.completions.create(
model="deepseek/deepseek-chat-v3.1",
messages=[
{"role": "user", "content": "What's the weather in Tokyo?"}
],
tools=tools
)
# Check if model wants to call a function
message = response.choices[0].message
if message.tool_calls:
tool_call = message.tool_calls[0]
print(f"Function: {tool_call.function.name}")
print(f"Arguments: {tool_call.function.arguments}")import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'YOUR_API_KEY',
baseURL: 'https://api.tensorx.ai/v1'
});
const tools = [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get the current weather for a location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'City name, e.g. London, UK'
},
unit: {
type: 'string',
enum: ['celsius', 'fahrenheit'],
description: 'Temperature unit'
}
},
required: ['location']
}
}
}
];
const response = await client.chat.completions.create({
model: 'deepseek/deepseek-chat-v3.1',
messages: [
{ role: 'user', content: "What's the weather in Tokyo?" }
],
tools
});
const message = response.choices[0].message;
if (message.tool_calls) {
const toolCall = message.tool_calls[0];
console.log(`Function: ${toolCall.function.name}`);
console.log(`Arguments: ${toolCall.function.arguments}`);
}curl https://api.tensorx.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "deepseek/deepseek-chat-v3.1",
"messages": [
{"role": "user", "content": "What'\''s the weather in Tokyo?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name, e.g. London, UK"
}
},
"required": ["location"]
}
}
}
]
}'{
"choices": [
{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"location\":\"Tokyo, Japan\",\"unit\":\"celsius\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
]
}import json
# Step 1: Send user message with tools
response = client.chat.completions.create(
model="deepseek/deepseek-chat-v3.1",
messages=[
{"role": "user", "content": "What's the weather in Paris?"}
],
tools=tools
)
message = response.choices[0].message
# Step 2: Check if model wants to call functions
if message.tool_calls:
# Step 3: Execute the function(s)
tool_call = message.tool_calls[0]
function_name = tool_call.function.name
arguments = json.loads(tool_call.function.arguments)
# Your actual function implementation
if function_name == "get_weather":
result = get_weather(arguments["location"]) # Your code
# Step 4: Send the result back
response = client.chat.completions.create(
model="deepseek/deepseek-chat-v3.1",
messages=[
{"role": "user", "content": "What's the weather in Paris?"},
message, # Include the assistant's tool_calls message
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result)
}
],
tools=tools
)
# Step 5: Get the final response
print(response.choices[0].message.content)# Let model decide (default)
tool_choice="auto"
# Never call functions
tool_choice="none"
# Must call a function
tool_choice="required"
# Force a specific function
tool_choice={"type": "function", "function": {"name": "get_weather"}}{
"tool_calls": [
{"id": "call_1", "function": {"name": "get_weather", "arguments": "{\"location\":\"Paris\"}"}},
{"id": "call_2", "function": {"name": "get_weather", "arguments": "{\"location\":\"London\"}"}}
]
}messages = [original_user_message, assistant_message]
for tool_call in message.tool_calls:
result = execute_function(tool_call.function.name, tool_call.function.arguments)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result)
})
response = client.chat.completions.create(
model="deepseek/deepseek-chat-v3.1",
messages=messages,
tools=tools
){
"type": "function",
"function": {
"name": "web_search",
"description": "Search the web for current information",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query"
}
},
"required": ["query"]
}
}
}{
"type": "function",
"function": {
"name": "calculate",
"description": "Perform mathematical calculations",
"parameters": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "Math expression, e.g. '2+2', 'sqrt(16)', '15% of 200'"
}
},
"required": ["expression"]
}
}
}{
"type": "function",
"function": {
"name": "query_database",
"description": "Query the product database",
"parameters": {
"type": "object",
"properties": {
"table": {
"type": "string",
"enum": ["products", "orders", "customers"]
},
"filters": {
"type": "object",
"description": "Key-value pairs to filter by"
},
"limit": {
"type": "integer",
"description": "Maximum results to return"
}
},
"required": ["table"]
}
}
}# ❌ Bad: Too vague
"description": "Gets data"
# ✅ Good: Clear and specific
"description": "Get the current weather conditions including temperature, humidity, and forecast for a specific city""location": {
"type": "string",
"description": "City and country, e.g. 'Paris, France' or 'New York, USA'"
}try:
result = get_weather(location)
except Exception as e:
result = {"error": str(e)}
# The model will see the error and can tell the user