Skip to main content
POST
/
v1
/
embeddings
Create Embeddings
curl --request POST \
  --url https://api.example.com/v1/embeddings \
  --header 'Content-Type: application/json' \
  --data '
{
  "input": [
    "<string>"
  ],
  "model": "<string>",
  "dimensions": 123,
  "encoding_format": {},
  "user": "<string>"
}
'
{
  "data": [
    {
      "embedding": {},
      "index": 123,
      "object": "<string>"
    }
  ],
  "model": "<string>",
  "usage": {
    "prompt_tokens": 123,
    "total_tokens": 123
  },
  "object": "<string>"
}

Method

client.embeddings.create(params)
Create embeddings for input text using the specified model. Embeddings are numerical representations of text that can be used for semantic search, clustering, recommendations, and other machine learning tasks.

Parameters

input
string | string[] | number[] | number[][]
required
Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a single request, pass an array of strings or array of token arrays.
  • The input must not exceed the max input tokens for the model (8192 tokens for all embedding models)
  • Cannot be an empty string
  • Any array must be 2048 dimensions or less
  • Maximum of 300,000 tokens summed across all inputs in a single request
model
string
required
ID of the model to use. Common values:
  • text-embedding-ada-002
  • text-embedding-3-small
  • text-embedding-3-large
You can also pass any custom model string supported by your provider.
dimensions
number
The number of dimensions the resulting output embeddings should have. Only supported in text-embedding-3 and later models.
encoding_format
'float' | 'base64'
The format to return the embeddings in. Can be either float or base64.Default: float
user
string
A unique identifier representing your end-user, which can help monitor and detect abuse.

Response

data
array
required
List of embedding objects.
model
string
required
The model used for generating embeddings.
usage
object
required
Usage statistics for the request.
object
string
The object type, always "list".

Example

Single text input

const response = await client.embeddings.create({
  model: "text-embedding-3-small",
  input: "The quick brown fox jumps over the lazy dog"
});

console.log(response.data[0].embedding);
// [0.0023, -0.0091, 0.0234, ...]

Multiple text inputs

const response = await client.embeddings.create({
  model: "text-embedding-3-small",
  input: [
    "First document to embed",
    "Second document to embed",
    "Third document to embed"
  ]
});

console.log(response.data.length); // 3
console.log(response.usage.total_tokens);

With custom dimensions

const response = await client.embeddings.create({
  model: "text-embedding-3-large",
  input: "Reduce dimensionality for storage",
  dimensions: 256
});

console.log(response.data[0].embedding.length); // 256

Base64 encoding

const response = await client.embeddings.create({
  model: "text-embedding-3-small",
  input: "Get embeddings as base64",
  encoding_format: "base64"
});

console.log(typeof response.data[0].embedding); // "string"

Response Example

{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "index": 0,
      "embedding": [
        0.0023064255,
        -0.009327292,
        0.015797347,
        -0.0077559738,
        -0.004068857,
        // ... (1536 dimensions for text-embedding-3-small)
      ]
    }
  ],
  "model": "text-embedding-3-small",
  "usage": {
    "prompt_tokens": 8,
    "total_tokens": 8
  }
}

Build docs developers (and LLMs) love