MongoDB Atlas
MongoDB Atlas 是一个完全托管的云端数据库,可在 AWS、Azure 和 GCP 上使用。它现在支持在 MongoDB 文档数据上原生的向量搜索。
安装与设置¶
查看 详细配置说明。
我们需要安装 langchain-mongodb 个 Python 包。
pip install langchain-mongodb
向量存储
查看 使用示例。
from langchain_mongodb import MongoDBAtlasVectorSearch
API 参考:MongoDBAtlas向量搜索
检索器
全文检索检索器
Hybrid Search Retriever使用 Lucene 的默认 (BM25) 分析器执行全文搜索。
from langchain_mongodb.retrievers import MongoDBAtlasFullTextSearchRetriever
API 参考:MongoDBAtlas全文搜索检索器
混合搜索检索器
Hybrid Search Retriever结合向量和全文搜索,通过Reciprocal Rank Fusion(RRF) 算法对其进行加权。
from langchain_mongodb.retrievers import MongoDBAtlasHybridSearchRetriever
API 参考:MongoDBAtlas混合搜索检索器
模型缓存
MongoDB 缓存
用于在 MongoDB 中存储简单缓存的抽象层。此实现不使用语义缓存,也不需要在生成前对集合创建索引。
导入此缓存:
from langchain_mongodb.cache import MongoDBCache
API 参考: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,
))
API 参考:set_llm_cache
MongoDB Atlas语义缓存
语义缓存允许用户根据用户输入与之前缓存结果之间的语义相似性检索缓存的提示。幕后它结合了MongoDB Atlas作为缓存和向量存储。
MongoDBAtlasSemanticCache继承自MongoDBAtlasVectorSearch,并且需要定义一个工作的Atlas向量搜索索引,请参阅使用示例了解如何设置该索引。
导入此缓存:
from langchain_mongodb.cache import MongoDBAtlasSemanticCache
API 参考:MongoDB Atlas 语义缓存
要使用此缓存与您的 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,
))
API 参考:set_llm_cache
``