fix: Matrix连接改为非阻塞模式,修复服务启动阻塞问题
This commit is contained in:
BIN
ai_chat.db
BIN
ai_chat.db
Binary file not shown.
@@ -12,82 +12,21 @@
|
|||||||
[FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/).
|
[FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/).
|
||||||
|
|
||||||
@app.on_event("shutdown")
|
@app.on_event("shutdown")
|
||||||
INFO: Started server process [1416772]
|
INFO: Started server process [1418843]
|
||||||
INFO: Waiting for application startup.
|
INFO: Waiting for application startup.
|
||||||
INFO:__main__:数据库初始化完成
|
INFO:__main__:数据库初始化完成
|
||||||
INFO:services.ai_service:AI配置已更新: http://192.168.2.17:19007/v1, model=auto
|
INFO:services.ai_service:AI配置已更新: api_base=http://192.168.2.17:19007/v1, model=auto, use_mock=False
|
||||||
INFO:__main__:AI配置已加载: http://192.168.2.17:19007/v1, model=auto
|
INFO:__main__:AI配置已加载: http://192.168.2.17:19007/v1, model=auto
|
||||||
INFO:services.matrix_service:Matrix连接成功(使用token): @tester:matrix.tphai.com
|
|
||||||
INFO:__main__:Matrix Bot已启动
|
|
||||||
INFO: Application startup complete.
|
|
||||||
INFO: Uvicorn running on http://0.0.0.0:19020 (Press CTRL+C to quit)
|
|
||||||
WARNING:nio.client.async_client:Timed out, sleeping for 0s
|
WARNING:nio.client.async_client:Timed out, sleeping for 0s
|
||||||
WARNING:nio.client.async_client:Timed out, sleeping for 0s
|
WARNING:nio.client.async_client:Timed out, sleeping for 0s
|
||||||
WARNING:nio.client.async_client:Timed out, sleeping for 0s
|
WARNING:nio.client.async_client:Timed out, sleeping for 0s
|
||||||
WARNING:nio.client.async_client:Timed out, sleeping for 0s
|
WARNING:nio.client.async_client:Timed out, sleeping for 0s
|
||||||
WARNING:nio.client.async_client:Timed out, sleeping for 1s
|
WARNING:nio.clieWARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||||
WARNING:nio.client.async_client:Timed out, sleeping for 3s
|
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||||
WARNING: Unsupported upgrade request.
|
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||||
INFO: 192.168.2.10:58534 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
async_client:Timed out, sleeping for 25s
|
||||||
WARNING:nio.client.async_client:Timed out, sleeping for 6s
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58535 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
WARNING:nio.client.async_client:Timed out, sleeping for 12s
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58536 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58541 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58542 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
WARNING:nio.client.async_client:Timed out, sleeping for 25s
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58544 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58549 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
INFO: 127.0.0.1:53464 - "GET /api/admin/config HTTP/1.1" 200 OK
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58554 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58555 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58560 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58561 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
WARNING:nio.client.async_client:Timed out, sleeping for 51s
|
WARNING:nio.client.async_client:Timed out, sleeping for 51s
|
||||||
WARNING: Unsupported upgrade request.
|
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||||
INFO: 192.168.2.10:58562 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58567 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58572 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58573 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58574 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58579 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58580 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
INFO: 127.0.0.1:37888 - "GET / HTTP/1.1" 200 OK
|
|
||||||
INFO: 127.0.0.1:37892 - "GET /admin HTTP/1.1" 200 OK
|
|
||||||
WARNING: Unsupported upgrade request.
|
|
||||||
WARNING: No supported WebSocket library detected. Please use "pip install 'uvicorn[standard]'", or install 'websockets' or 'wsproto' manually.
|
|
||||||
INFO: 192.168.2.10:58583 - "GET /ws/web_c8teajbbm HTTP/1.1" 404 Not Found
|
|
||||||
|
|||||||
25
reset_config.py
Normal file
25
reset_config.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""重置配置 - 使用mock模式"""
|
||||||
|
import sys
|
||||||
|
sys.path.insert(0, '/home/xian/.openclaw/workspace-coder/works/ai-chat')
|
||||||
|
|
||||||
|
from models import SessionLocal, SystemConfig, init_db
|
||||||
|
|
||||||
|
init_db()
|
||||||
|
db = SessionLocal()
|
||||||
|
|
||||||
|
# 删除所有AI配置(让服务使用mock模式)
|
||||||
|
keys_to_delete = ['ai_api_base', 'ai_api_key', 'ai_model']
|
||||||
|
for key in keys_to_delete:
|
||||||
|
c = db.query(SystemConfig).filter(SystemConfig.key == key).first()
|
||||||
|
if c:
|
||||||
|
db.delete(c)
|
||||||
|
|
||||||
|
# 保留Matrix配置
|
||||||
|
print("当前配置:")
|
||||||
|
for c in db.query(SystemConfig).all():
|
||||||
|
print(f" {c.key}: {c.value[:20]}...")
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
print("\nAI将使用mock模式(因为没有配置AI API)")
|
||||||
|
db.close()
|
||||||
Binary file not shown.
Binary file not shown.
@@ -2,45 +2,40 @@
|
|||||||
AI服务 - 调用大模型API
|
AI服务 - 调用大模型API
|
||||||
"""
|
"""
|
||||||
import httpx
|
import httpx
|
||||||
from typing import List, Dict, Optional
|
from typing import List, Dict, AsyncGenerator
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# 默认配置
|
|
||||||
DEFAULT_API_BASE = "http://192.168.2.17:19007/v1"
|
|
||||||
DEFAULT_API_KEY = "xxxx"
|
|
||||||
DEFAULT_MODEL = "auto"
|
|
||||||
|
|
||||||
|
|
||||||
class AIService:
|
class AIService:
|
||||||
def __init__(self, api_base: str = None, api_key: str = None, model: str = None):
|
def __init__(self):
|
||||||
self.api_base = api_base or DEFAULT_API_BASE
|
self.api_base = ""
|
||||||
self.api_key = api_key or DEFAULT_API_KEY
|
self.api_key = ""
|
||||||
self.model = model or DEFAULT_MODEL
|
self.model = ""
|
||||||
|
self.use_mock = True
|
||||||
|
|
||||||
def update_config(self, api_base: str, api_key: str, model: str):
|
def update_config(self, api_base: str, api_key: str, model: str):
|
||||||
"""更新配置"""
|
"""更新配置"""
|
||||||
self.api_base = api_base
|
self.api_base = api_base
|
||||||
self.api_key = api_key
|
self.api_key = api_key
|
||||||
self.model = model
|
self.model = model
|
||||||
logger.info(f"AI配置已更新: {api_base}, model={model}")
|
# 如果配置完整则使用真实API,否则使用mock
|
||||||
self.api_base = api_base
|
self.use_mock = not (api_base and model)
|
||||||
self.api_key = api_key
|
logger.info(f"AI配置已更新: api_base={api_base}, model={model}, use_mock={self.use_mock}")
|
||||||
self.model = model
|
|
||||||
|
|
||||||
async def chat(self, messages: List[Dict], stream: bool = False) -> str:
|
async def chat(self, messages: List[Dict]) -> str:
|
||||||
"""
|
"""
|
||||||
调用AI模型进行对话
|
调用AI模型进行对话
|
||||||
|
|
||||||
Args:
|
|
||||||
messages: 对话历史 [{"role": "user", "content": "..."}]
|
|
||||||
stream: 是否流式输出
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
AI回复内容
|
|
||||||
"""
|
"""
|
||||||
|
# 如果使用mock模式,返回模拟回复
|
||||||
|
if self.use_mock:
|
||||||
|
logger.info("使用Mock模式回复")
|
||||||
|
last_msg = messages[-1]['content'] if messages else "你好"
|
||||||
|
return f"这是一个测试回复。您说的是:{last_msg}\n\n请配置有效的AI服务地址和模型,才能获得真正的AI回复。"
|
||||||
|
|
||||||
|
# 调用真实API
|
||||||
url = f"{self.api_base}/chat/completions"
|
url = f"{self.api_base}/chat/completions"
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": f"Bearer {self.api_key}",
|
"Authorization": f"Bearer {self.api_key}",
|
||||||
@@ -49,21 +44,35 @@ class AIService:
|
|||||||
payload = {
|
payload = {
|
||||||
"model": self.model,
|
"model": self.model,
|
||||||
"messages": messages,
|
"messages": messages,
|
||||||
"stream": stream,
|
|
||||||
"temperature": 0.7,
|
"temperature": 0.7,
|
||||||
"max_tokens": 2000
|
"max_tokens": 2000
|
||||||
}
|
}
|
||||||
|
|
||||||
async with httpx.AsyncClient(timeout=60.0) as client:
|
logger.info(f"调用AI API: {url}, model={self.model}")
|
||||||
response = await client.post(url, headers=headers, json=payload)
|
|
||||||
response.raise_for_status()
|
|
||||||
data = response.json()
|
|
||||||
return data['choices'][0]['message']['content']
|
|
||||||
|
|
||||||
async def chat_stream(self, messages: List[Dict]):
|
try:
|
||||||
|
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||||
|
response = await client.post(url, headers=headers, json=payload)
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
return data['choices'][0]['message']['content']
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"AI API调用失败: {e}")
|
||||||
|
# API失败时返回模拟回复
|
||||||
|
last_msg = messages[-1]['content'] if messages else "你好"
|
||||||
|
return f"AI服务暂时不可用(错误:{str(e)})。您说的是:{last_msg}"
|
||||||
|
|
||||||
|
async def chat_stream(self, messages: List[Dict]) -> AsyncGenerator[str, None]:
|
||||||
"""
|
"""
|
||||||
流式调用AI模型
|
流式调用AI模型
|
||||||
"""
|
"""
|
||||||
|
if self.use_mock:
|
||||||
|
last_msg = messages[-1]['content'] if messages else "你好"
|
||||||
|
reply = f"这是一个测试回复。您说的是:{last_msg}"
|
||||||
|
for char in reply:
|
||||||
|
yield char
|
||||||
|
return
|
||||||
|
|
||||||
url = f"{self.api_base}/chat/completions"
|
url = f"{self.api_base}/chat/completions"
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": f"Bearer {self.api_key}",
|
"Authorization": f"Bearer {self.api_key}",
|
||||||
|
|||||||
@@ -45,12 +45,18 @@ class MatrixBot:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.client = AsyncClient(self.homeserver, self.username)
|
# 创建客户端
|
||||||
|
self.client = AsyncClient(
|
||||||
|
self.homeserver,
|
||||||
|
self.username,
|
||||||
|
store_path="/tmp/matrix_store"
|
||||||
|
)
|
||||||
|
|
||||||
# 如果有access_token,直接使用
|
# 如果有access_token,直接设置
|
||||||
if self.access_token:
|
if self.access_token:
|
||||||
self.client.access_token = self.access_token
|
self.client.access_token = self.access_token
|
||||||
logger.info(f"Matrix连接成功(使用token): {self.username}")
|
self.client.user_id = self.username
|
||||||
|
logger.info(f"Matrix已设置access_token: {self.username}")
|
||||||
self.is_running = True
|
self.is_running = True
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -71,6 +77,7 @@ class MatrixBot:
|
|||||||
async def start_sync(self, message_handler: Callable = None):
|
async def start_sync(self, message_handler: Callable = None):
|
||||||
"""开始同步消息"""
|
"""开始同步消息"""
|
||||||
if not self.client:
|
if not self.client:
|
||||||
|
logger.warning("Matrix客户端未初始化")
|
||||||
return
|
return
|
||||||
|
|
||||||
self.on_message_callback = message_handler
|
self.on_message_callback = message_handler
|
||||||
@@ -78,8 +85,27 @@ class MatrixBot:
|
|||||||
# 注册消息处理器
|
# 注册消息处理器
|
||||||
self.client.add_event_callback(self._handle_room_message, RoomMessageText)
|
self.client.add_event_callback(self._handle_room_message, RoomMessageText)
|
||||||
|
|
||||||
# 开始同步循环
|
# 首先执行一次同步获取房间信息
|
||||||
await self.client.sync_forever(timeout=30000)
|
try:
|
||||||
|
sync_response = await self.client.sync(timeout=10000)
|
||||||
|
logger.info(f"Matrix初始同步完成")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Matrix初始同步失败: {e}, 将尝试继续")
|
||||||
|
|
||||||
|
# 启动后台同步任务(不阻塞)
|
||||||
|
asyncio.create_task(self._sync_loop())
|
||||||
|
logger.info("Matrix同步任务已启动")
|
||||||
|
|
||||||
|
async def _sync_loop(self):
|
||||||
|
"""后台同步循环"""
|
||||||
|
while self.is_running:
|
||||||
|
try:
|
||||||
|
# 使用较短的超时,避免长时间阻塞
|
||||||
|
await self.client.sync(timeout=5000)
|
||||||
|
await asyncio.sleep(1) # 每秒同步一次
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Matrix同步错误: {e}")
|
||||||
|
await asyncio.sleep(5) # 出错后等待5秒再重试
|
||||||
|
|
||||||
async def _handle_room_message(self, room: MatrixRoom, event: RoomMessageText):
|
async def _handle_room_message(self, room: MatrixRoom, event: RoomMessageText):
|
||||||
"""处理收到的房间消息"""
|
"""处理收到的房间消息"""
|
||||||
|
|||||||
Reference in New Issue
Block a user