llama-index embeddings IBM watsonx.ai integration
Project description
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:
- Obtain an API Key: For more details on how to create and manage an API key, refer to Managing user API keys.
- 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:
- To provide context for the API call, you must pass the
project_id
orspace_id
. To get your project or space ID, open your project or space, go to the Manage tab, and click General. For more information see: Project documentation or Deployment space documentation. - Depending on the region of your provisioned service instance, use one of the URLs listed in watsonx.ai API Authentication.
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",
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",
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",
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])
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file llama_index_embeddings_ibm-0.2.0.tar.gz
.
File metadata
- Download URL: llama_index_embeddings_ibm-0.2.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.13 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 19f7625183ac1897ff8eed2507a96ef8ab91fae42d577582fce20b44a3e2c3ac |
|
MD5 | bc9d09acadfd9d675c02f8139d9f2723 |
|
BLAKE2b-256 | d40c9d13f7a33460583fa046d4eea4fc669f32a066eddfa4d43532def24e69cc |
Provenance
File details
Details for the file llama_index_embeddings_ibm-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: llama_index_embeddings_ibm-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.13 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79acd879dfa076e7bb0cde5931fd032e28c0ccecbe84a00cdb0548bd51374ede |
|
MD5 | 3591503439f251bfff9a774bdd11a995 |
|
BLAKE2b-256 | 7aac681f0443d009a6664680b64a248a33eb62b271e6f94da80ed285be19297d |