-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_tool_calls.py
More file actions
71 lines (59 loc) · 1.83 KB
/
python_tool_calls.py
File metadata and controls
71 lines (59 loc) · 1.83 KB
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
"""
Function / tool calling with DeepSeek V4 — OpenAI-compatible format.
pip install openai
export DEEPSEEK_API_KEY=sk-...
python python_tool_calls.py
"""
import json
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["DEEPSEEK_API_KEY"],
base_url="https://api.deepseek.com/v1",
)
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city.",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name, e.g. 'Tokyo'"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["city"],
},
},
}
]
def fake_weather(city: str, unit: str = "celsius") -> dict:
return {"city": city, "unit": unit, "temp": 22, "condition": "clear"}
messages = [{"role": "user", "content": "What's the weather in Tokyo in celsius?"}]
# Round 1: model decides to call the tool.
first = client.chat.completions.create(
model="deepseek-v4-pro",
messages=messages,
tools=tools,
)
assistant_msg = first.choices[0].message
messages.append(assistant_msg.model_dump(exclude_none=True))
# Execute each tool call the model requested.
for call in assistant_msg.tool_calls or []:
args = json.loads(call.function.arguments)
result = fake_weather(**args)
messages.append(
{
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(result),
}
)
# Round 2: model reads tool results and produces the final answer.
final = client.chat.completions.create(
model="deepseek-v4-pro",
messages=messages,
tools=tools,
)
print(final.choices[0].message.content)