最近更新时间:2025-11-27 20:56:13
使用function call时,您需要先向模型进行请求,模型会识别出需要调用的函数,并返回函数名称及对应参数。
request
curl -X POST https://kspmas.ksyun.com/v1/chat/completions \
-H "Content-Type: application/json"\
-H "Authorization: Bearer {API-KEY}" \
-d '{
"temperature": 0,
"max_tokens": 100,
"model": "deepseek-v3.1",
"tools": [
{
"type": "function",
"function": {
"name": "query_weather",
"description": "Get weather of an city, the user should supply a city first",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city, e.g. WuHan"
}
},
"required": ["city"]
}
}
}
],
"messages": [
{
"role": "user",
"content": "Hows the weather like in WuHan today"
}
]
}'response
{
"choices": [
{
"finish_reason": "tool_calls",
"index": 0,
"message": {
"content": "I'll check the weather in Wuhan for you today.",
"role": "assistant",
"tool_calls": [
{
"function": {
"arguments": "{\"city\": \"WuHan\"}",
"name": "query_weather"
},
"id": "call_p4tt0vtg",
"index": 0,
"type": "function"
}
]
}
}
],
"created": 1758616056,
"id": "c92efe17411c89f02a5c168b9828ecc5",
"model": "deepseek-v3.1",
"object": "chat.completion",
"usage": {
"completion_tokens": 26,
"completion_tokens_details": {
"reasoning_tokens": 0
},
"prompt_tokens": 183,
"prompt_tokens_details": {
"cached_tokens": 0
},
"total_tokens": 209
}
}
您在服务端根据模型返回的信息执行实际的函数或外部 API,获得结果数据,工具函数示例如下。
import json
def execute_tool_from_model(response_json):
# 解析 JSON
response = json.loads(response_json)
choice = response["choices"][0]
# 检查是否有工具调用
tool_calls = choice["message"].get("tool_calls")
if not tool_calls:
print("No tool calls found.")
return None
results = []
for call in tool_calls:
func_name = call["function"]["name"]
func_args = json.loads(call["function"]["arguments"])
# 这里根据函数名称调用你自己的函数或外部 API
if func_name == "query_weather":
city = func_args.get("city")
# 这里模拟调用天气 API
weather_result = {
"city": city,
"weather": "Sunny, 27°C" # 假设返回结果
}
results.append(weather_result)
else:
print(f"Unknown tool function: {func_name}")
return results
# 执行工具
tool_results = execute_tool_from_model(model_response_json)
print(tool_results)
工具输出结果如下:
[{"city": "WuHan", "weather": "Sunny, 27°C"}]随后,您需将该结果以工具消息的形式补充到对话中,组合成新的请求。
curl -X POST https://kspmas.ksyun.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer {API-KEY}" \
-d '{
"temperature": 0,
"max_tokens": 100,
"model": "deepseek-v3.1",
"messages": [
{
"role": "user",
"content": "Hows the weather like in WuHan today"
},
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_p4tt0vtg",
"type": "function",
"function": {
"name": "query_weather",
"arguments": "{\"city\": \"WuHan\"}"
}
}
]
},
{
"role": "tool",
"tool_call_id": "call_p4tt0vtg",
"content": "{\"city\": \"WuHan\", \"weather\": \"Sunny, 27°C\"}"
}
]
}'模型在接收到工具执行结果后,会基于此生成面向用户的自然语言回复。
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "The weather in Wuhan today is **sunny** with a temperature of **27°C**. It's a pleasant day! ?",
"role": "assistant"
}
}
],
"created": 1758616457,
"id": "a896b0cd8902576ed8a288cb174c6d03",
"model": "deepseek-v3.1",
"object": "chat.completion",
"usage": {
"completion_tokens": 27,
"completion_tokens_details": {
"reasoning_tokens": 0
},
"prompt_tokens": 51,
"prompt_tokens_details": {
"cached_tokens": 0
},
"total_tokens": 78
}
}
纯净模式
