题意:OpenAI Embeddings API:如何更改嵌入输出维度?
问题背景:
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
.
在官方的 OpenAI Node.js 库中,如果使用 text-embedding-ada-002
模型创建嵌入,返回的嵌入是一个长度大约为 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.
我希望能够限制返回的嵌入列表的长度。
问题解决:
You need to use the dimensions
parameter with the OpenAI Embeddings API.
你需要在 OpenAI Embeddings API 中使用 dimensions
参数。
As stated in the official OpenAI documentation:
正如官方 OpenAI 文档中所述:
By default, the length of the embedding vector will be
1536
fortext-embedding-3-small
or3072
fortext-embedding-3-large
. You can reduce the dimensions of the embedding by passing in thedimensions
parameter without the embedding losing its concept-representing properties. We go into more detail on embedding dimensions in the embedding use case section.