跳到主要內容
Open In ColabOpen on GitHub

Vectara Chat

Vectara 是值得信賴的 AI 助理和代理平台,專注於企業關鍵任務應用程式的準備度。

Vectara 無伺服器 RAG 即服務提供 RAG 的所有組件,並透過易於使用的 API 提供,包括

  1. 一種從檔案(PDF、PPT、DOCX 等)中提取文字的方法
  2. 基於 ML 的分塊,可提供最先進的效能。
  3. Boomerang 嵌入模型。
  4. 其自身的內部向量資料庫,用於儲存文字塊和嵌入向量。
  5. 一種查詢服務,可自動將查詢編碼為嵌入,並檢索最相關的文字片段(包括支援混合搜尋以及多種重新排序選項,例如多語言相關性重新排序器MMRUDF 重新排序器
  6. 一個 LLM,用於根據檢索到的文件(上下文)建立生成式摘要,包括引文。

請參閱Vectara API 文件,以取得有關如何使用 API 的更多資訊。

本筆記本展示如何使用 Vectara 的 Chat 功能,該功能提供對話歷史記錄的自動儲存,並確保後續問題會考慮該歷史記錄。

開始使用

若要開始使用,請依照下列步驟操作

  1. 如果您還沒有帳戶,請註冊免費的 Vectara 試用版。完成註冊後,您將擁有一個 Vectara 客戶 ID。您可以點擊 Vectara 控制台視窗右上角的名字找到您的客戶 ID。
  2. 在您的帳戶中,您可以建立一個或多個語料庫。每個語料庫代表一個區域,用於儲存從輸入文件中擷取的文字資料。若要建立語料庫,請使用「建立語料庫」按鈕。然後,您需要為您的語料庫提供名稱和描述。您可以選擇性地定義篩選屬性並應用一些進階選項。如果您點擊您建立的語料庫,您可以在頂部看到其名稱和語料庫 ID。
  3. 接下來,您需要建立 API 金鑰才能存取語料庫。點擊語料庫檢視中的「存取控制」標籤,然後點擊「建立 API 金鑰」按鈕。為您的金鑰命名,並選擇您想要查詢專用或查詢+索引的金鑰。點擊「建立」,您現在就有了一個有效的 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.

此頁面是否對您有幫助?