Overview
Baseline RAG struggles with queries that require aggregation of information across the dataset. Queries such as “What are the top 5 themes in the data?” perform poorly because baseline RAG relies on vector search of semantically similar text content, with nothing in the query to direct it to the correct information.GraphRAG’s global search solves this by leveraging the structure of the LLM-generated knowledge graph, which reveals the dataset’s structure and themes. The dataset is organized into meaningful semantic clusters that are pre-summarized.
How it works
Global search operates in two stages: map and reduce.Map stage
Given a user query and optional conversation history, global search uses LLM-generated community reports from a specified level of the graph’s community hierarchy as context data.- Segmentation: Community reports are divided into text chunks of pre-defined size
- Shuffling: Reports are randomly shuffled and distributed across batches
- Parallel processing: Each batch generates an intermediate response
- Rating: Each point in the intermediate responses receives a numerical importance rating
Reduce stage
The reduce stage aggregates and refines the intermediate responses:- Filtering: Points are filtered based on importance scores (score > 0)
- Ranking: Remaining points are sorted by descending importance
- Selection: Top-ranked points are selected within the token budget
- Aggregation: Selected points are combined to generate the final response
Configuration
TheGlobalSearch class accepts the following key parameters:
Language model chat completion object for response generation
Context builder object for preparing context data from community reports
Prompt template for the map stage. Default:
MAP_SYSTEM_PROMPTPrompt template for the reduce stage. Default:
REDUCE_SYSTEM_PROMPTFree-form text describing the desired response format (e.g., “Multiple Paragraphs”, “Multi-Page Report”)
If
true, prompts the LLM to incorporate relevant real-world knowledge outside the dataset. May increase hallucinations but useful for certain scenarios.Instruction added to the reduce prompt when
allow_general_knowledge is enabled. Default: GENERAL_KNOWLEDGE_INSTRUCTIONToken budget for context data
Additional parameters (e.g., temperature, max_tokens) for the LLM call at the map stage
Additional parameters (e.g., temperature, max_tokens) for the LLM call at the reduce stage
Additional parameters passed to the context builder when building the context window for the map stage
Controls the degree of parallelism in the map stage
Optional callback functions for custom event handlers during LLM completion streaming
API usage
Basic usage
Streaming usage
Dynamic community selection
Performance considerations
Community hierarchy level
Thecommunity_level parameter significantly impacts search performance:
- Lower levels (closer to leaf nodes): More detailed reports, higher quality responses, but increased time and LLM resource usage
- Higher levels (closer to root): Broader summaries, faster responses, but potentially less detailed
Token budget optimization
Adjustmax_data_tokens to balance quality and cost:
Parallelism tuning
Control parallel processing withconcurrent_coroutines:
Best practices
Choose the right community level
Start with level 2 and adjust based on your dataset size and query complexity
Use dynamic community selection for complex queries
Enable
dynamic_community_selection=True for queries requiring variable depthCustomize response types
Specify clear response formats: “Single Paragraph”, “Multiple Paragraphs”, “Multi-Page Report”, “List of 5-10 Items”
Examples
Thematic analysis
Comprehensive report generation
Next steps
Local search
Learn about entity-based search
DRIFT search
Explore hybrid search methods
Example notebooks
See global search in action
Configuration
Configure global search settings