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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
| # 定义多个函数
def get_stock_price(symbol):
"""获取股票价格"""
# 模拟股票数据
stock_data = {
"AAPL": {"price": 175.43, "change": "+2.15%"},
"GOOGL": {"price": 2847.31, "change": "-0.82%"},
"TSLA": {"price": 248.50, "change": "+3.27%"}
}
return stock_data.get(symbol, {"error": "股票代码不存在"})
def calculate_tip(bill_amount, tip_percentage=15):
"""计算小费"""
tip = bill_amount * tip_percentage / 100
total = bill_amount + tip
return {
"bill_amount": bill_amount,
"tip_percentage": tip_percentage,
"tip_amount": round(tip, 2),
"total_amount": round(total, 2)
}
# 函数描述列表
function_schemas = [
{
"name": "get_weather",
"description": "获取指定城市的天气信息",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "城市名称"}
},
"required": ["location"]
}
},
{
"name": "get_stock_price",
"description": "获取股票价格信息",
"parameters": {
"type": "object",
"properties": {
"symbol": {"type": "string", "description": "股票代码,如AAPL、GOOGL"}
},
"required": ["symbol"]
}
},
{
"name": "calculate_tip",
"description": "计算小费和总金额",
"parameters": {
"type": "object",
"properties": {
"bill_amount": {"type": "number", "description": "账单金额"},
"tip_percentage": {"type": "number", "description": "小费百分比,默认15%"}
},
"required": ["bill_amount"]
}
}
]
# 函数映射
function_map = {
"get_weather": get_weather,
"get_stock_price": get_stock_price,
"calculate_tip": calculate_tip
}
def advanced_function_call(user_message):
"""支持多函数的高级Function Call"""
messages = [{"role": "user", "content": user_message}]
tools = [{"type": "function", "function": schema} for schema in function_schemas]
response = Generation.call(
model='qwen-plus',
messages=messages,
tools=tools,
tool_choice="auto"
)
assistant_message = response.output.message
if hasattr(assistant_message, 'tool_calls') and assistant_message.tool_calls:
# 处理函数调用
messages.append(assistant_message)
for tool_call in assistant_message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
# 执行对应的函数
if function_name in function_map:
function_result = function_map[function_name](**function_args)
else:
function_result = {"error": f"未知函数: {function_name}"}
# 添加函数结果到消息历史
messages.append({
"role": "tool",
"content": json.dumps(function_result, ensure_ascii=False),
"tool_call_id": tool_call.id
})
# 生成最终回复
final_response = Generation.call(
model='qwen-plus',
messages=messages
)
return final_response.output.message.content
else:
return assistant_message.content
# 测试多函数功能
test_queries = [
"北京天气如何?",
"AAPL股票价格是多少?",
"账单是120元,小费按20%计算,总共多少钱?",
"帮我查一下上海天气,还有TSLA的股价"
]
for query in test_queries:
print(f"\n用户: {query}")
response = advanced_function_call(query)
print(f"AI: {response}")
|