跳到主要內容
Open In ColabOpen on GitHub

Vectara 自查詢

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

Vectara 無伺服器 RAG 即服務提供 RAG 的所有組件,背後是一個易於使用的 API,包括

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

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

此筆記本示範如何將 SelfQueryRetriever 與 Vectara 一起使用。

開始使用

若要開始使用,請按照以下步驟操作

  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
)

在此筆記本中,我們假設它們是在環境中提供的。

注意: 自查詢檢索器要求您安裝 lark (pip install lark)。

從 LangChain 連接到 Vectara

在此範例中,我們假設您已建立帳戶和語料庫,並將您的 VECTARA_CUSTOMER_IDVECTARA_CORPUS_IDVECTARA_API_KEY (使用索引和查詢的權限建立) 新增為環境變數。

我們進一步假設語料庫已定義 4 個欄位作為可篩選的中繼資料屬性:yeardirectorratinggenre

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'})]

此頁面是否對您有幫助?