MongoDB Atlas
MongoDB Atlas 是一个完全托管的云数据库,可在 AWS、Azure 和 GCP 上使用。它现在支持在 MongoDB 文档数据上进行原生向量搜索。
安装和设置
请参阅 详细配置说明。
我们需要安装 langchain-mongodb Python 包。
pip install langchain-mongodb
Vector Store
请参阅 用法示例。
from langchain_mongodb import MongoDBAtlasVectorSearch
API Reference:MongoDBAtlasVectorSearch
Retrievers
Full Text Search Retriever
Hybrid Search Retriever使用 Lucene 的标准 (BM25) 分析器执行全文搜索。
from langchain_mongodb.retrievers import MongoDBAtlasFullTextSearchRetriever
API Reference:MongoDBAtlasFullTextSearchRetriever
Hybrid Search Retriever
Hybrid Search Retriever结合了向量搜索和全文搜索,并通过Reciprocal Rank Fusion(RRF) 算 法对它们进行加权。
from langchain_mongodb.retrievers import MongoDBAtlasHybridSearchRetriever
API Reference:MongoDBAtlasHybridSearchRetriever
Model Caches
MongoDBCache
一个在 MongoDB 中存储简单缓存的抽象。它不使用语义缓存,也不需要在生成前对集合建立索引。
导入此缓存:
from langchain_mongodb.cache import MongoDBCache
API Reference:MongoDBCache
将此缓存与您的 LLM 一起使用:
from langchain_core.globals import set_llm_cache
# 使用任何 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 Reference:set_llm_cache
MongoDBAtlasSemanticCache
语义缓存允许用户根据用户输入和先前缓存的结果之间的语义相似性来检索缓存的提示。在底层,它将 MongoDB Atlas 同时用作缓存和向量存储。
MongoDBAtlasSemanticCache 继承自 MongoDBAtlasVectorSearch,需要定义一个 Atlas Vector Search 索引才能工作。请参阅 用法示例 以了解如何设置索引。
导入此缓存:
from langchain_mongodb.cache import MongoDBAtlasSemanticCache
API Reference:MongoDBAtlasSemanticCache
将此缓存与您的 LLM 一起使用:
from langchain_core.globals import set_llm_cache
# 使用任何 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 Reference:set_llm_cache