Quick code tutorial: Create text embeddings

You can convert text to embeddings programmatically with the watsonx.ai API and SDKs.

This tutorial demonstrates how to use an embedding model and the embeddings API to create text embeddings that capture the meaning of sentences or passages for use in your generative AI applications. Converting text into text embeddings, or vectorizing text, helps with document comparison, question-answering, and in retrieval-augmented generation (RAG) tasks, where you need to retrieve relevant content quickly. Work with Python, Node.js, or Curl.

alt="" You can complete this tutorial in less than 5 minutes.

Detailed information
Text embeddings overview
Embeddings models

Prerequisite

Complete the one-time steps to gather your credentials and other information and prepare your system. See Prerequisite one-time tasks.

Create text embeddings

Select code example language

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:

  1. 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>"
    }
    
  2. 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
}

Next steps

  • Try your request against another foundation model by changing the value of the model_id parameter. See Embeddings models.
  • Try another quick start coding tutorial.

Learn more