mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-15 03:41:58 +00:00
6a46e4b55c
压测量出来的三个问题,10MB 文本(切 7914 段):
上传耗时 150s(本地哈希) 换成 Qwen3-Embedding-4B 要 40 分钟以上
而这 150 秒是压在一个 HTTP 请求里同步跑完的
浏览器和反向代理的默认超时都在 60 秒上下,也就是 5MB 以上的文件用户必然
看到超时——而后端其实还在跑,跑完文档也确实入了库。界面显示失败、数据其实
成功,比直接拒绝还糟。
**分批算向量。** 原来 ingest 是 embed_texts(pieces) 一次全发。本地哈希只是慢,
换成远端模型就是必挂:近八千段一次请求塞不下(OpenAI 单次上限 2048)。
reindex 那边早就分批了,ingest 这条一直漏着——而我上一轮刚把"换真模型"做成
设置页里点两下的事。
**上传转后台。** create_document / process_document 拆开,上传立刻返回
status=processing,切块和算向量在后台跑,进度写回文档行,前端轮询显示
"正在切块并算向量… 4288/7914 段"。实测上传返回 0.07 秒。
进度这件事返工过一次:先用另一个 session 单独写,结果整个过程界面上都是空的、
只在结束时闪一下——SQLite 只允许一个写者,主事务开着的时候那次写被堵住了。
改成主事务自己按批提交,顺手把进度写进去。
失败要写回 failed,否则永远停在"处理中"转圈。服务重启时也把残留的
processing 收尾成 failed——和 runner 对 running 的 run 是同一个道理。
**边读边数大小。** 原来是 raw = await file.read() 读完再判断,传个 1GB 的
文件内存在报 413 之前就吃掉了——那个检查拦的是"入库",不是"占内存"。改成
按块读、超了立刻停。上限从写死的 10MB 变成配置项,默认 50MB。
端到端验过:10MB 上传返回 0.07s,后台进度 768→1472→…→7914 实时可见,
最终 ready / 7914 段。
118 lines
3.4 KiB
Python
118 lines
3.4 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
from typing import AsyncIterator
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from app.api import (
|
|
artifacts, conversations, copilot, datasources, governance, knowledge, runs,
|
|
sandbox, settings as settings_api, tools, workflows,
|
|
)
|
|
from app.core.config import settings
|
|
from app.db.base import init_db
|
|
from app.engine.runner import run_manager
|
|
from app.seed import seed_defaults
|
|
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format="%(asctime)s %(levelname)-7s %(name)s | %(message)s",
|
|
datefmt="%H:%M:%S",
|
|
)
|
|
logger = logging.getLogger("agentlab")
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
|
settings.ensure_dirs()
|
|
await init_db()
|
|
await seed_defaults()
|
|
|
|
# 把存的 embedding 配置读进来。不读的话重启后会悄悄退回本地哈希向量,
|
|
# 而库里的向量是上次那个模型建的——检索会整体退回关键词,
|
|
# 用户只看到"重启之后搜得不准了"
|
|
from app.db.base import SessionLocal
|
|
from app.memory.embeddings import load_setting
|
|
|
|
async with SessionLocal() as _session:
|
|
try:
|
|
await load_setting(_session)
|
|
except Exception as e: # noqa: BLE001 - key 没了不该拦住整个服务起不来
|
|
logger.warning("embedding 配置没能生效,暂用本地向量:%s", e)
|
|
|
|
# 上次被强杀时留下的"处理中"文档。后台任务不会自己回来,留着它只会在
|
|
# 界面上永远转圈——和 runner 对 running 的 run 是同一个道理
|
|
from sqlalchemy import update as _update
|
|
|
|
from app.db.models import Document as _Doc
|
|
|
|
async with SessionLocal() as _s:
|
|
await _s.execute(
|
|
_update(_Doc).where(_Doc.status == "processing")
|
|
.values(status="failed", error="服务重启,这份文档没有处理完,请重新上传")
|
|
)
|
|
await _s.commit()
|
|
|
|
await run_manager.setup()
|
|
from app.sandbox.manager import sandbox_manager as _sbm
|
|
|
|
await _sbm.start_reaper()
|
|
logger.info("AgentLab 就绪 · 数据目录 %s", settings.data_dir)
|
|
try:
|
|
yield
|
|
finally:
|
|
await run_manager.shutdown()
|
|
from app.sandbox.manager import sandbox_manager
|
|
|
|
await sandbox_manager.close()
|
|
logger.info("已停止")
|
|
|
|
|
|
app = FastAPI(
|
|
title="AgentLab",
|
|
description="可视化 Agent 编排实验台",
|
|
version="0.1.0",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.cors_origins,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
for router in (
|
|
workflows.router,
|
|
runs.router,
|
|
runs.approvals_router,
|
|
settings_api.router,
|
|
tools.router,
|
|
tools.custom_router,
|
|
tools.mcp_router,
|
|
knowledge.memory_router,
|
|
knowledge.kb_router,
|
|
knowledge.skills_router,
|
|
sandbox.router,
|
|
copilot.router,
|
|
conversations.router,
|
|
artifacts.router,
|
|
governance.router,
|
|
datasources.router,
|
|
):
|
|
app.include_router(router)
|
|
|
|
|
|
@app.exception_handler(ValueError)
|
|
async def value_error_handler(_: Request, exc: ValueError) -> JSONResponse:
|
|
return JSONResponse(status_code=400, content={"detail": str(exc)})
|
|
|
|
|
|
@app.get("/api/health")
|
|
async def health() -> dict[str, str]:
|
|
return {"status": "ok", "service": "agentlab"}
|