Embeddings
Create an embedding
Creates an embedding from the supplied request.
Текст запроса
The request body.
input
string | string[] | OpenAI.integer[] | OpenAI.integer[][]
обязательно
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, and any array must be 2048 dimensions or less. Example Python code for counting tokens. In addition to the per-input token limit, all embedding models enforce a maximum of 300,000 tokens summed across all inputs in a single request.
model
string | enum
обязательно
ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them.
enum
encoding_format
enum
The format to return the embeddings in. Can be either float or base64.
user
string
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.
Ответы
The request has succeeded.
Текст
data
OpenAI.Embedding[]
обязательно
The list of embeddings generated by the model.
model
string
обязательно
The name of the model used to generate the embedding.
object
enum
обязательно
The object type, which is always "list".
usage
OpenAI.CreateEmbeddingResponseUsage
обязательно
Запрос
curl https://api.openai.com/v1/embeddings \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"input": "The food was delicious and the waiter...",
"model": "text-embedding-ada-002",
"encoding_format": "float"
}'
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"), # This is the default and can be omitted
)
create_embedding_response = client.embeddings.create(
input="The quick brown fox jumped over the lazy dog",
model="text-embedding-3-small",
)
print(create_embedding_response.data)
import OpenAI from "openai";
const openai = new OpenAI();
async function main() {
const embedding = await openai.embeddings.create({
model: "text-embedding-ada-002",
input: "The quick brown fox jumped over the lazy dog",
encoding_format: "float",
});
console.log(embedding);
}
main();
using System;
using OpenAI.Embeddings;
EmbeddingClient client = new(
model: "text-embedding-3-small",
apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY")
);
OpenAIEmbedding embedding = client.GenerateEmbedding(input: "The quick brown fox jumped over the lazy dog");
ReadOnlyMemory<float> vector = embedding.ToFloats();
for (int i = 0; i < vector.Length; i++)
{
Console.WriteLine($" [{i,4}] = {vector.Span[i]}");
}
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
});
const createEmbeddingResponse = await client.embeddings.create({
input: 'The quick brown fox jumped over the lazy dog',
model: 'text-embedding-3-small',
});
console.log(createEmbeddingResponse.data);
package main
import (
"context"
"fmt"
"github.com/openai/openai-go"
"github.com/openai/openai-go/option"
)
func main() {
client := openai.NewClient(
option.WithAPIKey("My API Key"),
)
createEmbeddingResponse, err := client.Embeddings.New(context.TODO(), openai.EmbeddingNewParams{
Input: openai.EmbeddingNewParamsInputUnion{
OfString: openai.String("The quick brown fox jumped over the lazy dog"),
},
Model: openai.EmbeddingModelTextEmbedding3Small,
})
if err != nil {
panic(err.Error())
}
fmt.Printf("%+v
", createEmbeddingResponse.Data)
}
package com.openai.example;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.embeddings.CreateEmbeddingResponse;
import com.openai.models.embeddings.EmbeddingCreateParams;
import com.openai.models.embeddings.EmbeddingModel;
public final class Main {
private Main() {}
public static void main(String[] args) {
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
EmbeddingCreateParams params = EmbeddingCreateParams.builder()
.input("The quick brown fox jumped over the lazy dog")
.model(EmbeddingModel.TEXT_EMBEDDING_3_SMALL)
.build();
CreateEmbeddingResponse createEmbeddingResponse = client.embeddings().create(params);
}
}
require "openai"
openai = OpenAI::Client.new(api_key: "My API Key")
create_embedding_response = openai.embeddings.create(
input: "The quick brown fox jumped over the lazy dog",
model: :"text-embedding-3-small"
)
puts(create_embedding_response)
Response
{
"object": "list",
"data": [
{
"object": "embedding",
"embedding": [
0.0023064255,
-0.009327292,
.... (1536 floats total for ada-002)
-0.0028842222,
],
"index": 0
}
],
"model": "text-embedding-ada-002",
"usage": {
"prompt_tokens": 8,
"total_tokens": 8
}
}