34 lines
1.6 KiB
Markdown
34 lines
1.6 KiB
Markdown
---
|
||
title: "Generation"
|
||
type: concept
|
||
tags: [rag, generation, llm, prompt, reasoning]
|
||
last_updated: 2025-01-16
|
||
---
|
||
|
||
## Definition
|
||
Generation(生成阶段)是 RAG Pipeline 的第三步,将用户问题与 Retrieval 阶段检索到的相关文档块组合为 Prompt,输入 LLM 生成最终答案。
|
||
|
||
## Process
|
||
1. **Context Assembly**:将用户问题(Question)与 Top-k 个相关文档块(Context)放入字典结构:`{"question": ..., "context": ...}`
|
||
2. **Prompt Templating**:通过 PromptTemplate 将 Question 和 Context 组合为结构化的 Prompt String
|
||
3. **LLM Inference**:将 Prompt 输入 LLM,LLM 严格基于上下文中提供的信息生成答案
|
||
4. **Output Parsing**:从 LLM 输出中提取纯字符串结果
|
||
|
||
## Key Requirements for Generation
|
||
- **Source Grounding**:LLM 必须严格基于检索到的上下文生成,不能凭空发挥
|
||
- **Answer Attribution**:理想情况下应提供答案的来源引用(哪些文档块支持该答案)
|
||
|
||
## In RAG Pipeline
|
||
- **上游**:接收 Retrieval 阶段返回的文档块作为上下文
|
||
- **下游**:输出最终答案给用户
|
||
|
||
## Frameworks Simplify This
|
||
LangChain 和 LlamaIndex 将 Retrieval + Generation 封装为 RAG Chain(如 RetrievalQA Chain),只需几行代码即可完成端到端 Pipeline。
|
||
|
||
## Related Concepts
|
||
- [[RAG]] — Generation 是 RAG Pipeline 的第三阶段
|
||
- [[Retrieval]] — Generation 的上游,提供上下文
|
||
- [[PromptTemplate]] — 组装 Question + Context 的模板技术
|
||
- [[Chain]] — LangChain 中串联 Retrieval 和 Generation 的抽象
|
||
- [[Large Language Model]] — 实际执行生成任务的模型
|