跳到主要内容

WatsonxRerank

WatsonxRerank 是 IBM watsonx.ai 基础模型的封装器。

此笔记本展示如何在检索器中使用 watsonx 的 rerank 端点。 这建立在 ContextualCompressionRetriever 中的想法之上。

概述

集成详细信息

JS 支持包下载包最新
WatsonxReranklangchain-ibmPyPI - DownloadsPyPI - Version

设置

要访问 IBM watsonx.ai 模型,您需要创建一个 IBM watsonx.ai 帐户,获取 API 密钥,并安装 langchain-ibm 集成包。

凭据

下面的单元格定义了使用 watsonx 基础模型推理所需的凭据。

操作: 提供 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 和达拉斯 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="ibm/slate-125m-english-rtrvr",
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 参考


此页是否对您有帮助?