IBM watsonx.ai
WatsonxEmbeddings 是 IBM watsonx.ai 基礎模型的封裝器。
此範例展示如何使用 LangChain
與 watsonx.ai
模型進行通訊。
概觀 (Overview)
整合細節 (Integration details)
供應商 (Provider) | 套件 (Package) |
---|---|
IBM | langchain-ibm |
設定 (Setup)
要存取 IBM watsonx.ai 模型,您需要建立一個 IBM watsonx.ai 帳戶、取得 API 金鑰,並安裝 langchain-ibm
整合套件。
憑證 (Credentials)
此儲存格定義了使用 watsonx Embeddings 所需的 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"
安裝 (Installation)
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
類別。
注意 (Note):
在此示例中,我們將使用 project_id
和 Dallas 網址。
您需要指定將用於推論的 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,
)
索引和檢索 (Indexing and Retrieval)
嵌入模型通常用於檢索增強生成 (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'
直接使用 (Direct Usage)
在底層,vectorstore 和檢索器實作會呼叫 embeddings.embed_documents(...)
和 embeddings.embed_query(...)
,以分別為 from_texts
和檢索 invoke
操作中使用的文本建立嵌入。
您可以直接呼叫這些方法來取得嵌入,以用於您自己的使用案例。
嵌入單個文本 (Embed single texts)
您可以使用 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 multiple texts)
您可以使用 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 參考 (API Reference)
如需所有 WatsonxEmbeddings
功能和設定的詳細文檔,請前往 API 參考。