IBM watsonx.ai
WatsonxEmbeddings is a wrapper for IBMwatsonx.ai foundation models.
This example shows how to communicate withwatsonx.ai
models usingLangChain
.
Overview
Integration details
Provider | Package |
---|---|
IBM | langchain-ibm |
Setup
To access IBM watsonx.ai models you'll need to create an IBM watsonx.ai account, get an API key, and install thelangchain-ibm
integration package.
Credentials
This cell defines the WML credentials required to work with watsonx Embeddings.
Action: Provide the IBM Cloud user API key. For details, seedocumentation.
import os
from getpassimport getpass
watsonx_api_key= getpass()
os.environ["WATSONX_APIKEY"]= watsonx_api_key
Additionaly you are able to pass additional secrets as an environment variable.
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
The LangChain IBM integration lives in thelangchain-ibm
package:
!pip install-qU langchain-ibm
Instantiation
You might need to adjust modelparameters
for different models.
from ibm_watsonx_ai.metanamesimport EmbedTextParamsMetaNames
embed_params={
EmbedTextParamsMetaNames.TRUNCATE_INPUT_TOKENS:3,
EmbedTextParamsMetaNames.RETURN_OPTIONS:{"input_text":True},
}
Initialize theWatsonxEmbeddings
class with previously set parameters.
Note:
- To provide context for the API call, you must add
project_id
orspace_id
. For more information seedocumentation. - Depending on the region of your provisioned service instance, use one of the urls describedhere.
In this example, we’ll use theproject_id
and Dallas url.
You need to specifymodel_id
that will be used for inferencing.
from langchain_ibmimport WatsonxEmbeddings
watsonx_embedding= WatsonxEmbeddings(
model_id="ibm/granite-embedding-107m-multilingual",
url="https://us-south.ml.cloud.ibm.com",
project_id="PASTE YOUR PROJECT_ID HERE",
params=embed_params,
)
Alternatively you can use Cloud Pak for Data credentials. For details, seedocumentation.
watsonx_embedding= WatsonxEmbeddings(
model_id="ibm/granite-embedding-107m-multilingual",
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,
)
For certain requirements, there is an option to pass the IBM'sAPIClient
object into theWatsonxEmbeddings
class.
from ibm_watsonx_aiimport APIClient
api_client= APIClient(...)
watsonx_embedding= WatsonxEmbeddings(
model_id="ibm/granite-embedding-107m-multilingual",
watsonx_client=api_client,
)
Indexing and Retrieval
Embedding models are often used in retrieval-augmented generation (RAG) flows, both as part of indexing data as well as later retrieving it. For more detailed instructions, please see ourRAG tutorials.
Below, see how to index and retrieve data using theembeddings
object we initialized above. In this example, we will index and retrieve a sample document in theInMemoryVectorStore
.
# Create a vector store with a sample text
from langchain_core.vectorstoresimport 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
Under the hood, the vectorstore and retriever implementations are callingembeddings.embed_documents(...)
andembeddings.embed_query(...)
to create embeddings for the text(s) used infrom_texts
and retrievalinvoke
operations, respectively.
You can directly call these methods to get embeddings for your own use cases.
Embed single texts
You can embed single texts or documents withembed_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
You can embed multiple texts withembed_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 Reference
For detailed documentation of allWatsonxEmbeddings
features and configurations head to theAPI reference.
Related
- Embedding modelconceptual guide
- Embedding modelhow-to guides