Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: 📊
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
|
@@ -10,19 +10,19 @@ pinned: false
|
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
-
#
|
| 14 |
|
| 15 |
-
A Gradio-based AI
|
| 16 |
|
| 17 |
## ✨ Features
|
| 18 |
|
| 19 |
-
- 🤖 **Intelligent AI Assistant**:
|
| 20 |
-
-
|
| 21 |
-
-
|
| 22 |
-
-
|
| 23 |
-
-
|
| 24 |
-
-
|
| 25 |
-
-
|
| 26 |
|
| 27 |
## 🚀 Quick Start
|
| 28 |
|
|
@@ -43,69 +43,54 @@ A Gradio-based AI-powered web application for querying SEC financial data throug
|
|
| 43 |
|
| 44 |
### Usage
|
| 45 |
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
- "
|
| 52 |
-
- "
|
| 53 |
-
- "
|
| 54 |
-
- "How is Microsoft performing?"
|
| 55 |
-
- "Give me Alibaba's financial overview"
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
3. 📊 Analyze the data
|
| 61 |
-
4. 💬 Provide a comprehensive answer
|
| 62 |
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 66 |
-
-
|
| 67 |
-
-
|
| 68 |
-
|
| 69 |
-
- **3-Year Trends**: Financial trends over 3 years
|
| 70 |
-
- **5-Year Trends**: Financial trends over 5 years
|
| 71 |
-
- **Company Filings**: List of SEC filings
|
| 72 |
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
- "What can you tell me about Apple?"
|
| 77 |
-
- "How is Tesla doing financially?"
|
| 78 |
|
| 79 |
-
**
|
| 80 |
-
-
|
| 81 |
-
- "What's Microsoft's latest EPS?"
|
| 82 |
-
|
| 83 |
-
**Comparisons:**
|
| 84 |
-
- "Compare Amazon's revenue and expenses"
|
| 85 |
-
- "How does Google's cash flow look?"
|
| 86 |
-
|
| 87 |
-
**Trends:**
|
| 88 |
-
- "Give me a 5-year financial overview of Alibaba"
|
| 89 |
-
- "Show me Meta's financial trends"
|
| 90 |
-
|
| 91 |
-
## 💾 Data Source
|
| 92 |
-
|
| 93 |
-
SEC EDGAR data via MCP Server: https://huggingface.co/spaces/JC321/EasyReportDateMCP
|
| 94 |
|
| 95 |
## 🛠️ Technology Stack
|
| 96 |
|
| 97 |
-
- **Frontend**: Gradio 6.0.1
|
| 98 |
-
- **
|
| 99 |
-
- **
|
| 100 |
-
- **
|
| 101 |
-
-
|
|
|
|
| 102 |
|
| 103 |
## 💡 Tips
|
| 104 |
|
| 105 |
-
-
|
| 106 |
-
-
|
| 107 |
-
- Both company names and ticker symbols work (
|
| 108 |
-
- The AI
|
| 109 |
|
| 110 |
## 👍 Supported Companies
|
| 111 |
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Financial & Market AI Assistant
|
| 3 |
emoji: 📊
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
|
|
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Financial & Market AI Assistant
|
| 14 |
|
| 15 |
+
A streamlined Gradio-based AI assistant that integrates **SEC financial data** and **real-time market information** through two MCP servers.
|
| 16 |
|
| 17 |
## ✨ Features
|
| 18 |
|
| 19 |
+
- 🤖 **Intelligent AI Assistant**: Powered by **Qwen/Qwen2.5-72B-Instruct:novita** (supports tool calling)
|
| 20 |
+
- 📊 **Dual Data Sources**:
|
| 21 |
+
- **SEC Financial Reports**: Official 10-K/10-Q data (revenue, earnings, cash flow, etc.)
|
| 22 |
+
- **Market & Stock Data**: Real-time stock quotes and news (powered by Finnhub)
|
| 23 |
+
- 🛠️ **Automatic MCP Tool Calling**: AI automatically uses 6 tools across 2 MCP servers
|
| 24 |
+
- 💬 **Natural Language Interface**: Just ask questions naturally
|
| 25 |
+
- 🔍 **Smart Analysis**: AI provides data-driven insights, not just raw data
|
| 26 |
|
| 27 |
## 🚀 Quick Start
|
| 28 |
|
|
|
|
| 43 |
|
| 44 |
### Usage
|
| 45 |
|
| 46 |
+
Just start asking questions! The AI will automatically fetch data from the right source:
|
| 47 |
|
| 48 |
+
**Financial Questions:**
|
| 49 |
+
- "What's Apple's latest revenue and profit?"
|
| 50 |
+
- "Show me NVIDIA's 3-year financial trends"
|
| 51 |
+
- "Compare Microsoft's latest earnings with its operating expenses"
|
| 52 |
|
| 53 |
+
**Market Questions:**
|
| 54 |
+
- "How is Tesla's stock performing today?"
|
| 55 |
+
- "Get the latest market news about crypto"
|
| 56 |
+
- "What's the current price of AAPL?"
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
**Combined Analysis:**
|
| 59 |
+
- "Compare Microsoft's latest earnings with its current stock price"
|
| 60 |
+
- "Show me Amazon's financial performance and recent news"
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
## 🛠️ Available MCP Tools
|
| 63 |
|
| 64 |
+
**SEC Financial Reports MCP:**
|
| 65 |
+
1. `advanced_search_company` - Find US companies by name/ticker
|
| 66 |
+
2. `get_latest_financial_data` - Get latest 10-K/10-Q data
|
| 67 |
+
3. `extract_financial_metrics` - Get 3-year or 5-year trends
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
**Market & Stock Data MCP (Finnhub):**
|
| 70 |
+
4. `get_quote` - Real-time stock price, volume, change
|
| 71 |
+
5. `get_market_news` - Latest market news (general/forex/crypto/merger)
|
| 72 |
+
6. `get_company_news` - Company-specific news with date range
|
| 73 |
|
| 74 |
+
## 💾 Data Sources
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
- **SEC Financial Data**: https://huggingface.co/spaces/JC321/EasyReportDateMCP
|
| 77 |
+
- **Market & Stock Data**: https://huggingface.co/spaces/JC321/MarketandStockMCP (Finnhub API)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
## 🛠️ Technology Stack
|
| 80 |
|
| 81 |
+
- **Frontend**: Gradio 6.0.1 (ChatInterface)
|
| 82 |
+
- **AI Model**: Qwen/Qwen2.5-72B-Instruct:novita (Hugging Face Inference API)
|
| 83 |
+
- **MCP Protocol**: 2 MCP Servers (HTTP + SSE transports)
|
| 84 |
+
- **Data Sources**:
|
| 85 |
+
- SEC EDGAR (official financial filings)
|
| 86 |
+
- Finnhub API (real-time market data)
|
| 87 |
|
| 88 |
## 💡 Tips
|
| 89 |
|
| 90 |
+
- Ask about **financials** (revenue, profit) or **market data** (stock price, news)
|
| 91 |
+
- AI understands context and can combine data from both sources
|
| 92 |
+
- Both company names and ticker symbols work ("Apple" or "AAPL")
|
| 93 |
+
- The AI shows which tools it used for transparency
|
| 94 |
|
| 95 |
## 👍 Supported Companies
|
| 96 |
|
app.py
CHANGED
|
@@ -2,73 +2,32 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
-
import time
|
| 6 |
-
from requests.adapters import HTTPAdapter
|
| 7 |
-
from urllib3.util.retry import Retry
|
| 8 |
from huggingface_hub import InferenceClient
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
"
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
def create_session_with_retry():
|
| 22 |
-
"""创建带重试机制的 requests session"""
|
| 23 |
-
session = requests.Session()
|
| 24 |
-
retry = Retry(
|
| 25 |
-
total=3, # 最多重试3次
|
| 26 |
-
backoff_factor=1, # 重试间隔:1秒, 2秒, 4秒
|
| 27 |
-
status_forcelist=[500, 502, 503, 504], # 这些状态码会触发重试
|
| 28 |
-
)
|
| 29 |
-
adapter = HTTPAdapter(max_retries=retry)
|
| 30 |
-
session.mount('http://', adapter)
|
| 31 |
-
session.mount('https://', adapter)
|
| 32 |
-
return session
|
| 33 |
-
|
| 34 |
-
# 创建全局 session
|
| 35 |
-
session = create_session_with_retry()
|
| 36 |
-
|
| 37 |
-
# 初始化 Hugging Face Inference Client
|
| 38 |
-
# 使用 Qwen/Qwen2.5-72B-Instruct 模型(支持 tool calling)
|
| 39 |
-
try:
|
| 40 |
-
# Hugging Face Space 会自动提供 HF_TOKEN 环境变量
|
| 41 |
-
# 也支持手动在 Settings > Secrets 中配置 HF_TOKEN
|
| 42 |
-
hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
|
| 43 |
-
|
| 44 |
-
if hf_token:
|
| 45 |
-
client = InferenceClient(api_key=hf_token)
|
| 46 |
-
print(f"✅ Hugging Face client initialized with Qwen/Qwen2.5-72B-Instruct:novita model")
|
| 47 |
-
print(f" Using authenticated access (HF_TOKEN detected)")
|
| 48 |
-
else:
|
| 49 |
-
# 如果没有 token,使用无认证模式(免费层,有速率限制)
|
| 50 |
-
client = InferenceClient()
|
| 51 |
-
print("⚠️ Using Hugging Face Inference API without authentication (rate limited)")
|
| 52 |
-
print("💡 To remove rate limits, add HF_TOKEN in Space Settings > Repository secrets")
|
| 53 |
-
print(" Get your token from: https://huggingface.co/settings/tokens")
|
| 54 |
-
except Exception as e:
|
| 55 |
-
print(f"❌ Warning: Failed to initialize Hugging Face client: {e}")
|
| 56 |
-
client = None
|
| 57 |
-
|
| 58 |
-
# 定义可用的 MCP 工具
|
| 59 |
MCP_TOOLS = [
|
|
|
|
| 60 |
{
|
| 61 |
"type": "function",
|
| 62 |
"function": {
|
| 63 |
"name": "advanced_search_company",
|
| 64 |
-
"description": "Search for
|
| 65 |
"parameters": {
|
| 66 |
"type": "object",
|
| 67 |
"properties": {
|
| 68 |
-
"company_input": {
|
| 69 |
-
"type": "string",
|
| 70 |
-
"description": "Company name or stock ticker symbol (e.g., 'Apple', 'AAPL', 'Microsoft', 'TSLA')"
|
| 71 |
-
}
|
| 72 |
},
|
| 73 |
"required": ["company_input"]
|
| 74 |
}
|
|
@@ -78,14 +37,11 @@ MCP_TOOLS = [
|
|
| 78 |
"type": "function",
|
| 79 |
"function": {
|
| 80 |
"name": "get_latest_financial_data",
|
| 81 |
-
"description": "Get
|
| 82 |
"parameters": {
|
| 83 |
"type": "object",
|
| 84 |
"properties": {
|
| 85 |
-
"cik": {
|
| 86 |
-
"type": "string",
|
| 87 |
-
"description": "10-digit CIK number of the company (must be obtained from advanced_search_company first)"
|
| 88 |
-
}
|
| 89 |
},
|
| 90 |
"required": ["cik"]
|
| 91 |
}
|
|
@@ -95,681 +51,202 @@ MCP_TOOLS = [
|
|
| 95 |
"type": "function",
|
| 96 |
"function": {
|
| 97 |
"name": "extract_financial_metrics",
|
| 98 |
-
"description": "
|
| 99 |
"parameters": {
|
| 100 |
"type": "object",
|
| 101 |
"properties": {
|
| 102 |
-
"cik": {
|
| 103 |
-
|
| 104 |
-
"description": "10-digit CIK number of the company"
|
| 105 |
-
},
|
| 106 |
-
"years": {
|
| 107 |
-
"type": "integer",
|
| 108 |
-
"description": "Number of years to retrieve (typically 3 or 5)",
|
| 109 |
-
"enum": [3, 5]
|
| 110 |
-
}
|
| 111 |
},
|
| 112 |
"required": ["cik", "years"]
|
| 113 |
}
|
| 114 |
}
|
| 115 |
-
}
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
return "N/A"
|
| 127 |
-
|
| 128 |
-
if value_type == "money":
|
| 129 |
-
return f"${value:.2f}B"
|
| 130 |
-
elif value_type == "eps":
|
| 131 |
-
return f"${value:.2f}"
|
| 132 |
-
else: # number
|
| 133 |
-
return f"{value:.2f}"
|
| 134 |
-
|
| 135 |
-
def call_mcp_tool(tool_name, arguments):
|
| 136 |
-
"""调用 MCP 工具并返回结果"""
|
| 137 |
-
try:
|
| 138 |
-
# 构建完整的 URL
|
| 139 |
-
full_url = f"{MCP_URL}{MCP_ENDPOINT}"
|
| 140 |
-
|
| 141 |
-
# FastMCP HTTP Server 使用 /mcp 端点
|
| 142 |
-
response = session.post(
|
| 143 |
-
full_url,
|
| 144 |
-
json={
|
| 145 |
-
"jsonrpc": "2.0",
|
| 146 |
-
"method": "tools/call",
|
| 147 |
-
"params": {
|
| 148 |
-
"name": tool_name,
|
| 149 |
-
"arguments": arguments
|
| 150 |
},
|
| 151 |
-
"
|
| 152 |
-
},
|
| 153 |
-
headers=HEADERS,
|
| 154 |
-
timeout=60
|
| 155 |
-
)
|
| 156 |
-
|
| 157 |
-
# 调试信息
|
| 158 |
-
print(f"DEBUG: Calling {full_url}")
|
| 159 |
-
print(f"DEBUG: Tool: {tool_name}, Args: {arguments}")
|
| 160 |
-
print(f"DEBUG: Status Code: {response.status_code}")
|
| 161 |
-
print(f"DEBUG: Response: {response.text[:500]}")
|
| 162 |
-
|
| 163 |
-
if response.status_code != 200:
|
| 164 |
-
return {
|
| 165 |
-
"error": f"HTTP {response.status_code}",
|
| 166 |
-
"detail": response.text,
|
| 167 |
-
"url": full_url
|
| 168 |
}
|
| 169 |
-
|
| 170 |
-
return response.json()
|
| 171 |
-
except Exception as e:
|
| 172 |
-
return {
|
| 173 |
-
"error": str(e),
|
| 174 |
-
"url": full_url if 'full_url' in locals() else MCP_URL
|
| 175 |
}
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
return None
|
| 183 |
-
# 转换为字符串并移除非数字字符
|
| 184 |
-
cik_str = str(cik).replace('-', '').replace(' ', '')
|
| 185 |
-
# 仅保留数字
|
| 186 |
-
cik_str = ''.join(c for c in cik_str if c.isdigit())
|
| 187 |
-
# 填充前导 0 至 10 位
|
| 188 |
-
return cik_str.zfill(10) if cik_str else None
|
| 189 |
-
|
| 190 |
-
def parse_mcp_response(response_data):
|
| 191 |
-
"""
|
| 192 |
-
解析 MCP 协议响应数据
|
| 193 |
-
支持格式:
|
| 194 |
-
1. {"result": {"content": [{"type": "text", "text": "{...}"}]}}
|
| 195 |
-
2. {"content": [{"type": "text", "text": "{...}"}]}
|
| 196 |
-
3. 直接的 JSON 数据
|
| 197 |
-
"""
|
| 198 |
-
if not isinstance(response_data, dict):
|
| 199 |
-
return response_data
|
| 200 |
-
|
| 201 |
-
# 格式 1: {"result": {"content": [...]}}
|
| 202 |
-
if "result" in response_data and "content" in response_data["result"]:
|
| 203 |
-
content = response_data["result"]["content"]
|
| 204 |
-
if content and len(content) > 0:
|
| 205 |
-
text_content = content[0].get("text", "{}")
|
| 206 |
-
# 直接解析 JSON(MCP Server 已移除 emoji 前缀)
|
| 207 |
-
try:
|
| 208 |
-
return json.loads(text_content)
|
| 209 |
-
except json.JSONDecodeError:
|
| 210 |
-
return text_content
|
| 211 |
-
return {}
|
| 212 |
-
|
| 213 |
-
# 格式 2: {"content": [...]}
|
| 214 |
-
elif "content" in response_data:
|
| 215 |
-
content = response_data.get("content", [])
|
| 216 |
-
if content and len(content) > 0:
|
| 217 |
-
text_content = content[0].get("text", "{}")
|
| 218 |
-
# 直接解析 JSON
|
| 219 |
-
try:
|
| 220 |
-
return json.loads(text_content)
|
| 221 |
-
except json.JSONDecodeError:
|
| 222 |
-
return text_content
|
| 223 |
-
return {}
|
| 224 |
-
|
| 225 |
-
# 格式 3: 直接返回
|
| 226 |
-
return response_data
|
| 227 |
-
|
| 228 |
-
# MCP 工具定义
|
| 229 |
-
def create_mcp_tools():
|
| 230 |
-
"""创建 MCP 工具列表"""
|
| 231 |
-
return [
|
| 232 |
-
{
|
| 233 |
-
"name": "query_financial_data",
|
| 234 |
-
"description": "Query SEC financial data for US listed companies",
|
| 235 |
"parameters": {
|
| 236 |
"type": "object",
|
| 237 |
"properties": {
|
| 238 |
-
"
|
| 239 |
-
|
| 240 |
-
"description": "Company name or stock symbol (e.g., Apple, NVIDIA, AAPL)"
|
| 241 |
-
},
|
| 242 |
-
"query_type": {
|
| 243 |
-
"type": "string",
|
| 244 |
-
"enum": ["Latest Financial Data", "3-Year Trends", "5-Year Trends"],
|
| 245 |
-
"description": "Type of financial query"
|
| 246 |
-
}
|
| 247 |
},
|
| 248 |
-
"required": ["
|
| 249 |
}
|
| 250 |
}
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
-
#
|
| 254 |
-
|
| 255 |
-
""
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
if not source_form or source_form == 'N/A':
|
| 263 |
-
return source_form
|
| 264 |
-
|
| 265 |
-
# 如果后端提供了URL,使用后端的URL
|
| 266 |
-
if source_url and source_url != 'N/A':
|
| 267 |
-
return f"[{source_form}]({source_url})"
|
| 268 |
-
|
| 269 |
-
# 如果没有URL,只显示文本
|
| 270 |
-
return source_form
|
| 271 |
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
if "error" in search_result:
|
| 297 |
-
return f"❌ Server Error: {search_result.get('error')}\n\nResponse: {search_result.get('detail', 'N/A')}\n\nURL: {search_result.get('url', MCP_URL)}"
|
| 298 |
-
|
| 299 |
-
# 解析搜索结果
|
| 300 |
-
company = parse_mcp_response(search_result)
|
| 301 |
-
|
| 302 |
-
if isinstance(company, dict) and company.get("error"):
|
| 303 |
-
return f"❌ Error: {company['error']}"
|
| 304 |
-
|
| 305 |
-
# advanced_search 返回的字段: cik, name, ticker
|
| 306 |
-
# 注意: 不是 tickers 和 sic_description
|
| 307 |
-
company_name = company.get('name', 'Unknown')
|
| 308 |
-
ticker = company.get('ticker', 'N/A')
|
| 309 |
-
|
| 310 |
-
result = f"# {company_name}\n\n"
|
| 311 |
-
result += f"**Stock Symbol**: {ticker}\n"
|
| 312 |
-
# sic_description 需要后续通过 get_company_info 获取,这里暂时不显示
|
| 313 |
-
result += "\n---\n\n"
|
| 314 |
-
|
| 315 |
-
# 获取并格式化 CIK 为 10 位标准格式
|
| 316 |
-
cik = normalize_cik(company.get('cik'))
|
| 317 |
-
if not cik:
|
| 318 |
-
return result + f"❌ Error: Invalid CIK from company search\n\nDebug: company data = {json.dumps(company, indent=2)}"
|
| 319 |
-
|
| 320 |
-
# 根据查询类型获取数据
|
| 321 |
-
if internal_query_type == "最新财务数据":
|
| 322 |
-
data_resp = session.post(
|
| 323 |
-
f"{MCP_URL}/mcp",
|
| 324 |
-
json={
|
| 325 |
-
"jsonrpc": "2.0",
|
| 326 |
-
"method": "tools/call",
|
| 327 |
-
"params": {
|
| 328 |
-
"name": "get_latest_financial_data",
|
| 329 |
-
"arguments": {"cik": cik}
|
| 330 |
-
},
|
| 331 |
-
"id": 1
|
| 332 |
-
},
|
| 333 |
-
headers=HEADERS,
|
| 334 |
-
timeout=60 # 增加到60秒
|
| 335 |
-
)
|
| 336 |
-
|
| 337 |
-
if data_resp.status_code != 200:
|
| 338 |
-
return result + f"❌ Server Error: HTTP {data_resp.status_code}\n\n{data_resp.text[:500]}"
|
| 339 |
-
|
| 340 |
-
try:
|
| 341 |
-
data_result = data_resp.json()
|
| 342 |
-
# 使用统一的 MCP 响应解析函数
|
| 343 |
-
data = parse_mcp_response(data_result)
|
| 344 |
-
except (ValueError, KeyError, json.JSONDecodeError) as e:
|
| 345 |
-
return result + f"❌ JSON Parse Error: {str(e)}\n\n{data_resp.text[:500]}"
|
| 346 |
-
|
| 347 |
-
if isinstance(data, dict) and data.get("error"):
|
| 348 |
-
return result + f"❌ {data['error']}"
|
| 349 |
-
|
| 350 |
-
cik = data.get('cik')
|
| 351 |
-
result += f"## Fiscal Year {data.get('period', 'N/A')}\n\n"
|
| 352 |
-
|
| 353 |
-
total_revenue = data.get('total_revenue', 0) / 1e9 if data.get('total_revenue') else 0
|
| 354 |
-
net_income = data.get('net_income', 0) / 1e9 if data.get('net_income') else 0
|
| 355 |
-
eps = data.get('earnings_per_share', 0) if data.get('earnings_per_share') else 0
|
| 356 |
-
opex = data.get('operating_expenses', 0) / 1e9 if data.get('operating_expenses') else 0
|
| 357 |
-
ocf = data.get('operating_cash_flow', 0) / 1e9 if data.get('operating_cash_flow') else 0
|
| 358 |
-
|
| 359 |
-
result += f"- **Total Revenue**: {format_value(total_revenue)}\n"
|
| 360 |
-
result += f"- **Net Income**: {format_value(net_income)}\n"
|
| 361 |
-
result += f"- **Earnings Per Share**: {format_value(eps, 'eps')}\n"
|
| 362 |
-
result += f"- **Operating Expenses**: {format_value(opex)}\n"
|
| 363 |
-
result += f"- **Operating Cash Flow**: {format_value(ocf)}\n"
|
| 364 |
-
# 使用后端返回的 source_url
|
| 365 |
-
source_form = data.get('source_form', 'N/A')
|
| 366 |
-
source_url = data.get('source_url', None) # 从后端获取URL
|
| 367 |
-
result += f"- **Source Form**: {create_source_link(source_form, source_url)}\n"
|
| 368 |
-
|
| 369 |
-
elif internal_query_type == "3年趋势":
|
| 370 |
-
metrics_resp = session.post(
|
| 371 |
-
f"{MCP_URL}/mcp",
|
| 372 |
-
json={
|
| 373 |
-
"jsonrpc": "2.0",
|
| 374 |
-
"method": "tools/call",
|
| 375 |
-
"params": {
|
| 376 |
-
"name": "extract_financial_metrics",
|
| 377 |
-
"arguments": {"cik": cik, "years": 3}
|
| 378 |
-
},
|
| 379 |
-
"id": 1
|
| 380 |
-
},
|
| 381 |
-
headers=HEADERS,
|
| 382 |
-
timeout=120 # 3年趋势需要更长时间,增加到120秒
|
| 383 |
-
)
|
| 384 |
-
|
| 385 |
-
if metrics_resp.status_code != 200:
|
| 386 |
-
return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
|
| 387 |
-
|
| 388 |
-
try:
|
| 389 |
-
metrics_result = metrics_resp.json()
|
| 390 |
-
# 使用统一的 MCP 响应解析函数
|
| 391 |
-
metrics = parse_mcp_response(metrics_result)
|
| 392 |
-
except (ValueError, KeyError, json.JSONDecodeError) as e:
|
| 393 |
-
return result + f"❌ JSON Parse Error: {str(e)}\n\nResponse: {metrics_resp.text[:500]}"
|
| 394 |
-
|
| 395 |
-
if isinstance(metrics, dict) and metrics.get("error"):
|
| 396 |
-
return result + f"❌ {metrics['error']}"
|
| 397 |
-
|
| 398 |
-
result += f"## 3-Year Financial Trends ({metrics.get('periods', 0)} periods)\n\n"
|
| 399 |
-
|
| 400 |
-
# 显示所有数据(包括年度和季度)
|
| 401 |
-
all_data = metrics.get('data', []) # MCP Server 返回的字段是 'data'
|
| 402 |
-
|
| 403 |
-
# 去重:根据period和source_form去重
|
| 404 |
-
seen = set()
|
| 405 |
-
unique_data = []
|
| 406 |
-
for m in all_data:
|
| 407 |
-
key = (m.get('period', 'N/A'), m.get('source_form', 'N/A'))
|
| 408 |
-
if key not in seen:
|
| 409 |
-
seen.add(key)
|
| 410 |
-
unique_data.append(m)
|
| 411 |
-
|
| 412 |
-
# 按期间降序排序,确保显示最近的3年数据
|
| 413 |
-
# 使用更智能的排序:先按年份,再按是否是季度
|
| 414 |
-
# 正确顺序:FY2024 → 2024Q3 → 2024Q2 → 2024Q1 → FY2023
|
| 415 |
-
def sort_key(x):
|
| 416 |
-
period = x.get('period', '0000')
|
| 417 |
-
# 提取年份(前4位)
|
| 418 |
-
year = period[:4] if len(period) >= 4 else '0000'
|
| 419 |
-
# 如果有Q,提取季度号
|
| 420 |
-
if 'Q' in period:
|
| 421 |
-
quarter = period[period.index('Q')+1] if period.index('Q')+1 < len(period) else '0'
|
| 422 |
-
return (year, 1, 4 - int(quarter)) # Q在FY后面:Q3, Q2, Q1 (4-3=1, 4-2=2, 4-1=3)
|
| 423 |
-
else:
|
| 424 |
-
return (year, 0, 0) # FY 排在同年的所有Q之前
|
| 425 |
-
|
| 426 |
-
unique_data = sorted(unique_data, key=sort_key, reverse=True)
|
| 427 |
-
|
| 428 |
-
result += "| Period | Revenue (B) | Net Income (B) | EPS | Operating Expenses (B) | Operating Cash Flow (B) | Source Form |\n"
|
| 429 |
-
result += "|--------|-------------|----------------|-----|------------------------|-------------------------|-------------|\n"
|
| 430 |
-
|
| 431 |
-
for m in unique_data:
|
| 432 |
-
period = m.get('period', 'N/A')
|
| 433 |
-
rev = (m.get('total_revenue') or 0) / 1e9
|
| 434 |
-
inc = (m.get('net_income') or 0) / 1e9
|
| 435 |
-
eps_val = m.get('earnings_per_share') or 0
|
| 436 |
-
opex = (m.get('operating_expenses') or 0) / 1e9
|
| 437 |
-
ocf = (m.get('operating_cash_flow') or 0) / 1e9
|
| 438 |
-
source_form = m.get('source_form', 'N/A')
|
| 439 |
-
source_url = m.get('source_url', None) # 从后端获取URL
|
| 440 |
-
|
| 441 |
-
# 区分年度和季度,修复双重FY前缀问题
|
| 442 |
-
if 'Q' in period:
|
| 443 |
-
# 季度数据,不添加前缀
|
| 444 |
-
display_period = period
|
| 445 |
-
else:
|
| 446 |
-
# 年度数据,只在没有FY的情况下添加
|
| 447 |
-
display_period = period if period.startswith('FY') else f"FY{period}"
|
| 448 |
-
|
| 449 |
-
source_link = create_source_link(source_form, source_url)
|
| 450 |
-
|
| 451 |
-
result += f"| {display_period} | {format_value(rev)} | {format_value(inc)} | {format_value(eps_val, 'eps')} | {format_value(opex)} | {format_value(ocf)} | {source_link} |\n"
|
| 452 |
-
|
| 453 |
-
elif internal_query_type == "5年趋势":
|
| 454 |
-
metrics_resp = session.post(
|
| 455 |
-
f"{MCP_URL}/mcp",
|
| 456 |
-
json={
|
| 457 |
-
"jsonrpc": "2.0",
|
| 458 |
-
"method": "tools/call",
|
| 459 |
-
"params": {
|
| 460 |
-
"name": "extract_financial_metrics",
|
| 461 |
-
"arguments": {"cik": cik, "years": 5}
|
| 462 |
-
},
|
| 463 |
-
"id": 1
|
| 464 |
-
},
|
| 465 |
-
headers=HEADERS,
|
| 466 |
-
timeout=180 # 5年趋势需要更长时间,增加到180秒
|
| 467 |
-
)
|
| 468 |
-
|
| 469 |
-
if metrics_resp.status_code != 200:
|
| 470 |
-
return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
|
| 471 |
-
|
| 472 |
-
try:
|
| 473 |
-
metrics_result = metrics_resp.json()
|
| 474 |
-
# 使用统一的 MCP 响应解析函数
|
| 475 |
-
metrics = parse_mcp_response(metrics_result)
|
| 476 |
-
except (ValueError, KeyError, json.JSONDecodeError) as e:
|
| 477 |
-
return result + f"❌ JSON Parse Error: {str(e)}\n\nResponse: {metrics_resp.text[:500]}"
|
| 478 |
-
|
| 479 |
-
if isinstance(metrics, dict) and metrics.get("error"):
|
| 480 |
-
return result + f"❌ {metrics['error']}"
|
| 481 |
-
|
| 482 |
-
# 显示所有数据(包括年度和季度)
|
| 483 |
-
all_data = metrics.get('data', []) # MCP Server 返回的字段是 'data'
|
| 484 |
-
|
| 485 |
-
# 去重:根据period和source_form去重
|
| 486 |
-
seen = set()
|
| 487 |
-
unique_data = []
|
| 488 |
-
for m in all_data:
|
| 489 |
-
key = (m.get('period', 'N/A'), m.get('source_form', 'N/A'))
|
| 490 |
-
if key not in seen:
|
| 491 |
-
seen.add(key)
|
| 492 |
-
unique_data.append(m)
|
| 493 |
-
|
| 494 |
-
# 按期间降序排序,确保显示最近的5年数据
|
| 495 |
-
# 使用更智能的排序:先按年份,再按是否是季度
|
| 496 |
-
# 正确顺序:FY2024 → 2024Q3 → 2024Q2 → 2024Q1 → FY2023
|
| 497 |
-
def sort_key(x):
|
| 498 |
-
period = x.get('period', '0000')
|
| 499 |
-
# 提取年份(前4位)
|
| 500 |
-
year = period[:4] if len(period) >= 4 else '0000'
|
| 501 |
-
# 如果有Q,提取季度号
|
| 502 |
-
if 'Q' in period:
|
| 503 |
-
quarter = period[period.index('Q')+1] if period.index('Q')+1 < len(period) else '0'
|
| 504 |
-
return (year, 1, 4 - int(quarter)) # Q在FY后面:Q3, Q2, Q1 (4-3=1, 4-2=2, 4-1=3)
|
| 505 |
-
else:
|
| 506 |
-
return (year, 0, 0) # FY 排在同年的所有Q之前
|
| 507 |
-
|
| 508 |
-
unique_data = sorted(unique_data, key=sort_key, reverse=True)
|
| 509 |
-
|
| 510 |
-
result += f"## 5-Year Financial Trends ({metrics.get('periods', 0)} periods)\n\n"
|
| 511 |
-
result += "| Period | Revenue (B) | Net Income (B) | EPS | Operating Expenses (B) | Operating Cash Flow (B) | Source Form |\n"
|
| 512 |
-
result += "|--------|-------------|----------------|-----|------------------------|-------------------------|-------------|\n"
|
| 513 |
-
|
| 514 |
-
for m in unique_data:
|
| 515 |
-
period = m.get('period', 'N/A')
|
| 516 |
-
rev = (m.get('total_revenue') or 0) / 1e9
|
| 517 |
-
inc = (m.get('net_income') or 0) / 1e9
|
| 518 |
-
eps_val = m.get('earnings_per_share') or 0
|
| 519 |
-
opex = (m.get('operating_expenses') or 0) / 1e9
|
| 520 |
-
ocf = (m.get('operating_cash_flow') or 0) / 1e9
|
| 521 |
-
source_form = m.get('source_form', 'N/A')
|
| 522 |
-
source_url = m.get('source_url', None) # 从后端获取URL
|
| 523 |
-
|
| 524 |
-
# 区分年度和季度,修复双重FY前缀问题
|
| 525 |
-
if 'Q' in period:
|
| 526 |
-
# 季度数据,不添加前缀
|
| 527 |
-
display_period = period
|
| 528 |
-
else:
|
| 529 |
-
# 年度数据,只在没有FY的情况下添加
|
| 530 |
-
display_period = period if period.startswith('FY') else f"FY{period}"
|
| 531 |
-
|
| 532 |
-
source_link = create_source_link(source_form, source_url)
|
| 533 |
-
|
| 534 |
-
result += f"| {display_period} | {format_value(rev)} | {format_value(inc)} | {format_value(eps_val, 'eps')} | {format_value(opex)} | {format_value(ocf)} | {source_link} |\n"
|
| 535 |
-
|
| 536 |
-
elif internal_query_type == "公司报表列表":
|
| 537 |
-
# 查询公司所有报表
|
| 538 |
-
filings_resp = session.post(
|
| 539 |
-
f"{MCP_URL}/mcp",
|
| 540 |
-
json={
|
| 541 |
-
"jsonrpc": "2.0",
|
| 542 |
-
"method": "tools/call",
|
| 543 |
-
"params": {
|
| 544 |
-
"name": "get_company_filings",
|
| 545 |
-
"arguments": {"cik": cik, "limit": 50}
|
| 546 |
-
},
|
| 547 |
-
"id": 1
|
| 548 |
-
},
|
| 549 |
-
headers=HEADERS,
|
| 550 |
-
timeout=90 # 增加到90秒
|
| 551 |
-
)
|
| 552 |
-
|
| 553 |
-
if filings_resp.status_code != 200:
|
| 554 |
-
return result + f"❌ Server Error: HTTP {filings_resp.status_code}\n\n{filings_resp.text[:500]}"
|
| 555 |
-
|
| 556 |
-
try:
|
| 557 |
-
filings_result = filings_resp.json()
|
| 558 |
-
# 使用统一的 MCP 响应解析函数
|
| 559 |
-
filings_data = parse_mcp_response(filings_result)
|
| 560 |
-
except (ValueError, KeyError, json.JSONDecodeError) as e:
|
| 561 |
-
return result + f"❌ JSON Parse Error: {str(e)}\n\n{filings_resp.text[:500]}"
|
| 562 |
-
|
| 563 |
-
if isinstance(filings_data, dict) and filings_data.get("error"):
|
| 564 |
-
return result + f"❌ {filings_data['error']}"
|
| 565 |
-
|
| 566 |
-
filings = filings_data.get('filings', []) if isinstance(filings_data, dict) else filings_data
|
| 567 |
-
|
| 568 |
-
result += f"## Company Filings ({len(filings)} records)\n\n"
|
| 569 |
-
result += "| Form Type | Filing Date | Accession Number | Primary Document |\n"
|
| 570 |
-
result += "|-----------|-------------|------------------|------------------|\n"
|
| 571 |
-
|
| 572 |
-
for filing in filings:
|
| 573 |
-
form_type = filing.get('form_type', 'N/A')
|
| 574 |
-
filing_date = filing.get('filing_date', 'N/A')
|
| 575 |
-
accession_num = filing.get('accession_number', 'N/A')
|
| 576 |
-
primary_doc = filing.get('primary_document', 'N/A')
|
| 577 |
-
filing_url = filing.get('filing_url', None) # 从后端获取URL
|
| 578 |
-
|
| 579 |
-
# 使用后端返回的URL创建链接
|
| 580 |
-
if filing_url and filing_url != 'N/A':
|
| 581 |
-
form_link = f"[{form_type}]({filing_url})"
|
| 582 |
-
primary_doc_link = f"[{primary_doc}]({filing_url})"
|
| 583 |
-
else:
|
| 584 |
-
form_link = form_type
|
| 585 |
-
primary_doc_link = primary_doc
|
| 586 |
-
|
| 587 |
-
result += f"| {form_link} | {filing_date} | {accession_num} | {primary_doc_link} |\n"
|
| 588 |
-
|
| 589 |
-
return result
|
| 590 |
-
|
| 591 |
-
except requests.exceptions.RequestException as e:
|
| 592 |
-
return f"❌ Network Error: {str(e)}\n\nMCP Server: {MCP_URL}"
|
| 593 |
-
except Exception as e:
|
| 594 |
-
import traceback
|
| 595 |
-
return f"❌ Unexpected Error: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
| 596 |
|
| 597 |
-
#
|
| 598 |
def call_mcp_tool(tool_name, arguments):
|
| 599 |
-
"""调用 MCP
|
|
|
|
|
|
|
|
|
|
|
|
|
| 600 |
try:
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
f"{MCP_URL}/mcp",
|
| 604 |
json={
|
| 605 |
"jsonrpc": "2.0",
|
| 606 |
"method": "tools/call",
|
| 607 |
-
"params": {
|
| 608 |
-
"name": tool_name,
|
| 609 |
-
"arguments": arguments
|
| 610 |
-
},
|
| 611 |
"id": 1
|
| 612 |
},
|
| 613 |
-
headers=
|
| 614 |
timeout=60
|
| 615 |
)
|
| 616 |
|
| 617 |
-
if response.status_code
|
| 618 |
-
return
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
return parse_mcp_response(result)
|
| 622 |
except Exception as e:
|
| 623 |
return {"error": str(e)}
|
| 624 |
|
| 625 |
-
#
|
| 626 |
def chatbot_response(message, history):
|
| 627 |
-
"""
|
| 628 |
try:
|
| 629 |
-
#
|
| 630 |
-
messages = []
|
| 631 |
|
| 632 |
-
#
|
| 633 |
-
system_prompt = """You are an intelligent financial analysis assistant with access to real-time SEC EDGAR data.
|
| 634 |
-
|
| 635 |
-
You have 3 powerful tools to fetch financial data:
|
| 636 |
-
1. advanced_search_company(company_input) - Search for any US company
|
| 637 |
-
2. get_latest_financial_data(cik) - Get latest financial report
|
| 638 |
-
3. extract_financial_metrics(cik, years) - Get multi-year trends (3 or 5 years)
|
| 639 |
-
|
| 640 |
-
When users ask about a company:
|
| 641 |
-
- Automatically use tools to fetch the data they need
|
| 642 |
-
- Analyze the numbers and provide insights
|
| 643 |
-
- Explain trends, growth rates, and what they mean
|
| 644 |
-
- Be conversational and helpful
|
| 645 |
-
|
| 646 |
-
You can handle any financial question - from simple data queries to complex multi-company comparisons. Be creative and thorough in your analysis."""
|
| 647 |
-
|
| 648 |
-
messages.append({"role": "system", "content": system_prompt})
|
| 649 |
-
|
| 650 |
-
# 添加历史对话(最近 5 轮)
|
| 651 |
-
# Gradio 6.x 的 history 格式可能是 [{"role": "user", "content": ...}, {"role": "assistant", "content": ...}]
|
| 652 |
-
# 或者是 [(user_msg, assistant_msg), ...] 的元组列表
|
| 653 |
if history:
|
| 654 |
for item in history[-5:]:
|
| 655 |
if isinstance(item, dict):
|
| 656 |
-
# 新格式:字典列表
|
| 657 |
messages.append(item)
|
| 658 |
elif isinstance(item, (list, tuple)) and len(item) == 2:
|
| 659 |
-
# 旧格式:元组列表
|
| 660 |
user_msg, assistant_msg = item
|
| 661 |
messages.append({"role": "user", "content": user_msg})
|
| 662 |
messages.append({"role": "assistant", "content": assistant_msg})
|
| 663 |
|
| 664 |
-
# 添加当前消息
|
| 665 |
messages.append({"role": "user", "content": message})
|
| 666 |
|
| 667 |
-
#
|
| 668 |
-
response_text = ""
|
| 669 |
tool_calls_log = []
|
| 670 |
-
max_iterations = 5
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 675 |
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
return fallback_chatbot_response(message)
|
| 682 |
-
|
| 683 |
-
response = client.chat_completion(
|
| 684 |
-
messages=messages,
|
| 685 |
-
model="Qwen/Qwen2.5-72B-Instruct:novita", # 使用 novita provider,更稳定
|
| 686 |
-
tools=MCP_TOOLS,
|
| 687 |
-
max_tokens=3000,
|
| 688 |
-
temperature=0.7,
|
| 689 |
-
tool_choice="auto"
|
| 690 |
-
)
|
| 691 |
-
|
| 692 |
-
print(f"✅ LLM response received (iteration {iteration})")
|
| 693 |
-
|
| 694 |
-
choice = response.choices[0]
|
| 695 |
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
print(f"🔧 Tool calls detected: {len(choice.message.tool_calls)}")
|
| 700 |
-
messages.append(choice.message)
|
| 701 |
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
tool_args = json.loads(tool_call.function.arguments)
|
| 705 |
-
|
| 706 |
-
print(f" → Calling tool: {tool_name} with args: {tool_args}")
|
| 707 |
-
|
| 708 |
-
# 记录工具调用
|
| 709 |
-
tool_calls_log.append({
|
| 710 |
-
"name": tool_name,
|
| 711 |
-
"arguments": tool_args
|
| 712 |
-
})
|
| 713 |
-
|
| 714 |
-
# 调用 MCP 工具
|
| 715 |
-
tool_result = call_mcp_tool(tool_name, tool_args)
|
| 716 |
-
|
| 717 |
-
print(f" ← Tool result received")
|
| 718 |
-
|
| 719 |
-
# 将工具结果添加到消息列表
|
| 720 |
-
messages.append({
|
| 721 |
-
"role": "tool",
|
| 722 |
-
"name": tool_name,
|
| 723 |
-
"content": json.dumps(tool_result),
|
| 724 |
-
"tool_call_id": tool_call.id
|
| 725 |
-
})
|
| 726 |
|
| 727 |
-
#
|
| 728 |
-
|
| 729 |
-
else:
|
| 730 |
-
# 没有工具调用,直接返回回答
|
| 731 |
-
print(f"💬 Final response generated")
|
| 732 |
-
response_text = choice.message.content
|
| 733 |
-
break
|
| 734 |
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
#
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
try:
|
| 749 |
-
response = client.chat_completion(
|
| 750 |
-
messages=messages,
|
| 751 |
-
model="Qwen/Qwen2.5-72B-Instruct:novita",
|
| 752 |
-
max_tokens=3000,
|
| 753 |
-
temperature=0.7
|
| 754 |
-
)
|
| 755 |
-
response_text = response.choices[0].message.content
|
| 756 |
-
return response_text
|
| 757 |
-
except:
|
| 758 |
-
pass
|
| 759 |
-
print("ℹ️ Falling back to simple response logic")
|
| 760 |
-
return fallback_chatbot_response(message)
|
| 761 |
|
| 762 |
# 构建最终响应
|
| 763 |
final_response = ""
|
| 764 |
|
| 765 |
-
#
|
| 766 |
-
final_response += f"<div style='padding: 8px; background: #e3f2fd; border-left: 3px solid #2196f3; margin-bottom: 10px; font-size: 0.9em;'>🤖 <strong>Model:</strong> Qwen/Qwen2.5-72B-Instruct:novita
|
| 767 |
|
| 768 |
-
#
|
| 769 |
if tool_calls_log:
|
| 770 |
final_response += "**🛠️ MCP Tools Used:**\n\n"
|
| 771 |
for i, tool_call in enumerate(tool_calls_log, 1):
|
| 772 |
-
final_response += f"{i}. `{tool_call['name']}`
|
| 773 |
final_response += "\n---\n\n"
|
| 774 |
|
| 775 |
final_response += response_text
|
|
@@ -777,156 +254,38 @@ You can handle any financial question - from simple data queries to complex mult
|
|
| 777 |
return final_response
|
| 778 |
|
| 779 |
except Exception as e:
|
| 780 |
-
|
| 781 |
-
return f"❌ Error: {str(e)}\n\nTraceback:\n```\n{traceback.format_exc()}\n```"
|
| 782 |
-
|
| 783 |
-
def fallback_chatbot_response(message):
|
| 784 |
-
"""退回策略:当 LLM API 不可用时使用的简单逻辑"""
|
| 785 |
-
# 显示警告信息
|
| 786 |
-
fallback_warning = "<div style='padding: 10px; background: #fff3cd; border-left: 4px solid #ffc107; margin-bottom: 15px;'>⚠️ <strong>Notice:</strong> LLM API unavailable, using fallback logic.</div>\n\n"
|
| 787 |
-
|
| 788 |
-
# 检查是否是财务查询相关问题
|
| 789 |
-
financial_keywords = ['financial', 'revenue', 'income', 'earnings', 'cash flow', 'expenses', '财务', '收入', '利润', 'data', 'trend', 'performance']
|
| 790 |
-
|
| 791 |
-
if any(keyword in message.lower() for keyword in financial_keywords):
|
| 792 |
-
# 提取公司名称和查询类型
|
| 793 |
-
company_keywords = ['apple', 'microsoft', 'nvidia', 'tesla', 'alibaba', 'google', 'amazon', 'meta', 'tsla', 'aapl', 'msft', 'nvda', 'googl', 'amzn']
|
| 794 |
-
detected_company = None
|
| 795 |
-
|
| 796 |
-
for company in company_keywords:
|
| 797 |
-
if company in message.lower():
|
| 798 |
-
if company in ['aapl']: detected_company = 'Apple'
|
| 799 |
-
elif company in ['msft']: detected_company = 'Microsoft'
|
| 800 |
-
elif company in ['nvda']: detected_company = 'NVIDIA'
|
| 801 |
-
elif company in ['tsla']: detected_company = 'Tesla'
|
| 802 |
-
elif company in ['googl']: detected_company = 'Google'
|
| 803 |
-
elif company in ['amzn']: detected_company = 'Amazon'
|
| 804 |
-
else: detected_company = company.capitalize()
|
| 805 |
-
break
|
| 806 |
-
|
| 807 |
-
if detected_company:
|
| 808 |
-
# 根据问题内容选择查询类型
|
| 809 |
-
if any(word in message.lower() for word in ['trend', '趋势', 'history', 'historical', 'over time']):
|
| 810 |
-
if any(word in message for word in ['5', 'five', '五年']):
|
| 811 |
-
query_type = '5-Year Trends'
|
| 812 |
-
else:
|
| 813 |
-
query_type = '3-Year Trends'
|
| 814 |
-
else:
|
| 815 |
-
query_type = 'Latest Financial Data'
|
| 816 |
-
|
| 817 |
-
# 调用财务查询函数
|
| 818 |
-
result = query_financial_data(detected_company, query_type)
|
| 819 |
-
return fallback_warning + f"Here's the financial information for {detected_company}:\n\n{result}"
|
| 820 |
-
else:
|
| 821 |
-
return fallback_warning + "I can help you query financial data! Please specify a company name. For example: 'Show me Apple's latest financial data' or 'What's NVIDIA's 3-year trend?' \n\nSupported companies include: Apple, Microsoft, NVIDIA, Tesla, Alibaba, Google, Amazon, and more."
|
| 822 |
-
|
| 823 |
-
# 如果不是财务查询,返回通用回复
|
| 824 |
-
return fallback_warning + "Hello! I'm a financial data assistant powered by SEC EDGAR data. I can help you query financial information for US listed companies.\n\n**What I can do:**\n- Get latest financial data (revenue, income, EPS, etc.)\n- Show 3-year or 5-year financial trends\n- Provide detailed financial metrics\n\n**Try asking:**\n- 'Show me Apple's latest financial data'\n- 'What's NVIDIA's 3-year financial trend?'\n- 'How is Microsoft performing financially?'"
|
| 825 |
-
|
| 826 |
-
# 包装函数,显示加载状态
|
| 827 |
-
def query_with_status(company, query_type):
|
| 828 |
-
"""Query with loading status indicator"""
|
| 829 |
-
try:
|
| 830 |
-
# 返回加载状态和结果
|
| 831 |
-
yield "<div style='padding: 10px; background: #e3f2fd; border-left: 4px solid #2196f3; margin: 10px 0;'>🔄 <strong>Loading...</strong> Querying SEC EDGAR data for <strong>{}</strong>...</div>".format(company), ""
|
| 832 |
-
|
| 833 |
-
# 执行实际查询
|
| 834 |
-
result = query_financial_data(company, query_type)
|
| 835 |
-
|
| 836 |
-
# 返回成功状态和结果
|
| 837 |
-
yield "<div style='padding: 10px; background: #e8f5e9; border-left: 4px solid #4caf50; margin: 10px 0;'>✅ <strong>Query completed successfully!</strong></div>", result
|
| 838 |
-
|
| 839 |
-
except Exception as e:
|
| 840 |
-
# 返回错误状态
|
| 841 |
-
yield "<div style='padding: 10px; background: #ffebee; border-left: 4px solid #f44336; margin: 10px 0;'>❌ <strong>Error:</strong> {}</div>".format(str(e)), ""
|
| 842 |
|
| 843 |
-
#
|
| 844 |
-
with gr.Blocks(title="
|
| 845 |
-
gr.Markdown("# 🤖
|
| 846 |
|
| 847 |
-
# 显示 AI 功能说明
|
| 848 |
gr.Markdown("""
|
| 849 |
<div style='padding: 15px; background: #d4edda; border-left: 4px solid #28a745; margin: 10px 0; border-radius: 4px;'>
|
| 850 |
-
<strong>✅ AI
|
| 851 |
<br>
|
| 852 |
-
<strong
|
| 853 |
</div>
|
| 854 |
""")
|
| 855 |
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
"Evaluate Google's revenue growth and profit margins",
|
| 869 |
-
],
|
| 870 |
-
cache_examples=False,
|
| 871 |
-
title="📊 Financial Reporting Analysis Expert",
|
| 872 |
-
description="I'm a financial analysis expert specializing in SEC EDGAR data. Ask me to analyze any US-listed company's financial performance, and I'll provide professional insights based on real financial reports."
|
| 873 |
-
)
|
| 874 |
-
|
| 875 |
-
with gr.Tab("Direct Query"):
|
| 876 |
-
gr.Markdown("## 🔍 Direct Financial Data Query")
|
| 877 |
-
gr.Markdown("Select a company and query type to retrieve financial information.")
|
| 878 |
-
|
| 879 |
-
with gr.Row():
|
| 880 |
-
company_input = gr.Textbox(
|
| 881 |
-
label="Company Name or Stock Symbol",
|
| 882 |
-
placeholder="e.g., NVIDIA, Apple, Alibaba, AAPL",
|
| 883 |
-
scale=2
|
| 884 |
-
)
|
| 885 |
-
query_type = gr.Radio(
|
| 886 |
-
["Latest Financial Data", "3-Year Trends", "5-Year Trends", "Company Filings"],
|
| 887 |
-
label="Query Type",
|
| 888 |
-
value="Latest Financial Data",
|
| 889 |
-
scale=1
|
| 890 |
-
)
|
| 891 |
-
|
| 892 |
-
submit_btn = gr.Button("🔍 Query Financial Data", variant="primary", size="lg")
|
| 893 |
-
|
| 894 |
-
# 添加加载状态指示器
|
| 895 |
-
with gr.Row():
|
| 896 |
-
status_text = gr.Markdown("")
|
| 897 |
-
|
| 898 |
-
output = gr.Markdown(label="Query Results")
|
| 899 |
-
|
| 900 |
-
# 示例
|
| 901 |
-
gr.Examples(
|
| 902 |
-
examples=[
|
| 903 |
-
["NVIDIA", "Latest Financial Data"],
|
| 904 |
-
["Apple", "3-Year Trends"],
|
| 905 |
-
["Microsoft", "5-Year Trends"],
|
| 906 |
-
["Alibaba", "Company Filings"],
|
| 907 |
-
["Tesla", "3-Year Trends"]
|
| 908 |
-
],
|
| 909 |
-
inputs=[company_input, query_type],
|
| 910 |
-
outputs=output,
|
| 911 |
-
fn=query_financial_data,
|
| 912 |
-
cache_examples=False
|
| 913 |
-
)
|
| 914 |
-
|
| 915 |
-
submit_btn.click(
|
| 916 |
-
fn=query_with_status,
|
| 917 |
-
inputs=[company_input, query_type],
|
| 918 |
-
outputs=[status_text, output],
|
| 919 |
-
show_progress="full" # 显示完整的进度条
|
| 920 |
-
)
|
| 921 |
-
|
| 922 |
-
gr.Markdown("---")
|
| 923 |
-
gr.Markdown("**Data Source**: SEC EDGAR | **MCP Server**: https://huggingface.co/spaces/JC321/EasyReportDateMCP")
|
| 924 |
|
| 925 |
-
#
|
| 926 |
if __name__ == "__main__":
|
| 927 |
demo.launch(
|
| 928 |
server_name="0.0.0.0",
|
| 929 |
server_port=7860,
|
| 930 |
show_error=True,
|
| 931 |
-
ssr_mode=False
|
| 932 |
-
)
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
+
# ========== 配置两个 MCP 服务 ==========
|
| 8 |
+
MCP_SERVICES = {
|
| 9 |
+
"financial": {
|
| 10 |
+
"name": "SEC Financial Reports",
|
| 11 |
+
"url": "https://jc321-easyreportdatemcp.hf.space/mcp"
|
| 12 |
+
},
|
| 13 |
+
"market": {
|
| 14 |
+
"name": "Market & Stock Data (Finnhub)",
|
| 15 |
+
"url": "https://jc321-marketandstockmcp.hf.space/gradio_api/mcp/sse"
|
| 16 |
+
}
|
| 17 |
}
|
| 18 |
|
| 19 |
+
# MCP 工具定义(两个服务的工具合并)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
MCP_TOOLS = [
|
| 21 |
+
# Financial Reports Tools
|
| 22 |
{
|
| 23 |
"type": "function",
|
| 24 |
"function": {
|
| 25 |
"name": "advanced_search_company",
|
| 26 |
+
"description": "Search for US listed companies by name or ticker to get CIK and basic info",
|
| 27 |
"parameters": {
|
| 28 |
"type": "object",
|
| 29 |
"properties": {
|
| 30 |
+
"company_input": {"type": "string", "description": "Company name or ticker (e.g., 'Apple', 'AAPL')"}
|
|
|
|
|
|
|
|
|
|
| 31 |
},
|
| 32 |
"required": ["company_input"]
|
| 33 |
}
|
|
|
|
| 37 |
"type": "function",
|
| 38 |
"function": {
|
| 39 |
"name": "get_latest_financial_data",
|
| 40 |
+
"description": "Get latest SEC financial data (revenue, net income, EPS, cash flow, etc.)",
|
| 41 |
"parameters": {
|
| 42 |
"type": "object",
|
| 43 |
"properties": {
|
| 44 |
+
"cik": {"type": "string", "description": "10-digit CIK number"}
|
|
|
|
|
|
|
|
|
|
| 45 |
},
|
| 46 |
"required": ["cik"]
|
| 47 |
}
|
|
|
|
| 51 |
"type": "function",
|
| 52 |
"function": {
|
| 53 |
"name": "extract_financial_metrics",
|
| 54 |
+
"description": "Get multi-year financial trends (3 or 5 years)",
|
| 55 |
"parameters": {
|
| 56 |
"type": "object",
|
| 57 |
"properties": {
|
| 58 |
+
"cik": {"type": "string", "description": "10-digit CIK number"},
|
| 59 |
+
"years": {"type": "integer", "enum": [3, 5]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
},
|
| 61 |
"required": ["cik", "years"]
|
| 62 |
}
|
| 63 |
}
|
| 64 |
+
},
|
| 65 |
+
# Market & Stock Tools (Finnhub API)
|
| 66 |
+
{
|
| 67 |
+
"type": "function",
|
| 68 |
+
"function": {
|
| 69 |
+
"name": "get_quote",
|
| 70 |
+
"description": "Get real-time stock quote (price, volume, change, etc.) for a ticker symbol",
|
| 71 |
+
"parameters": {
|
| 72 |
+
"type": "object",
|
| 73 |
+
"properties": {
|
| 74 |
+
"symbol": {"type": "string", "description": "Stock ticker symbol (e.g., 'AAPL')"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
},
|
| 76 |
+
"required": ["symbol"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
}
|
| 79 |
+
},
|
| 80 |
+
{
|
| 81 |
+
"type": "function",
|
| 82 |
+
"function": {
|
| 83 |
+
"name": "get_market_news",
|
| 84 |
+
"description": "Get latest market news by category (general, forex, crypto, merger)",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
"parameters": {
|
| 86 |
"type": "object",
|
| 87 |
"properties": {
|
| 88 |
+
"category": {"type": "string", "enum": ["general", "forex", "crypto", "merger"], "description": "News category"},
|
| 89 |
+
"min_id": {"type": "integer", "description": "Minimum news ID (optional)"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
},
|
| 91 |
+
"required": ["category"]
|
| 92 |
}
|
| 93 |
}
|
| 94 |
+
},
|
| 95 |
+
{
|
| 96 |
+
"type": "function",
|
| 97 |
+
"function": {
|
| 98 |
+
"name": "get_company_news",
|
| 99 |
+
"description": "Get company-specific news for a stock symbol within a date range",
|
| 100 |
+
"parameters": {
|
| 101 |
+
"type": "object",
|
| 102 |
+
"properties": {
|
| 103 |
+
"symbol": {"type": "string", "description": "Stock ticker symbol (e.g., 'AAPL')"},
|
| 104 |
+
"from_date": {"type": "string", "description": "Start date (YYYY-MM-DD, default: 7 days ago)"},
|
| 105 |
+
"to_date": {"type": "string", "description": "End date (YYYY-MM-DD, default: today)"}
|
| 106 |
+
},
|
| 107 |
+
"required": ["symbol"]
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
]
|
| 112 |
|
| 113 |
+
# 工具路由:工具名 -> MCP 服务 URL
|
| 114 |
+
TOOL_ROUTING = {
|
| 115 |
+
"advanced_search_company": MCP_SERVICES["financial"]["url"],
|
| 116 |
+
"get_latest_financial_data": MCP_SERVICES["financial"]["url"],
|
| 117 |
+
"extract_financial_metrics": MCP_SERVICES["financial"]["url"],
|
| 118 |
+
"get_quote": MCP_SERVICES["market"]["url"],
|
| 119 |
+
"get_market_news": MCP_SERVICES["market"]["url"],
|
| 120 |
+
"get_company_news": MCP_SERVICES["market"]["url"],
|
| 121 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
# ========== 初始化 LLM 客户端 ==========
|
| 124 |
+
hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
|
| 125 |
+
client = InferenceClient(api_key=hf_token) if hf_token else InferenceClient()
|
| 126 |
+
print(f"✅ LLM initialized: Qwen/Qwen2.5-72B-Instruct:novita")
|
| 127 |
+
print(f"📊 MCP Services: {len(MCP_SERVICES)} services, {len(MCP_TOOLS)} tools")
|
| 128 |
+
print(f" - Financial: advanced_search_company, get_latest_financial_data, extract_financial_metrics")
|
| 129 |
+
print(f" - Market: get_quote, get_market_news, get_company_news")
|
| 130 |
+
|
| 131 |
+
# ========== 系统提示词 ==========
|
| 132 |
+
SYSTEM_PROMPT = """You are an intelligent financial and market analysis assistant.
|
| 133 |
+
|
| 134 |
+
You have access to TWO data sources:
|
| 135 |
+
|
| 136 |
+
1. **SEC Financial Reports** (Official filings)
|
| 137 |
+
- advanced_search_company: Find US companies
|
| 138 |
+
- get_latest_financial_data: Get latest 10-K/10-Q data
|
| 139 |
+
- extract_financial_metrics: Get multi-year trends
|
| 140 |
+
|
| 141 |
+
2. **Market & Stock Data** (Finnhub real-time)
|
| 142 |
+
- get_quote: Real-time stock price, volume, change
|
| 143 |
+
- get_market_news: Latest market news (general/forex/crypto/merger)
|
| 144 |
+
- get_company_news: Company-specific news
|
| 145 |
+
|
| 146 |
+
Automatically use the right tools and provide clear, data-driven insights."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
# ========== 核心函数:调用 MCP 工具 ==========
|
| 149 |
def call_mcp_tool(tool_name, arguments):
|
| 150 |
+
"""调用 MCP 工具"""
|
| 151 |
+
mcp_url = TOOL_ROUTING.get(tool_name)
|
| 152 |
+
if not mcp_url:
|
| 153 |
+
return {"error": f"Unknown tool: {tool_name}"}
|
| 154 |
+
|
| 155 |
try:
|
| 156 |
+
response = requests.post(
|
| 157 |
+
mcp_url,
|
|
|
|
| 158 |
json={
|
| 159 |
"jsonrpc": "2.0",
|
| 160 |
"method": "tools/call",
|
| 161 |
+
"params": {"name": tool_name, "arguments": arguments},
|
|
|
|
|
|
|
|
|
|
| 162 |
"id": 1
|
| 163 |
},
|
| 164 |
+
headers={"Content-Type": "application/json"},
|
| 165 |
timeout=60
|
| 166 |
)
|
| 167 |
|
| 168 |
+
if response.status_code == 200:
|
| 169 |
+
return response.json()
|
| 170 |
+
else:
|
| 171 |
+
return {"error": f"HTTP {response.status_code}", "detail": response.text[:200]}
|
|
|
|
| 172 |
except Exception as e:
|
| 173 |
return {"error": str(e)}
|
| 174 |
|
| 175 |
+
# ========== 核心函数:AI 助手 ==========
|
| 176 |
def chatbot_response(message, history):
|
| 177 |
+
"""AI 助手主函数"""
|
| 178 |
try:
|
| 179 |
+
# 构建消息历史
|
| 180 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 181 |
|
| 182 |
+
# 添加对话历史(最近5轮)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
if history:
|
| 184 |
for item in history[-5:]:
|
| 185 |
if isinstance(item, dict):
|
|
|
|
| 186 |
messages.append(item)
|
| 187 |
elif isinstance(item, (list, tuple)) and len(item) == 2:
|
|
|
|
| 188 |
user_msg, assistant_msg = item
|
| 189 |
messages.append({"role": "user", "content": user_msg})
|
| 190 |
messages.append({"role": "assistant", "content": assistant_msg})
|
| 191 |
|
|
|
|
| 192 |
messages.append({"role": "user", "content": message})
|
| 193 |
|
| 194 |
+
# LLM 调用循环(支持多轮工具调用)
|
|
|
|
| 195 |
tool_calls_log = []
|
| 196 |
+
max_iterations = 5
|
| 197 |
+
|
| 198 |
+
for iteration in range(max_iterations):
|
| 199 |
+
# 调用 LLM
|
| 200 |
+
response = client.chat_completion(
|
| 201 |
+
messages=messages,
|
| 202 |
+
model="Qwen/Qwen2.5-72B-Instruct:novita",
|
| 203 |
+
tools=MCP_TOOLS,
|
| 204 |
+
max_tokens=3000,
|
| 205 |
+
temperature=0.7,
|
| 206 |
+
tool_choice="auto"
|
| 207 |
+
)
|
| 208 |
|
| 209 |
+
choice = response.choices[0]
|
| 210 |
+
|
| 211 |
+
# 检查是否有工具调用
|
| 212 |
+
if choice.message.tool_calls:
|
| 213 |
+
messages.append(choice.message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
+
for tool_call in choice.message.tool_calls:
|
| 216 |
+
tool_name = tool_call.function.name
|
| 217 |
+
tool_args = json.loads(tool_call.function.arguments)
|
|
|
|
|
|
|
| 218 |
|
| 219 |
+
# 记录工具调用
|
| 220 |
+
tool_calls_log.append({"name": tool_name, "arguments": tool_args})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
+
# 调用 MCP 工具
|
| 223 |
+
tool_result = call_mcp_tool(tool_name, tool_args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
+
# 添加工具结果到消息
|
| 226 |
+
messages.append({
|
| 227 |
+
"role": "tool",
|
| 228 |
+
"name": tool_name,
|
| 229 |
+
"content": json.dumps(tool_result),
|
| 230 |
+
"tool_call_id": tool_call.id
|
| 231 |
+
})
|
| 232 |
+
|
| 233 |
+
continue # 继续下一轮
|
| 234 |
+
else:
|
| 235 |
+
# 无工具调用,返回最终答案
|
| 236 |
+
response_text = choice.message.content
|
| 237 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
# 构建最终响应
|
| 240 |
final_response = ""
|
| 241 |
|
| 242 |
+
# 显示模型信息
|
| 243 |
+
final_response += f"<div style='padding: 8px; background: #e3f2fd; border-left: 3px solid #2196f3; margin-bottom: 10px; font-size: 0.9em;'>🤖 <strong>Model:</strong> Qwen/Qwen2.5-72B-Instruct:novita</div>\n\n"
|
| 244 |
|
| 245 |
+
# 显示工具调用日志
|
| 246 |
if tool_calls_log:
|
| 247 |
final_response += "**🛠️ MCP Tools Used:**\n\n"
|
| 248 |
for i, tool_call in enumerate(tool_calls_log, 1):
|
| 249 |
+
final_response += f"{i}. `{tool_call['name']}` - {json.dumps(tool_call['arguments'])}\n"
|
| 250 |
final_response += "\n---\n\n"
|
| 251 |
|
| 252 |
final_response += response_text
|
|
|
|
| 254 |
return final_response
|
| 255 |
|
| 256 |
except Exception as e:
|
| 257 |
+
return f"❌ Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
+
# ========== Gradio 界面 ==========
|
| 260 |
+
with gr.Blocks(title="Financial & Market AI Assistant") as demo:
|
| 261 |
+
gr.Markdown("# 🤖 Financial & Market AI Assistant")
|
| 262 |
|
|
|
|
| 263 |
gr.Markdown("""
|
| 264 |
<div style='padding: 15px; background: #d4edda; border-left: 4px solid #28a745; margin: 10px 0; border-radius: 4px;'>
|
| 265 |
+
<strong>✅ AI Powered by:</strong> Qwen/Qwen2.5-72B-Instruct:novita
|
| 266 |
<br>
|
| 267 |
+
<strong>📊 Data Sources:</strong> SEC Financial Reports + Market & Stock Data
|
| 268 |
</div>
|
| 269 |
""")
|
| 270 |
|
| 271 |
+
chat = gr.ChatInterface(
|
| 272 |
+
fn=chatbot_response,
|
| 273 |
+
examples=[
|
| 274 |
+
"What's Apple's latest revenue and profit?",
|
| 275 |
+
"Show me NVIDIA's 3-year financial trends",
|
| 276 |
+
"How is Tesla's stock performing today?",
|
| 277 |
+
"Get the latest market news about crypto",
|
| 278 |
+
"Compare Microsoft's latest earnings with its current stock price",
|
| 279 |
+
],
|
| 280 |
+
title="💬 AI Assistant",
|
| 281 |
+
description="Ask me about company financials, stock prices, or market trends. I'll automatically fetch the data you need!"
|
| 282 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
+
# 启动应用
|
| 285 |
if __name__ == "__main__":
|
| 286 |
demo.launch(
|
| 287 |
server_name="0.0.0.0",
|
| 288 |
server_port=7860,
|
| 289 |
show_error=True,
|
| 290 |
+
ssr_mode=False
|
| 291 |
+
)
|