Supports integration of two models from DeepSeek for analysis!
Showing
3 changed files
with
74 additions
and
6 deletions
| @@ -42,7 +42,10 @@ | @@ -42,7 +42,10 @@ | ||
| 42 | - [MySQL](https://www.mysql.com/) 数据库 | 42 | - [MySQL](https://www.mysql.com/) 数据库 |
| 43 | - [Conda](https://docs.conda.io/en/latest/)(可选,用于环境管理) | 43 | - [Conda](https://docs.conda.io/en/latest/)(可选,用于环境管理) |
| 44 | - 合法的微博账号(用于数据采集) | 44 | - 合法的微博账号(用于数据采集) |
| 45 | -- OpenAI API密钥或Anthropic(Claude)API密钥(用于AI分析功能) | 45 | +- 以下API密钥中至少需要一个(用于AI分析功能): |
| 46 | + - OpenAI API密钥 | ||
| 47 | + - Anthropic(Claude)API密钥 | ||
| 48 | + - DeepSeek API密钥 | ||
| 46 | 49 | ||
| 47 | ### 安装步骤 | 50 | ### 安装步骤 |
| 48 | 51 | ||
| @@ -78,10 +81,18 @@ | @@ -78,10 +81,18 @@ | ||
| 78 | 81 | ||
| 79 | # Anthropic API配置(使用Claude模型必需) | 82 | # Anthropic API配置(使用Claude模型必需) |
| 80 | export ANTHROPIC_API_KEY="你的anthropic密钥" | 83 | export ANTHROPIC_API_KEY="你的anthropic密钥" |
| 84 | + | ||
| 85 | + # DeepSeek API配置(使用DeepSeek模型必需) | ||
| 86 | + export DEEPSEEK_API_KEY="你的deepseek密钥" | ||
| 81 | ``` | 87 | ``` |
| 82 | 88 | ||
| 83 | 注意:至少需要配置一个API密钥才能使用AI分析功能。 | 89 | 注意:至少需要配置一个API密钥才能使用AI分析功能。 |
| 84 | 90 | ||
| 91 | + 支持的AI模型: | ||
| 92 | + - OpenAI:GPT-3.5-Turbo、GPT-4 | ||
| 93 | + - Anthropic:Claude-3(Opus、Sonnet、Haiku) | ||
| 94 | + - DeepSeek:DeepSeek-V3(deepseek-chat)、DeepSeek-R1(deepseek-reasoner) | ||
| 95 | + | ||
| 85 | 5. 启动Flask应用: | 96 | 5. 启动Flask应用: |
| 86 | 97 | ||
| 87 | ```bash | 98 | ```bash |
| @@ -106,6 +117,7 @@ | @@ -106,6 +117,7 @@ | ||
| 106 | - **[TensorFlow](https://www.tensorflow.org/)** 或 **[PyTorch](https://pytorch.org/)** - 深度学习框架,用于高级模型开发。 | 117 | - **[TensorFlow](https://www.tensorflow.org/)** 或 **[PyTorch](https://pytorch.org/)** - 深度学习框架,用于高级模型开发。 |
| 107 | - **[OpenAI GPT](https://openai.com/)** - 先进的语言模型,用于文本分析。 | 118 | - **[OpenAI GPT](https://openai.com/)** - 先进的语言模型,用于文本分析。 |
| 108 | - **[Anthropic Claude](https://www.anthropic.com/)** - 智能AI模型,用于复杂文本分析。 | 119 | - **[Anthropic Claude](https://www.anthropic.com/)** - 智能AI模型,用于复杂文本分析。 |
| 120 | +- **[DeepSeek](https://deepseek.com/)** - 先进的中英双语AI模型。 | ||
| 109 | 121 | ||
| 110 | ## 🤝 贡献 | 122 | ## 🤝 贡献 |
| 111 | 123 |
| @@ -40,7 +40,10 @@ Follow the steps below to run the project on your system. | @@ -40,7 +40,10 @@ Follow the steps below to run the project on your system. | ||
| 40 | - [MySQL](https://www.mysql.com/) Database | 40 | - [MySQL](https://www.mysql.com/) Database |
| 41 | - [Conda](https://docs.conda.io/en/latest/) (optional, for environment management) | 41 | - [Conda](https://docs.conda.io/en/latest/) (optional, for environment management) |
| 42 | - A valid Weibo account (for data collection) | 42 | - A valid Weibo account (for data collection) |
| 43 | -- OpenAI API key or Anthropic (Claude) API key for AI analysis features | 43 | +- At least one of the following API keys for AI analysis features: |
| 44 | + - OpenAI API key | ||
| 45 | + - Anthropic (Claude) API key | ||
| 46 | + - DeepSeek API key | ||
| 44 | 47 | ||
| 45 | ### Installation Steps | 48 | ### Installation Steps |
| 46 | 49 | ||
| @@ -76,10 +79,18 @@ Follow the steps below to run the project on your system. | @@ -76,10 +79,18 @@ Follow the steps below to run the project on your system. | ||
| 76 | 79 | ||
| 77 | # For Anthropic API (Required for Claude models) | 80 | # For Anthropic API (Required for Claude models) |
| 78 | export ANTHROPIC_API_KEY="your-anthropic-key" | 81 | export ANTHROPIC_API_KEY="your-anthropic-key" |
| 82 | + | ||
| 83 | + # For DeepSeek API (Required for DeepSeek models) | ||
| 84 | + export DEEPSEEK_API_KEY="your-deepseek-key" | ||
| 79 | ``` | 85 | ``` |
| 80 | 86 | ||
| 81 | Note: At least one API key must be configured to use AI analysis features. | 87 | Note: At least one API key must be configured to use AI analysis features. |
| 82 | 88 | ||
| 89 | + Supported AI Models: | ||
| 90 | + - OpenAI: GPT-3.5-Turbo, GPT-4 | ||
| 91 | + - Anthropic: Claude-3 (Opus, Sonnet, Haiku) | ||
| 92 | + - DeepSeek: DeepSeek-V3 (deepseek-chat), DeepSeek-R1 (deepseek-reasoner) | ||
| 93 | + | ||
| 83 | 6. Start the Flask application: | 94 | 6. Start the Flask application: |
| 84 | 95 | ||
| 85 | ```bash | 96 | ```bash |
| @@ -104,6 +115,7 @@ The Weibo Public Opinion Analysis and Prediction System employs a range of moder | @@ -104,6 +115,7 @@ The Weibo Public Opinion Analysis and Prediction System employs a range of moder | ||
| 104 | - **[TensorFlow](https://www.tensorflow.org/)** 或 **[PyTorch](https://pytorch.org/)** - Deep learning frameworks used for advanced model development. | 115 | - **[TensorFlow](https://www.tensorflow.org/)** 或 **[PyTorch](https://pytorch.org/)** - Deep learning frameworks used for advanced model development. |
| 105 | - **[OpenAI GPT](https://openai.com/)** - Advanced language models for text analysis. | 116 | - **[OpenAI GPT](https://openai.com/)** - Advanced language models for text analysis. |
| 106 | - **[Anthropic Claude](https://www.anthropic.com/)** - AI models for sophisticated text analysis. | 117 | - **[Anthropic Claude](https://www.anthropic.com/)** - AI models for sophisticated text analysis. |
| 118 | +- **[DeepSeek](https://deepseek.com/)** - Advanced Chinese-English bilingual AI models. | ||
| 107 | 119 | ||
| 108 | ## 🤝 Contribution | 120 | ## 🤝 Contribution |
| 109 | 121 |
| @@ -11,14 +11,21 @@ class AIAnalyzer: | @@ -11,14 +11,21 @@ class AIAnalyzer: | ||
| 11 | # 从环境变量获取API密钥 | 11 | # 从环境变量获取API密钥 |
| 12 | self.openai_key = os.getenv('OPENAI_API_KEY') | 12 | self.openai_key = os.getenv('OPENAI_API_KEY') |
| 13 | self.claude_key = os.getenv('ANTHROPIC_API_KEY') | 13 | self.claude_key = os.getenv('ANTHROPIC_API_KEY') |
| 14 | + self.deepseek_key = os.getenv('DEEPSEEK_API_KEY') | ||
| 14 | 15 | ||
| 15 | - if not self.openai_key and not self.claude_key: | ||
| 16 | - raise ValueError("请至少设置一个API密钥 (OPENAI_API_KEY 或 ANTHROPIC_API_KEY)") | 16 | + if not any([self.openai_key, self.claude_key, self.deepseek_key]): |
| 17 | + raise ValueError("请至少设置一个API密钥 (OPENAI_API_KEY, ANTHROPIC_API_KEY 或 DEEPSEEK_API_KEY)") | ||
| 17 | 18 | ||
| 18 | if self.openai_key: | 19 | if self.openai_key: |
| 19 | openai.api_key = self.openai_key | 20 | openai.api_key = self.openai_key |
| 20 | if self.claude_key: | 21 | if self.claude_key: |
| 21 | self.claude_client = anthropic.Anthropic(api_key=self.claude_key) | 22 | self.claude_client = anthropic.Anthropic(api_key=self.claude_key) |
| 23 | + if self.deepseek_key: | ||
| 24 | + # 配置DeepSeek API | ||
| 25 | + self.deepseek_client = openai.OpenAI( | ||
| 26 | + api_key=self.deepseek_key, | ||
| 27 | + base_url="https://api.deepseek.com/v1" | ||
| 28 | + ) | ||
| 22 | 29 | ||
| 23 | # 支持的模型列表 | 30 | # 支持的模型列表 |
| 24 | self.supported_models = { | 31 | self.supported_models = { |
| @@ -35,7 +42,11 @@ class AIAnalyzer: | @@ -35,7 +42,11 @@ class AIAnalyzer: | ||
| 35 | 'claude-3-haiku-20240307': {'provider': 'anthropic', 'max_tokens': 2000, 'cost_per_1k': 0.0025}, | 42 | 'claude-3-haiku-20240307': {'provider': 'anthropic', 'max_tokens': 2000, 'cost_per_1k': 0.0025}, |
| 36 | 'claude-2.1': {'provider': 'anthropic', 'max_tokens': 100000, 'cost_per_1k': 0.008}, | 43 | 'claude-2.1': {'provider': 'anthropic', 'max_tokens': 100000, 'cost_per_1k': 0.008}, |
| 37 | 'claude-2.0': {'provider': 'anthropic', 'max_tokens': 100000, 'cost_per_1k': 0.008}, | 44 | 'claude-2.0': {'provider': 'anthropic', 'max_tokens': 100000, 'cost_per_1k': 0.008}, |
| 38 | - 'claude-instant-1.2': {'provider': 'anthropic', 'max_tokens': 100000, 'cost_per_1k': 0.0015} | 45 | + 'claude-instant-1.2': {'provider': 'anthropic', 'max_tokens': 100000, 'cost_per_1k': 0.0015}, |
| 46 | + | ||
| 47 | + # DeepSeek 模型 | ||
| 48 | + 'deepseek-chat': {'provider': 'deepseek', 'max_tokens': 4000, 'cost_per_1k': 0.002}, # DeepSeek-V3 | ||
| 49 | + 'deepseek-reasoner': {'provider': 'deepseek', 'max_tokens': 4000, 'cost_per_1k': 0.003} # DeepSeek-R1 | ||
| 39 | } | 50 | } |
| 40 | 51 | ||
| 41 | # 不同深度的分析提示词 | 52 | # 不同深度的分析提示词 |
| @@ -129,13 +140,20 @@ class AIAnalyzer: | @@ -129,13 +140,20 @@ class AIAnalyzer: | ||
| 129 | model_type, | 140 | model_type, |
| 130 | max_tokens | 141 | max_tokens |
| 131 | ) | 142 | ) |
| 132 | - else: # anthropic | 143 | + elif provider == 'anthropic': |
| 133 | result = await self._analyze_with_claude( | 144 | result = await self._analyze_with_claude( |
| 134 | messages_text, | 145 | messages_text, |
| 135 | system_prompt, | 146 | system_prompt, |
| 136 | model_type, | 147 | model_type, |
| 137 | max_tokens | 148 | max_tokens |
| 138 | ) | 149 | ) |
| 150 | + elif provider == 'deepseek': | ||
| 151 | + result = await self._analyze_with_deepseek( | ||
| 152 | + messages_text, | ||
| 153 | + system_prompt, | ||
| 154 | + model_type, | ||
| 155 | + max_tokens | ||
| 156 | + ) | ||
| 139 | 157 | ||
| 140 | if result: | 158 | if result: |
| 141 | all_results.extend(result) | 159 | all_results.extend(result) |
| @@ -235,6 +253,32 @@ class AIAnalyzer: | @@ -235,6 +253,32 @@ class AIAnalyzer: | ||
| 235 | logging.error(f"Claude API调用失败: {e}") | 253 | logging.error(f"Claude API调用失败: {e}") |
| 236 | return [] | 254 | return [] |
| 237 | 255 | ||
| 256 | + async def _analyze_with_deepseek(self, messages_text: str, system_prompt: str, | ||
| 257 | + model: str, max_tokens: int) -> List[Dict]: | ||
| 258 | + """使用DeepSeek API进行分析""" | ||
| 259 | + try: | ||
| 260 | + response = await self.deepseek_client.chat.completions.create( | ||
| 261 | + model=model, | ||
| 262 | + messages=[ | ||
| 263 | + {"role": "system", "content": system_prompt}, | ||
| 264 | + {"role": "user", "content": f"请分析以下消息:\n{messages_text}"} | ||
| 265 | + ], | ||
| 266 | + temperature=0.3, | ||
| 267 | + max_tokens=max_tokens, | ||
| 268 | + response_format={"type": "json_object"} # 强制JSON响应格式 | ||
| 269 | + ) | ||
| 270 | + | ||
| 271 | + result = json.loads(response.choices[0].message.content) | ||
| 272 | + if isinstance(result, dict) and 'analysis_results' in result: | ||
| 273 | + return result['analysis_results'] | ||
| 274 | + else: | ||
| 275 | + logging.error(f"DeepSeek API返回格式不正确: {response.choices[0].message.content}") | ||
| 276 | + return [] | ||
| 277 | + | ||
| 278 | + except Exception as e: | ||
| 279 | + logging.error(f"DeepSeek API调用失败: {e}") | ||
| 280 | + return [] | ||
| 281 | + | ||
| 238 | def format_analysis_for_display(self, analysis: Dict) -> Dict: | 282 | def format_analysis_for_display(self, analysis: Dict) -> Dict: |
| 239 | """将分析结果格式化为前端显示格式""" | 283 | """将分析结果格式化为前端显示格式""" |
| 240 | base_result = { | 284 | base_result = { |
-
Please register or login to post a comment