Embeddings
POST /v1/embeddings: turn text into vectors for semantic search, clustering, deduplication and retrieval-augmented generation (RAG).
The Embeddings endpoint turns text into a list of floating-point numbers (a vector). Texts with similar meanings get similar vectors, which is useful for semantic search, clustering, deduplication and retrieval-augmented generation (RAG). The embedding models available now are text-embedding-3-small and text-embedding-3-large (in the OpenAI-官转 and OpenAI-AZ groups) and text-embedding-ada-002 (in the OpenAI-AZ group only). Your key needs a group that has these models, and Model Square is the source of truth.
POST https://codaapi.com/v1/embeddings
Request headers
| Name | Required | Description |
|---|---|---|
Authorization |
Yes | Bearer sk-…, your API key |
Content-Type |
Yes | application/json |
Request body
| Parameter | Type | Required | Description |
|---|---|---|---|
model |
string | Yes | Model name, such as text-embedding-3-large |
input |
string or array | Yes | The text to convert. To convert several at once, pass an array of strings; data in the response is in the same order. See Batch embeddings |
dimensions |
integer | No | Number of dimensions of the output vectors; only the text-embedding-3 models support it. Without it, the model's default is used: 1536 for text-embedding-3-small and 3072 for text-embedding-3-large |
encoding_format |
string | No | float (default, an array of floats) or base64 |
user |
string | No | An identifier for your end user, passed to the upstream as is |
The gateway passes these fields to the upstream as is and returns the upstream's result to you unchanged. The upstream sets the length limit for each input and how many inputs one request can carry.
Within a project, use the same model (and the same dimensions) for storing vectors and for querying them; vectors from different models can't be compared with each other.
Request examples
curl
curl https://codaapi.com/v1/embeddings \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "text-embedding-3-large",
"input": "The food was delicious and the waiter was friendly.",
"encoding_format": "float"
}'
Python (pip install openai)
from openai import OpenAI
client = OpenAI() # reads OPENAI_BASE_URL and OPENAI_API_KEY
response = client.embeddings.create(
model="text-embedding-3-large",
input="The food was delicious and the waiter was friendly.",
encoding_format="float",
)
print(len(response.data[0].embedding)) # 3072
Node.js (npm install openai)
import OpenAI from 'openai';
const client = new OpenAI(); // reads OPENAI_BASE_URL and OPENAI_API_KEY
const response = await client.embeddings.create({
model: 'text-embedding-3-large',
input: 'The food was delicious and the waiter was friendly.',
encoding_format: 'float',
});
console.log(response.data[0].embedding.length); // 3072
Batch embeddings
To convert several texts at once, pass an array of strings as input. The returned data has one item per input, and index is its position in input, so you can match each vector to its text by index.
curl https://codaapi.com/v1/embeddings \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "text-embedding-3-large",
"input": ["The food was delicious", "The waiter was friendly"],
"encoding_format": "float"
}'
texts = ["The food was delicious", "The waiter was friendly"]
response = client.embeddings.create(model="text-embedding-3-large", input=texts, encoding_format="float")
for item in response.data:
print(texts[item.index], len(item.embedding))
When you have many texts, send them in several batches, keeping each batch within the upstream's limits on the number and length of inputs.
Response
{
"object": "list",
"data": [
{ "object": "embedding", "index": 0, "embedding": [0.0023, -0.0094, 0.0157] }
],
"model": "text-embedding-3-large",
"usage": { "prompt_tokens": 11, "total_tokens": 11 }
}
The vector in the example shows only 3 numbers; the real length equals the number of dimensions (3072 by default for text-embedding-3-large). In a batch request, data has one item for each input.
| Field | Description |
|---|---|
data[].index |
Which item of input this is, starting from 0 |
data[].embedding |
The vector; a base64 string when encoding_format is base64 |
usage.prompt_tokens |
Input tokens |
Errors
Errors come back with a 4xx or 5xx status code and an OpenAI-style error body, {"error": {"message": "…", "type": "…", "code": "…"}}, where message ends with the ID of the request (the request id). For common status codes and what to do about them, see Error codes.
Billing
Billing is by input tokens only; there are no output tokens. For each model's price, see Model Square and the price list; for the rules, see Pricing and multipliers.