Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -230,13 +230,15 @@ def query_financial_data(company_name, query_type):
|
|
| 230 |
# 先搜索公司(使用 advanced_search_company)
|
| 231 |
try:
|
| 232 |
search_resp = session.post(
|
| 233 |
-
f"{MCP_URL}/
|
| 234 |
json={
|
|
|
|
| 235 |
"method": "tools/call",
|
| 236 |
"params": {
|
| 237 |
"name": "advanced_search_company",
|
| 238 |
"arguments": {"company_input": company_name}
|
| 239 |
-
}
|
|
|
|
| 240 |
},
|
| 241 |
headers=HEADERS,
|
| 242 |
timeout=60 # 增加到60秒
|
|
@@ -275,13 +277,15 @@ def query_financial_data(company_name, query_type):
|
|
| 275 |
# 根据查询类型获取数据
|
| 276 |
if internal_query_type == "最新财务数据":
|
| 277 |
data_resp = session.post(
|
| 278 |
-
f"{MCP_URL}/
|
| 279 |
json={
|
|
|
|
| 280 |
"method": "tools/call",
|
| 281 |
"params": {
|
| 282 |
"name": "get_latest_financial_data",
|
| 283 |
"arguments": {"cik": cik}
|
| 284 |
-
}
|
|
|
|
| 285 |
},
|
| 286 |
headers=HEADERS,
|
| 287 |
timeout=60 # 增加到60秒
|
|
@@ -321,13 +325,15 @@ def query_financial_data(company_name, query_type):
|
|
| 321 |
|
| 322 |
elif internal_query_type == "3年趋势":
|
| 323 |
metrics_resp = session.post(
|
| 324 |
-
f"{MCP_URL}/
|
| 325 |
json={
|
|
|
|
| 326 |
"method": "tools/call",
|
| 327 |
"params": {
|
| 328 |
"name": "extract_financial_metrics",
|
| 329 |
"arguments": {"cik": cik, "years": 3}
|
| 330 |
-
}
|
|
|
|
| 331 |
},
|
| 332 |
headers=HEADERS,
|
| 333 |
timeout=120 # 3年趋势需要更长时间,增加到120秒
|
|
@@ -403,13 +409,15 @@ def query_financial_data(company_name, query_type):
|
|
| 403 |
|
| 404 |
elif internal_query_type == "5年趋势":
|
| 405 |
metrics_resp = session.post(
|
| 406 |
-
f"{MCP_URL}/
|
| 407 |
json={
|
|
|
|
| 408 |
"method": "tools/call",
|
| 409 |
"params": {
|
| 410 |
"name": "extract_financial_metrics",
|
| 411 |
"arguments": {"cik": cik, "years": 5}
|
| 412 |
-
}
|
|
|
|
| 413 |
},
|
| 414 |
headers=HEADERS,
|
| 415 |
timeout=180 # 5年趋势需要更长时间,增加到180秒
|
|
@@ -485,13 +493,15 @@ def query_financial_data(company_name, query_type):
|
|
| 485 |
elif internal_query_type == "公司报表列表":
|
| 486 |
# 查询公司所有报表
|
| 487 |
filings_resp = session.post(
|
| 488 |
-
f"{MCP_URL}/
|
| 489 |
json={
|
|
|
|
| 490 |
"method": "tools/call",
|
| 491 |
"params": {
|
| 492 |
"name": "get_company_filings",
|
| 493 |
"arguments": {"cik": cik, "limit": 50}
|
| 494 |
-
}
|
|
|
|
| 495 |
},
|
| 496 |
headers=HEADERS,
|
| 497 |
timeout=90 # 增加到90秒
|
|
@@ -545,14 +555,17 @@ def query_financial_data(company_name, query_type):
|
|
| 545 |
def call_mcp_tool(tool_name, arguments):
|
| 546 |
"""调用 MCP 工具并返回结果"""
|
| 547 |
try:
|
|
|
|
| 548 |
response = session.post(
|
| 549 |
-
f"{MCP_URL}/
|
| 550 |
json={
|
|
|
|
| 551 |
"method": "tools/call",
|
| 552 |
"params": {
|
| 553 |
"name": tool_name,
|
| 554 |
"arguments": arguments
|
| 555 |
-
}
|
|
|
|
| 556 |
},
|
| 557 |
headers=HEADERS,
|
| 558 |
timeout=60
|
|
@@ -593,9 +606,18 @@ Always be helpful, accurate, and cite the data sources when providing financial
|
|
| 593 |
messages.append({"role": "system", "content": system_prompt})
|
| 594 |
|
| 595 |
# 添加历史对话(最近 5 轮)
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 599 |
|
| 600 |
# 添加当前消息
|
| 601 |
messages.append({"role": "user", "content": message})
|
|
|
|
| 230 |
# 先搜索公司(使用 advanced_search_company)
|
| 231 |
try:
|
| 232 |
search_resp = session.post(
|
| 233 |
+
f"{MCP_URL}/gradio_api/mcp/",
|
| 234 |
json={
|
| 235 |
+
"jsonrpc": "2.0",
|
| 236 |
"method": "tools/call",
|
| 237 |
"params": {
|
| 238 |
"name": "advanced_search_company",
|
| 239 |
"arguments": {"company_input": company_name}
|
| 240 |
+
},
|
| 241 |
+
"id": 1
|
| 242 |
},
|
| 243 |
headers=HEADERS,
|
| 244 |
timeout=60 # 增加到60秒
|
|
|
|
| 277 |
# 根据查询类型获取数据
|
| 278 |
if internal_query_type == "最新财务数据":
|
| 279 |
data_resp = session.post(
|
| 280 |
+
f"{MCP_URL}/gradio_api/mcp/",
|
| 281 |
json={
|
| 282 |
+
"jsonrpc": "2.0",
|
| 283 |
"method": "tools/call",
|
| 284 |
"params": {
|
| 285 |
"name": "get_latest_financial_data",
|
| 286 |
"arguments": {"cik": cik}
|
| 287 |
+
},
|
| 288 |
+
"id": 1
|
| 289 |
},
|
| 290 |
headers=HEADERS,
|
| 291 |
timeout=60 # 增加到60秒
|
|
|
|
| 325 |
|
| 326 |
elif internal_query_type == "3年趋势":
|
| 327 |
metrics_resp = session.post(
|
| 328 |
+
f"{MCP_URL}/gradio_api/mcp/",
|
| 329 |
json={
|
| 330 |
+
"jsonrpc": "2.0",
|
| 331 |
"method": "tools/call",
|
| 332 |
"params": {
|
| 333 |
"name": "extract_financial_metrics",
|
| 334 |
"arguments": {"cik": cik, "years": 3}
|
| 335 |
+
},
|
| 336 |
+
"id": 1
|
| 337 |
},
|
| 338 |
headers=HEADERS,
|
| 339 |
timeout=120 # 3年趋势需要更长时间,增加到120秒
|
|
|
|
| 409 |
|
| 410 |
elif internal_query_type == "5年趋势":
|
| 411 |
metrics_resp = session.post(
|
| 412 |
+
f"{MCP_URL}/gradio_api/mcp/",
|
| 413 |
json={
|
| 414 |
+
"jsonrpc": "2.0",
|
| 415 |
"method": "tools/call",
|
| 416 |
"params": {
|
| 417 |
"name": "extract_financial_metrics",
|
| 418 |
"arguments": {"cik": cik, "years": 5}
|
| 419 |
+
},
|
| 420 |
+
"id": 1
|
| 421 |
},
|
| 422 |
headers=HEADERS,
|
| 423 |
timeout=180 # 5年趋势需要更长时间,增加到180秒
|
|
|
|
| 493 |
elif internal_query_type == "公司报表列表":
|
| 494 |
# 查询公司所有报表
|
| 495 |
filings_resp = session.post(
|
| 496 |
+
f"{MCP_URL}/gradio_api/mcp/",
|
| 497 |
json={
|
| 498 |
+
"jsonrpc": "2.0",
|
| 499 |
"method": "tools/call",
|
| 500 |
"params": {
|
| 501 |
"name": "get_company_filings",
|
| 502 |
"arguments": {"cik": cik, "limit": 50}
|
| 503 |
+
},
|
| 504 |
+
"id": 1
|
| 505 |
},
|
| 506 |
headers=HEADERS,
|
| 507 |
timeout=90 # 增加到90秒
|
|
|
|
| 555 |
def call_mcp_tool(tool_name, arguments):
|
| 556 |
"""调用 MCP 工具并返回结果"""
|
| 557 |
try:
|
| 558 |
+
# Gradio MCP Server 使用 /gradio_api/mcp/ 端点
|
| 559 |
response = session.post(
|
| 560 |
+
f"{MCP_URL}/gradio_api/mcp/",
|
| 561 |
json={
|
| 562 |
+
"jsonrpc": "2.0",
|
| 563 |
"method": "tools/call",
|
| 564 |
"params": {
|
| 565 |
"name": tool_name,
|
| 566 |
"arguments": arguments
|
| 567 |
+
},
|
| 568 |
+
"id": 1
|
| 569 |
},
|
| 570 |
headers=HEADERS,
|
| 571 |
timeout=60
|
|
|
|
| 606 |
messages.append({"role": "system", "content": system_prompt})
|
| 607 |
|
| 608 |
# 添加历史对话(最近 5 轮)
|
| 609 |
+
# Gradio 6.x 的 history 格式可能是 [{"role": "user", "content": ...}, {"role": "assistant", "content": ...}]
|
| 610 |
+
# 或者是 [(user_msg, assistant_msg), ...] 的元组列表
|
| 611 |
+
if history:
|
| 612 |
+
for item in history[-5:]:
|
| 613 |
+
if isinstance(item, dict):
|
| 614 |
+
# 新格式:字��列表
|
| 615 |
+
messages.append(item)
|
| 616 |
+
elif isinstance(item, (list, tuple)) and len(item) == 2:
|
| 617 |
+
# 旧格式:元组列表
|
| 618 |
+
user_msg, assistant_msg = item
|
| 619 |
+
messages.append({"role": "user", "content": user_msg})
|
| 620 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 621 |
|
| 622 |
# 添加当前消息
|
| 623 |
messages.append({"role": "user", "content": message})
|