Migrate Forum Host and Keyword Optimizer to use OpenAI client.
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
使用Qwen AI将Agent生成的搜索词优化为更适合舆情数据库查询的关键词
|
||||
"""
|
||||
|
||||
import requests
|
||||
from openai import OpenAI
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
@@ -46,11 +46,15 @@ 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": [
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": user_prompt}
|
||||
],
|
||||
"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"]
|
||||
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,
|
||||
)
|
||||
|
||||
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