跳到主要內容
Open In ColabOpen on GitHub

WatsonxRerank

WatsonxRerank 是 IBM watsonx.ai 基礎模型的包裝器。

本筆記本示範如何在檢索器中使用 watsonx 的 rerank 端點。這是建立在 ContextualCompressionRetriever 中的概念之上。

概觀

整合詳細資訊

類別套件JS 支援套件下載套件最新版
WatsonxReranklangchain-ibmPyPI - DownloadsPyPI - Version

設定

若要存取 IBM watsonx.ai 模型,您需要建立 IBM watsonx.ai 帳戶、取得 API 金鑰,並安裝 langchain-ibm 整合套件。

憑證

以下儲存格定義了使用 watsonx Foundation Model 推論所需的憑證。

操作: 提供 IBM Cloud 使用者 API 金鑰。如需詳細資訊,請參閱管理使用者 API 金鑰

import os
from getpass import getpass

watsonx_api_key = getpass()
os.environ["WATSONX_APIKEY"] = watsonx_api_key

此外,您可以將其他密碼作為環境變數傳遞。

import os

os.environ["WATSONX_URL"] = "your service instance url"
os.environ["WATSONX_TOKEN"] = "your token for accessing the CPD cluster"
os.environ["WATSONX_PASSWORD"] = "your password for accessing the CPD cluster"
os.environ["WATSONX_USERNAME"] = "your username for accessing the CPD cluster"
os.environ["WATSONX_INSTANCE_ID"] = "your instance_id for accessing the CPD cluster"

安裝

LangChain IBM 整合位於 langchain-ibm 套件中

!pip install -qU langchain-ibm
!pip install -qU langchain-community
!pip install -qU langchain_text_splitters

為了實驗目的,請同時安裝 faissfaiss-cpu 套件

!pip install --upgrade --quiet  faiss

# OR (depending on Python version)

!pip install --upgrade --quiet faiss-cpu

用於列印文件的輔助函數

def pretty_print_docs(docs):
print(
f"\n{'-' * 100}\n".join(
[f"Document {i+1}:\n\n" + d.page_content for i, d in enumerate(docs)]
)
)

例項化

設定基本向量儲存庫檢索器

讓我們先初始化一個簡單的向量儲存庫檢索器,並儲存 2023 年國情咨文演講(分塊)。我們可以設定檢索器來檢索大量 (20) 文件。

初始化 WatsonxEmbeddings。如需更多詳細資訊,請參閱WatsonxEmbeddings

注意:

  • 若要為 API 呼叫提供背景資訊,您必須新增 project_idspace_id。如需更多資訊,請參閱文件
  • 根據您佈建的服務實例的區域,使用 此處 描述的 URL 之一。

在本範例中,我們將使用 project_id 和 Dallas URL。

您需要指定將用於嵌入的 model_id。您可以在文件中找到所有可用的模型。

from langchain_ibm import WatsonxEmbeddings

wx_embeddings = WatsonxEmbeddings(
model_id="ibm/slate-125m-english-rtrvr",
url="https://us-south.ml.cloud.ibm.com",
project_id="PASTE YOUR PROJECT_ID HERE",
)
API 參考:WatsonxEmbeddings
from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import FAISS
from langchain_text_splitters import RecursiveCharacterTextSplitter

documents = TextLoader("../../how_to/state_of_the_union.txt").load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
texts = text_splitter.split_documents(documents)
retriever = FAISS.from_documents(texts, wx_embeddings).as_retriever(
search_kwargs={"k": 20}
)

query = "What did the president say about Ketanji Brown Jackson"
docs = retriever.invoke(query)
pretty_print_docs(docs[:5]) # Printing the first 5 documents
Document 1:

One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court.

And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.
----------------------------------------------------------------------------------------------------
Document 2:

I spoke with their families and told them that we are forever in debt for their sacrifice, and we will carry on their mission to restore the trust and safety every community deserves.

I’ve worked on these issues a long time.

I know what works: Investing in crime prevention and community police officers who’ll walk the beat, who’ll know the neighborhood, and who can restore trust and safety.

So let’s not abandon our streets. Or choose between safety and equal justice.
----------------------------------------------------------------------------------------------------
Document 3:

He met the Ukrainian people.

From President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.

Groups of citizens blocking tanks with their bodies. Everyone from students to retirees teachers turned soldiers defending their homeland.

In this struggle as President Zelenskyy said in his speech to the European Parliament “Light will win over darkness.” The Ukrainian Ambassador to the United States is here tonight.
----------------------------------------------------------------------------------------------------
Document 4:

As I said last year, especially to our younger transgender Americans, I will always have your back as your President, so you can be yourself and reach your God-given potential.

While it often appears that we never agree, that isn’t true. I signed 80 bipartisan bills into law last year. From preventing government shutdowns to protecting Asian-Americans from still-too-common hate crimes to reforming military justice.
----------------------------------------------------------------------------------------------------
Document 5:

To all Americans, I will be honest with you, as I’ve always promised. A Russian dictator, invading a foreign country, has costs around the world.

And I’m taking robust action to make sure the pain of our sanctions is targeted at Russia’s economy. And I will use every tool at our disposal to protect American businesses and consumers.

Tonight, I can announce that the United States has worked with 30 other countries to release 60 Million barrels of oil from reserves around the world.

用法

使用 WatsonxRerank 進行重新排序

現在,讓我們使用 ContextualCompressionRetriever 包裝我們的基本檢索器。我們將新增一個 WatsonxRerank,它使用 watsonx rerank 端點來重新排序傳回的結果。請注意,在 WatsonxRerank 中強制指定模型名稱!

from langchain_ibm import WatsonxRerank

wx_rerank = WatsonxRerank(
model_id="cross-encoder/ms-marco-minilm-l-12-v2",
url="https://us-south.ml.cloud.ibm.com",
project_id="PASTE YOUR PROJECT_ID HERE",
)
API 參考:WatsonxRerank
from langchain.retrievers.contextual_compression import ContextualCompressionRetriever

compression_retriever = ContextualCompressionRetriever(
base_compressor=wx_rerank, base_retriever=retriever
)

compressed_docs = compression_retriever.invoke(
"What did the president say about Ketanji Jackson Brown"
)
pretty_print_docs(compressed_docs[:5]) # Printing the first 5 compressed documents
Document 1:

One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court.

And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.
----------------------------------------------------------------------------------------------------
Document 2:

As I said last year, especially to our younger transgender Americans, I will always have your back as your President, so you can be yourself and reach your God-given potential.

While it often appears that we never agree, that isn’t true. I signed 80 bipartisan bills into law last year. From preventing government shutdowns to protecting Asian-Americans from still-too-common hate crimes to reforming military justice.
----------------------------------------------------------------------------------------------------
Document 3:

To all Americans, I will be honest with you, as I’ve always promised. A Russian dictator, invading a foreign country, has costs around the world.

And I’m taking robust action to make sure the pain of our sanctions is targeted at Russia’s economy. And I will use every tool at our disposal to protect American businesses and consumers.

Tonight, I can announce that the United States has worked with 30 other countries to release 60 Million barrels of oil from reserves around the world.
----------------------------------------------------------------------------------------------------
Document 4:

I spoke with their families and told them that we are forever in debt for their sacrifice, and we will carry on their mission to restore the trust and safety every community deserves.

I’ve worked on these issues a long time.

I know what works: Investing in crime prevention and community police officers who’ll walk the beat, who’ll know the neighborhood, and who can restore trust and safety.

So let’s not abandon our streets. Or choose between safety and equal justice.
----------------------------------------------------------------------------------------------------
Document 5:

He met the Ukrainian people.

From President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.

Groups of citizens blocking tanks with their bodies. Everyone from students to retirees teachers turned soldiers defending their homeland.

In this struggle as President Zelenskyy said in his speech to the European Parliament “Light will win over darkness.” The Ukrainian Ambassador to the United States is here tonight.

在鏈中使用

您當然可以在 QA 管道中使用此檢索器

初始化 ChatWatsonx。如需更多詳細資訊,請參閱ChatWatsonx

from langchain_ibm import ChatWatsonx

wx_chat = ChatWatsonx(
model_id="meta-llama/llama-3-1-70b-instruct",
url="https://us-south.ml.cloud.ibm.com",
project_id="PASTE YOUR PROJECT_ID HERE",
)
API 參考:ChatWatsonx
from langchain.chains import RetrievalQA

chain = RetrievalQA.from_chain_type(llm=wx_chat, retriever=compression_retriever)
API 參考:RetrievalQA
chain.invoke(query)
{'query': 'What did the president say about Ketanji Brown Jackson',
'result': 'The President said that he had nominated Circuit Court of Appeals Judge Ketanji Brown Jackson to serve on the United States Supreme Court, and described her as "one of our nation\'s top legal minds" who will continue Justice Breyer\'s legacy of excellence.'}

API 參考

如需所有 WatsonxRerank 功能和組態的詳細文件,請前往 API 參考


此頁面是否對您有幫助?