Skip to main content
Open In ColabOpen on GitHub

Momento 向量索引 (MVI)

MVI: 最高效、最易用的无服务器向量索引服务,适用于您的数据。要开始使用MVI,只需注册一个账户。无需处理基础设施、管理服务器或担心扩展问题。MVI是一项自动扩展以满足您需求的服务。

要注册并访问MVI,请访问Momento控制台

设置

安装先决条件

你将需要:

  • 用于与MVI交互的momento包,以及
  • 用于与OpenAI API交互的openai包。
  • 用于标记化文本的tiktoken包。
%pip install --upgrade --quiet  momento langchain-openai langchain-community tiktoken

输入API密钥

import getpass
import os

Momento: 用于索引数据

访问Momento控制台以获取您的API密钥。

if "MOMENTO_API_KEY" not in os.environ:
os.environ["MOMENTO_API_KEY"] = getpass.getpass("Momento API Key:")

OpenAI: 用于文本嵌入

if "OPENAI_API_KEY" not in os.environ:
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")

加载您的数据

这里我们使用来自Langchain的示例数据集,即国情咨文。

首先我们加载相关模块:

from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import MomentoVectorIndex
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter

然后我们加载数据:

loader = TextLoader("../../how_to/state_of_the_union.txt")
documents = loader.load()
len(documents)
1

注意数据是一个大文件,因此只有一个文档:

len(documents[0].page_content)
38539

因为这是一个大文本文件,我们将其分割成多个块用于问答。这样,用户的问题将从最相关的块中得到回答。

text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
len(docs)
42

索引您的数据

索引你的数据就像实例化MomentoVectorIndex对象一样简单。这里我们使用from_documents助手来同时实例化和索引数据:

vector_db = MomentoVectorIndex.from_documents(
docs, OpenAIEmbeddings(), index_name="sotu"
)

这使用您的API密钥连接到Momento向量索引服务并索引数据。如果之前索引不存在,此过程将为您创建它。数据现在可搜索。

查询您的数据

直接对索引提问

查询数据的最直接方法是针对索引进行搜索。我们可以使用VectorStore API如下操作:

query = "What did the president say about Ketanji Brown Jackson"
docs = vector_db.similarity_search(query)
docs[0].page_content
'Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \n\nTonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.'

虽然这确实包含了关于Ketanji Brown Jackson的相关信息,但我们没有一个简洁、易于理解的答案。我们将在下一节中解决这个问题。

使用LLM生成流畅的答案

随着数据在MVI中被索引,我们可以与任何利用向量相似性搜索的链进行集成。这里我们使用RetrievalQA链来演示如何从索引数据中回答问题。

首先我们加载相关的模块:

from langchain.chains import RetrievalQA
from langchain_openai import ChatOpenAI
API Reference:RetrievalQA | ChatOpenAI

然后我们实例化检索QA链:

llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
qa_chain = RetrievalQA.from_chain_type(llm, retriever=vector_db.as_retriever())
qa_chain({"query": "What did the president say about Ketanji Brown Jackson?"})
{'query': 'What did the president say about Ketanji Brown Jackson?',
'result': "The President said that he nominated Circuit Court of Appeals Judge Ketanji Brown Jackson to serve on the United States Supreme Court. He described her as one of the nation's top legal minds and mentioned that she has received broad support from various groups, including the Fraternal Order of Police and former judges appointed by Democrats and Republicans."}

下一步

就是这样!您现在已为数据建立了索引,并可以使用Momento向量索引进行查询。您可以使用相同的索引从任何支持向量相似性搜索的链中查询您的数据。

使用Momento,您不仅可以索引您的向量数据,还可以缓存您的API调用并存储您的聊天消息历史记录。查看其他Momento langchain集成以了解更多信息。

要了解更多关于Momento向量索引的信息,请访问Momento文档


这个页面有帮助吗?