Skip to main content
Open on GitHub

MongoDB Atlas

MongoDB Atlas is a fully-managed cloud database available in AWS, Azure, and GCP. It now has support for native Vector Search on the MongoDB document data.

安装与设置

查看 详细配置说明

我们需要安装 langchain-mongodb 个 Python 包。

pip install langchain-mongodb

向量存储

查看一个 使用示例

from langchain_mongodb import MongoDBAtlasVectorSearch

检索器

全文搜索检索器

Hybrid Search Retriever performs full-text searches using Lucene’s standard (BM25) analyzer.

from langchain_mongodb.retrievers import MongoDBAtlasFullTextSearchRetriever

混合搜索检索器

Hybrid Search Retriever combines vector and full-text searches weighting them the via Reciprocal Rank Fusion (RRF) algorithm.

from langchain_mongodb.retrievers import MongoDBAtlasHybridSearchRetriever

模型缓存

MongoDBCache

一个用于在 MongoDB 中存储简单缓存的抽象。它不使用语义缓存,也不要求在生成之前对集合创建索引。

要导入此缓存:

from langchain_mongodb.cache import MongoDBCache
API 参考:MongoDBCache

要将此缓存与您的大语言模型一起使用:

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,
))
API 参考:set_llm_cache

MongoDBAtlasSemanticCache

语义缓存允许用户根据用户输入与之前缓存结果之间的语义相似性来检索缓存的提示。在底层,它将MongoDBAtlas同时用作缓存和向量存储。 MongoDBAtlasSemanticCache 继承自 MongoDBAtlasVectorSearch,并需要定义一个 Atlas 向量搜索索引才能工作。请查看使用示例,了解如何设置索引。

要导入此缓存:

from langchain_mongodb.cache import MongoDBAtlasSemanticCache

要将此缓存与您的大语言模型一起使用:

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,
))
API 参考:set_llm_cache

``