Momento 向量索引(MVI)
MVI: the most productive, easiest to use, serverless vector index for your data. To get started with MVI, simply sign up for an account. There's no need to handle infrastructure, manage servers, or be concerned about scaling. MVI is a service that scales automatically to meet your needs.
要注册并访问 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.'
虽然这包含有关凯坦吉·布朗·杰克逊的相关信息,但我们没有一个简洁、易于阅读的答案。我们将在下一节中解决这个问题。
使用大型语言模型生成流畅的回答
在数据被索引到 MVI 之后,我们可以与任何利用向量相似性搜索的链进行集成。这里我们使用 RetrievalQA 链来演示如何从已索引的数据中回答问题。
首先我们加载相关的模块:
from langchain.chains import RetrievalQA
from langchain_openai import ChatOpenAI
然后我们实例化检索问答链:
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 文档。