To extract text from a document, add the following code to the example.py file. Replace {watsonx_ai_url}, {apikey}, and {project_id} with your values.
from ibm_watsonx_ai import Credentials
from ibm_watsonx_ai.foundation_models import Embeddings
import json
credentials = Credentials(
api_key = "{apikey}",
url = "{watsonx_ai_url}"
)
embedding = Embeddings(
model_id= "ibm/slate-125m-english-rtrvr",
credentials = credentials,
project_id = "{project_id}"
)
texts = [
"Youth craves thrills while adulthood cherishes wisdom.",
"Youth seeks ambition while adulthood finds contentment.",
"Dreams chased in youth while goals pursued in adulthood."
]
result = embedding.embed_documents( texts = texts )
print( json.dumps( result, indent=3 ) )
Sample output:
[
{
"embedding": [
-0.024644956,
0.063319616,
-0.020406976,
...
]
},
{
"embedding": [
0.0020158237,
0.025287563,
-0.016845388,
...
]
},
{
"embedding": [
-0.015106457,
0.026971959,
0.024798397,
...
]
}
]
To extract text from a document, add the following code to the example.js file. Replace {apikey} and {project_id} with your values.
const { IamAuthenticator } = require( "ibm-cloud-sdk-core" );
const { WatsonXAI } = require( "@ibm-cloud/watsonx-ai" );
const authenticator = new IamAuthenticator( {
"apikey" : "{apikey}"
} );
const inputs = [
"Youth craves thrills while adulthood cherishes wisdom.",
"Youth seeks ambition while adulthood finds contentment.",
"Dreams chased in youth while goals pursued in adulthood."
];
watsonxAI_service.embedText( {
"inputs" : inputs,
"modelId" : "ibm/slate-125m-english-rtrvr",
"projectId" : "{project_id}"
} ).then( ( response ) => {
console.log( JSON.stringify( response.results, null, 3 ) );
} );
Sample output:
[
{
"embedding": [
-0.024644956,
0.063319616,
-0.020406976,
...
]
},
{
"embedding": [
0.0020158237,
0.025287563,
-0.016845388,
...
]
},
{
"embedding": [
-0.015106457,
0.026971959,
0.024798397,
...
]
}
]
To extract text from a document:
-
Create a JSON file with the following content:
{
"inputs" : [
"Youth craves thrills while adulthood cherishes wisdom.",
"Youth seeks ambition while adulthood finds contentment.",
"Dreams chased in youth while goals pursued in adulthood."
],
"model_id" : "ibm/slate-125m-english-rtrvr",
"project_id" : "<your-project-id>"
}
-
Run the following curl request. Replace {token} and {JSON-file} with your values.
curl -X POST \
-H "Authorization: Bearer {token}" \
-H "Content-Type: application/json" \
<your-service-url>/ml/v1/text/embeddings?version=2024-05-31 \
--data "@{JSON-file}"
Sample output:
{
"model_id":"ibm/slate-125m-english-rtrvr",
"created_at":"2025-03-01T23:34:13.973Z",
"results":[
{
"embedding":[-0.024644956,0.063319616,-0.020406976, ... ]
},
{
"embedding":[0.0020158237,0.025287563,-0.016845388, ... ]
},
{
"embedding":[-0.015106457,0.026971959,0.024798397, ... ]
}
],
"input_token_count":39
}