Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b05a03e198 | |||
| fd583132d7 |
BIN
ai_chat.db
BIN
ai_chat.db
Binary file not shown.
40
init_config.py
Normal file
40
init_config.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""初始化配置"""
|
||||||
|
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模型
|
||||||
|
configs = [
|
||||||
|
{'key': 'ai_api_base', 'value': 'http://192.168.2.17:19007/v1', 'description': 'AI模型API地址'},
|
||||||
|
{'key': 'ai_api_key', 'value': 'xxxx', 'description': 'AI模型API Key'},
|
||||||
|
{'key': 'ai_model', 'value': 'auto', 'description': 'AI模型名称'},
|
||||||
|
|
||||||
|
# 配置Matrix Bot
|
||||||
|
{'key': 'matrix_homeserver', 'value': 'https://matrix.tphai.com', 'description': 'Matrix服务器地址'},
|
||||||
|
{'key': 'matrix_username', 'value': '@tester:matrix.tphai.com', 'description': 'Matrix Bot用户名'},
|
||||||
|
{'key': 'matrix_access_token', 'value': 'syt_dGVzdGVy_eMwWfezCXSyBgHzvkmly_4dWFtM', 'description': 'Matrix Bot Access Token'},
|
||||||
|
]
|
||||||
|
|
||||||
|
for config in configs:
|
||||||
|
existing = db.query(SystemConfig).filter(SystemConfig.key == config['key']).first()
|
||||||
|
if existing:
|
||||||
|
existing.value = config['value']
|
||||||
|
existing.description = config['description']
|
||||||
|
else:
|
||||||
|
db.add(SystemConfig(**config))
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
print("配置已写入数据库")
|
||||||
|
|
||||||
|
# 显示配置
|
||||||
|
for c in db.query(SystemConfig).all():
|
||||||
|
print(f"{c.key}: {c.value}")
|
||||||
|
|
||||||
|
db.close()
|
||||||
@@ -1,18 +1,32 @@
|
|||||||
Traceback (most recent call last):
|
/home/xian/.openclaw/workspace-coder/works/ai-chat/main.py:445: DeprecationWarning:
|
||||||
File "/home/xian/.openclaw/workspace-coder/works/ai-chat/main.py", line 17, in <module>
|
on_event is deprecated, use lifespan event handlers instead.
|
||||||
from models import init_db, get_db, User, Conversation, Message, SystemConfig
|
|
||||||
File "/home/xian/.openclaw/workspace-coder/works/ai-chat/models/__init__.py", line 1, in <module>
|
Read more about it in the
|
||||||
from .database import Base, engine, SessionLocal, get_db, init_db
|
[FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/).
|
||||||
File "/home/xian/.openclaw/workspace-coder/works/ai-chat/models/database.py", line 33, in <module>
|
|
||||||
class Conversation(Base):
|
@app.on_event("startup")
|
||||||
File "/home/xian/.local/lib/python3.10/site-packages/sqlalchemy/orm/decl_api.py", line 195, in __init__
|
/home/xian/.openclaw/workspace-coder/works/ai-chat/main.py:470: DeprecationWarning:
|
||||||
_as_declarative(reg, cls, dict_)
|
on_event is deprecated, use lifespan event handlers instead.
|
||||||
File "/home/xian/.local/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 247, in _as_declarative
|
|
||||||
return _MapperConfig.setup_mapping(registry, cls, dict_, None, {})
|
Read more about it in the
|
||||||
File "/home/xian/.local/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 328, in setup_mapping
|
[FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/).
|
||||||
return _ClassScanMapperConfig(
|
|
||||||
File "/home/xian/.local/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 574, in __init__
|
@app.on_event("shutdown")
|
||||||
self._extract_mappable_attributes()
|
INFO: Started server process [1418843]
|
||||||
File "/home/xian/.local/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 1507, in _extract_mappable_attributes
|
INFO: Waiting for application startup.
|
||||||
raise exc.InvalidRequestError(
|
INFO:__main__:数据库初始化完成
|
||||||
sqlalchemy.exc.InvalidRequestError: Attribute name 'metadata' is reserved when using the Declarative API.
|
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
|
||||||
|
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.clieWARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||||
|
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||||
|
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||||
|
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||||
|
async_client:Timed out, sleeping for 25s
|
||||||
|
WARNING:nio.client.async_client:Timed out, sleeping for 51s
|
||||||
|
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||||
|
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||||
|
WARNING:nio.client.async_client:Timed out, sleeping for 60s
|
||||||
|
|||||||
24
main.py
24
main.py
@@ -14,7 +14,7 @@ import logging
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from models import init_db, get_db, User, Conversation, Message, SystemConfig
|
from models import init_db, get_db, SessionLocal, User, Conversation, Message, SystemConfig
|
||||||
from services import ai_service, ConversationService, matrix_bot
|
from services import ai_service, ConversationService, matrix_bot
|
||||||
|
|
||||||
# 配置日志
|
# 配置日志
|
||||||
@@ -379,7 +379,15 @@ async def update_config(data: dict, db: Session = Depends(get_db)):
|
|||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
# 如果更新了Matrix配置,重新连接
|
# 根据配置类型执行相应操作
|
||||||
|
if key.startswith('ai_'):
|
||||||
|
# 更新AI服务配置
|
||||||
|
configs = {c.key: c.value for c in db.query(SystemConfig).all()}
|
||||||
|
api_base = configs.get('ai_api_base', 'http://192.168.2.17:19007/v1')
|
||||||
|
api_key = configs.get('ai_api_key', 'xxxx')
|
||||||
|
model = configs.get('ai_model', 'auto')
|
||||||
|
ai_service.update_config(api_base, api_key, model)
|
||||||
|
|
||||||
if key.startswith('matrix_') and matrix_bot.is_running:
|
if key.startswith('matrix_') and matrix_bot.is_running:
|
||||||
await matrix_bot.disconnect()
|
await matrix_bot.disconnect()
|
||||||
await matrix_bot.init_from_config()
|
await matrix_bot.init_from_config()
|
||||||
@@ -440,6 +448,18 @@ async def startup():
|
|||||||
init_db()
|
init_db()
|
||||||
logger.info("数据库初始化完成")
|
logger.info("数据库初始化完成")
|
||||||
|
|
||||||
|
# 从数据库加载AI配置
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
configs = {c.key: c.value for c in db.query(SystemConfig).all()}
|
||||||
|
api_base = configs.get('ai_api_base', 'http://192.168.2.17:19007/v1')
|
||||||
|
api_key = configs.get('ai_api_key', 'xxxx')
|
||||||
|
model = configs.get('ai_model', 'auto')
|
||||||
|
ai_service.update_config(api_base, api_key, model)
|
||||||
|
logger.info(f"AI配置已加载: {api_base}, model={model}")
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
# 初始化Matrix Bot
|
# 初始化Matrix Bot
|
||||||
await matrix_bot.init_from_config()
|
await matrix_bot.init_from_config()
|
||||||
if matrix_bot.is_running:
|
if matrix_bot.is_running:
|
||||||
|
|||||||
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,27 +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
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class AIService:
|
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):
|
||||||
|
self.api_base = ""
|
||||||
|
self.api_key = ""
|
||||||
|
self.model = ""
|
||||||
|
self.use_mock = True
|
||||||
|
|
||||||
|
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
|
||||||
|
# 如果配置完整则使用真实API,否则使用mock
|
||||||
|
self.use_mock = not (api_base and model)
|
||||||
|
logger.info(f"AI配置已更新: api_base={api_base}, model={model}, use_mock={self.use_mock}")
|
||||||
|
|
||||||
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}",
|
||||||
@@ -31,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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.info(f"调用AI API: {url}, model={self.model}")
|
||||||
|
|
||||||
|
try:
|
||||||
async with httpx.AsyncClient(timeout=60.0) as client:
|
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||||
response = await client.post(url, headers=headers, json=payload)
|
response = await client.post(url, headers=headers, json=payload)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
data = response.json()
|
data = response.json()
|
||||||
return data['choices'][0]['message']['content']
|
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]):
|
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}",
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ class MatrixBot:
|
|||||||
self.homeserver: str = ""
|
self.homeserver: str = ""
|
||||||
self.username: str = ""
|
self.username: str = ""
|
||||||
self.password: str = ""
|
self.password: str = ""
|
||||||
|
self.access_token: str = ""
|
||||||
self.is_running: bool = False
|
self.is_running: bool = False
|
||||||
self.on_message_callback: Optional[Callable] = None
|
self.on_message_callback: Optional[Callable] = None
|
||||||
|
|
||||||
@@ -30,8 +31,9 @@ class MatrixBot:
|
|||||||
self.homeserver = configs.get('matrix_homeserver', 'https://matrix.tphai.com')
|
self.homeserver = configs.get('matrix_homeserver', 'https://matrix.tphai.com')
|
||||||
self.username = configs.get('matrix_username', '')
|
self.username = configs.get('matrix_username', '')
|
||||||
self.password = configs.get('matrix_password', '')
|
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()
|
await self.connect()
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
@@ -43,7 +45,22 @@ 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,直接设置
|
||||||
|
if self.access_token:
|
||||||
|
self.client.access_token = self.access_token
|
||||||
|
self.client.user_id = self.username
|
||||||
|
logger.info(f"Matrix已设置access_token: {self.username}")
|
||||||
|
self.is_running = True
|
||||||
|
return True
|
||||||
|
|
||||||
|
# 否则使用密码登录
|
||||||
response = await self.client.login(self.password)
|
response = await self.client.login(self.password)
|
||||||
|
|
||||||
if isinstance(response, LoginResponse):
|
if isinstance(response, LoginResponse):
|
||||||
@@ -60,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
|
||||||
@@ -67,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