- 完善资讯子图nodes.py:优化format_result的展示效果 - 创建资讯子图API调用工具:api_client.py - 更新资讯子图__init__.py,导出所有模块和API客户端 - 所有功能已通过测试验证
This commit is contained in:
129
backend/app/agent_subgraphs/news_analysis/api_client.py
Normal file
129
backend/app/agent_subgraphs/news_analysis/api_client.py
Normal file
@@ -0,0 +1,129 @@
|
||||
"""
|
||||
资讯子图API调用工具
|
||||
News Analysis API Client
|
||||
"""
|
||||
|
||||
from typing import Dict, Any, Optional, List
|
||||
import random
|
||||
from datetime import datetime
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class NewsAPIClient:
|
||||
"""
|
||||
资讯API客户端 - 可扩展支持多种API
|
||||
"""
|
||||
|
||||
# 可以配置多个API(如 NewsAPI, 今日头条, 百度新闻等)
|
||||
newsapi_key: Optional[str] = None
|
||||
|
||||
def query_news_mock(self, query: str) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
模拟查询资讯 - 目前用于演示
|
||||
"""
|
||||
# 模拟资讯数据库
|
||||
mock_news = [
|
||||
{
|
||||
"title": "OpenAI发布GPT-5:智能再升级",
|
||||
"source": "Tech News",
|
||||
"summary": "最新消息,OpenAI刚刚发布了GPT-5模型,智能水平再次取得重大突破...",
|
||||
"keywords": ["AI", "GPT-5", "OpenAI"],
|
||||
"author": "AI Team",
|
||||
"published_at": datetime.now().isoformat()
|
||||
},
|
||||
{
|
||||
"title": "大模型在医疗领域的应用",
|
||||
"source": "Health Tech",
|
||||
"summary": "大模型AI技术正在医疗领域展现巨大潜力,从辅助诊断到药物研发...",
|
||||
"keywords": ["医疗", "大模型", "应用"],
|
||||
"author": "Medical Team",
|
||||
"published_at": datetime.now().isoformat()
|
||||
},
|
||||
{
|
||||
"title": "2026年AI行业发展趋势报告",
|
||||
"source": "Business Daily",
|
||||
"summary": "最新行业报告显示,AI行业将继续保持高速增长,企业数字化转型加速...",
|
||||
"keywords": ["趋势", "AI", "商业"],
|
||||
"author": "Business Team",
|
||||
"published_at": datetime.now().isoformat()
|
||||
}
|
||||
]
|
||||
|
||||
# 根据查询词简单过滤
|
||||
results = []
|
||||
query_lower = query.lower()
|
||||
|
||||
for news in mock_news:
|
||||
if (query_lower in news["title"].lower() or
|
||||
query_lower in news["summary"].lower() or
|
||||
any(keyword.lower() in query_lower for keyword in news["keywords"])):
|
||||
results.append(news)
|
||||
|
||||
# 如果没有匹配到,返回前两条
|
||||
if not results:
|
||||
results = mock_news[:2]
|
||||
|
||||
return results
|
||||
|
||||
def analyze_url_mock(self, url: str) -> Dict[str, Any]:
|
||||
"""
|
||||
模拟URL分析 - 目前用于演示
|
||||
"""
|
||||
return {
|
||||
"title": f"分析结果:{url}",
|
||||
"source": "URL Analyzer",
|
||||
"summary": "已完成对该URL的内容分析,包含文章摘要和情感倾向判断...",
|
||||
"keywords": ["News", "Analysis", url.split("/")[-1] if url else "unknown"]
|
||||
}
|
||||
|
||||
def extract_keywords_mock(self, text: str) -> List[str]:
|
||||
"""
|
||||
模拟关键词提取 - 目前用于演示
|
||||
"""
|
||||
# 简单的关键词提取模拟
|
||||
common_keywords = ["AI", "大模型", "应用场景", "行业趋势", "创新", "技术"]
|
||||
result = []
|
||||
|
||||
for keyword in common_keywords:
|
||||
if keyword.lower() in text.lower():
|
||||
result.append(keyword)
|
||||
|
||||
# 如果没找到,返回默认关键词
|
||||
if not result:
|
||||
result = ["AI", "大模型", "应用场景", "行业趋势"]
|
||||
|
||||
return result
|
||||
|
||||
def generate_report_mock(self, query: str) -> str:
|
||||
"""
|
||||
模拟报告生成 - 目前用于演示
|
||||
"""
|
||||
report = f"""═══════════════════════════════════════════
|
||||
📊 资讯分析报告
|
||||
═══════════════════════════════════════════
|
||||
|
||||
主题:{query}
|
||||
|
||||
📋 摘要:
|
||||
这是一份关于 {query} 的资讯分析综合报告,包含最新行业动态和趋势分析。
|
||||
|
||||
🔍 主要发现:
|
||||
1. AI技术持续快速发展
|
||||
2. 大模型应用场景不断拓展
|
||||
3. 行业数字化转型加速
|
||||
|
||||
🏷️ 关键词:
|
||||
- AI
|
||||
- 大模型
|
||||
- 数字化转型
|
||||
- 创新
|
||||
|
||||
═══════════════════════════════════════════
|
||||
💡 建议:继续关注行业动态,把握发展机遇!
|
||||
"""
|
||||
return report
|
||||
|
||||
|
||||
# 单例实例
|
||||
news_api = NewsAPIClient()
|
||||
Reference in New Issue
Block a user