Overview
TheResponseGenerator class creates natural language responses from retrieved document sections. It extracts key information and includes inline citations with section titles and page numbers.
Class Definition
Methods
generate
Generates a response from document sections with inline citations.Example
Response Generation Strategy
The generator follows a simple but effective approach:- Empty Check: Returns fallback message if no sections provided
- Key Info Extraction: Extracts the first sentence from each section
- Citation Addition: Appends inline citation with section title and page number
- Concatenation: Joins all parts with spaces
Fallback Handling
When no sections are provided:Key Information Extraction
Extracts the first sentence (or first 200 characters as fallback):r'(?<=[.!?])\s+'
- Splits on periods, exclamation marks, or question marks
- Preserves the punctuation in the sentence
- Requires whitespace after punctuation
Citation Format
Inline citations follow this format:(See Late Payment Penalties, page 5)(See Intellectual Property Rights, page 12)(See Termination for Convenience, page 8)
Implementation Details
Full Source Code
Usage Examples
Example 1: Payment Query
Example 2: Multiple Sections
Example 3: No Sections
Example 4: Missing Fields
Design Philosophy
Conciseness
Extracts only the first sentence to provide focused, relevant information without overwhelming the user.Transparency
Every statement includes a citation showing exactly where the information came from.Verifiability
Page numbers enable users to quickly locate and verify information in source documents.Simplicity
No LLM calls or complex synthesis—just direct extraction and formatting for maximum reliability.Integration
The generator sits between the retriever and the judge in the RAG pipeline:Performance Characteristics
- Synchronous: No async operations required
- Fast: Simple string operations and regex
- Deterministic: Same sections always produce same response
- No API calls: No dependency on external LLM services
- Extraction-based: Uses source text directly (no generation)
Comparison with LLM-Based Generation
| Feature | ResponseGenerator | LLM-Based |
|---|---|---|
| Speed | Fast (under 1ms) | Slow (100-1000ms) |
| Cost | Free | $$ per request |
| Reliability | Deterministic | Stochastic |
| Hallucination Risk | Low (extraction) | High (generation) |
| Synthesis | None | Advanced |
| Citations | Always included | Often missing |
ResponseGenerator trades synthesis capability for reliability, transparency, and speed. For applications where accuracy is paramount, extraction-based generation is often preferable to LLM-based synthesis.