-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_calling.py
More file actions
51 lines (45 loc) · 1.41 KB
/
function_calling.py
File metadata and controls
51 lines (45 loc) · 1.41 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
"""
Function calling / Tool use - 让模型调用外部函数
"""
import json
from openai import OpenAI
client = OpenAI(api_key="sk-xxx", base_url="http://xdhdancer.top/v1")
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]
# 第一轮:让模型决定是否调工具
resp = client.chat.completions.create(
model="claude-opus-4-7",
messages=[{"role": "user", "content": "天气怎么样?北京。"}],
tools=tools,
)
msg = resp.choices[0].message
if msg.tool_calls:
tool_call = msg.tool_calls[0]
args = json.loads(tool_call.function.arguments)
print(f"模型要调用 {tool_call.function.name}({args})")
# 模拟工具结果
tool_result = {"city": args["city"], "temp": 22, "condition": "sunny"}
# 第二轮:把工具结果丢回去
resp2 = client.chat.completions.create(
model="claude-opus-4-7",
messages=[
{"role": "user", "content": "天气怎么样?北京。"},
msg,
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(tool_result),
},
],
)
print(resp2.choices[0].message.content)