跳到主要内容
Open In ColabOpen on GitHub

Vectara Chat

Vectara 是值得信赖的 AI 助手和代理平台,专注于任务关键型应用程序的企业就绪性。

Vectara 无服务器 RAG 即服务提供易于使用的 API 背后的所有 RAG 组件,包括

  1. 一种从文件(PDF、PPT、DOCX 等)中提取文本的方法
  2. 基于机器学习的分块,提供最先进的性能。
  3. Boomerang 嵌入模型。
  4. 其自身的内部向量数据库,用于存储文本块和嵌入向量。
  5. 一种查询服务,可自动将查询编码为嵌入,并检索最相关的文本段(包括对 混合搜索 以及多种重新排序选项的支持,例如 多语言相关性重新排序器MMRUDF 重新排序器
  6. 一个 LLM,用于创建基于检索到的文档(上下文)的 生成式摘要,包括引文。

有关如何使用 API 的更多信息,请参阅 Vectara API 文档

本笔记本展示了如何使用 Vectara 的 Chat 功能,该功能提供对话历史记录的自动存储,并确保后续问题考虑该历史记录。

入门指南

要开始使用,请按照以下步骤操作

  1. 如果您还没有 Vectara 试用帐户,请注册免费试用版。完成注册后,您将获得一个 Vectara 客户 ID。您可以通过单击 Vectara 控制台窗口右上角的您的姓名来找到您的客户 ID。
  2. 在您的帐户中,您可以创建一个或多个语料库。每个语料库代表一个区域,用于存储从输入文档摄取的文本数据。要创建语料库,请使用 “创建语料库” 按钮。然后,您需要为语料库提供名称和描述。您可以选择定义过滤属性并应用一些高级选项。如果您单击您创建的语料库,您可以在顶部看到其名称和语料库 ID。
  3. 接下来,您需要创建 API 密钥才能访问语料库。单击语料库视图中的 “访问控制” 选项卡,然后单击 “创建 API 密钥” 按钮。为您的密钥命名,并选择您想要 query-only 还是 query+index 用于您的密钥。单击“创建”,您现在就拥有了一个活动的 API 密钥。请务必对该密钥保密。

要将 LangChain 与 Vectara 结合使用,您需要以下三个值:customer IDcorpus IDapi_key。您可以通过两种方式将这些值提供给 LangChain

  1. 在您的环境中包含以下三个变量:VECTARA_CUSTOMER_IDVECTARA_CORPUS_IDVECTARA_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:")
  1. 将它们添加到 Vectara 向量存储构造函数中
vectara = Vectara(
vectara_customer_id=vectara_customer_id,
vectara_corpus_id=vectara_corpus_id,
vectara_api_key=vectara_api_key
)

在本笔记本中,我们假设它们是在环境中提供的。

import os

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_community.vectorstores import Vectara
from langchain_community.vectorstores.vectara import (
RerankConfig,
SummaryConfig,
VectaraQueryConfig,
)

Vectara Chat 详解

在大多数使用 LangChain 创建聊天机器人的场景中,必须集成一个特殊的 memory 组件,该组件维护聊天会话的历史记录,然后使用该历史记录来确保聊天机器人了解对话历史记录。

使用 Vectara Chat,所有这些都在后端由 Vectara 自动执行。您可以查看 Chat 文档以了解更多详细信息,了解其内部实现方式,但在 LangChain 中,您只需在 Vectara 向量存储中启用该功能即可。

让我们看一个例子。首先,我们加载 SOTU 文档(请记住,文本提取和分块都在 Vectara 平台上自动进行)

from langchain_community.document_loaders import TextLoader

loader = TextLoader("state_of_the_union.txt")
documents = loader.load()

vectara = Vectara.from_documents(documents, embedding=None)
API 参考:TextLoader

现在我们使用 as_chat 方法创建一个 Chat Runnable

summary_config = SummaryConfig(is_enabled=True, max_results=7, response_lang="eng")
rerank_config = RerankConfig(reranker="mmr", rerank_k=50, mmr_diversity_bias=0.2)
config = VectaraQueryConfig(
k=10, lambda_val=0.005, rerank_config=rerank_config, summary_config=summary_config
)

bot = vectara.as_chat(config)

这是一个询问没有聊天历史记录的问题的示例

bot.invoke("What did the president say about Ketanji Brown Jackson?")["answer"]
'The President expressed gratitude to Justice Breyer and highlighted the significance of nominating Ketanji Brown Jackson to the Supreme Court, praising her legal expertise and commitment to upholding excellence [1]. The President also reassured the public about the situation with gas prices and the conflict in Ukraine, emphasizing unity with allies and the belief that the world will emerge stronger from these challenges [2][4]. Additionally, the President shared personal experiences related to economic struggles and the importance of passing the American Rescue Plan to support those in need [3]. The focus was also on job creation and economic growth, acknowledging the impact of inflation on families [5]. While addressing cancer as a significant issue, the President discussed plans to enhance cancer research and support for patients and families [7].'

这是一个询问带有某些聊天历史记录的问题的示例

bot.invoke("Did he mention who she suceeded?")["answer"]
"In his remarks, the President specified that Ketanji Brown Jackson is succeeding Justice Breyer on the United States Supreme Court[1]. The President praised Jackson as a top legal mind who will continue Justice Breyer's legacy of excellence. The nomination of Jackson was highlighted as a significant constitutional responsibility of the President[1]. The President emphasized the importance of this nomination and the qualities that Jackson brings to the role. The focus was on the transition from Justice Breyer to Judge Ketanji Brown Jackson on the Supreme Court[1]."

流式聊天

当然,聊天机器人界面也支持流式传输。只需使用 stream 而不是 invoke 方法

output = {}
curr_key = None
for chunk in bot.stream("what about her accopmlishments?"):
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
Judge Ketanji Brown Jackson is a nominee for the United States Supreme Court, known for her legal expertise and experience as a former litigator. She is praised for her potential to continue the legacy of excellence on the Court[1]. While the search results provide information on various topics like innovation, economic growth, and healthcare initiatives, they do not directly address Judge Ketanji Brown Jackson's specific accomplishments. Therefore, I do not have enough information to answer this question.

此页面是否对您有帮助?