llama-index embeddings IBM watsonx.ai integration
Project description
LlamaIndex Embeddings Integration: IBM
This package provides the integration between LlamaIndex and IBM watsonx.ai through the ibm-watsonx-ai
SDK.
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 IBM's documentation.
- 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
In alternative, 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 previously set parameters.
Note:
- To provide context for the API call, you must add
project_id
orspace_id
. For more information see documentation. - Depending on the region of your provisioned service instance, use one of the urls described here.
In this example, we’ll use the project_id
and Dallas url.
You need to specify 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 documentation.
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="5.0",
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
Hashes for llama_index_embeddings_ibm-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a7ff3b8a37e5fc9e3d4c5464456f3e57f1aa269615ec52a7c2c883cbe00706f |
|
MD5 | fd6b015ae26ef19d87e647244c4ceed0 |
|
BLAKE2b-256 | 5ac955a6411e37b5ab244d9f6439b6244dfc8d02c17aea481a7c42ef6fdb958c |
Hashes for llama_index_embeddings_ibm-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40b61f84b2ce2817ccb29ed99e2f7bd9770081e5ee62cce4a25ee2766c8397f0 |
|
MD5 | 442a65d81a3b27714c313ba191e3c950 |
|
BLAKE2b-256 | 4eadeb45df4dae57ad7c1263568aa8228121cae5d0dde95cb23697f783125033 |