Delete EasyFinancialAgent/chat_direct copy.py
Browse files
EasyFinancialAgent/chat_direct copy.py
DELETED
|
@@ -1,291 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Financial AI Assistant - Direct Method Library (不依赖 HTTP)
|
| 3 |
-
直接导入并调用 easy_financial_mcp.py 中的函数
|
| 4 |
-
支持本地和 HF Space 部署
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
import sys
|
| 8 |
-
from pathlib import Path
|
| 9 |
-
|
| 10 |
-
# 添加服务模块路径
|
| 11 |
-
PROJECT_ROOT = Path(__file__).parent.parent.absolute()
|
| 12 |
-
sys.path.insert(0, str(PROJECT_ROOT))
|
| 13 |
-
|
| 14 |
-
# 直接导入 MCP 中定义的函数
|
| 15 |
-
try:
|
| 16 |
-
from EasyFinancialAgent.easy_financial_mcp import (
|
| 17 |
-
search_company as _search_company,
|
| 18 |
-
get_company_info as _get_company_info,
|
| 19 |
-
get_company_filings as _get_company_filings,
|
| 20 |
-
get_financial_data as _get_financial_data,
|
| 21 |
-
extract_financial_metrics as _extract_financial_metrics,
|
| 22 |
-
get_latest_financial_data as _get_latest_financial_data,
|
| 23 |
-
advanced_search_company as _advanced_search_company
|
| 24 |
-
)
|
| 25 |
-
MCP_DIRECT_AVAILABLE = True
|
| 26 |
-
print("[FinancialAI] ✓ Direct MCP functions imported successfully")
|
| 27 |
-
except ImportError as e:
|
| 28 |
-
MCP_DIRECT_AVAILABLE = False
|
| 29 |
-
print(f"[FinancialAI] ✗ Failed to import MCP functions: {e}")
|
| 30 |
-
# 定义占位符函数
|
| 31 |
-
def _advanced_search_company(x):
|
| 32 |
-
return {"error": "MCP not available"}
|
| 33 |
-
def _get_company_info(x):
|
| 34 |
-
return {"error": "MCP not available"}
|
| 35 |
-
def _get_company_filings(x):
|
| 36 |
-
return {"error": "MCP not available"}
|
| 37 |
-
def _get_latest_financial_data(x):
|
| 38 |
-
return {"error": "MCP not available"}
|
| 39 |
-
def _extract_financial_metrics(x, y=3):
|
| 40 |
-
return {"error": "MCP not available"}
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# ============================================================
|
| 44 |
-
# 便捷方法 - 公司搜索相关
|
| 45 |
-
# ============================================================
|
| 46 |
-
|
| 47 |
-
def search_company_direct(company_input):
|
| 48 |
-
"""
|
| 49 |
-
搜索公司信息(直接调用)
|
| 50 |
-
|
| 51 |
-
Args:
|
| 52 |
-
company_input: 公司名称或代码
|
| 53 |
-
|
| 54 |
-
Returns:
|
| 55 |
-
搜索结果
|
| 56 |
-
|
| 57 |
-
Example:
|
| 58 |
-
result = search_company_direct("Apple")
|
| 59 |
-
"""
|
| 60 |
-
if not MCP_DIRECT_AVAILABLE:
|
| 61 |
-
return {"error": "MCP functions not available"}
|
| 62 |
-
|
| 63 |
-
try:
|
| 64 |
-
return _advanced_search_company(company_input)
|
| 65 |
-
except Exception as e:
|
| 66 |
-
return {"error": str(e)}
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
def get_company_info_direct(cik):
|
| 70 |
-
"""
|
| 71 |
-
获取公司详细信息(直接调用)
|
| 72 |
-
|
| 73 |
-
Args:
|
| 74 |
-
cik: 公司 CIK 代码
|
| 75 |
-
|
| 76 |
-
Returns:
|
| 77 |
-
公司信息
|
| 78 |
-
|
| 79 |
-
Example:
|
| 80 |
-
result = get_company_info_direct("0000320193")
|
| 81 |
-
"""
|
| 82 |
-
if not MCP_DIRECT_AVAILABLE:
|
| 83 |
-
return {"error": "MCP functions not available"}
|
| 84 |
-
|
| 85 |
-
try:
|
| 86 |
-
return _get_company_info(cik)
|
| 87 |
-
except Exception as e:
|
| 88 |
-
return {"error": str(e)}
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
def get_company_filings_direct(cik):
|
| 92 |
-
"""
|
| 93 |
-
获取公司 SEC 文件列表(直接调用)
|
| 94 |
-
|
| 95 |
-
Args:
|
| 96 |
-
cik: 公司 CIK 代码
|
| 97 |
-
|
| 98 |
-
Returns:
|
| 99 |
-
文件列表
|
| 100 |
-
|
| 101 |
-
Example:
|
| 102 |
-
result = get_company_filings_direct("0000320193")
|
| 103 |
-
"""
|
| 104 |
-
if not MCP_DIRECT_AVAILABLE:
|
| 105 |
-
return {"error": "MCP functions not available"}
|
| 106 |
-
|
| 107 |
-
try:
|
| 108 |
-
return _get_company_filings(cik)
|
| 109 |
-
except Exception as e:
|
| 110 |
-
return {"error": str(e)}
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
# ============================================================
|
| 114 |
-
# 便捷方法 - 财务数据相关
|
| 115 |
-
# ============================================================
|
| 116 |
-
|
| 117 |
-
def get_latest_financial_data_direct(cik):
|
| 118 |
-
"""
|
| 119 |
-
获取公司最新财务数据(直接调用)
|
| 120 |
-
|
| 121 |
-
Args:
|
| 122 |
-
cik: 公司 CIK 代码
|
| 123 |
-
|
| 124 |
-
Returns:
|
| 125 |
-
最新财务数据
|
| 126 |
-
|
| 127 |
-
Example:
|
| 128 |
-
result = get_latest_financial_data_direct("0000320193")
|
| 129 |
-
"""
|
| 130 |
-
if not MCP_DIRECT_AVAILABLE:
|
| 131 |
-
return {"error": "MCP functions not available"}
|
| 132 |
-
|
| 133 |
-
try:
|
| 134 |
-
return _get_latest_financial_data(cik)
|
| 135 |
-
except Exception as e:
|
| 136 |
-
return {"error": str(e)}
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
def extract_financial_metrics_direct(cik, years=5):
|
| 140 |
-
"""
|
| 141 |
-
提取多年财务指标趋势(直接调用)
|
| 142 |
-
|
| 143 |
-
Args:
|
| 144 |
-
cik: 公司 CIK 代码
|
| 145 |
-
years: 年数(默认 3 年)
|
| 146 |
-
|
| 147 |
-
Returns:
|
| 148 |
-
财务指标数据
|
| 149 |
-
|
| 150 |
-
Example:
|
| 151 |
-
result = extract_financial_metrics_direct("0000320193", years=5)
|
| 152 |
-
"""
|
| 153 |
-
if not MCP_DIRECT_AVAILABLE:
|
| 154 |
-
return {"error": "MCP functions not available"}
|
| 155 |
-
|
| 156 |
-
try:
|
| 157 |
-
return _extract_financial_metrics(cik, years)
|
| 158 |
-
except Exception as e:
|
| 159 |
-
return {"error": str(e)}
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
# ============================================================
|
| 163 |
-
# 高级方法 - 综合查询
|
| 164 |
-
# ============================================================
|
| 165 |
-
|
| 166 |
-
def query_company_direct(company_input, get_filings=True, get_metrics=True):
|
| 167 |
-
"""
|
| 168 |
-
综合查询公司信息(直接调用)
|
| 169 |
-
包括搜索、基本信息、文件列表和财务指标
|
| 170 |
-
|
| 171 |
-
Args:
|
| 172 |
-
company_input: 公司名称或代码
|
| 173 |
-
get_filings: 是否获取文件列表
|
| 174 |
-
get_metrics: 是否获取财务指标
|
| 175 |
-
|
| 176 |
-
Returns:
|
| 177 |
-
综合结果字典,包含 search, company_info, filings, metrics
|
| 178 |
-
|
| 179 |
-
Example:
|
| 180 |
-
result = query_company_direct("Apple", get_filings=True, get_metrics=True)
|
| 181 |
-
"""
|
| 182 |
-
from datetime import datetime
|
| 183 |
-
|
| 184 |
-
result = {
|
| 185 |
-
"timestamp": datetime.now().isoformat(),
|
| 186 |
-
"query_input": company_input,
|
| 187 |
-
"status": "success",
|
| 188 |
-
"data": {
|
| 189 |
-
"company_search": None,
|
| 190 |
-
"company_info": None,
|
| 191 |
-
"filings": None,
|
| 192 |
-
"metrics": None
|
| 193 |
-
},
|
| 194 |
-
"errors": []
|
| 195 |
-
}
|
| 196 |
-
|
| 197 |
-
if not MCP_DIRECT_AVAILABLE:
|
| 198 |
-
result["status"] = "error"
|
| 199 |
-
result["errors"].append("MCP functions not available")
|
| 200 |
-
return result
|
| 201 |
-
|
| 202 |
-
try:
|
| 203 |
-
# 1. 搜索公司
|
| 204 |
-
search_result = search_company_direct(company_input)
|
| 205 |
-
if "error" in search_result:
|
| 206 |
-
result["errors"].append(f"Search error: {search_result['error']}")
|
| 207 |
-
result["status"] = "error"
|
| 208 |
-
return result
|
| 209 |
-
|
| 210 |
-
result["data"]["company_search"] = search_result
|
| 211 |
-
|
| 212 |
-
# 从搜索结果提取 CIK
|
| 213 |
-
cik = None
|
| 214 |
-
if isinstance(search_result, dict):
|
| 215 |
-
cik = search_result.get("cik")
|
| 216 |
-
elif isinstance(search_result, (list, tuple)) and len(search_result) > 0:
|
| 217 |
-
# 从列表中获取第一个元素
|
| 218 |
-
try:
|
| 219 |
-
first_item = search_result[0] if isinstance(search_result, (list, tuple)) else None
|
| 220 |
-
if isinstance(first_item, dict):
|
| 221 |
-
cik = first_item.get("cik")
|
| 222 |
-
except (IndexError, TypeError):
|
| 223 |
-
pass
|
| 224 |
-
|
| 225 |
-
if not cik:
|
| 226 |
-
result["errors"].append("Could not extract CIK from search result")
|
| 227 |
-
result["status"] = "error"
|
| 228 |
-
return result
|
| 229 |
-
|
| 230 |
-
# 2. 获取公司信息
|
| 231 |
-
company_info = get_company_info_direct(cik)
|
| 232 |
-
if "error" not in company_info:
|
| 233 |
-
result["data"]["company_info"] = company_info
|
| 234 |
-
else:
|
| 235 |
-
result["errors"].append(f"Failed to get company info: {company_info.get('error')}")
|
| 236 |
-
|
| 237 |
-
# 3. 获取文件列表
|
| 238 |
-
if get_filings:
|
| 239 |
-
filings = get_company_filings_direct(cik)
|
| 240 |
-
if "error" not in filings:
|
| 241 |
-
result["data"]["filings"] = filings
|
| 242 |
-
else:
|
| 243 |
-
result["errors"].append(f"Failed to get filings: {filings.get('error')}")
|
| 244 |
-
|
| 245 |
-
# 4. 获取财务指标
|
| 246 |
-
if get_metrics:
|
| 247 |
-
metrics = extract_financial_metrics_direct(cik, years=3)
|
| 248 |
-
if "error" not in metrics:
|
| 249 |
-
result["data"]["metrics"] = metrics
|
| 250 |
-
else:
|
| 251 |
-
result["errors"].append(f"Failed to get metrics: {metrics.get('error')}")
|
| 252 |
-
|
| 253 |
-
except Exception as e:
|
| 254 |
-
result["status"] = "error"
|
| 255 |
-
result["errors"].append(f"Exception: {str(e)}")
|
| 256 |
-
import traceback
|
| 257 |
-
result["errors"].append(traceback.format_exc())
|
| 258 |
-
|
| 259 |
-
return result
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
# ============================================================
|
| 263 |
-
# 测试函数
|
| 264 |
-
# ============================================================
|
| 265 |
-
|
| 266 |
-
if __name__ == "__main__":
|
| 267 |
-
print("\n" + "="*60)
|
| 268 |
-
print("Financial AI Assistant - Direct Method Test")
|
| 269 |
-
print("="*60)
|
| 270 |
-
|
| 271 |
-
# 测试 1: 公司搜索
|
| 272 |
-
print("\n1. 搜索公司 (Apple)...")
|
| 273 |
-
result = search_company_direct("Apple")
|
| 274 |
-
print(f" 结果: {result}")
|
| 275 |
-
|
| 276 |
-
# 测试 2: 公司摘要
|
| 277 |
-
print("\n2. 获取 Tesla 完整信息...")
|
| 278 |
-
summary = query_company_direct("Tesla", get_filings=False, get_metrics=False)
|
| 279 |
-
print(f" 状态: {summary['status']}")
|
| 280 |
-
print(f" 数据: {summary['data']}")
|
| 281 |
-
print(f" 错误: {summary['errors']}")
|
| 282 |
-
|
| 283 |
-
# 测试 3: 财务指标
|
| 284 |
-
print("\n3. 获取 Microsoft 财务指标...")
|
| 285 |
-
if summary['data']['company_search']:
|
| 286 |
-
cik = summary['data']['company_search'].get('cik')
|
| 287 |
-
if cik:
|
| 288 |
-
metrics = extract_financial_metrics_direct(cik, years=3)
|
| 289 |
-
print(f" 指标: {metrics}")
|
| 290 |
-
|
| 291 |
-
print("\n" + "="*60)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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