跳到主要内容
MongoDB Atlas 是 AWS、Azure 和 GCP 上可用的全托管云数据库。它现在支持在 MongoDB 文档数据上进行原生向量搜索。

安装和设置

请参阅详细配置说明 我们需要安装 langchain-mongodb python 包。
pip install langchain-mongodb

向量存储

请参阅使用示例
from langchain_mongodb import MongoDBAtlasVectorSearch

检索器

全文搜索检索器

Hybrid Search Retriever 使用 Lucene 的标准 (BM25) 分析器执行全文搜索。
from langchain_mongodb.retrievers import MongoDBAtlasFullTextSearchRetriever

混合搜索检索器

Hybrid Search Retriever 通过 Reciprocal Rank Fusion (RRF) 算法结合向量和全文搜索并为其加权。
from langchain_mongodb.retrievers import MongoDBAtlasHybridSearchRetriever

模型缓存

MongoDBCache

一个用于在 MongoDB 中存储简单缓存的抽象。它不使用语义缓存,也不需要在生成之前在集合上创建索引。 要导入此缓存:
from langchain_mongodb.cache import MongoDBCache
将此缓存与您的 LLM 一起使用
from langchain_core.globals import set_llm_cache

# use any embedding provider...
from tests.integration_tests.vectorstores.fake_embeddings import FakeEmbeddings

mongodb_atlas_uri = "<YOUR_CONNECTION_STRING>"
COLLECTION_NAME="<YOUR_CACHE_COLLECTION_NAME>"
DATABASE_NAME="<YOUR_DATABASE_NAME>"

set_llm_cache(MongoDBCache(
    connection_string=mongodb_atlas_uri,
    collection_name=COLLECTION_NAME,
    database_name=DATABASE_NAME,
))

MongoDBAtlasSemanticCache

语义缓存允许用户根据用户输入和先前缓存结果之间的语义相似性检索缓存的提示。它在底层将 MongoDB Atlas 既用作缓存又用作向量存储。MongoDBAtlasSemanticCache 继承自 MongoDBAtlasVectorSearch,需要定义 Atlas 向量搜索索引才能工作。请参阅使用示例了解如何设置索引。 要导入此缓存:
from langchain_mongodb.cache import MongoDBAtlasSemanticCache
将此缓存与您的 LLM 一起使用
from langchain_core.globals import set_llm_cache

# use any embedding provider...
from tests.integration_tests.vectorstores.fake_embeddings import FakeEmbeddings

mongodb_atlas_uri = "<YOUR_CONNECTION_STRING>"
COLLECTION_NAME="<YOUR_CACHE_COLLECTION_NAME>"
DATABASE_NAME="<YOUR_DATABASE_NAME>"

set_llm_cache(MongoDBAtlasSemanticCache(
    embedding=FakeEmbeddings(),
    connection_string=mongodb_atlas_uri,
    collection_name=COLLECTION_NAME,
    database_name=DATABASE_NAME,
))

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.