Vectara 是一个值得信赖的 AI 助手和 Agent 平台,专注于企业关键任务应用的成熟度。 Vectara 无服务器 RAG 即服务通过易于使用的 API 提供 RAG 的所有组件,包括:
- 从文件中提取文本的方法(PDF、PPT、DOCX 等)
- 基于机器学习的分块技术,提供卓越的性能。
- Boomerang 嵌入模型。
- 其自身的内部向量数据库,用于存储文本块和嵌入向量。
- 查询服务,自动将查询编码为嵌入,并检索最相关的文本片段,包括支持 混合搜索 以及多种重排序选项,例如 多语言相关性重排序器, MMR, UDF 重排序器.
- 用于创建的 LLM 生成式摘要,基于检索到的文档(上下文),包括引用。
更多信息:
- - 文档
- - API Playground
- - 快速入门
本笔记本展示如何仅将 Vectara 用作向量存储(无摘要)时使用基本检索功能,包括: similarity_search 和 similarity_search_with_score 以及使用 LangChain as_retriever functionality.
设置
要使用 VectaraVectorStore 您首先需要安装合作伙伴包。
!uv pip install -U pip && uv pip install -qU langchain-vectara
入门指南
要开始,请按照以下步骤操作:
- 如果您还没有账户, 注册 获取免费 Vectara 试用版。
- 在您的账户中,您可以创建一个或多个语料库。每个语料库代表一个存储区域,用于存放从输入文档中提取的文本数据。要创建语料库,请使用 **"创建语料库"** 按钮。然后您可以为语料库提供名称和描述。可选地,您可以定义过滤属性并应用一些高级选项。点击创建的语料库,您可以在顶部看到其名称和语料库 ID。
- 接下来,您需要创建 API 密钥来访问语料库。点击 **"访问控制"** 选项卡,然后点击 **"创建 API 密钥"** 按钮。为密钥命名,然后选择密钥是仅查询还是查询+索引。点击"创建",您就拥有了一个活动 API 密钥。请妥善保管此密钥。
要将 LangChain 与 Vectara 结合使用,您需要提供这两个值: corpus_key 和 api_key. 您可以通过以下方式提供 VECTARA_API_KEY 给 LangChain:
- 在您的环境中包含这两个变量:
VECTARA_API_KEY.
例如,您可以使用 os.environ 和 getpass 设置这些变量,如下所示:
os.environ["VECTARA_API_KEY"] = getpass.getpass("Vectara API Key:")
- 将它们添加到
Vectaravectorstore 构造函数:
vectara = Vectara(
vectara_api_key=vectara_api_key
)
在本笔记本中,我们假设它们已在环境中提供。
os.environ["VECTARA_API_KEY"] = ""
os.environ["VECTARA_CORPUS_KEY"] = "VECTARA_CORPUS_KEY"
from langchain_vectara import Vectara
from langchain_vectara.vectorstores import (
ChainReranker,
CorpusConfig,
CustomerSpecificReranker,
File,
GenerationConfig,
MmrReranker,
SearchConfig,
VectaraQueryConfig,
)
vectara = Vectara(vectara_api_key=os.getenv("VECTARA_API_KEY"))
首先,我们将国情咨文文本加载到 Vectara 中。
请注意,我们使用 add_files 接口,该接口不需要任何本地处理或分块——Vectara 接收文件内容并执行所有必要的预处理、分块和嵌入操作,将文件存入其知识库中。
在本例中它使用 .txt 文件,但同样的方法也适用于许多其他 文件类型.
corpus_key = os.getenv("VECTARA_CORPUS_KEY")
file_obj = File(
file_path="../document_loaders/example_data/state_of_the_union.txt",
metadata={"source": "text_file"},
)
vectara.add_files([file_obj], corpus_key)
['state_of_the_union.txt']
Vectara RAG(检索增强生成)
我们现在创建一个 VectaraQueryConfig 对象来控制检索和摘要选项: - 我们启用摘要功能,指定希望 LLM 选择前 7 个匹配块并用英文回复
使用此配置,让我们创建一个封装完整 Vectara RAG 管道的 LangChain Runnable 对象,使用 as_rag method:
generation_config = GenerationConfig(
max_used_search_results=7,
response_language="eng",
generation_preset_name="vectara-summary-ext-24-05-med-omni",
enable_factual_consistency_score=True,
)
search_config = SearchConfig(
corpora=[CorpusConfig(corpus_key=corpus_key)],
limit=25,
reranker=ChainReranker(
rerankers=[
CustomerSpecificReranker(reranker_id="rnk_272725719", limit=100),
MmrReranker(diversity_bias=0.2, limit=100),
]
),
)
config = VectaraQueryConfig(
search=search_config,
generation=generation_config,
)
query_str = "what did Biden say?"
rag = vectara.as_rag(config)
rag.invoke(query_str)["answer"]
"President Biden discussed several key issues in his recent statements. He emphasized the importance of keeping schools open and noted that with a high vaccination rate and reduced hospitalizations, most Americans can safely return to normal activities without masks [1]. He addressed the need to hold social media platforms accountable for their impact on children and called for stronger privacy protections and mental health services [2]. Biden also announced measures against Russia, including preventing its central bank from defending the Ruble and targeting Russian oligarchs' assets, as part of efforts to weaken Russia's economy and military [3]. Additionally, he highlighted the importance of protecting women's rights, specifically the right to choose as affirmed in Roe v. Wade [5]. Lastly, he advocated for funding the police with necessary resources and training to ensure community safety [6]."
我们也可以像这样使用流式接口:
output = {}
curr_key = None
for chunk in rag.stream(query_str):
for key in chunk:
if key not in output:
output[key] = chunk[key]
else:
output[key] += chunk[key]
if key == "answer":
print(chunk[key], end="", flush=True)
curr_key = key
President Biden emphasized several key points in his statements. He highlighted the importance of keeping schools open and noted that with a high vaccination rate and reduced hospitalizations, most Americans can safely return to normal activities without masks [1]. He addressed the need to hold social media platforms accountable for their impact on children and called for stronger privacy protections and mental health services [2]. Biden also discussed measures against Russia, including preventing their central bank from defending the Ruble and targeting Russian oligarchs' assets [3]. Additionally, he reaffirmed the commitment to protect women's rights, particularly the right to choose as affirmed in Roe v. Wade [5]. Lastly, he advocated for funding the police to ensure community safety [6].
有关 Vectara 作为 VectorStore 的更多详细信息 请访问此笔记本.
Vectara 聊天
在大多数使用 LangChain 创建聊天机器人的场景中,必须集成一个特殊的 memory 组件来维护聊天会话的历史记录,然后使用该历史记录来确保聊天机器人了解对话历史。
使用 Vectara Chat——所有这些都由 Vectara 在后端自动执行。
generation_config = GenerationConfig(
max_used_search_results=7,
response_language="eng",
generation_preset_name="vectara-summary-ext-24-05-med-omni",
enable_factual_consistency_score=True,
)
search_config = SearchConfig(
corpora=[CorpusConfig(corpus_key=corpus_key, limit=25)],
reranker=MmrReranker(diversity_bias=0.2),
)
config = VectaraQueryConfig(
search=search_config,
generation=generation_config,
)
bot = vectara.as_chat(config)
bot.invoke("What did the president say about Ketanji Brown Jackson?")["answer"]
'The president stated that nominating someone to serve on the United States Supreme Court is one of the most serious constitutional responsibilities he has. He nominated Circuit Court of Appeals Judge Ketanji Brown Jackson, describing her as one of the nation’s top legal minds who will continue Justice Breyer’s legacy of excellence [1].'
Vectara 作为自查询检索器
Vectara 提供智能查询重写选项,通过从自然语言查询自动生成元数据过滤表达式来增强搜索精度。此功能分析用户查询,提取相关元数据过滤器,并重新表述查询以聚焦核心信息需求。