feat: 知识批量沉淀改为后台任务,避免 nginx 504
- /ingest/threads/batch 新增 background 参数(默认 true),立即返回 {queued:true}
- 抽取循环抽出 _run_batch_ingest,后台路径显式传入 created_by
- 前端知识库页与输入框、线程 hooks/types 配套调整
- config.yaml 上下文压缩测试值
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
b380575f16
commit
342fa8265a
12 files changed
+170
-22
No files matched your search
@@ -65,6 +65,10 @@ class BatchIngestRequest(BaseModel):
|
||||
include_sources: bool = True
|
||||
skip_existing: bool = True
|
||||
mode: str = "summary"
|
||||
background: bool = Field(
|
||||
default=True,
|
||||
description="When true (default), the per-thread extraction loop runs in a background task and the endpoint returns immediately ({queued:true}). Batch capture of many threads is far too slow to finish inside one HTTP request — running it inline makes nginx time out (504), which the frontend surfaces as a generic 'request failed'.",
|
||||
)
|
||||
model_name: str | None = Field(default=None, description="可选:指定用于知识提炼的大模型名称;留空使用默认配置")
|
||||
template_id: str | None = Field(default=None, description="可选:抽取提示词模板 id;留空使用对话默认模板")
|
||||
folder: str | None = Field(default=None, description="可选:沉淀结果归入的目录(斜杠分隔路径)")
|
||||
@@ -285,15 +289,14 @@ async def ingest_thread(thread_id: str, body: IngestThreadRequest, request: Requ
|
||||
}
|
||||
|
||||
|
||||
@router.post("/ingest/threads/batch")
|
||||
@require_auth
|
||||
async def ingest_threads_batch(body: BatchIngestRequest, request: Request) -> dict[str, Any]:
|
||||
"""Batch-sediment the caller's recent threads into knowledge notes."""
|
||||
service = get_knowledge_service(request)
|
||||
checkpointer = get_checkpointer(request)
|
||||
thread_store = get_thread_store(request)
|
||||
async def _run_batch_ingest(service, checkpointer, threads: list[dict[str, Any]], body: BatchIngestRequest, created_by: str | None) -> dict[str, Any]:
|
||||
"""Run the per-thread sediment loop and tally the outcome.
|
||||
|
||||
threads = await thread_store.search(limit=body.limit, offset=body.offset)
|
||||
Shared by the inline and background paths; ``created_by`` is passed in
|
||||
explicitly because the user contextvar is gone once a background task runs.
|
||||
Each thread's transcript is truncated by the extractor (``max_input_chars``)
|
||||
before the LLM call, so a single huge conversation can't blow the context.
|
||||
"""
|
||||
created = skipped = failed = 0
|
||||
# Notes that landed via the rule-based fallback because the chosen model
|
||||
# produced nothing usable (timeout / API error / non-JSON). Surfaced so the
|
||||
@@ -319,7 +322,7 @@ async def ingest_threads_batch(body: BatchIngestRequest, request: Request) -> di
|
||||
status=body.status,
|
||||
include_sources=body.include_sources,
|
||||
reference_batches=batches,
|
||||
created_by=get_effective_user_id(),
|
||||
created_by=created_by,
|
||||
skip_existing=body.skip_existing,
|
||||
model_name=body.model_name,
|
||||
template_id=body.template_id,
|
||||
@@ -347,6 +350,49 @@ async def ingest_threads_batch(body: BatchIngestRequest, request: Request) -> di
|
||||
return {"ok": True, "processed": len(items), "created": created, "skipped": skipped, "failed": failed, "llm_fallback": llm_fallback, "items": items}
|
||||
|
||||
|
||||
async def _ingest_threads_batch_background(service, checkpointer, threads: list[dict[str, Any]], body: BatchIngestRequest, created_by: str | None) -> None:
|
||||
"""Background variant of the batch loop (failures swallowed/logged)."""
|
||||
try:
|
||||
result = await _run_batch_ingest(service, checkpointer, threads, body, created_by)
|
||||
logger.info(
|
||||
"Background batch ingest finished: processed=%d created=%d skipped=%d failed=%d llm_fallback=%d",
|
||||
result["processed"], result["created"], result["skipped"], result["failed"], result["llm_fallback"],
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Background batch ingest failed")
|
||||
|
||||
|
||||
@router.post("/ingest/threads/batch")
|
||||
@require_auth
|
||||
async def ingest_threads_batch(body: BatchIngestRequest, request: Request, background_tasks: BackgroundTasks) -> dict[str, Any]:
|
||||
"""Batch-sediment the caller's recent threads into knowledge notes.
|
||||
|
||||
Defaults to ``background=true``: the (slow) per-thread extraction runs in a
|
||||
background task and the endpoint returns ``{"queued": true, "processed": N}``
|
||||
immediately, so the request never outlives the nginx gateway timeout. Pass
|
||||
``background=false`` to block until every thread is processed (small batches /
|
||||
scripts that want the full per-thread tally).
|
||||
"""
|
||||
service = get_knowledge_service(request)
|
||||
checkpointer = get_checkpointer(request)
|
||||
thread_store = get_thread_store(request)
|
||||
|
||||
threads = await thread_store.search(limit=body.limit, offset=body.offset)
|
||||
if body.background:
|
||||
# Capture the user id now — the contextvar is gone once the task runs.
|
||||
background_tasks.add_task(
|
||||
_ingest_threads_batch_background,
|
||||
service,
|
||||
checkpointer,
|
||||
threads,
|
||||
body,
|
||||
get_effective_user_id(),
|
||||
)
|
||||
return {"ok": True, "queued": True, "processed": len(threads), "created": 0, "skipped": 0, "failed": 0, "llm_fallback": 0, "items": []}
|
||||
|
||||
return await _run_batch_ingest(service, checkpointer, threads, body, get_effective_user_id())
|
||||
|
||||
|
||||
async def _extract_upload_text(file: UploadFile) -> tuple[str, str]:
|
||||
"""Read an uploaded file and return ``(filename, markdown_text)``.
|
||||
|
||||
@@ -476,7 +522,7 @@ async def list_notes(
|
||||
offset: int = 0,
|
||||
) -> dict[str, Any]:
|
||||
service = get_knowledge_service(request)
|
||||
limit = max(1, min(limit, 100))
|
||||
limit = max(1, min(limit, 1000))
|
||||
offset = max(0, offset)
|
||||
return await service.list_notes(keyword=keyword, tag=tag, entity=entity, source_type=source_type, status=status, folder=folder, limit=limit, offset=offset)
|
||||
|
||||
|
||||
@@ -243,6 +243,9 @@ _CONTEXT_CONFIGURABLE_KEYS: frozenset[str] = frozenset(
|
||||
# 关掉个人记忆召回 / builtin 记忆注入(见 multi_agent._run_payload)。
|
||||
"memory_recall_disabled",
|
||||
"memory_injection_enabled",
|
||||
# 上下文压缩开关:由聊天框下方的按钮控制(默认关闭),透传到 run config 后
|
||||
# 在 _build_middlewares 里决定是否挂载 SummarizationMiddleware。
|
||||
"summarization_enabled",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user