Function calling lets AI decide when to call your API. Here's how to implement it in 30 lines of code — with real examples.
AI is great at generating text. But you know what's better? AI that can take actions.
Function calling lets AI decide: "I need to call this API to answer the user's question."
Here's how to implement it.
The Setup
Let's say you have a weather API. Instead of AI making up the weather, it can call your API to get real data.
Step 1: Define Your Functions
functions = [
{
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"}
},
"required": ["city"]
}
}
]
Step 2: Let AI Decide
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
tools=[{"type": "function", "function": f} for f in functions]
)
# AI decides to call get_weather("Tokyo")
message = response.choices[0].message
Step 3: Execute the Function
if message.tool_calls:
# AI wants to call a function
func_name = message.tool_calls[0].function.name
args = json.loads(message.tool_calls[0].function.arguments)
if func_name == "get_weather":
result = requests.get(f"https://api.weather.com/v1/{args['city']}").json()
# Send result back to AI
final_response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[
{"role": "user", "content": "What's the weather in Tokyo?"},
message,
{"role": "tool", "tool_call_id": message.tool_calls[0].id,
"content": json.dumps(result)}
]
)
print(final_response.choices[0].message.content)
Real Example: AI Assistant with Tools
# Full example: AI that can check weather, search DB, and send emails
functions = [get_weather, query_database, send_email]
while True:
user_input = input("You: ")
response = client.chat.completions.create(
model="deepseek-v4-pro",
messages=[{"role": "user", "content": user_input}],
tools=[{"type": "function", "function": f} for f in functions]
)
# AI automatically calls the right function
# You just execute it and return the result
Try It
- Get a free API key → aibridge-api.com
- Copy the code above
- Define your own functions
- Let AI call them
Pro tip: Use deepseek-v4-pro for function calling (best at reasoning which function to call).




Top comments (0)