IBM watsonx.ai
WatsonxEmbeddings is a wrapper for IBM watsonx.ai foundation models.
此示例展示了如何使用 LangChain 与 watsonx.ai 个模型进行通信。
概览
集成详情
| 提供者 | 包 |
|---|---|
| IBM | langchain-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 类。
注意:
在此示例中,我们将使用 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,
)
或者您可以使用 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
'LangChain is the framework for building context-aware reasoning applications'
直接使用
在内部,向量存储和检索器的实现分别调用 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 参考。