-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool_calling.py
More file actions
73 lines (61 loc) · 1.94 KB
/
tool_calling.py
File metadata and controls
73 lines (61 loc) · 1.94 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
72
73
"""Function/tool calling with CheapestInference.
Works with models that support tool use (DeepSeek, Qwen, Llama 3.3+).
"""
import json
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["CHEAPEST_API_KEY"],
base_url="https://api.cheapestinference.com/v1",
)
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. 'San Francisco'",
}
},
"required": ["location"],
},
},
}
]
def get_weather(location: str) -> str:
"""Fake weather function for demo purposes."""
return json.dumps({"location": location, "temp_f": 72, "condition": "sunny"})
# First call: model decides to use the tool
response = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
tools=tools,
tool_choice="auto",
)
message = response.choices[0].message
if message.tool_calls:
tool_call = message.tool_calls[0]
print(f"Model called: {tool_call.function.name}({tool_call.function.arguments})")
# Execute the function
result = get_weather(**json.loads(tool_call.function.arguments))
# Second call: send the tool result back
final = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "user", "content": "What's the weather in Tokyo?"},
message,
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": result,
},
],
)
print(final.choices[0].message.content)
else:
print(message.content)