Migrate Forum Host and Keyword Optimizer to use OpenAI client.
Showing
2 changed files
with
33 additions
and
59 deletions
| @@ -3,8 +3,7 @@ | @@ -3,8 +3,7 @@ | ||
| 3 | 使用硅基流动的Qwen3模型作为论坛主持人,引导多个agent进行讨论 | 3 | 使用硅基流动的Qwen3模型作为论坛主持人,引导多个agent进行讨论 |
| 4 | """ | 4 | """ |
| 5 | 5 | ||
| 6 | -import requests | ||
| 7 | -import json | 6 | +from openai import OpenAI |
| 8 | import sys | 7 | import sys |
| 9 | import os | 8 | import os |
| 10 | from typing import List, Dict, Any, Optional | 9 | from typing import List, Dict, Any, Optional |
| @@ -39,13 +38,17 @@ class ForumHost: | @@ -39,13 +38,17 @@ class ForumHost: | ||
| 39 | api_key: 硅基流动API密钥,如果不提供则从配置文件读取 | 38 | api_key: 硅基流动API密钥,如果不提供则从配置文件读取 |
| 40 | """ | 39 | """ |
| 41 | self.api_key = api_key or GUIJI_QWEN3_API_KEY | 40 | self.api_key = api_key or GUIJI_QWEN3_API_KEY |
| 42 | - self.base_url = "https://api.siliconflow.cn/v1/chat/completions" | ||
| 43 | - self.model = "Qwen/Qwen3-235B-A22B-Instruct-2507" # 使用更大的模型 | ||
| 44 | 41 | ||
| 45 | if not self.api_key: | 42 | if not self.api_key: |
| 46 | raise ValueError("未找到硅基流动API密钥,请在config.py中设置GUIJI_QWEN3_API_KEY") | 43 | raise ValueError("未找到硅基流动API密钥,请在config.py中设置GUIJI_QWEN3_API_KEY") |
| 47 | 44 | ||
| 48 | - # 记录历史发言,避免重复 | 45 | + self.client = OpenAI( |
| 46 | + api_key=self.api_key, | ||
| 47 | + base_url="https://api.siliconflow.cn/v1" | ||
| 48 | + ) | ||
| 49 | + self.model = "Qwen/Qwen3-235B-A22B-Instruct-2507" # Use larger model variant | ||
| 50 | + | ||
| 51 | + # Track previous summaries to avoid duplicates | ||
| 49 | self.previous_summaries = [] | 52 | self.previous_summaries = [] |
| 50 | 53 | ||
| 51 | def generate_host_speech(self, forum_logs: List[str]) -> Optional[str]: | 54 | def generate_host_speech(self, forum_logs: List[str]) -> Optional[str]: |
| @@ -204,43 +207,23 @@ class ForumHost: | @@ -204,43 +207,23 @@ class ForumHost: | ||
| 204 | @with_graceful_retry(SEARCH_API_RETRY_CONFIG, default_return={"success": False, "error": "API服务暂时不可用"}) | 207 | @with_graceful_retry(SEARCH_API_RETRY_CONFIG, default_return={"success": False, "error": "API服务暂时不可用"}) |
| 205 | def _call_qwen_api(self, system_prompt: str, user_prompt: str) -> Dict[str, Any]: | 208 | def _call_qwen_api(self, system_prompt: str, user_prompt: str) -> Dict[str, Any]: |
| 206 | """调用Qwen API""" | 209 | """调用Qwen API""" |
| 207 | - headers = { | ||
| 208 | - "Authorization": f"Bearer {self.api_key}", | ||
| 209 | - "Content-Type": "application/json" | ||
| 210 | - } | ||
| 211 | - | ||
| 212 | - data = { | ||
| 213 | - "model": self.model, | ||
| 214 | - "messages": [ | 210 | + try: |
| 211 | + response = self.client.chat.completions.create( | ||
| 212 | + model=self.model, | ||
| 213 | + messages=[ | ||
| 215 | {"role": "system", "content": system_prompt}, | 214 | {"role": "system", "content": system_prompt}, |
| 216 | {"role": "user", "content": user_prompt} | 215 | {"role": "user", "content": user_prompt} |
| 217 | ], | 216 | ], |
| 218 | - "max_tokens": 14639, | ||
| 219 | - "temperature": 0.6, | ||
| 220 | - "top_p": 0.9 | ||
| 221 | - } | ||
| 222 | - | ||
| 223 | - try: | ||
| 224 | - response = requests.post( | ||
| 225 | - self.base_url, | ||
| 226 | - headers=headers, | ||
| 227 | - json=data, | ||
| 228 | - timeout=300 # 超时设置300s | 217 | + max_tokens=14639, |
| 218 | + temperature=0.6, | ||
| 219 | + top_p=0.9, | ||
| 229 | ) | 220 | ) |
| 230 | - response.raise_for_status() | ||
| 231 | 221 | ||
| 232 | - result = response.json() | ||
| 233 | - | ||
| 234 | - if "choices" in result and len(result["choices"]) > 0: | ||
| 235 | - content = result["choices"][0]["message"]["content"] | 222 | + if response.choices: |
| 223 | + content = response.choices[0].message.content | ||
| 236 | return {"success": True, "content": content} | 224 | return {"success": True, "content": content} |
| 237 | else: | 225 | else: |
| 238 | return {"success": False, "error": "API返回格式异常"} | 226 | return {"success": False, "error": "API返回格式异常"} |
| 239 | - | ||
| 240 | - except requests.exceptions.Timeout: | ||
| 241 | - return {"success": False, "error": "API请求超时"} | ||
| 242 | - except requests.exceptions.RequestException as e: | ||
| 243 | - return {"success": False, "error": f"网络请求错误: {str(e)}"} | ||
| 244 | except Exception as e: | 227 | except Exception as e: |
| 245 | return {"success": False, "error": f"API调用异常: {str(e)}"} | 228 | return {"success": False, "error": f"API调用异常: {str(e)}"} |
| 246 | 229 |
| @@ -3,7 +3,7 @@ | @@ -3,7 +3,7 @@ | ||
| 3 | 使用Qwen AI将Agent生成的搜索词优化为更适合舆情数据库查询的关键词 | 3 | 使用Qwen AI将Agent生成的搜索词优化为更适合舆情数据库查询的关键词 |
| 4 | """ | 4 | """ |
| 5 | 5 | ||
| 6 | -import requests | 6 | +from openai import OpenAI |
| 7 | import json | 7 | import json |
| 8 | import sys | 8 | import sys |
| 9 | import os | 9 | import os |
| @@ -46,12 +46,16 @@ class KeywordOptimizer: | @@ -46,12 +46,16 @@ class KeywordOptimizer: | ||
| 46 | api_key: 硅基流动API密钥,如果不提供则从配置文件读取 | 46 | api_key: 硅基流动API密钥,如果不提供则从配置文件读取 |
| 47 | """ | 47 | """ |
| 48 | self.api_key = api_key or GUIJI_QWEN3_API_KEY | 48 | self.api_key = api_key or GUIJI_QWEN3_API_KEY |
| 49 | - self.base_url = "https://api.siliconflow.cn/v1/chat/completions" | ||
| 50 | - self.model = "Qwen/Qwen3-30B-A3B-Instruct-2507" | ||
| 51 | 49 | ||
| 52 | if not self.api_key: | 50 | if not self.api_key: |
| 53 | raise ValueError("未找到硅基流动API密钥,请在config.py中设置GUIJI_QWEN3_API_KEY") | 51 | raise ValueError("未找到硅基流动API密钥,请在config.py中设置GUIJI_QWEN3_API_KEY") |
| 54 | 52 | ||
| 53 | + self.client = OpenAI( | ||
| 54 | + api_key=self.api_key, | ||
| 55 | + base_url="https://api.siliconflow.cn/v1" | ||
| 56 | + ) | ||
| 57 | + self.model = "Qwen/Qwen3-30B-A3B-Instruct-2507" | ||
| 58 | + | ||
| 55 | def optimize_keywords(self, original_query: str, context: str = "") -> KeywordOptimizationResponse: | 59 | def optimize_keywords(self, original_query: str, context: str = "") -> KeywordOptimizationResponse: |
| 56 | """ | 60 | """ |
| 57 | 优化搜索关键词 | 61 | 优化搜索关键词 |
| @@ -178,35 +182,22 @@ class KeywordOptimizer: | @@ -178,35 +182,22 @@ class KeywordOptimizer: | ||
| 178 | @with_graceful_retry(SEARCH_API_RETRY_CONFIG, default_return={"success": False, "error": "关键词优化服务暂时不可用"}) | 182 | @with_graceful_retry(SEARCH_API_RETRY_CONFIG, default_return={"success": False, "error": "关键词优化服务暂时不可用"}) |
| 179 | def _call_qwen_api(self, system_prompt: str, user_prompt: str) -> Dict[str, Any]: | 183 | def _call_qwen_api(self, system_prompt: str, user_prompt: str) -> Dict[str, Any]: |
| 180 | """调用Qwen API""" | 184 | """调用Qwen API""" |
| 181 | - headers = { | ||
| 182 | - "Authorization": f"Bearer {self.api_key}", | ||
| 183 | - "Content-Type": "application/json" | ||
| 184 | - } | ||
| 185 | - | ||
| 186 | - data = { | ||
| 187 | - "model": self.model, | ||
| 188 | - "messages": [ | 185 | + try: |
| 186 | + response = self.client.chat.completions.create( | ||
| 187 | + model=self.model, | ||
| 188 | + messages=[ | ||
| 189 | {"role": "system", "content": system_prompt}, | 189 | {"role": "system", "content": system_prompt}, |
| 190 | {"role": "user", "content": user_prompt} | 190 | {"role": "user", "content": user_prompt} |
| 191 | ], | 191 | ], |
| 192 | - "max_tokens": 10000, | ||
| 193 | - "temperature": 0.7 | ||
| 194 | - } | ||
| 195 | - | ||
| 196 | - try: | ||
| 197 | - response = requests.post(self.base_url, headers=headers, json=data, timeout=30) | ||
| 198 | - response.raise_for_status() | ||
| 199 | - | ||
| 200 | - result = response.json() | 192 | + max_tokens=10000, |
| 193 | + temperature=0.7, | ||
| 194 | + ) | ||
| 201 | 195 | ||
| 202 | - if "choices" in result and len(result["choices"]) > 0: | ||
| 203 | - content = result["choices"][0]["message"]["content"] | 196 | + if response.choices: |
| 197 | + content = response.choices[0].message.content | ||
| 204 | return {"success": True, "content": content} | 198 | return {"success": True, "content": content} |
| 205 | else: | 199 | else: |
| 206 | return {"success": False, "error": "API返回格式异常"} | 200 | return {"success": False, "error": "API返回格式异常"} |
| 207 | - | ||
| 208 | - except requests.exceptions.RequestException as e: | ||
| 209 | - return {"success": False, "error": f"网络请求错误: {str(e)}"} | ||
| 210 | except Exception as e: | 201 | except Exception as e: |
| 211 | return {"success": False, "error": f"API调用异常: {str(e)}"} | 202 | return {"success": False, "error": f"API调用异常: {str(e)}"} |
| 212 | 203 |
-
Please register or login to post a comment