This guide has an independently written Vietnamese edition. Đọc bằng Tiếng Việt
Create embeddings
Call POST /v1/embeddings with a concrete model name and a non-empty string or batch. The model must be supported by the gateway and available to the authenticated application. The default strict idempotency policy also applies to Embeddings POST requests; reuse one Idempotency-Key for one logical request and its retries.
import os
from uuid import uuid4
from openai import OpenAI
client = OpenAI(
api_key=os.environ["TASKWAY_API_KEY"],
base_url=os.environ["TASKWAY_BASE_URL"],
)
result = client.embeddings.create(
model="text-embedding-3-small",
input=["AI gateway", "Model routing"],
extra_headers={"Idempotency-Key": str(uuid4())},
)
vector = result.data[0].embedding
print(len(vector))
import { randomUUID } from "node:crypto";
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.TASKWAY_API_KEY,
baseURL: process.env.TASKWAY_BASE_URL,
});
const result = await client.embeddings.create(
{ model: "text-embedding-3-small", input: ["AI gateway", "Model routing"] },
{ headers: { "Idempotency-Key": randomUUID() } },
);
console.log(result.data[0].embedding.length);
Models and limits
The source profile recognizes text-embedding-3-small (native 1536 dimensions) and text-embedding-3-large (native 3072 dimensions). Your application must also have access to a configured eligible model/provider. auto is not an embedding model.
- Up to 128 input items per request.
- Up to 8,192 tokens in each input and 300,000 tokens in total.
- Optional
dimensionsmust be between 1 and the selected model’s native dimension. - The default request-body cap is 8 MiB; the embedding response cap is 16 MiB.
Service operators can configure limits such as request size and provider availability. If your deployment is configured more strictly, its limit applies.
Response and encoding
The response has a data array containing each vector and its input index, plus token usage. Specify encoding_format="float" for numeric arrays. The gateway also accepts encoding_format="base64"; this response is a wire-format string, and strict Python SDK typing does not fully describe that case. Float encoding is the simplest interoperable choice.
Do not send an empty string, empty batch, heterogeneous text/token-ID list, or token IDs outside the tokenizer’s vocabulary.