How LlamaIndex SummaryIndex Works
1. Core Concepts and Design Approach
SummaryIndex is an index type in LlamaIndex built specifically for handling global questions. Its core design idea is: pass the entire document to the LLM as full context, and rely on the LLM’s comprehension ability to generate a global summary or answer a global question.
Unlike VectorStoreIndex, SummaryIndex does not vectorize or index documents. Instead, it stores all document chunks directly and passes them to the LLM all at once (or in batches) at query time.
2. The Complete Workflow
2.1 Document Loading and Splitting
# 在 load_pdf 函数中
documents = SimpleDirectoryReader(input_files=[pdf_path]).load_data()
- Purpose: Load a PDF file into a list of document objects
- Key point: Based on document size and the default splitting strategy, a single PDF is split into multiple document chunks
- Result: A
documentslist containing multiple document chunks, each holding text content and metadata
2.2 Building the SummaryIndex
# 在 load_pdf 函数中
summary_index = SummaryIndex.from_documents(documents)
Internal mechanics:
- Receive the
documentslist - Create a
SummaryIndexinstance - Store all document chunks in
index_struct - No vectorization — only the raw text and metadata are stored
- Receive the
Storage structure:
SummaryIndex ├── index_struct (IndexGraph) │ └── nodes (List[TextNode]) │ ├── node_1 (包含文档块1的文本和元数据) │ ├── node_2 (包含文档块2的文本和元数据) │ └── ... └── service_context (包含LLM、嵌入模型等配置)
2.3 Creating the Query Engine
# 在 main 函数中
summary_query_engine = create_summary_query_engine(summary_index)
- Internal mechanics:
- Create a
SummaryIndexQueryEngineinstance - Configure query parameters (such as
response_mode,summary_mode, etc.) - Associate the
SummaryIndexwith theservice_context
- Create a
2.4 Handling Global Questions
# 在 answer_question 函数中
if is_global_question(question):
response = summary_query_engine.query(question)
2.4.1 Core Processing Flow
When summary_query_engine.query(question) is called, the following steps run internally:
- Collect all document chunks: Retrieve every stored document chunk from the
SummaryIndex - Build the full context: Concatenate the text of all document chunks into a single complete context
- Generate the system prompt: Produce an appropriate system prompt based on the
summary_modeconfiguration - Call the LLM: Combine the system prompt, context, and user question into a complete request and send it to the configured LLM
- Process the model response: Receive the answer generated by the LLM and post-process it (e.g. strip redundant content)
- Return the final result: Hand the processed answer back to the user
2.4.2 Batch Processing Mechanism
For very long documents, SummaryIndex may adopt a batch processing strategy:
- Group document chunks: Divide all document chunks into multiple batches
- Multiple rounds of model calls:
- First round: Pass the first batch of document chunks to the LLM to generate a preliminary summary
- Subsequent rounds: Pass the previous round’s summary together with the next batch of document chunks to the LLM, asking it to update/merge the summary
- Final round: Combine the results from all batches to generate the final answer
- Iterative summary refinement: Each round builds on the previous results to improve the answer, ensuring all key information is captured
3. Comparison with VectorStoreIndex
| Feature | SummaryIndex | VectorStoreIndex |
|---|---|---|
| Use case | Global questions, document summarization | Specific questions, detail lookups |
| Document handling | Stores all document chunks, no vectorization | Vectorizes document chunks, creates a vector index |
| Query mechanism | Passes all document chunks to the LLM as context | Finds relevant document chunks via similarity search |
| Model calls | Single or multiple rounds, processes the full document | Single call, processes relevant document chunks |
| Answer characteristics | Comprehensive, highly generalized | Precise, highly targeted |
4. Implementation Details in the Example Code
4.1 Global Question Detection
def is_global_question(question: str) -> bool:
global_keywords = ["整篇", "全部", "哪些内容", "罗列", "概括", "主要内容"]
return any(keyword in question.lower() for keyword in global_keywords)
- Purpose: Determine the question type via keyword matching
- Key point: Identify global questions that need to be handled by
SummaryIndex - Result: Return a boolean that decides which query engine to use
4.2 Dual-Engine Switching Mechanism
def answer_question(question: str):
if is_global_question(question):
print("\n检测到全局问题,使用摘要查询引擎...")
return summary_query_engine.query(question)
else:
print("\n检测到具体问题,使用向量查询引擎...")
return vector_query_engine.query(question)
- Purpose: Automatically pick the appropriate query engine based on question type
- Key point: Seamless switching between the two index types
- Result: More accurate answers for the user
5. Code Suggestions
5.1 Add Summary Mode Configuration
def create_summary_query_engine(index: SummaryIndex):
"""创建摘要查询引擎(用于全局问题)"""
return index.as_query_engine(
response_mode="tree_summarize", # 树形摘要模式,适合长文档
summary_mode="refine", # 迭代优化模式,逐步完善摘要
verbose=True # 显示详细日志
)
- response_mode: Controls how the summary is generated; options include
tree_summarize,refine,simple_summarize - summary_mode: Controls the summary refinement strategy; options include
refine,map_reduce - verbose: Displays detailed processing logs, useful for debugging and understanding
5.2 Configure Batch Processing Parameters
def create_summary_query_engine(index: SummaryIndex):
"""创建摘要查询引擎(用于全局问题)"""
return index.as_query_engine(
chunk_size=1024, # 每批次处理的文档块大小
chunk_overlap=200, # 批次间的重叠大小,确保上下文连贯
max_tries=3 # 最大尝试次数
)
- chunk_size: Controls the size of document chunks processed per batch, affecting the number of model calls and memory usage
- chunk_overlap: Ensures contextual continuity between batches and prevents key information from being lost
- max_tries: Controls the maximum number of attempts, improving system stability
6. Things to Watch Out For in Practice
- Model selection: Use an LLM that supports long context, such as GPT-4o or Claude 3, for better summaries
- Document size limits: For very long documents (e.g. PDFs over 100 pages), filter or split them first to avoid overflowing the model context
- Performance optimization: For frequently accessed documents, pre-generate and cache summaries to speed up query responses
- Result verification: Since the LLM may hallucinate, verify summary results to make sure they match the source content
- Cost control: Batch processing increases the number of model calls, so API costs need to be taken into account
7. Summary
SummaryIndex is a powerful tool in LlamaIndex for handling global questions. Its core mechanism is to pass the complete document to the LLM as context and rely on the LLM’s comprehension ability to generate a global summary or answer. With sensible parameter configuration and a well-optimized flow, you can get high-quality global answers.
In practice, it’s worth combining VectorStoreIndex and SummaryIndex and automatically selecting the right query engine based on question type, so users get more comprehensive and accurate answers.
This dual-engine architecture makes the most of both index types: it can handle specific local questions as well as questions that require reading the whole document to answer, providing solid support for building a high-quality question-answering system.