n8n 中的 RAG(RAG in n8n)#
什么是 RAG?(What is RAG)#
检索增强生成(RAG) 是一种通过将语言模型与外部数据源结合来提升 AI 响应能力的技术。RAG 系统不单依赖模型内部的训练数据,而是检索相关文档,以将响应基于最新的、特定领域的或专有的知识上。RAG 的工作流程通常依赖向量存储来高效管理和搜索这些外部数据。
什么是向量存储?(What is a vector store?)#
向量存储是一种专门设计用来存储和搜索高维向量的数据库:这些向量是文本、图片或其他数据的数值表示。当你上传文档时,向量存储会将其拆分成若干块,并使用嵌入模型将每一块转换为向量。
🌐 A vector store is a special database designed to store and search high-dimensional vectors: numerical representations of text, images, or other data. When you upload a document, the vector store splits it into chunks and converts each chunk into a vector using an embedding model.
你可以使用相似性搜索来查询这些向量,这种方法是基于语义意义构建结果,而不是关键词匹配。这使得向量存储成为 RAG 和其他需要在大量知识中进行检索和推理的 AI 系统的强大基础。
🌐 You can query these vectors using similarity searches, which construct results based on semantic meaning, rather than keyword matches. This makes vector stores a powerful foundation for RAG and other AI systems that need to retrieve and reason over large sets of knowledge.
如何在 n8n 中使用 RAG(How to use RAG in n8n)#
从 RAG 模板开始
👉 在 n8n 中试用 RAG,使用 RAG 入门模板。该模板包含两个现成的工作流:一个用于上传文件,另一个用于查询文件。
插入中将数据导入你的矢量存储(Inserting data into your vector store)#
你的代理可以访问自定义知识库之前,你需要将数据上传到向量存储:
🌐 Before your agent can access custom knowledge, you need to upload that data to a vector store:
- 添加获取源数据所需的节点。
- 插入一个 向量存储 节点(例如 简单向量存储),并选择 插入文档 操作。
- 选择一个嵌入模型,它可以将你的文本转换成向量嵌入。有关更多信息,请查阅常见问题解答,了解如何选择合适的嵌入模型。
- 添加一个默认数据加载器节点,它会将内容拆分成小块。你可以使用默认设置或自定义分块策略:
- 字符文本分割器:按字符长度分割。
- 递归字符文本分割器:通过 Markdown、HTML、代码块或简单字符递归分割(推荐用于大多数使用场景)。
- 令牌文本拆分器:按令牌数量拆分。
- (可选) 为每个块添加元数据以丰富上下文,并便于之后更好地筛选。
查询你的数据(Querying your data)#
您可以通过两种主要方式查询数据:使用代理或直接通过节点。
🌐 You can query the data in two main ways: using an agent or directly through a node.
使用代理(Using agents)#
- 将一个代理添加到你的工作流程中。
- 将向量存储添加为一个工具,并为其提供一个描述,以帮助代理了解何时使用它:
- 设置限制以定义要返回的块数。
- 启用 包含元数据 以为每个块提供额外的上下文。
- 添加与插入数据时使用的相同嵌入模型。
直接使用节点(Using the node directly)#
- 将你的向量存储节点添加到画布中,并选择 获取多个 操作。
- 输入查询或提示:
- 设置要返回的块数量的限制。
- 如有需要,请启用包含元数据。
常见问题(FAQs)#
如何选择合适的嵌入模型?(How do I choose the right embedding model?)#
合适的嵌入模型因具体情况而异。
🌐 The right embedding model differs from case to case.
一般来说,较小的模型(例如 text-embedding-ada-002)速度更快、成本更低,因此非常适合简短的通用文档或轻量级的 RAG 工作流程。较大的模型(例如 text-embedding-3-large)提供更好的语义理解。它们最适合处理长文档、复杂主题或需要高精度的情况。
🌐 In general, smaller models (for example, text-embedding-ada-002) are faster and cheaper and thus ideal for short, general-purpose documents or lightweight RAG workflows. Larger models (for example, text-embedding-3-large) offer better semantic understanding. These are best for long documents, complex topics, or when accuracy is critical.
哪种文本分割方式最适合我的使用场景?(What is the best text splitting for my use case?)#
这很大程度上取决于你的数据:
🌐 This again depends a lot on your data:
- 小块数据(例如,200 到 500 个令牌)适合细粒度检索。
- 较大的音频块可能包含更多上下文信息,但也可能变得模糊或嘈杂。
使用合适的重叠大小对于AI理解文本块的上下文非常重要。这也是为什么使用Markdown或代码块拆分通常有助于更好地划分文本块的原因。
🌐 Using the right overlap size is important for the AI to understand the context of the chunk. That's also why using the Markdown or Code Block splitting can often help to make chunks better.
另一个不错的方法是为其添加更多的上下文信息(例如,该片段来自的文档)。如果你想了解更多相关内容,可以查看 Anthropic 的这篇精彩文章。
🌐 Another good approach is to add more context to it (for example, about the document where the chunk came from). If you want you can read more about this, you can check out this great article from Anthropic.