Vectara 自查询
Vectara 是值得信赖的人工智能助手和代理平台,专注于企业关键任务应用的准备。
Vectara 无服务器 RAG 即服务提供了一个易于使用的 API,包含了 RAG 的所有组件,包括:
- 一种从文件(PDF、PPT、DOCX等)中提取文本的方法
- 基于机器学习的分块技术,提供最先进的性能。
- Boomerang 嵌入模型。
- 它自己的内部向量数据库,用于存储文本块和嵌入向量。
- 一个查询服务,自动将查询编码为嵌入,并检索最相关的文本片段,包括支持混合搜索以及多种重新排序选项,如多语言相关性重新排序器、MMR、UDF重新排序器。
- 一个LLM用于根据检索到的文档(上下文)创建生成式摘要,包括引用。
查看Vectara API文档以获取有关如何使用API的更多信息。
本笔记本展示了如何将SelfQueryRetriever
与Vectara一起使用。
入门指南
要开始使用,请按照以下步骤操作:
- 如果您还没有Vectara账户,请注册免费试用。完成注册后,您将获得一个Vectara客户ID。您可以通过点击Vectara控制台窗口右上角的您的名字来找到您的客户ID。
- 在您的账户中,您可以创建一个或多个语料库。每个语料库代表一个区域,用于存储从输入文档中提取的文本数据。要创建语料库,请使用"创建语料库"按钮。然后,您需要为您的语料库提供一个名称和描述。您还可以选择定义过滤属性并应用一些高级选项。如果您点击您创建的语料库,您可以在顶部看到它的名称和语料库ID。
- 接下来,您需要创建API密钥以访问语料库。在语料库视图中点击"访问控制"标签,然后点击"创建API密钥"按钮。为您的密钥命名,并选择您希望密钥是仅查询还是查询+索引。点击“创建”,您现在就有了一个有效的API密钥。请保密此密钥。
要将LangChain与Vectara一起使用,您需要具备以下三个值:customer ID
、corpus ID
和api_key
。
您可以通过两种方式将这些值提供给LangChain:
-
在你的环境中包含这三个变量:
VECTARA_CUSTOMER_ID
,VECTARA_CORPUS_ID
和VECTARA_API_KEY
。例如,你可以使用 os.environ 和 getpass 来设置这些变量,如下所示:
import os
import getpass
os.environ["VECTARA_CUSTOMER_ID"] = getpass.getpass("Vectara Customer ID:")
os.environ["VECTARA_CORPUS_ID"] = getpass.getpass("Vectara Corpus ID:")
os.environ["VECTARA_API_KEY"] = getpass.getpass("Vectara API Key:")
- 将它们添加到
Vectara
向量存储构造函数中:
vectara = Vectara(
vectara_customer_id=vectara_customer_id,
vectara_corpus_id=vectara_corpus_id,
vectara_api_key=vectara_api_key
)
在本笔记本中,我们假设它们在环境中提供。
注意: 自查询检索器需要你安装 lark
(pip install lark
)。
从LangChain连接到Vectara
在这个例子中,我们假设您已经创建了一个账户和一个语料库,并且已将VECTARA_CUSTOMER_ID
、VECTARA_CORPUS_ID
和VECTARA_API_KEY
(创建时具有索引和查询权限)添加为环境变量。
我们进一步假设语料库有4个字段定义为可过滤的元数据属性:year
、director
、rating
和genre
import os
from langchain_core.documents import Document
os.environ["VECTARA_API_KEY"] = "<YOUR_VECTARA_API_KEY>"
os.environ["VECTARA_CORPUS_ID"] = "<YOUR_VECTARA_CORPUS_ID>"
os.environ["VECTARA_CUSTOMER_ID"] = "<YOUR_VECTARA_CUSTOMER_ID>"
from langchain.chains.query_constructor.schema import AttributeInfo
from langchain.retrievers.self_query.base import SelfQueryRetriever
from langchain_community.vectorstores import Vectara
from langchain_openai.chat_models import ChatOpenAI
数据集
我们首先定义一个电影示例数据集,并将这些数据连同元数据上传到语料库中:
docs = [
Document(
page_content="A bunch of scientists bring back dinosaurs and mayhem breaks loose",
metadata={"year": 1993, "rating": 7.7, "genre": "science fiction"},
),
Document(
page_content="Leo DiCaprio gets lost in a dream within a dream within a dream within a ...",
metadata={"year": 2010, "director": "Christopher Nolan", "rating": 8.2},
),
Document(
page_content="A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea",
metadata={"year": 2006, "director": "Satoshi Kon", "rating": 8.6},
),
Document(
page_content="A bunch of normal-sized women are supremely wholesome and some men pine after them",
metadata={"year": 2019, "director": "Greta Gerwig", "rating": 8.3},
),
Document(
page_content="Toys come alive and have a blast doing so",
metadata={"year": 1995, "genre": "animated"},
),
Document(
page_content="Three men walk into the Zone, three men walk out of the Zone",
metadata={
"year": 1979,
"rating": 9.9,
"director": "Andrei Tarkovsky",
"genre": "science fiction",
},
),
]
vectara = Vectara()
for doc in docs:
vectara.add_texts([doc.page_content], doc_metadata=doc.metadata)
创建自查询检索器
现在我们可以实例化我们的检索器。为此,我们需要提前提供一些关于我们的文档支持的元数据字段的信息以及文档内容的简短描述。
然后我们提供一个llm(在这个例子中是OpenAI)和vectara
向量存储作为参数:
metadata_field_info = [
AttributeInfo(
name="genre",
description="The genre of the movie",
type="string or list[string]",
),
AttributeInfo(
name="year",
description="The year the movie was released",
type="integer",
),
AttributeInfo(
name="director",
description="The name of the movie director",
type="string",
),
AttributeInfo(
name="rating", description="A 1-10 rating for the movie", type="float"
),
]
document_content_description = "Brief summary of a movie"
llm = ChatOpenAI(temperature=0, model="gpt-4o", max_tokens=4069)
retriever = SelfQueryRetriever.from_llm(
llm, vectara, document_content_description, metadata_field_info, verbose=True
)
自我检索查询
现在我们可以尝试实际使用我们的检索器了!
# This example only specifies a relevant query
retriever.invoke("What are movies about scientists")
[Document(page_content='A bunch of scientists bring back dinosaurs and mayhem breaks loose', metadata={'lang': 'eng', 'offset': '0', 'len': '66', 'year': '1993', 'rating': '7.7', 'genre': 'science fiction', 'source': 'langchain'}),
Document(page_content='A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea', metadata={'lang': 'eng', 'offset': '0', 'len': '116', 'year': '2006', 'director': 'Satoshi Kon', 'rating': '8.6', 'source': 'langchain'}),
Document(page_content='Toys come alive and have a blast doing so', metadata={'lang': 'eng', 'offset': '0', 'len': '41', 'year': '1995', 'genre': 'animated', 'source': 'langchain'}),
Document(page_content='Three men walk into the Zone, three men walk out of the Zone', metadata={'lang': 'eng', 'offset': '0', 'len': '60', 'year': '1979', 'rating': '9.9', 'director': 'Andrei Tarkovsky', 'genre': 'science fiction', 'source': 'langchain'}),
Document(page_content='A bunch of normal-sized women are supremely wholesome and some men pine after them', metadata={'lang': 'eng', 'offset': '0', 'len': '82', 'year': '2019', 'director': 'Greta Gerwig', 'rating': '8.3', 'source': 'langchain'}),
Document(page_content='Leo DiCaprio gets lost in a dream within a dream within a dream within a ...', metadata={'lang': 'eng', 'offset': '0', 'len': '76', 'year': '2010', 'director': 'Christopher Nolan', 'rating': '8.2', 'source': 'langchain'})]
# This example only specifies a filter
retriever.invoke("I want to watch a movie rated higher than 8.5")
[Document(page_content='A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea', metadata={'lang': 'eng', 'offset': '0', 'len': '116', 'year': '2006', 'director': 'Satoshi Kon', 'rating': '8.6', 'source': 'langchain'}),
Document(page_content='Three men walk into the Zone, three men walk out of the Zone', metadata={'lang': 'eng', 'offset': '0', 'len': '60', 'year': '1979', 'rating': '9.9', 'director': 'Andrei Tarkovsky', 'genre': 'science fiction', 'source': 'langchain'})]
# This example specifies a query and a filter
retriever.invoke("Has Greta Gerwig directed any movies about women")
[Document(page_content='A bunch of normal-sized women are supremely wholesome and some men pine after them', metadata={'lang': 'eng', 'offset': '0', 'len': '82', 'year': '2019', 'director': 'Greta Gerwig', 'rating': '8.3', 'source': 'langchain'})]
# This example specifies a composite filter
retriever.invoke("What's a highly rated (above 8.5) science fiction film?")
[Document(page_content='A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea', metadata={'lang': 'eng', 'offset': '0', 'len': '116', 'year': '2006', 'director': 'Satoshi Kon', 'rating': '8.6', 'source': 'langchain'}),
Document(page_content='Three men walk into the Zone, three men walk out of the Zone', metadata={'lang': 'eng', 'offset': '0', 'len': '60', 'year': '1979', 'rating': '9.9', 'director': 'Andrei Tarkovsky', 'genre': 'science fiction', 'source': 'langchain'})]
# This example specifies a query and composite filter
retriever.invoke(
"What's a movie after 1990 but before 2005 that's all about toys, and preferably is animated"
)
[Document(page_content='Toys come alive and have a blast doing so', metadata={'lang': 'eng', 'offset': '0', 'len': '41', 'year': '1995', 'genre': 'animated', 'source': 'langchain'}),
Document(page_content='A bunch of scientists bring back dinosaurs and mayhem breaks loose', metadata={'lang': 'eng', 'offset': '0', 'len': '66', 'year': '1993', 'rating': '7.7', 'genre': 'science fiction', 'source': 'langchain'})]
筛选 k
我们也可以使用自我查询检索器来指定k
:要获取的文档数量。
我们可以通过将enable_limit=True
传递给构造函数来实现这一点。
retriever = SelfQueryRetriever.from_llm(
llm,
vectara,
document_content_description,
metadata_field_info,
enable_limit=True,
verbose=True,
)
这很酷,我们可以在查询中包含我们想要看到的结果数量,自我检索器会正确理解它。例如,让我们查找
# This example only specifies a relevant query
retriever.invoke("what are two movies with a rating above 8.5")
[Document(page_content='A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea', metadata={'lang': 'eng', 'offset': '0', 'len': '116', 'year': '2006', 'director': 'Satoshi Kon', 'rating': '8.6', 'source': 'langchain'}),
Document(page_content='Three men walk into the Zone, three men walk out of the Zone', metadata={'lang': 'eng', 'offset': '0', 'len': '60', 'year': '1979', 'rating': '9.9', 'director': 'Andrei Tarkovsky', 'genre': 'science fiction', 'source': 'langchain'})]