Files
nexus/wiki/concepts/Generation.md

34 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
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 输入 LLMLLM 严格基于上下文中提供的信息生成答案
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]] — 实际执行生成任务的模型