Skip to main content

LlamaIndex Embeddings Integration: IBM

This package integrates the LlamaIndex LLMs API with the IBM watsonx.ai Foundation Models API by leveraging ibm-watsonx-ai SDK. With this integration, you can use one of the embedding models that are available in IBM watsonx.ai to embed a single string or a list of strings.

Installation

pip install llama-index-embeddings-ibm

Usage

Setting up

To use IBM's models, you must have an IBM Cloud user API key. Here's how to obtain and set up your API key:

  1. Obtain an API Key: For more details on how to create and manage an API key, refer to Managing user API keys.
  2. Set the API Key as an Environment Variable: For security reasons, it's recommended to not hard-code your API key directly in your scripts. Instead, set it up as an environment variable. You can use the following code to prompt for the API key and set it as an environment variable:
import os
from getpass import getpass

watsonx_api_key = getpass()
os.environ["WATSONX_APIKEY"] = watsonx_api_key

Alternatively, you can set the environment variable in your terminal.

  • Linux/macOS: Open your terminal and execute the following command:

    export WATSONX_APIKEY='your_ibm_api_key'
    

    To make this environment variable persistent across terminal sessions, add the above line to your ~/.bashrc, ~/.bash_profile, or ~/.zshrc file.

  • Windows: For Command Prompt, use:

    set WATSONX_APIKEY=your_ibm_api_key
    

Load the model

You might need to adjust embedding parameters for different tasks.

truncate_input_tokens = 3

Initialize the WatsonxEmbeddings class with the previously set parameters.

Note:

In this example, we’ll use the project_id and Dallas URL.

You need to specify the model_id that will be used for inferencing.

from llama_index.embeddings.ibm import WatsonxEmbeddings

watsonx_embedding = WatsonxEmbeddings(
    model_id="ibm/slate-125m-english-rtrvr-v2",
    url="https://us-south.ml.cloud.ibm.com",
    project_id="PASTE_YOUR_PROJECT_ID_HERE",
    truncate_input_tokens=truncate_input_tokens,
)

Alternatively, you can use Cloud Pak for Data credentials. For details, see watsonx.ai software setup.

watsonx_embedding = WatsonxEmbeddings(
    model_id="ibm/slate-125m-english-rtrvr-v2",
    url="PASTE YOUR URL HERE",
    username="PASTE_YOUR_USERNAME_HERE",
    password="PASTE_YOUR_PASSWORD_HERE",
    instance_id="openshift",
    version="5.2",
    project_id="PASTE YOUR PROJECT_ID HERE",
    truncate_input_tokens=truncate_input_tokens,
)

Usage

Embed query

query = "Example query."

query_result = watsonx_embedding.get_query_embedding(query)
print(query_result[:5])

Embed list of texts

texts = ["This is a content of one document", "This is another document"]

doc_result = watsonx_embedding.get_text_embedding_batch(texts)
print(doc_result[0][:5])

Release files for llama-index-embeddings-ibm 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llama-index-embeddings-ibm 0.7.0
File Size Uploaded
llama_index_embeddings_ibm-0.7.0.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for llama-index-embeddings-ibm 0.7.0
File Interpreter ABI Platform
llama_index_embeddings_ibm-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.0 kB

Release files / llama_index_embeddings_ibm-0.7.0.tar.gz

Download URL llama_index_embeddings_ibm-0.7.0.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
98763be8d5c814740c78fe961f47544f59e9ed6571a51e594b38697ce74d47e2
BLAKE2b-256 checksum
How to use checksums
5a38330e1ee4bc75a7935b63c91574679b9251eb623bfb8b6470f22f29267cd8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / llama_index_embeddings_ibm-0.7.0-py3-none-any.whl

Download URL llama_index_embeddings_ibm-0.7.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
267ea38faf61abe80393a7d59d839d88777e4797558474287f483c338216a0a0
BLAKE2b-256 checksum
How to use checksums
c397506502725c76b4600df93962aa4a4d84fc49cbe493a3b5f0f103bc8b13e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page