Skip to main content
Open In Colab在 GitHub 上打开

维克塔拉

概述

Vectara 是值得信赖的 AI Assistant 和 Agent 平台,专注于为任务关键型应用程序做好企业准备。 Vectara 无服务器 RAG-as-a-service 在易于使用的 API 后面提供 RAG 的所有组件,包括:

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

有关更多信息,请访问:

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

设置

要使用VectaraVectorStore您首先需要安装 Partner Package。

!uv pip install -U pip && uv pip install -qU langchain-vectara

开始

要开始使用,请使用以下步骤:

  1. 如果您还没有,请注册您的 Vectara 免费试用版。
  2. 在您的帐户中,您可以创建一个或多个语料库。每个语料库都表示一个区域,该区域在从输入文档摄取时存储文本数据。要创建语料库,请使用 “Create Corpus” 按钮。然后,您为语料库提供名称以及描述。(可选)您可以定义筛选属性并应用一些高级选项。如果你点击你创建的语料库,你可以在顶部看到它的名称和语料库 ID。
  3. 接下来,您需要创建 API 密钥来访问语料库。单击语料库中的 “Access Control” 选项卡,然后单击 “Create API Key” 按钮。为您的密钥命名,然后选择是要对密钥进行 query-only 还是 query+index。单击 “Create”(创建),您现在拥有一个有效的 API 密钥。对此密钥保密。

要将 LangChain 与 Vectara 一起使用,您需要具有以下两个值:corpus_keyapi_key. 您可以提供VECTARA_API_KEY以两种方式发送到 LangChain:

实例

  1. 在您的环境中包括这两个变量:VECTARA_API_KEY.

    例如,您可以使用 os.environ 和 getpass 设置这些变量,如下所示:

import os
import getpass

os.environ["VECTARA_API_KEY"] = getpass.getpass("Vectara API Key:")
  1. 将它们添加到Vectaravectorstore 构造函数:
vectara = Vectara(
vectara_api_key=vectara_api_key
)

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

import os

os.environ["VECTARA_API_KEY"] = "<VECTARA_API_KEY>"
os.environ["VECTARA_CORPUS_KEY"] = "<VECTARA_CORPUS_KEY>"

from langchain_vectara import Vectara
from langchain_vectara.vectorstores import (
CorpusConfig,
GenerationConfig,
MmrReranker,
SearchConfig,
VectaraQueryConfig,
)

Vectara 聊天解释

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

使用 Vectara Chat - 所有这些都由 Vectara 在后端自动执行。你可以查看 Chat 文档了解详细信息,以了解有关如何实现此功能的内部结构的更多信息,但使用 LangChain,你所要做的就是在 Vectara vectorstore 中打开该功能。

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

from langchain_community.document_loaders import TextLoader

loader = TextLoader("../document_loaders/example_data/state_of_the_union.txt")
documents = loader.load()

corpus_key = os.getenv("VECTARA_CORPUS_KEY")
vectara = Vectara.from_documents(documents, embedding=None, corpus_key=corpus_key)
API 参考:TextLoader

现在,我们使用as_chat方法:

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 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 and noting her experience as a former top litigator in private practice [1].'

下面是一个带有一些聊天历史记录的问题的提问示例

bot.invoke("Did he mention who she suceeded?")["answer"]
'Yes, the president mentioned that Ketanji Brown Jackson succeeded Justice Breyer [1].'

通过流媒体聊天

当然,Chatbot 界面也支持流式传输。 而不是invoke方法,您只需使用stream:

output = {}
curr_key = None
for chunk in bot.stream("what did he said about the covid?"):
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
The president acknowledged the significant impact of COVID-19 on the nation, expressing understanding of the public's fatigue and frustration. He emphasized the need to view COVID-19 not as a partisan issue but as a serious disease, urging unity among Americans. The president highlighted the progress made, noting that severe cases have decreased significantly, and mentioned new CDC guidelines allowing most Americans to be mask-free. He also pointed out the efforts to vaccinate the nation and provide economic relief, and the ongoing commitment to vaccinate the world [2], [3], [5].

链接

有关其他功能,您可以使用 chaining。

from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai.chat_models import ChatOpenAI

llm = ChatOpenAI(temperature=0)

prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant that explains the stuff to a five year old. Vectara is providing the answer.",
),
("human", "{vectara_response}"),
]
)


def get_vectara_response(question: dict) -> str:
"""
Calls Vectara as_chat and returns the answer string. This encapsulates
the Vectara call.
"""
try:
response = bot.invoke(question["question"])
return response["answer"]
except Exception as e:
return "I'm sorry, I couldn't get an answer from Vectara."


# Create the chain
chain = get_vectara_response | prompt | llm | StrOutputParser()


# Invoke the chain
result = chain.invoke({"question": "what did he say about the covid?"})
print(result)
So, the president talked about how the COVID-19 sickness has affected a lot of people in the country. He said that it's important for everyone to work together to fight the sickness, no matter what political party they are in. The president also mentioned that they are working hard to give vaccines to people to help protect them from getting sick. They are also giving money and help to people who need it, like food, housing, and cheaper health insurance. The president also said that they are sending vaccines to many other countries to help people all around the world stay healthy.

API 参考

有关详细信息,您可以查看 Chat 文档。