Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -159,24 +159,28 @@ def chatbot_response(message, history):
|
|
| 159 |
try:
|
| 160 |
messages = [{"role": "system", "content": get_system_prompt()}]
|
| 161 |
|
| 162 |
-
# 添加历史(最近
|
| 163 |
if history:
|
| 164 |
-
for item in history[-
|
| 165 |
if isinstance(item, (list, tuple)) and len(item) == 2:
|
| 166 |
messages.append({"role": "user", "content": item[0]})
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
messages.append({"role": "user", "content": message})
|
| 170 |
|
| 171 |
tool_calls_log = []
|
| 172 |
|
| 173 |
-
# LLM 调用循环(最多
|
| 174 |
-
for iteration in range(
|
| 175 |
response = client.chat_completion(
|
| 176 |
messages=messages,
|
| 177 |
model="Qwen/Qwen2.5-72B-Instruct:novita",
|
| 178 |
tools=MCP_TOOLS,
|
| 179 |
-
max_tokens=2000
|
| 180 |
temperature=0.5,
|
| 181 |
tool_choice="auto",
|
| 182 |
stream=False
|
|
@@ -194,16 +198,20 @@ def chatbot_response(message, history):
|
|
| 194 |
# 调用 MCP 工具
|
| 195 |
tool_result = call_mcp_tool(tool_name, tool_args)
|
| 196 |
|
| 197 |
-
#
|
| 198 |
result_str = json.dumps(tool_result, ensure_ascii=False)
|
| 199 |
-
if len(result_str) > 4000
|
| 200 |
-
#
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
result_for_llm = json.dumps(tool_result_truncated)
|
| 203 |
else:
|
| 204 |
result_for_llm = result_str
|
| 205 |
|
| 206 |
-
# 记录工具调用(
|
| 207 |
tool_calls_log.append({"name": tool_name, "arguments": tool_args, "result": tool_result})
|
| 208 |
|
| 209 |
messages.append({
|
|
@@ -250,7 +258,7 @@ def chatbot_response(message, history):
|
|
| 250 |
messages=messages,
|
| 251 |
model="Qwen/Qwen2.5-72B-Instruct:novita",
|
| 252 |
tools=MCP_TOOLS,
|
| 253 |
-
max_tokens=2000
|
| 254 |
temperature=0.5,
|
| 255 |
stream=True
|
| 256 |
)
|
|
|
|
| 159 |
try:
|
| 160 |
messages = [{"role": "system", "content": get_system_prompt()}]
|
| 161 |
|
| 162 |
+
# 添加历史(最近3轭) - 减少上下文长度
|
| 163 |
if history:
|
| 164 |
+
for item in history[-3:]: # 从5轮改为3轮
|
| 165 |
if isinstance(item, (list, tuple)) and len(item) == 2:
|
| 166 |
messages.append({"role": "user", "content": item[0]})
|
| 167 |
+
# 截断过长的历史回复
|
| 168 |
+
assistant_msg = item[1]
|
| 169 |
+
if len(assistant_msg) > 1000:
|
| 170 |
+
assistant_msg = assistant_msg[:1000] + "...[truncated]"
|
| 171 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 172 |
|
| 173 |
messages.append({"role": "user", "content": message})
|
| 174 |
|
| 175 |
tool_calls_log = []
|
| 176 |
|
| 177 |
+
# LLM 调用循环(最多3轮工具调用) - 减少迭代次数
|
| 178 |
+
for iteration in range(3): # 从5轮改为3轮
|
| 179 |
response = client.chat_completion(
|
| 180 |
messages=messages,
|
| 181 |
model="Qwen/Qwen2.5-72B-Instruct:novita",
|
| 182 |
tools=MCP_TOOLS,
|
| 183 |
+
max_tokens=1500, # 从2000降到1500
|
| 184 |
temperature=0.5,
|
| 185 |
tool_choice="auto",
|
| 186 |
stream=False
|
|
|
|
| 198 |
# 调用 MCP 工具
|
| 199 |
tool_result = call_mcp_tool(tool_name, tool_args)
|
| 200 |
|
| 201 |
+
# 大幅限制返回结果大小,避免超长内容导致500错误
|
| 202 |
result_str = json.dumps(tool_result, ensure_ascii=False)
|
| 203 |
+
if len(result_str) > 2000: # 从4000降到2000
|
| 204 |
+
# 截断过长的结果,只保留关键信息
|
| 205 |
+
if isinstance(tool_result, dict) and "text" in tool_result:
|
| 206 |
+
# 如果是文本格式,截取前1500字符
|
| 207 |
+
tool_result_truncated = {"text": tool_result["text"][:1500] + "...[truncated]", "_truncated": True}
|
| 208 |
+
else:
|
| 209 |
+
tool_result_truncated = {"_truncated": True, "preview": result_str[:1500] + "...[truncated]"}
|
| 210 |
result_for_llm = json.dumps(tool_result_truncated)
|
| 211 |
else:
|
| 212 |
result_for_llm = result_str
|
| 213 |
|
| 214 |
+
# 记录工具调用(包含完整结果用于UI显示)
|
| 215 |
tool_calls_log.append({"name": tool_name, "arguments": tool_args, "result": tool_result})
|
| 216 |
|
| 217 |
messages.append({
|
|
|
|
| 258 |
messages=messages,
|
| 259 |
model="Qwen/Qwen2.5-72B-Instruct:novita",
|
| 260 |
tools=MCP_TOOLS,
|
| 261 |
+
max_tokens=1500, # 从2000降到1500
|
| 262 |
temperature=0.5,
|
| 263 |
stream=True
|
| 264 |
)
|