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

IBM watsonx.ai

WatsonxEmbeddings 是 IBM watsonx.ai 基础模型的包装器。

此示例说明如何与watsonx.ai使用LangChain.

概述

集成详细信息

提供商
IBMlangchain-ibm

设置

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

凭据

此单元格定义使用 watsonx 嵌入所需的 WML 凭证。

行动:提供 IBM Cloud 用户 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

实例

您可能需要调整模型parameters适用于不同的型号。

from ibm_watsonx_ai.metanames import EmbedTextParamsMetaNames

embed_params = {
EmbedTextParamsMetaNames.TRUNCATE_INPUT_TOKENS: 3,
EmbedTextParamsMetaNames.RETURN_OPTIONS: {"input_text": True},
}

初始化WatsonxEmbeddings类。

注意

  • 要为 API 调用提供上下文,您必须添加project_idspace_id.有关更多信息,请参阅文档
  • 根据预置服务实例的区域,请使用此处描述的 URL 之一。

在此示例中,我们将使用project_id和 Dallas url.

您需要指定model_id这将用于推理。

from langchain_ibm import WatsonxEmbeddings

watsonx_embedding = WatsonxEmbeddings(
model_id="ibm/slate-125m-english-rtrvr",
url="https://us-south.ml.cloud.ibm.com",
project_id="PASTE YOUR PROJECT_ID HERE",
params=embed_params,
)
API 参考:WatsonxEmbeddings

或者,您可以使用 Cloud Pak for Data 凭证。有关详细信息,请参阅文档

watsonx_embedding = WatsonxEmbeddings(
model_id="ibm/slate-125m-english-rtrvr",
url="PASTE YOUR URL HERE",
username="PASTE YOUR USERNAME HERE",
password="PASTE YOUR PASSWORD HERE",
instance_id="openshift",
version="4.8",
project_id="PASTE YOUR PROJECT_ID HERE",
params=embed_params,
)

对于某些要求,可以选择将 IBM 的APIClient对象复制到WatsonxEmbeddings类。

from ibm_watsonx_ai import APIClient

api_client = APIClient(...)

watsonx_embedding = WatsonxEmbeddings(
model_id="ibm/slate-125m-english-rtrvr",
watsonx_client=api_client,
)

索引和检索

嵌入模型通常用于检索增强生成 (RAG) 流程中,既可以作为索引数据的一部分,也可以作为以后检索数据的一部分。有关更详细的说明,请参阅我们的 RAG 教程

下面,了解如何使用embeddings对象。在此示例中,我们将在InMemoryVectorStore.

# Create a vector store with a sample text
from langchain_core.vectorstores import InMemoryVectorStore

text = "LangChain is the framework for building context-aware reasoning applications"

vectorstore = InMemoryVectorStore.from_texts(
[text],
embedding=watsonx_embedding,
)

# Use the vectorstore as a retriever
retriever = vectorstore.as_retriever()

# Retrieve the most similar text
retrieved_documents = retriever.invoke("What is LangChain?")

# show the retrieved document's content
retrieved_documents[0].page_content
API 参考:InMemoryVectorStore
'LangChain is the framework for building context-aware reasoning applications'

直接使用

在后台,vectorstore 和 retriever 实现正在调用embeddings.embed_documents(...)embeddings.embed_query(...)为 中使用的文本创建嵌入from_texts和检索invoke作。

您可以直接调用这些方法来获取您自己的使用案例的嵌入。

嵌入单个文本

您可以嵌入单个文本或文档embed_query:

text = "This is a test document."

query_result = watsonx_embedding.embed_query(text)
query_result[:5]
[0.009447193, -0.024981951, -0.026013248, -0.040483937, -0.05780445]

嵌入多个文本

您可以使用embed_documents:

texts = ["This is a content of the document", "This is another document"]

doc_result = watsonx_embedding.embed_documents(texts)
doc_result[0][:5]
[0.009447167, -0.024981938, -0.02601326, -0.04048393, -0.05780444]

API 参考

有关所有WatsonxEmbeddings功能和配置可参考 API 参考