4

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

Okay thank you, for the clarification! so if I want a Limited numbers of embeddings I will have to use some other tokenizer?
Yes, you need to use any other Python library where you have an option to choose the embedding output dimension. This looks promising. Seems like you can set vector_size.
Is there a way to take the top n most relevant embeddings out of the 1536 in the output dimension?
@kumail You can't simply "take the top n most relevant embeddings out of the 1536". You don't get 1536 embeddings from the OpenAI Embeddings API. You get one(!) 1536-dimensional embedding. Anyway, what you can try to do is translate the embedding you get from the OpenAI Embeddings API to a lower-dimensional space. I edited my answer.
OpenAi now supports a dimension parameter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.