Mastra中的RAG(检索增强生成)
🌐 RAG (Retrieval-Augmented Generation) in Mastra
Mastra 中的 RAG 帮助你通过整合来自自己数据源的相关上下文来增强 LLM 的输出,提高准确性并使回复基于真实信息。
🌐 RAG in Mastra helps you enhance LLM outputs by incorporating relevant context from your own data sources, improving accuracy and grounding responses in real information.
Mastra 的 RAG 系统提供:
🌐 Mastra's RAG system provides:
- 用于处理和嵌入文档的标准化 API
- 支持多个向量存储
- 用于最佳检索的分块和嵌入策略
- 用于跟踪嵌入和检索性能的可观察性
示例Direct link to 示例
🌐 Example
要实现RAG,你需要将文档分块、创建嵌入、将其存储在向量数据库中,然后在查询时检索相关的上下文。
🌐 To implement RAG, you process your documents into chunks, create embeddings, store them in a vector database, and then retrieve relevant context at query time.
import { embedMany } from "ai";
import { PgVector } from "@mastra/pg";
import { MDocument } from "@mastra/rag";
import { z } from "zod";
// 1. Initialize document
const doc = MDocument.fromText(`Your document text here...`);
// 2. Create chunks
const chunks = await doc.chunk({
strategy: "recursive",
size: 512,
overlap: 50,
});
// 3. Generate embeddings; we need to pass the text of each chunk
import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
const { embeddings } = await embedMany({
values: chunks.map((chunk) => chunk.text),
model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small")
});
// 4. Store in vector database
const pgVector = new PgVector({
id: 'pg-vector',
connectionString: process.env.POSTGRES_CONNECTION_STRING,
});
await pgVector.upsert({
indexName: "embeddings",
vectors: embeddings,
}); // using an index name of 'embeddings'
// 5. Query similar chunks
const results = await pgVector.query({
indexName: "embeddings",
queryVector: queryVector,
topK: 3,
}); // queryVector is the embedding of the query
console.log("Similar chunks:", results);
这个例子展示了基本步骤:初始化文档、创建块、生成嵌入、存储它们,并查询相似内容。
🌐 This example shows the essentials: initialize a document, create chunks, generate embeddings, store them, and query for similar content.
文档处理Direct link to 文档处理
🌐 Document Processing
RAG 的基本构建模块是文档处理。文档可以使用各种策略进行分块(递归、滑动窗口等),并添加元数据。请参阅 分块和嵌入文档。
🌐 The basic building block of RAG is document processing. Documents can be chunked using various strategies (recursive, sliding window, etc.) and enriched with metadata. See the chunking and embedding doc.
向量存储Direct link to 向量存储
🌐 Vector Storage
Mastra 支持多种向量存储来进行嵌入持久化和相似性搜索,包括 pgvector、Pinecone、Qdrant 和 MongoDB。请参见 向量数据库文档。
🌐 Mastra supports multiple vector stores for embedding persistence and similarity search, including pgvector, Pinecone, Qdrant, and MongoDB. See the vector database doc.
更多资源Direct link to 更多资源
🌐 More resources