Migrate Forum Host and Keyword Optimizer to use OpenAI client.
This commit is contained in:
+17
-34
@@ -3,8 +3,7 @@
|
||||
使用硅基流动的Qwen3模型作为论坛主持人,引导多个agent进行讨论
|
||||
"""
|
||||
|
||||
import requests
|
||||
import json
|
||||
from openai import OpenAI
|
||||
import sys
|
||||
import os
|
||||
from typing import List, Dict, Any, Optional
|
||||
@@ -39,13 +38,17 @@ class ForumHost:
|
||||
api_key: 硅基流动API密钥,如果不提供则从配置文件读取
|
||||
"""
|
||||
self.api_key = api_key or GUIJI_QWEN3_API_KEY
|
||||
self.base_url = "https://api.siliconflow.cn/v1/chat/completions"
|
||||
self.model = "Qwen/Qwen3-235B-A22B-Instruct-2507" # 使用更大的模型
|
||||
|
||||
if not self.api_key:
|
||||
raise ValueError("未找到硅基流动API密钥,请在config.py中设置GUIJI_QWEN3_API_KEY")
|
||||
|
||||
# 记录历史发言,避免重复
|
||||
self.client = OpenAI(
|
||||
api_key=self.api_key,
|
||||
base_url="https://api.siliconflow.cn/v1"
|
||||
)
|
||||
self.model = "Qwen/Qwen3-235B-A22B-Instruct-2507" # Use larger model variant
|
||||
|
||||
# Track previous summaries to avoid duplicates
|
||||
self.previous_summaries = []
|
||||
|
||||
def generate_host_speech(self, forum_logs: List[str]) -> Optional[str]:
|
||||
@@ -204,43 +207,23 @@ class ForumHost:
|
||||
@with_graceful_retry(SEARCH_API_RETRY_CONFIG, default_return={"success": False, "error": "API服务暂时不可用"})
|
||||
def _call_qwen_api(self, system_prompt: str, user_prompt: str) -> Dict[str, Any]:
|
||||
"""调用Qwen API"""
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
data = {
|
||||
"model": self.model,
|
||||
"messages": [
|
||||
try:
|
||||
response = self.client.chat.completions.create(
|
||||
model=self.model,
|
||||
messages=[
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": user_prompt}
|
||||
],
|
||||
"max_tokens": 14639,
|
||||
"temperature": 0.6,
|
||||
"top_p": 0.9
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
self.base_url,
|
||||
headers=headers,
|
||||
json=data,
|
||||
timeout=300 # 超时设置300s
|
||||
max_tokens=14639,
|
||||
temperature=0.6,
|
||||
top_p=0.9,
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
result = response.json()
|
||||
|
||||
if "choices" in result and len(result["choices"]) > 0:
|
||||
content = result["choices"][0]["message"]["content"]
|
||||
if response.choices:
|
||||
content = response.choices[0].message.content
|
||||
return {"success": True, "content": content}
|
||||
else:
|
||||
return {"success": False, "error": "API返回格式异常"}
|
||||
|
||||
except requests.exceptions.Timeout:
|
||||
return {"success": False, "error": "API请求超时"}
|
||||
except requests.exceptions.RequestException as e:
|
||||
return {"success": False, "error": f"网络请求错误: {str(e)}"}
|
||||
except Exception as e:
|
||||
return {"success": False, "error": f"API调用异常: {str(e)}"}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
使用Qwen AI将Agent生成的搜索词优化为更适合舆情数据库查询的关键词
|
||||
"""
|
||||
|
||||
import requests
|
||||
from openai import OpenAI
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
@@ -46,12 +46,16 @@ class KeywordOptimizer:
|
||||
api_key: 硅基流动API密钥,如果不提供则从配置文件读取
|
||||
"""
|
||||
self.api_key = api_key or GUIJI_QWEN3_API_KEY
|
||||
self.base_url = "https://api.siliconflow.cn/v1/chat/completions"
|
||||
self.model = "Qwen/Qwen3-30B-A3B-Instruct-2507"
|
||||
|
||||
if not self.api_key:
|
||||
raise ValueError("未找到硅基流动API密钥,请在config.py中设置GUIJI_QWEN3_API_KEY")
|
||||
|
||||
self.client = OpenAI(
|
||||
api_key=self.api_key,
|
||||
base_url="https://api.siliconflow.cn/v1"
|
||||
)
|
||||
self.model = "Qwen/Qwen3-30B-A3B-Instruct-2507"
|
||||
|
||||
def optimize_keywords(self, original_query: str, context: str = "") -> KeywordOptimizationResponse:
|
||||
"""
|
||||
优化搜索关键词
|
||||
@@ -178,35 +182,22 @@ class KeywordOptimizer:
|
||||
@with_graceful_retry(SEARCH_API_RETRY_CONFIG, default_return={"success": False, "error": "关键词优化服务暂时不可用"})
|
||||
def _call_qwen_api(self, system_prompt: str, user_prompt: str) -> Dict[str, Any]:
|
||||
"""调用Qwen API"""
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
data = {
|
||||
"model": self.model,
|
||||
"messages": [
|
||||
try:
|
||||
response = self.client.chat.completions.create(
|
||||
model=self.model,
|
||||
messages=[
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": user_prompt}
|
||||
],
|
||||
"max_tokens": 10000,
|
||||
"temperature": 0.7
|
||||
}
|
||||
max_tokens=10000,
|
||||
temperature=0.7,
|
||||
)
|
||||
|
||||
try:
|
||||
response = requests.post(self.base_url, headers=headers, json=data, timeout=30)
|
||||
response.raise_for_status()
|
||||
|
||||
result = response.json()
|
||||
|
||||
if "choices" in result and len(result["choices"]) > 0:
|
||||
content = result["choices"][0]["message"]["content"]
|
||||
if response.choices:
|
||||
content = response.choices[0].message.content
|
||||
return {"success": True, "content": content}
|
||||
else:
|
||||
return {"success": False, "error": "API返回格式异常"}
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
return {"success": False, "error": f"网络请求错误: {str(e)}"}
|
||||
except Exception as e:
|
||||
return {"success": False, "error": f"API调用异常: {str(e)}"}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user