重要
自动嵌入功能在自管理mongot中处于预览阶段。在预览期间,该功能和相应文档可能会随时更改。要了解更多信息,请参阅预览功能。
本教程介绍如何在自管理 mongot 部署上配置自动嵌入。借助自动嵌入,mongot 可在索引时间为文本数据生成向量嵌入,在查询时间为查询文本生成向量嵌入。您无需在应用程序中维护独立的嵌入管道。
This tutorial is for developers who are building semantic search or RAG features and want mongot to manage embeddings. For MongoDB Controllers for Kubernetes Operator deployments, see Automated Embedding.
自动嵌入的功能
无自动嵌入,您的应用程序必须:
为插入或更新的每个文档生成嵌入。
将该嵌入与集合中的文档一起存储。
在查询时间使用相同模型为每个查询生成嵌入。
向
$vectorSearch提交查询嵌入。
借助自动嵌入,mongot 可以处理步骤 1、2 和 3。您的应用程序:
为文本字段配置具有
autoEmbed字段类型的 MongoDB 向量搜索索引。正常插入和更新文档。
mongot读取文本字段,通过配置的嵌入模型生成嵌入,并将其存储。使用查询文本而非预计算嵌入来发出
$vectorSearch查询。mongot为查询文本生成嵌入。
支持的嵌入提供商
自管理 mongot 自动嵌入与 Voyage AI 嵌入模型集成。
模型 | 说明 |
|---|---|
| 推荐。平衡一般文本搜索的性能。 |
| 大容量、成本敏感的应用程序。 |
| 复杂语义关系的最高准确性。 |
| 专门用于代码搜索和技术文档。 |
您需要支付嵌入生成的费用。要学习更多信息,请参阅管理自动嵌入的计费。
先决条件
要使用自动嵌入,您必须具备以下先决条件:
mongot在自管理部署(Community tarball 或容器、本地开发和通过 Kubernetes 操作符的企业版)上使用 1.70.1 或更高版本。MongoDB 8.3 or later for standalone and Kubernetes Operator-managed deployments. To learn more, see Compatibility and Requirements for
mongot.Two Voyage AI API keys.
使用两个独立的密钥,一个用于索引时间嵌入生成,一个用于查询时间。独立密钥可以将查询工作负载与索引工作负载隔离,并允许独立的速率限制计费。要学习;了解更多信息,请参阅 速率限制。
从
mongot到嵌入终结点的出站网络访问。The default endpoint is
https://ai.mongodb.com/v1/embeddings. This endpoint proxies Voyage AI with MongoDB-managed billing, for keys generated using the Atlas UI. You can also usehttps://api.voyageai.com/v1/embeddingsfor direct Voyage AI access if you generated the API key directly from Voyage AI.
步骤
Generate Voyage AI API Keys.
You can generate Voyage AI API keys through either of the following methods:
(Recommended) Through your Atlas account. Atlas provides API key management with built-in rate-limit configuration. The keys are not tied to an Atlas cluster. To learn more, see Manage API Keys.
Through Voyage AI directly at voyageai.com.
生成两个密钥并清楚命名,例如 mongot-prod-index 和 mongot-prod-query。将密钥存储在安全的秘密存储中。
Configure mongot for Automated Embedding.
配置取决于您的部署路径。选择部署的标签页
自动嵌入需要 preview 本地开发图像 (mongodb/mongodb-atlas-local:preview)。preview 图像包含 mongot 的 Community 构建,其中包括自动嵌入集成。
If you launch the container directly with docker run, pass the Voyage AI API key with the VOYAGE_API_KEY environment variable and use the :preview tag:
docker run \ -e VOYAGE_API_KEY=<your-voyage-api-key> \ -p 27017:27017 \ mongodb/mongodb-atlas-local:preview
如果您通过 Voyage AI 而非 Atlas 直接颁发了 Voyage AI 密钥,请覆盖默认终结点:
docker run \ -e VOYAGE_API_KEY=<your-voyage-api-key> \ -e EMBEDDING_PROVIDER_ENDPOINT=https://api.voyageai.com/v1/embeddings \ -p 27017:27017 \ mongodb/mongodb-atlas-local:preview
注意
Local Development accepts only a single Voyage AI API key and uses it for both index-time and query-time embedding generation. A single key is acceptable for development. For production deployments that need separate index-time and query-time keys, use the Community Edition or Kubernetes Operator deployment path.
将 embedding 块添加到 mongot 配置文件:
embedding: queryKeyFile: /etc/mongot/secrets/voyage-api-query-key indexingKeyFile: /etc/mongot/secrets/voyage-api-indexing-key providerEndpoint: https://ai.mongodb.com/v1/embeddings
Each key file contains the API key as its only content. Set the file permissions to 0600 and set the file owner to the user that runs the mongot process.
创建自动嵌入索引。
After you configure mongot with your Voyage AI API keys, define a MongoDB Vector Search index that uses the autoEmbed field type. The index definition specifies which text field mongot embeds and which model it uses. It also specifies the indexing method, quantization, and other parameters.
db.movies.createSearchIndex( "movie-semantic-index", "vectorSearch", { "fields": [ { "type": "autoEmbed", "modality": "text", "path": "plot", "model": "voyage-4", "numDimensions": 1024, "similarity": "cosine", "indexingMethod": "hnsw", "hnswOptions": { "maxEdges": 16, "numEdgeCandidates": 50 }, "quantization": "scalar" }, { "type": "filter", "path": "genre" } ] } )
下表描述了 autoEmbed 索引定义字段:
字段 | 用途 |
|---|---|
| 将字段标记为自动嵌入。 |
| 数据模态。 |
| 要嵌入的集合中的字段。 |
| The Voyage AI model name. The model must be one that your API key has access to. |
| 嵌入向量的维数。该模型确定支持的维数。 |
| 用于向量搜索的相似度函数。支持的函数是 |
| 要使用的索引方法。支持的方法是 |
| (Optional) The HNSW index options. Required if |
| (可选)要使用的量化类型。支持的类型有 |
您可以在同一索引中混合 autoEmbed 字段和 filter 字段。不能在同一路径上将 autoEmbed 与原始向量字段混合使用。每个字段选择一种方法。
运行自动嵌入查询。
提交查询文本,而不是预计算的嵌入。mongot 为您生成查询嵌入。
db.movies.aggregate([ { "$vectorSearch": { "index": "movie-semantic-index", "path": "plot", "query": "a heist gone wrong in a rainy city", "model": "voyage-4", "numCandidates": 100, "limit": 10 } }, { "$project": { "_id": 0, "title": 1, "score": { $meta: "vectorSearchScore" } } } ])
要了解更多信息,请参阅运行向量搜索近似最近邻 (ANN) 和精确最近邻 (ENN) 查询。
嵌入存储
mongot 将嵌入持久存储在集群上的专用内部数据库中。mongot 不会在源集合中存储嵌入。mongot 可以从源文本重新生成嵌入,并且您需要支付嵌入生成费用。
对嵌入模型、输出维度或量化的更改 trigger 受影响索引的完全重新嵌入,因此,您需支付嵌入生成费用。请参阅 mongot 重新生成嵌入时。
要了解更多,请参阅 生成的嵌入集合。
When mongot Regenerates Embeddings
mongot 在发生以下任何情况时,会重新生成 autoEmbed 字段的嵌入:
您插入一个文档。
您更改文档中的嵌入字段。
mongot通过变更流检测到更改。您在索引定义中更改嵌入模型。
您更改嵌入输出维度或数据类型。
您更改了字段路径(Field Path)。
如果您更改任何后三个项,mongot 将从头开始重建整个索引。对于大型集合,重建成本可能很高。计划模型变更。
费用和速率限制
MongoDB 按模型特定费率按百万个令牌收费。索引费用与集合中索引字段的总文本量成正比。查询费用与查询文本量成正比。
If you use the Voyage AI API key that you created using your Atlas account, you can view API key usage and rate limits in the Atlas UI. To learn more, see Manage Billing for the Embedding and Reranking API. If you use keys created directly from Voyage AI, you must monitor your usage and rate limits in the Voyage AI dashboard.
Voyage AI 侧的速率限制错误会出现在 mongot 日志中,并作为受影响索引上的失败文档指示符。这些错误不会禁用索引,但会延迟新文档的索引。
失败模式
症状 | 可能的原因 | 操作 |
|---|---|---|
索引在 |
| 检查 |
索引延迟会间歇性地出现。 | Voyage AI 侧速率限制。 | 增加速率限制或减少索引时间嵌入负载。 |
针对索引的查询返回一个提及嵌入的错误。 | The query API key is invalid or expired. | 验证 |
对索引的所有查询都返回空结果。 | 索引可能已经重建,并仍在重新嵌入语料。 | 使用 |
限制
自动嵌入应用以下限制:
预览状态。在自动嵌入普遍可用之前,配置形状、支持的模型和默认终结点可能会发生变化。
每个字段一个嵌入模型。不支持混合模型索引。
无嵌入模型故障转移。如果 Voyage AI 终结点无法访问,则新文档的索引将停滞。