-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
103 lines (84 loc) · 2.83 KB
/
Copy pathtest.py
File metadata and controls
103 lines (84 loc) · 2.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
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
import asyncio
from rerank import rag_it
from dashscope import MultiModalConversation
from pydantic import BaseModel
import json
import os
class ChatRequest(BaseModel):
model: str = "qwen3.5-plus" # 默认模型
user_content: str # 用户输入的问题
enable_thinking:bool = False
MODEL_LIST = [
# 只能填入百炼平台(dashscope)的多模态模型
{"model":"qwen3.5-plus"},
{"model":"qwen3.5-flash"},
{"model":"qwen3-max"},
{"model":"kimi-k2.5"},
]
sys_content = """
You are an assistant that can use tools.
Available tools:
1.rag_it(question)
Tool descriptions:
- rag_it(...):查询rag库
Parameters for rag_it:
{
"question":"用户问的问题(str)"
}
When you need a tool, respond ONLY with JSON:
{
"action": "tool_name",
"arguments": {
...
}
}
Rules:
- Do NOT explain anything when calling tools
- Ensure arguments match the required parameter names exactly
- unit_price and quantity must be numbers
- If information is missing, ask the user instead of guessing
"""
def run_agent(model: str, user_content: str,enable_thinking:bool):
messages = [
{'role':'system','content': sys_content},
{'role': 'user','content': user_content}
]
enable_stream = False
while True:
responses = MultiModalConversation.call(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx"
api_key=os.getenv('DASHSCOPE_API_KEY'),
model=model, #模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
messages=messages,
result_format='message',
stream=enable_stream,
incremental_output=True,
enable_thinking=enable_thinking
)
full_content = responses["output"]["choices"][0]["message"]["content"][0]["text"]
print(full_content)
if full_content.strip().startswith("{"):
try:
obj = json.loads(full_content)
print(obj)
action = obj["action"]
args = obj["arguments"]
observation = ""
if action == "rag_it":
observation = rag_it(args.get("question"))
print("Tool result:")
print(observation)
messages.append({
"role": "assistant",
"content": f"Observation: {observation}"
})
continue
except Exception as e:
print("JSON解析失败:", e)
break
else:
for response in responses:
print(response)
break
if __name__ == "__main__":
run_agent("qwen3.5-plus","CloverCLC是谁",False)