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.'
尽管这包含了有关凯坦吉·布朗·杰克逊的相关信息,但我们还没有一个简洁、易于理解的答案。我们将在下一节中解决这个问题。
使用一个大语言模型(LLM)来生成流利的回答
数据已索引到 MVI 中后,我们可以与任何利用向量相似性搜索的链进行集成。在这里,我们使用 RetrievalQA 链来演示如何从已索引的数据中回答问题。
首先我们加载相关模块:
from langchain.chains import RetrievalQA
from langchain_openai import 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 文档。