Skip to content

Commit a12f7e8

Browse files
committed
add tool_calling test
1 parent 6b6dbff commit a12f7e8

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from openai import OpenAI
2+
import json
3+
4+
# 新版本 opnai
5+
client = OpenAI(api_key="EMPTY", base_url="http://localhost:8082/v1")
6+
7+
8+
def get_weather(location: str, unit: str):
9+
return f"Getting the weather for {location} in {unit}..."
10+
11+
12+
tool_functions = {"get_weather": get_weather}
13+
tools = [
14+
{
15+
"type": "function",
16+
"function": {
17+
"name": "get_weather",
18+
"description": "Get the current weather in a given location",
19+
"parameters": {
20+
"type": "object",
21+
"properties": {
22+
"location": {
23+
"type": "string",
24+
"description": "City and state, e.g., 'San Francisco, CA'",
25+
},
26+
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
27+
},
28+
"required": ["location", "unit"],
29+
},
30+
},
31+
}
32+
]
33+
# 方式一
34+
response = client.chat.completions.create(
35+
model="qwen-3b",
36+
messages=[{"role": "user", "content": "南京的天气怎么样"}],
37+
tools=tools,
38+
tool_choice="auto",
39+
)
40+
tool_call = response.choices[0].message.tool_calls[0].function
41+
print(response.choices[0].message.tool_calls)
42+
print(f"Function called: {tool_call.name}")
43+
print(f"Arguments: {tool_call.arguments}")
44+
print(f"Result: {get_weather(**json.loads(tool_call.arguments))}")

0 commit comments

Comments
 (0)