IBM watsonx.ai
WatsonxEmbeddings 是 IBM watsonx.ai 基础模型的封装。
该示例展示了如何使用 LangChain 与 watsonx.ai 模型进行通信。
概览
集成细节
| 提供者 | 包 |
|---|---|
| IBM | langchain-ibm |
设置
要访问IBM watsonx.ai模型,您需要创建一个IBM watsonx.ai账户、获取API密钥,并安装langchain-ibm集成包。
Credentials
此单元格定义了使用 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"
安装
The LangChain IBM集成位于langchain-ibm包中:
!pip install -qU langchain-ibm
Instantiation
您可能需要为不同的模型调整模型 parameters。
from ibm_watsonx_ai.metanames import EmbedTextParamsMetaNames
embed_params = {
EmbedTextParamsMetaNames.TRUNCATE_INPUT_TOKENS: 3,
EmbedTextParamsMetaNames.RETURN_OPTIONS: {"input_text": True},
}
使用先前设置的参数初始化 WatsonxEmbeddings 类。
注意:
在本示例中,我们将使用project_id和达拉斯的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 操作的文本生成嵌入。
您可以直接调用这些方法,以获取适用于您自身用例的嵌入(embeddings)。
嵌入单个文本
您可以使用 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参考。