feat: 支持Matrix access_token登录,配置AI模型接口

This commit is contained in:
2026-04-11 11:58:14 +08:00
parent c27fc8c02f
commit fd583132d7
8 changed files with 186 additions and 22 deletions

View File

@@ -4,10 +4,28 @@ AI服务 - 调用大模型API
import httpx
from typing import List, Dict, Optional
import json
import logging
logger = logging.getLogger(__name__)
# 默认配置
DEFAULT_API_BASE = "http://192.168.2.17:19007/v1"
DEFAULT_API_KEY = "xxxx"
DEFAULT_MODEL = "auto"
class AIService:
def __init__(self, api_base: str = "http://192.168.2.17:19007/v1", api_key: str = "sk-local", model: str = "qwen3.5-4b"):
def __init__(self, api_base: str = None, api_key: str = None, model: str = None):
self.api_base = api_base or DEFAULT_API_BASE
self.api_key = api_key or DEFAULT_API_KEY
self.model = model or DEFAULT_MODEL
def update_config(self, api_base: str, api_key: str, model: str):
"""更新配置"""
self.api_base = api_base
self.api_key = api_key
self.model = model
logger.info(f"AI配置已更新: {api_base}, model={model}")
self.api_base = api_base
self.api_key = api_key
self.model = model

View File

@@ -19,6 +19,7 @@ class MatrixBot:
self.homeserver: str = ""
self.username: str = ""
self.password: str = ""
self.access_token: str = ""
self.is_running: bool = False
self.on_message_callback: Optional[Callable] = None
@@ -30,8 +31,9 @@ class MatrixBot:
self.homeserver = configs.get('matrix_homeserver', 'https://matrix.tphai.com')
self.username = configs.get('matrix_username', '')
self.password = configs.get('matrix_password', '')
self.access_token = configs.get('matrix_access_token', '')
if self.username and self.password:
if self.username and (self.password or self.access_token):
await self.connect()
finally:
db.close()
@@ -44,6 +46,15 @@ class MatrixBot:
try:
self.client = AsyncClient(self.homeserver, self.username)
# 如果有access_token直接使用
if self.access_token:
self.client.access_token = self.access_token
logger.info(f"Matrix连接成功(使用token): {self.username}")
self.is_running = True
return True
# 否则使用密码登录
response = await self.client.login(self.password)
if isinstance(response, LoginResponse):