RAG & LLMs / 7. LLM GENERATION
Stage 7: LLM Generation
Decoder-only LLM grounded by reranked context
EXPLANATION
The LLM (decoder-only transformer) takes the reranked chunks as context and generates an answer token by token. LangChain's LCEL (LangChain Expression Language) chains everything together cleanly using the | pipe operator. The prompt is critical: • Inject retrieved context at the top • Explicitly tell the LLM to answer ONLY from context • Tell it to say "I don't know" if context is insufficient • Low temperature (0.1) = factual, consistent answers The model generates token by token conditioned on (context + question). This is the decoder-only part — pure autoregressive generation.
DATA FLOW
[reranked_chunk_1]
[reranked_chunk_2] ← context from retrieval
[reranked_chunk_3]
+
Question: {query}
↓
ChatPromptTemplate
↓
Gemini / GPT / Claude ← decoder-only LLM
generates token by token
↓
Grounded answer with source referencesCODE