mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-03 01:01:56 +08:00
## Problem When building or updating a knowledge graph with a large number of entities and edges, `set_graph()` in `rag/graphrag/utils.py` creates one `asyncio` task per entity and one per edge, each calling `embd_mdl.encode([single_name])` — a single-item HTTP request to the embedding server. For a graph with 17,000+ nodes and edges (real case reported in #16205), this generates **34,000+ individual embedding API round-trips** instead of ~266 batched calls at the default `_INSERT_BULK_SIZE=128`. The asyncio gather over thousands of tasks makes the embedding server the bottleneck; under load, a single slow/failed call aborts all remaining tasks, causing the pipeline to stall and never complete. Closes #16205. Related: #15921. ## Root Cause ```python # Before (in set_graph, node loop): tasks = [asyncio.create_task(graph_node_to_chunk(n, ...)) for n in nodes] # Each task calls embd_mdl.encode([single_name]) — 1 HTTP call per node ``` `graph_node_to_chunk` checks the embed cache first, but the cache is cold on first build, so every task makes a live API call. ## Fix Pre-warm the embedding cache with batched calls before spawning tasks. Each batch pre-warm calls `embd_mdl.encode(batch_of_128)` once, populating the cache. Then every individual task hits the cache and makes zero embedding API calls. - Only encodes names not already in cache (no-op on warm cache / small incremental updates) - Uses existing project idioms: `thread_pool_exec`, `chat_limiter`, `_INSERT_BULK_SIZE`, `get_embed_cache`, `set_embed_cache` - Mirrors the `ENABLE_TIMEOUT_ASSERTION` timeout pattern from `graph_node_to_chunk` - Zero behavior change: per-task encode logic remains as a correct fallback ## Result | Graph size | Before | After | |---|---|---| | 17,576 edges | ~17,576 embedding calls → stall | ~138 batched calls | | 17,509 nodes | ~17,509 embedding calls → stall | ~137 batched calls | | **Total** | **~35,000 calls** | **~275 calls** | --------- Co-authored-by: Oti_B <oti@mac.speedport.ip>