diff --git a/backend/app/services/llm/enrichment.py b/backend/app/services/llm/enrichment.py index 39024d5..da4faa8 100644 --- a/backend/app/services/llm/enrichment.py +++ b/backend/app/services/llm/enrichment.py @@ -389,8 +389,8 @@ async def enrich_article(article_id: int) -> dict[str, str]: # === 后台循环 === # 与 translation_loop 一样,常驻从队列里取文章 -ENRICHMENT_INTERVAL_SEC = 5.0 # 没活时等待 -ENRICHMENT_BATCH_SIZE = 3 # 每轮并发拉取候选,然后顺序处理(LLM 客户端本身有节流) +ENRICHMENT_INTERVAL_SEC = 2.0 # 没活时等待 +ENRICHMENT_BATCH_SIZE = 8 # 每轮并发拉取候选,然后顺序处理(LLM 客户端本身有节流) async def enrichment_loop() -> None: @@ -438,12 +438,17 @@ async def enrichment_loop() -> None: await asyncio.sleep(ENRICHMENT_INTERVAL_SEC) continue - for aid in todo_ids: - try: - await enrich_article(aid) - except Exception as e: - logger.exception("enrich_article %s in loop failed: %s", aid, e) - await asyncio.sleep(0.2) # 文章间轻节流(LLM 内部还有 interval_sec) + # 并发 enrich 多篇(LlmClient 内部 interval_sec 已经做了限速,这里只并发不限并发上限) + # 但为了不让 Agnes API 同时打太多,加一层并发上限 + sem = asyncio.Semaphore(3) + async def _run_one(aid: int) -> None: + async with sem: + try: + await enrich_article(aid) + except Exception as e: + logger.exception("enrich_article %s in loop failed: %s", aid, e) + + await asyncio.gather(*[_run_one(aid) for aid in todo_ids]) except Exception as e: logger.exception("enrichment_loop error: %s", e) await asyncio.sleep(ENRICHMENT_INTERVAL_SEC)