Skip to main content
This will help you get started with IBM watsonx.ai embedding models using LangChain. For detailed documentation on IBM watsonx.ai features and configuration options, please refer to the API reference.

Overview

Integration details

Setup

To access IBM WatsonxAI embeddings you’ll need to create an IBM watsonx.ai account, get an API key or any other type of credentials, and install the @langchain/ibm integration package.

Credentials

Head to IBM Cloud to sign up to IBM watsonx.ai and generate an API key or provide any other authentication form as presented below.

IAM authentication

Bearer token authentication

IBM watsonx.ai software authentication

Once these are placed in your environment variables and object is initialized authentication will proceed automatically. Authentication can also be accomplished by passing these values as parameters to a new instance.

IAM authentication

Bearer token authentication

IBM watsonx.ai software authentication

If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

Installation

The LangChain IBM watsonx.ai integration lives in the @langchain/ibm package:

Instantiation

Now we can instantiate our model object and embed text:
Note:
  • You must provide spaceId or projectId in order to proceed.
  • Depending on the region of your provisioned service instance, use correct serviceUrl.

Model Gateway

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 our RAG tutorials under the Learn tab. Below, see how to index and retrieve data using the embeddings object we initialized above. In this example, we will index and retrieve a sample document using the demo MemoryVectorStore.

Direct usage

Under the hood, the vectorstore and retriever implementations are calling embeddings.embedDocument(...) and embeddings.embedQuery(...) to create embeddings for the text(s) used in fromDocuments and the retriever’s invoke operations, respectively. You can directly call these methods to get embeddings for your own use cases.

Embed single texts

You can embed queries for search with embedQuery. This generates a vector representation specific to the query:

Embed multiple texts

You can embed multiple texts for indexing with embedDocuments. The internals used for this method may (but do not have to) differ from embedding queries:

API reference

For detailed documentation of all module_name features and configurations head to the API reference: api_ref_module