Search code examples
node.jsopenai-apiazure-openaitext-embedding-ada-002

OpenAI Embeddings API: How to change the embedding output dimension?


In the official OpenAI node library Create embeddings if for example using the model text-embedding-ada-002 the embeddings returned is an array of around 1536.

import {Configuration, OpenAIApi} from 'openai'

openai = new OpenAIApi(this.configuration)
const parameters= {
      model: 'text-embedding-ada-002',
      input: text,
    }
// Make the embedding request and return the result
const resp = await openai.createEmbedding(parameters)
const embeddings = embedding?.data.data[0].embedding

I would like to be able to limit the length of the list of embeddings returned.


Solution

  • You need to use the dimensions parameter with the OpenAI Embeddings API.

    As stated in the official OpenAI documentation:

    By default, the length of the embedding vector will be 1536 for text-embedding-3-small or 3072 for text-embedding-3-large. You can reduce the dimensions of the embedding by passing in the dimensions parameter without the embedding losing its concept-representing properties. We go into more detail on embedding dimensions in the embedding use case section.

    Screenshot