Skip to content

Commit 0527cf4

Browse files
committed
refactor: 将同步IO操作改为异步以提高性能
1 parent 1d04842 commit 0527cf4

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

docs/latest/changelog/roadmap.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
- 集成 neo4j mcp (或者自己构建工具)
1111
- 文档解析部分的 markdown 中的图片替换为内部可访问的链接 (2/4)
1212
- chat_model 的 call 需要异步
13-
- 优化分块儿逻辑,移除 QA 分块,集成到普通分块儿中
13+
- 优化chunk逻辑,移除 QA 分割,集成到普通分块中
1414

1515
### Bugs
1616
- 部分异常状态下,智能体的模型名称出现重叠[#279](https://github.com/xerrors/Yuxi-Know/issues/279)
1717
- DeepSeek 官方接口适配会出现问题
1818
- 目前的知识库的图片存在公开访问风险
19-
- 工具传递给模型的时候,使用英文,显示的时候,使用中文(尽量保持一致
19+
- 工具传递给模型的时候,使用英文,但部分模型不支持中文函数名(如gpt-4o-mini
2020
- 首页加载的问题
2121
- lightrag 类型的图谱的节点数量统计有问题
2222

server/routers/mindmap_router.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- 保存和加载思维导图配置
88
"""
99

10+
import asyncio
1011
import json
1112
import traceback
1213
import textwrap
@@ -213,10 +214,10 @@ async def generate_mindmap(
213214
# 调用AI生成
214215
logger.info(f"开始生成思维导图,知识库: {db_name}, 文件数量: {len(files_info)}")
215216

216-
# 选择模型并调用
217+
# 选择模型并调用(使用异步包装)
217218
model = select_model()
218219
messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": user_message}]
219-
response = model.call(messages, stream=False)
220+
response = await asyncio.to_thread(model.call, messages, stream=False)
220221

221222
# 解析AI返回的JSON
222223
try:

server/routers/system_router.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from collections import deque
2+
import aiofiles
33
from pathlib import Path
44

55
import yaml
@@ -30,7 +30,7 @@ async def health_check():
3030

3131

3232
@system.get("/config")
33-
def get_config(current_user: User = Depends(get_admin_user)):
33+
async def get_config(current_user: User = Depends(get_admin_user)):
3434
"""获取系统配置"""
3535
return config.dump_config()
3636

@@ -52,15 +52,20 @@ async def update_config_batch(items: dict = Body(...), current_user: User = Depe
5252

5353

5454
@system.get("/logs")
55-
def get_system_logs(current_user: User = Depends(get_admin_user)):
55+
async def get_system_logs(current_user: User = Depends(get_admin_user)):
5656
"""获取系统日志"""
5757
try:
5858
from src.utils.logging_config import LOG_FILE
5959

60-
with open(LOG_FILE) as f:
61-
last_lines = deque(f, maxlen=1000)
60+
async with aiofiles.open(LOG_FILE) as f:
61+
# 读取最后1000行
62+
lines = []
63+
async for line in f:
64+
lines.append(line)
65+
if len(lines) > 1000:
66+
lines.pop(0)
6267

63-
log = "".join(last_lines)
68+
log = "".join(lines)
6469
return {"log": log, "message": "success", "log_file": LOG_FILE}
6570
except Exception as e:
6671
logger.error(f"获取系统日志失败: {e}")
@@ -72,7 +77,7 @@ def get_system_logs(current_user: User = Depends(get_admin_user)):
7277
# =============================================================================
7378

7479

75-
def load_info_config():
80+
async def load_info_config():
7681
"""加载信息配置文件"""
7782
try:
7883
# 配置文件路径
@@ -84,9 +89,10 @@ def load_info_config():
8489
logger.debug(f"The config file {config_path} does not exist, using default config")
8590
config_path = Path("src/config/static/info.template.yaml")
8691

87-
# 读取配置文件
88-
with open(config_path, encoding="utf-8") as file:
89-
config = yaml.safe_load(file)
92+
# 异步读取配置文件
93+
async with aiofiles.open(config_path, encoding="utf-8") as file:
94+
content = await file.read()
95+
config = yaml.safe_load(content)
9096

9197
return config
9298

@@ -99,7 +105,7 @@ def load_info_config():
99105
async def get_info_config():
100106
"""获取系统信息配置(公开接口,无需认证)"""
101107
try:
102-
config = load_info_config()
108+
config = await load_info_config()
103109
return {"success": True, "data": config}
104110
except Exception as e:
105111
logger.error(f"获取信息配置失败: {e}")
@@ -110,7 +116,7 @@ async def get_info_config():
110116
async def reload_info_config(current_user: User = Depends(get_admin_user)):
111117
"""重新加载信息配置"""
112118
try:
113-
config = load_info_config()
119+
config = await load_info_config()
114120
return {"success": True, "message": "配置重新加载成功", "data": config}
115121
except Exception as e:
116122
logger.error(f"重新加载信息配置失败: {e}")

src/agents/common/middlewares/context_middlewares.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
from langchain.agents.middleware import ModelRequest, ModelResponse, dynamic_prompt, wrap_model_call
66

7-
from src.utils import logger
87
from src.agents.common import load_chat_model
8+
from src.utils import logger
99

1010

1111
@dynamic_prompt

0 commit comments

Comments
 (0)