From 82089852c9151e99f9e9ea2419f342e4d9bcd50f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=92=E9=85=92=E7=9A=84=E6=9D=8E=E7=99=BD?= <670939375@qq.com> Date: Wed, 12 Feb 2025 01:45:45 +0800 Subject: [PATCH] Supports integration of two models from DeepSeek for analysis! --- README-CN.md | 14 +++++++++++- README.md | 14 +++++++++++- utils/ai_analyzer.py | 52 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/README-CN.md b/README-CN.md index 420654a..66055b4 100644 --- a/README-CN.md +++ b/README-CN.md @@ -42,7 +42,10 @@ - [MySQL](https://www.mysql.com/) 数据库 - [Conda](https://docs.conda.io/en/latest/)(可选,用于环境管理) - 合法的微博账号(用于数据采集) -- OpenAI API密钥或Anthropic(Claude)API密钥(用于AI分析功能) +- 以下API密钥中至少需要一个(用于AI分析功能): + - OpenAI API密钥 + - Anthropic(Claude)API密钥 + - DeepSeek API密钥 ### 安装步骤 @@ -78,9 +81,17 @@ # Anthropic API配置(使用Claude模型必需) export ANTHROPIC_API_KEY="你的anthropic密钥" + + # DeepSeek API配置(使用DeepSeek模型必需) + export DEEPSEEK_API_KEY="你的deepseek密钥" ``` 注意:至少需要配置一个API密钥才能使用AI分析功能。 + + 支持的AI模型: + - OpenAI:GPT-3.5-Turbo、GPT-4 + - Anthropic:Claude-3(Opus、Sonnet、Haiku) + - DeepSeek:DeepSeek-V3(deepseek-chat)、DeepSeek-R1(deepseek-reasoner) 5. 启动Flask应用: @@ -106,6 +117,7 @@ - **[TensorFlow](https://www.tensorflow.org/)** 或 **[PyTorch](https://pytorch.org/)** - 深度学习框架,用于高级模型开发。 - **[OpenAI GPT](https://openai.com/)** - 先进的语言模型,用于文本分析。 - **[Anthropic Claude](https://www.anthropic.com/)** - 智能AI模型,用于复杂文本分析。 +- **[DeepSeek](https://deepseek.com/)** - 先进的中英双语AI模型。 ## 🤝 贡献 diff --git a/README.md b/README.md index 92fe2e3..79a07bc 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,10 @@ Follow the steps below to run the project on your system. - [MySQL](https://www.mysql.com/) Database - [Conda](https://docs.conda.io/en/latest/) (optional, for environment management) - A valid Weibo account (for data collection) -- OpenAI API key or Anthropic (Claude) API key for AI analysis features +- At least one of the following API keys for AI analysis features: + - OpenAI API key + - Anthropic (Claude) API key + - DeepSeek API key ### Installation Steps @@ -76,9 +79,17 @@ Follow the steps below to run the project on your system. # For Anthropic API (Required for Claude models) export ANTHROPIC_API_KEY="your-anthropic-key" + + # For DeepSeek API (Required for DeepSeek models) + export DEEPSEEK_API_KEY="your-deepseek-key" ``` Note: At least one API key must be configured to use AI analysis features. + + Supported AI Models: + - OpenAI: GPT-3.5-Turbo, GPT-4 + - Anthropic: Claude-3 (Opus, Sonnet, Haiku) + - DeepSeek: DeepSeek-V3 (deepseek-chat), DeepSeek-R1 (deepseek-reasoner) 6. Start the Flask application: @@ -104,6 +115,7 @@ The Weibo Public Opinion Analysis and Prediction System employs a range of moder - **[TensorFlow](https://www.tensorflow.org/)** 或 **[PyTorch](https://pytorch.org/)** - Deep learning frameworks used for advanced model development. - **[OpenAI GPT](https://openai.com/)** - Advanced language models for text analysis. - **[Anthropic Claude](https://www.anthropic.com/)** - AI models for sophisticated text analysis. +- **[DeepSeek](https://deepseek.com/)** - Advanced Chinese-English bilingual AI models. ## 🤝 Contribution diff --git a/utils/ai_analyzer.py b/utils/ai_analyzer.py index 083209c..0d01d60 100644 --- a/utils/ai_analyzer.py +++ b/utils/ai_analyzer.py @@ -11,14 +11,21 @@ class AIAnalyzer: # 从环境变量获取API密钥 self.openai_key = os.getenv('OPENAI_API_KEY') self.claude_key = os.getenv('ANTHROPIC_API_KEY') + self.deepseek_key = os.getenv('DEEPSEEK_API_KEY') - if not self.openai_key and not self.claude_key: - raise ValueError("请至少设置一个API密钥 (OPENAI_API_KEY 或 ANTHROPIC_API_KEY)") + if not any([self.openai_key, self.claude_key, self.deepseek_key]): + raise ValueError("请至少设置一个API密钥 (OPENAI_API_KEY, ANTHROPIC_API_KEY 或 DEEPSEEK_API_KEY)") if self.openai_key: openai.api_key = self.openai_key if self.claude_key: self.claude_client = anthropic.Anthropic(api_key=self.claude_key) + if self.deepseek_key: + # 配置DeepSeek API + self.deepseek_client = openai.OpenAI( + api_key=self.deepseek_key, + base_url="https://api.deepseek.com/v1" + ) # 支持的模型列表 self.supported_models = { @@ -35,7 +42,11 @@ class AIAnalyzer: 'claude-3-haiku-20240307': {'provider': 'anthropic', 'max_tokens': 2000, 'cost_per_1k': 0.0025}, 'claude-2.1': {'provider': 'anthropic', 'max_tokens': 100000, 'cost_per_1k': 0.008}, 'claude-2.0': {'provider': 'anthropic', 'max_tokens': 100000, 'cost_per_1k': 0.008}, - 'claude-instant-1.2': {'provider': 'anthropic', 'max_tokens': 100000, 'cost_per_1k': 0.0015} + 'claude-instant-1.2': {'provider': 'anthropic', 'max_tokens': 100000, 'cost_per_1k': 0.0015}, + + # DeepSeek 模型 + 'deepseek-chat': {'provider': 'deepseek', 'max_tokens': 4000, 'cost_per_1k': 0.002}, # DeepSeek-V3 + 'deepseek-reasoner': {'provider': 'deepseek', 'max_tokens': 4000, 'cost_per_1k': 0.003} # DeepSeek-R1 } # 不同深度的分析提示词 @@ -129,13 +140,20 @@ class AIAnalyzer: model_type, max_tokens ) - else: # anthropic + elif provider == 'anthropic': result = await self._analyze_with_claude( messages_text, system_prompt, model_type, max_tokens ) + elif provider == 'deepseek': + result = await self._analyze_with_deepseek( + messages_text, + system_prompt, + model_type, + max_tokens + ) if result: all_results.extend(result) @@ -235,6 +253,32 @@ class AIAnalyzer: logging.error(f"Claude API调用失败: {e}") return [] + async def _analyze_with_deepseek(self, messages_text: str, system_prompt: str, + model: str, max_tokens: int) -> List[Dict]: + """使用DeepSeek API进行分析""" + try: + response = await self.deepseek_client.chat.completions.create( + model=model, + messages=[ + {"role": "system", "content": system_prompt}, + {"role": "user", "content": f"请分析以下消息:\n{messages_text}"} + ], + temperature=0.3, + max_tokens=max_tokens, + response_format={"type": "json_object"} # 强制JSON响应格式 + ) + + result = json.loads(response.choices[0].message.content) + if isinstance(result, dict) and 'analysis_results' in result: + return result['analysis_results'] + else: + logging.error(f"DeepSeek API返回格式不正确: {response.choices[0].message.content}") + return [] + + except Exception as e: + logging.error(f"DeepSeek API调用失败: {e}") + return [] + def format_analysis_for_display(self, analysis: Dict) -> Dict: """将分析结果格式化为前端显示格式""" base_result = {