refactor: centralize llm backend configuration

This commit is contained in:
liuwei
2026-04-08 13:43:41 +08:00
parent df1939d60b
commit aecb62cb4d
19 changed files with 945 additions and 792 deletions

View File

@@ -12,8 +12,8 @@ from utils.robot_cmd.robot_command import Feature, PermissionStatus, GroupBotMan
from utils.decorator.points_decorator import points_reward_decorator
from db.connection import DBConnectionManager
from db.encyclopedia import EncyclopediaDB
import requests
import json
from utils.ai.unified_llm import UnifiedLLMClient
class GameTaskPlugin(MessagePluginInterface):
@@ -81,11 +81,25 @@ class GameTaskPlugin(MessagePluginInterface):
/l - 查看活跃任务
/h - 查看未完成任务
""")
self.authorization = self._config.get("GameTask", {}).get("authorization", "")
self.url = self._config.get("GameTask", {}).get("url", "")
self.model = self._config.get("GameTask", {}).get("model", "")
plugin_config = self._config.get("GameTask", {})
self.authorization = plugin_config.get("authorization", "")
self.url = plugin_config.get("url", "")
self.model = plugin_config.get("model", "")
llm_config = plugin_config.get("llm", {}) or {}
if not llm_config:
llm_config = {
"backend": plugin_config.get("backend", ""),
"provider": "openai_compatible",
"authorization": self.authorization,
"url": self.url,
"model": self.model,
"stream": False,
"temperature": 0.2,
"max_tokens": 1000,
}
self.llm_client = UnifiedLLMClient(llm_config)
self.enable = self._config.get("GameTask", {}).get("enable", True)
self.enable = plugin_config.get("enable", True)
# 初始化数据库连接
self.db_manager = DBConnectionManager.get_instance()
@@ -584,40 +598,14 @@ class GameTaskPlugin(MessagePluginInterface):
return None
def message_task_json(self, prompt, content):
# 设置Authorization和URL
authorization = self.authorization # 请替换为真实的Authorization token
url = self.url
data = {
# "stream": True,
"model": self.model,
"messages": [
{
"role": "system",
"content": f"{prompt}"
},
{
"role": "user",
"content": f"{content}"
}
]
}
# 设置请求头
headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": authorization
}
# 发送POST请求
response = requests.post(url, headers=headers, data=json.dumps(data), )
response.encoding = 'utf-8'
# 输出响应内容
print(response.status_code)
print(response.text)
return json.loads(self.extract_content(response.text))
response = self.llm_client.generate(
system_prompt=prompt,
user_prompt=str(content),
user="game_task_bot",
)
if not response or not response.get("text"):
raise RuntimeError(f"LLM 调用失败: {self.llm_client.last_error}")
return json.loads(response["text"])
def game_question_json(self, question):
fields = [