refactor: 将同步IO操作改为异步以提高性能

This commit is contained in:
Wenjie Zhang 2025-12-21 21:25:13 +08:00
parent 1d04842f02
commit 0527cf491d
4 changed files with 24 additions and 17 deletions

View File

@ -10,13 +10,13 @@
- 集成 neo4j mcp (或者自己构建工具)
- 文档解析部分的 markdown 中的图片替换为内部可访问的链接 (2/4)
- chat_model 的 call 需要异步
- 优化分块儿逻辑,移除 QA 分块,集成到普通分块儿
- 优化chunk逻辑移除 QA 分割,集成到普通分块
### Bugs
- 部分异常状态下,智能体的模型名称出现重叠[#279](https://github.com/xerrors/Yuxi-Know/issues/279)
- DeepSeek 官方接口适配会出现问题
- 目前的知识库的图片存在公开访问风险
- 工具传递给模型的时候,使用英文,显示的时候,使用中文(尽量保持一致
- 工具传递给模型的时候,使用英文,但部分模型不支持中文函数名如gpt-4o-mini
- 首页加载的问题
- lightrag 类型的图谱的节点数量统计有问题

View File

@ -7,6 +7,7 @@
- 保存和加载思维导图配置
"""
import asyncio
import json
import traceback
import textwrap
@ -213,10 +214,10 @@ async def generate_mindmap(
# 调用AI生成
logger.info(f"开始生成思维导图,知识库: {db_name}, 文件数量: {len(files_info)}")
# 选择模型并调用
# 选择模型并调用(使用异步包装)
model = select_model()
messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": user_message}]
response = model.call(messages, stream=False)
response = await asyncio.to_thread(model.call, messages, stream=False)
# 解析AI返回的JSON
try:

View File

@ -1,5 +1,5 @@
import os
from collections import deque
import aiofiles
from pathlib import Path
import yaml
@ -30,7 +30,7 @@ async def health_check():
@system.get("/config")
def get_config(current_user: User = Depends(get_admin_user)):
async def get_config(current_user: User = Depends(get_admin_user)):
"""获取系统配置"""
return config.dump_config()
@ -52,15 +52,20 @@ async def update_config_batch(items: dict = Body(...), current_user: User = Depe
@system.get("/logs")
def get_system_logs(current_user: User = Depends(get_admin_user)):
async def get_system_logs(current_user: User = Depends(get_admin_user)):
"""获取系统日志"""
try:
from src.utils.logging_config import LOG_FILE
with open(LOG_FILE) as f:
last_lines = deque(f, maxlen=1000)
async with aiofiles.open(LOG_FILE) as f:
# 读取最后1000行
lines = []
async for line in f:
lines.append(line)
if len(lines) > 1000:
lines.pop(0)
log = "".join(last_lines)
log = "".join(lines)
return {"log": log, "message": "success", "log_file": LOG_FILE}
except Exception as e:
logger.error(f"获取系统日志失败: {e}")
@ -72,7 +77,7 @@ def get_system_logs(current_user: User = Depends(get_admin_user)):
# =============================================================================
def load_info_config():
async def load_info_config():
"""加载信息配置文件"""
try:
# 配置文件路径
@ -84,9 +89,10 @@ def load_info_config():
logger.debug(f"The config file {config_path} does not exist, using default config")
config_path = Path("src/config/static/info.template.yaml")
# 读取配置文件
with open(config_path, encoding="utf-8") as file:
config = yaml.safe_load(file)
# 异步读取配置文件
async with aiofiles.open(config_path, encoding="utf-8") as file:
content = await file.read()
config = yaml.safe_load(content)
return config
@ -99,7 +105,7 @@ def load_info_config():
async def get_info_config():
"""获取系统信息配置(公开接口,无需认证)"""
try:
config = load_info_config()
config = await load_info_config()
return {"success": True, "data": config}
except Exception as e:
logger.error(f"获取信息配置失败: {e}")
@ -110,7 +116,7 @@ async def get_info_config():
async def reload_info_config(current_user: User = Depends(get_admin_user)):
"""重新加载信息配置"""
try:
config = load_info_config()
config = await load_info_config()
return {"success": True, "message": "配置重新加载成功", "data": config}
except Exception as e:
logger.error(f"重新加载信息配置失败: {e}")

View File

@ -4,8 +4,8 @@ from collections.abc import Callable
from langchain.agents.middleware import ModelRequest, ModelResponse, dynamic_prompt, wrap_model_call
from src.utils import logger
from src.agents.common import load_chat_model
from src.utils import logger
@dynamic_prompt