Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,7 +41,7 @@ TOOL_ROUTING = {
|
|
| 41 |
# ========== 初始化 LLM 客户端 ==========
|
| 42 |
hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
|
| 43 |
client = InferenceClient(api_key=hf_token) if hf_token else InferenceClient()
|
| 44 |
-
print(f"✅ LLM initialized: Qwen/
|
| 45 |
print(f"📊 MCP Services: {len(MCP_SERVICES)} services, {len(MCP_TOOLS)} tools")
|
| 46 |
|
| 47 |
# ========== Token 限制配置 ==========
|
|
@@ -195,9 +195,9 @@ def chatbot_response(message, history):
|
|
| 195 |
|
| 196 |
# LLM 调用循环(最多2轮工具调用) - 严格控制迭代次数
|
| 197 |
for iteration in range(MAX_TOOL_ITERATIONS):
|
| 198 |
-
response = client.
|
|
|
|
| 199 |
messages=messages,
|
| 200 |
-
model="Qwen/Qwen2.5-72B-Instruct:novita",
|
| 201 |
tools=MCP_TOOLS,
|
| 202 |
max_tokens=MAX_OUTPUT_TOKENS,
|
| 203 |
temperature=0.5,
|
|
@@ -260,29 +260,8 @@ def chatbot_response(message, history):
|
|
| 260 |
# 构建响应前缀(简化版)
|
| 261 |
response_prefix = ""
|
| 262 |
|
| 263 |
-
#
|
| 264 |
if tool_calls_log:
|
| 265 |
-
# 先添加 JavaScript 函数(确保在 HTML 之前加载)
|
| 266 |
-
response_prefix += """<script>
|
| 267 |
-
if (typeof window.toggleToolResult === 'undefined') {
|
| 268 |
-
window.toggleToolResult = function(id) {
|
| 269 |
-
var content = document.getElementById('content_' + id);
|
| 270 |
-
var arrow = document.getElementById('arrow_' + id);
|
| 271 |
-
if (content && arrow) {
|
| 272 |
-
if (content.style.display === 'none' || content.style.display === '') {
|
| 273 |
-
content.style.display = 'block';
|
| 274 |
-
arrow.textContent = '▼';
|
| 275 |
-
} else {
|
| 276 |
-
content.style.display = 'none';
|
| 277 |
-
arrow.textContent = '▶';
|
| 278 |
-
}
|
| 279 |
-
}
|
| 280 |
-
};
|
| 281 |
-
}
|
| 282 |
-
</script>
|
| 283 |
-
|
| 284 |
-
"""
|
| 285 |
-
|
| 286 |
response_prefix += """<div style='margin-bottom: 15px;'>
|
| 287 |
<div style='background: #f0f0f0; padding: 8px 12px; border-radius: 6px; font-weight: 600; color: #333;'>
|
| 288 |
🛠️ Tools Used ({} calls)
|
|
@@ -290,26 +269,24 @@ if (typeof window.toggleToolResult === 'undefined') {
|
|
| 290 |
""".format(len(tool_calls_log))
|
| 291 |
|
| 292 |
for idx, tool_call in enumerate(tool_calls_log):
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
response_prefix += f"""<div style='margin: 8px 0; border: 1px solid #ddd; border-radius: 6px; overflow: hidden;'>
|
| 297 |
-
<div style='background: #fff; padding: 10px; cursor: pointer; user-select: none;' onclick='window.toggleToolResult("{tool_id}")'>
|
| 298 |
<div style='display: flex; justify-content: space-between; align-items: center;'>
|
| 299 |
<div style='flex: 1;'>
|
| 300 |
<strong style='color: #2c5aa0;'>📌 {idx+1}. {tool_call['name']}</strong>
|
| 301 |
<div style='font-size: 0.85em; color: #666; margin-top: 4px;'>📥 Input: <code style='background: #f5f5f5; padding: 2px 6px; border-radius: 3px;'>{json.dumps(tool_call['arguments'], ensure_ascii=False)}</code></div>
|
| 302 |
</div>
|
| 303 |
-
<span
|
| 304 |
</div>
|
| 305 |
-
</
|
| 306 |
-
<div
|
| 307 |
<div style='font-size: 0.9em; color: #333;'>
|
| 308 |
<strong>📤 Output:</strong>
|
| 309 |
-
<pre style='background: #fff; padding: 10px; border-radius: 4px; overflow-x: auto; margin-top: 6px; font-size: 0.85em; border: 1px solid #e0e0e0; max-height: 400px;'>{json.dumps(tool_call.get('result', {}), ensure_ascii=False, indent=2)[:1500]}{'...' if len(json.dumps(tool_call.get('result', {}), ensure_ascii=False)) > 1500 else ''}</pre>
|
| 310 |
</div>
|
| 311 |
</div>
|
| 312 |
-
</
|
| 313 |
"""
|
| 314 |
|
| 315 |
response_prefix += """</div>
|
|
@@ -322,9 +299,9 @@ if (typeof window.toggleToolResult === 'undefined') {
|
|
| 322 |
# 流式输出最终答案
|
| 323 |
yield response_prefix
|
| 324 |
|
| 325 |
-
stream = client.
|
|
|
|
| 326 |
messages=messages,
|
| 327 |
-
model="Qwen/Qwen2.5-72B-Instruct:novita",
|
| 328 |
tools=MCP_TOOLS,
|
| 329 |
max_tokens=MAX_OUTPUT_TOKENS,
|
| 330 |
temperature=0.5,
|
|
|
|
| 41 |
# ========== 初始化 LLM 客户端 ==========
|
| 42 |
hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
|
| 43 |
client = InferenceClient(api_key=hf_token) if hf_token else InferenceClient()
|
| 44 |
+
print(f"✅ LLM initialized: Qwen/Qwen3-32B:groq")
|
| 45 |
print(f"📊 MCP Services: {len(MCP_SERVICES)} services, {len(MCP_TOOLS)} tools")
|
| 46 |
|
| 47 |
# ========== Token 限制配置 ==========
|
|
|
|
| 195 |
|
| 196 |
# LLM 调用循环(最多2轮工具调用) - 严格控制迭代次数
|
| 197 |
for iteration in range(MAX_TOOL_ITERATIONS):
|
| 198 |
+
response = client.chat.completions.create(
|
| 199 |
+
model="Qwen/Qwen3-32B:groq",
|
| 200 |
messages=messages,
|
|
|
|
| 201 |
tools=MCP_TOOLS,
|
| 202 |
max_tokens=MAX_OUTPUT_TOKENS,
|
| 203 |
temperature=0.5,
|
|
|
|
| 260 |
# 构建响应前缀(简化版)
|
| 261 |
response_prefix = ""
|
| 262 |
|
| 263 |
+
# 显示工具调用(使用原生HTML details标签)
|
| 264 |
if tool_calls_log:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
response_prefix += """<div style='margin-bottom: 15px;'>
|
| 266 |
<div style='background: #f0f0f0; padding: 8px 12px; border-radius: 6px; font-weight: 600; color: #333;'>
|
| 267 |
🛠️ Tools Used ({} calls)
|
|
|
|
| 269 |
""".format(len(tool_calls_log))
|
| 270 |
|
| 271 |
for idx, tool_call in enumerate(tool_calls_log):
|
| 272 |
+
# 使用原生 HTML5 details/summary 标签(不需要 JavaScript)
|
| 273 |
+
response_prefix += f"""<details style='margin: 8px 0; border: 1px solid #ddd; border-radius: 6px; overflow: hidden;'>
|
| 274 |
+
<summary style='background: #fff; padding: 10px; cursor: pointer; user-select: none; list-style: none;'>
|
|
|
|
|
|
|
| 275 |
<div style='display: flex; justify-content: space-between; align-items: center;'>
|
| 276 |
<div style='flex: 1;'>
|
| 277 |
<strong style='color: #2c5aa0;'>📌 {idx+1}. {tool_call['name']}</strong>
|
| 278 |
<div style='font-size: 0.85em; color: #666; margin-top: 4px;'>📥 Input: <code style='background: #f5f5f5; padding: 2px 6px; border-radius: 3px;'>{json.dumps(tool_call['arguments'], ensure_ascii=False)}</code></div>
|
| 279 |
</div>
|
| 280 |
+
<span style='font-size: 1.2em; color: #999; margin-left: 10px;'>▶</span>
|
| 281 |
</div>
|
| 282 |
+
</summary>
|
| 283 |
+
<div style='background: #f9f9f9; padding: 12px; border-top: 1px solid #eee;'>
|
| 284 |
<div style='font-size: 0.9em; color: #333;'>
|
| 285 |
<strong>📤 Output:</strong>
|
| 286 |
+
<pre style='background: #fff; padding: 10px; border-radius: 4px; overflow-x: auto; margin-top: 6px; font-size: 0.85em; border: 1px solid #e0e0e0; max-height: 400px; white-space: pre-wrap;'>{json.dumps(tool_call.get('result', {}), ensure_ascii=False, indent=2)[:1500]}{'...' if len(json.dumps(tool_call.get('result', {}), ensure_ascii=False)) > 1500 else ''}</pre>
|
| 287 |
</div>
|
| 288 |
</div>
|
| 289 |
+
</details>
|
| 290 |
"""
|
| 291 |
|
| 292 |
response_prefix += """</div>
|
|
|
|
| 299 |
# 流式输出最终答案
|
| 300 |
yield response_prefix
|
| 301 |
|
| 302 |
+
stream = client.chat.completions.create(
|
| 303 |
+
model="Qwen/Qwen3-32B:groq",
|
| 304 |
messages=messages,
|
|
|
|
| 305 |
tools=MCP_TOOLS,
|
| 306 |
max_tokens=MAX_OUTPUT_TOKENS,
|
| 307 |
temperature=0.5,
|