Auto-sync: update nexus workspace
This commit is contained in:
@@ -1,33 +1,54 @@
|
||||
---
|
||||
title: "Generation"
|
||||
type: concept
|
||||
tags: [rag, generation, llm, prompt, reasoning]
|
||||
tags: [RAG, LLM, 问答生成]
|
||||
sources: [rag从入门到精通系列1-基础rag]
|
||||
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 输出中提取纯字符串结果
|
||||
Generation(生成阶段)是 RAG 管道的第三阶段,将用户问题与检索到的相关文档块组合成 Prompt,输入 LLM 生成带事实依据的答案。
|
||||
|
||||
## Key Requirements for Generation
|
||||
- **Source Grounding**:LLM 必须严格基于检索到的上下文生成,不能凭空发挥
|
||||
- **Answer Attribution**:理想情况下应提供答案的来源引用(哪些文档块支持该答案)
|
||||
## Core Process
|
||||
|
||||
## In RAG Pipeline
|
||||
- **上游**:接收 Retrieval 阶段返回的文档块作为上下文
|
||||
- **下游**:输出最终答案给用户
|
||||
```
|
||||
问题 + Top-k 文档块 → PromptTemplate 组装 → LLM 生成 → 返回答案
|
||||
```
|
||||
|
||||
## Frameworks Simplify This
|
||||
LangChain 和 LlamaIndex 将 Retrieval + Generation 封装为 RAG Chain(如 RetrievalQA Chain),只需几行代码即可完成端到端 Pipeline。
|
||||
1. **组装上下文**:将问题(Question)和检索到的文档块(Context)放入预定义的字典结构
|
||||
2. **Prompt 模板**:通过 PromptTemplate 将问题+上下文组合成完整的 Prompt String
|
||||
3. **LLM 生成**:将 Prompt 输入 LLM,LLM 基于检索到的知识生成回答
|
||||
4. **可选链式组合**:使用 LangChain/LlamaIndex 的 Chain 将 Retrieval 和 Generation 串联为统一管道
|
||||
|
||||
## Related Concepts
|
||||
- [[RAG]] — Generation 是 RAG Pipeline 的第三阶段
|
||||
- [[Retrieval]] — Generation 的上游,提供上下文
|
||||
- [[PromptTemplate]] — 组装 Question + Context 的模板技术
|
||||
- [[Chain]] — LangChain 中串联 Retrieval 和 Generation 的抽象
|
||||
- [[Large Language Model]] — 实际执行生成任务的模型
|
||||
## Key Prompt Pattern
|
||||
|
||||
```
|
||||
你是一个问答助手。请根据以下参考资料回答用户问题。
|
||||
如果参考资料中没有相关信息,请如实说明。
|
||||
|
||||
参考资料:
|
||||
{context}
|
||||
|
||||
用户问题:{question}
|
||||
|
||||
回答:
|
||||
```
|
||||
|
||||
## LangChain Chain
|
||||
|
||||
LangChain 和 LlamaIndex 提供了 `RetrievalQA Chain` 等开箱即用的链,可将检索和生成过程自动化串联,避免手动拼装 Prompt。
|
||||
|
||||
## Connections
|
||||
|
||||
- [[Generation]] ← part_of ← [[RAG]]
|
||||
- [[Generation]] ← receives_input ← [[Retrieval]]
|
||||
- [[Generation]] ← uses ← [[Large-Language-Model]]
|
||||
- [[Generation]] ← uses ← [[Prompt]]
|
||||
|
||||
## Aliases
|
||||
|
||||
- Answer Generation
|
||||
- RAG Generation
|
||||
- LLM Response Generation
|
||||
- 答案生成
|
||||
|
||||
Reference in New Issue
Block a user