Embeddings
Turn text or images into vectors with an OpenAI-compatible embeddings endpoint.
The Embeddings API returns one vector for each input in one request. It is compatible with the OpenAI embeddings API. Use it with an embedding model. An embedding model serves only embeddings requests.
Endpoint
POST https://api.inference.net/v1/embeddingsSend your API key in the Authorization: Bearer header, as for other endpoints. The response is one JSON object. The endpoint does not stream.
To send embeddings requests asynchronously, use POST /v1/slow/embeddings. See Asynchronous Inference.
Request
| Field | Type | Required | Description |
|---|---|---|---|
model | string | Yes | The identifier of an embedding model. |
input | string or array | Yes | The input to embed: a string, an array of strings, a token-id array, or an array of token-id arrays. Image models also accept image messages (see below). |
encoding_format | string | No | float (the default) returns each vector as an array of numbers. base64 returns each vector as a base64 string of little-endian float32 values. |
dimensions | integer | No | The number of dimensions of each vector. Only models that support shortened vectors accept it. |
user | string | No | An identifier for your end user. |
An image input is an array that holds one user message with one image_url part. The URL is a base64 data URI (image/png, image/jpeg, image/gif or image/webp) or an https URL. To embed several images, send an array of these messages (at most 50).
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.inference.net/v1",
apiKey: process.env.INFERENCE_API_KEY,
});
const response = await client.embeddings.create({
model: "<embedding-model>",
input: ["The quick brown fox", "jumps over the lazy dog"],
});
console.log(response.data[0].embedding);An image input:
{
"model": "<image-embedding-model>",
"input": [
{
"role": "user",
"content": [{ "type": "image_url", "image_url": { "url": "https://example.com/cat.png" } }]
}
]
}Response
| Field | Type | Description |
|---|---|---|
object | string | Always list. |
data | array | One item for each input, in input order. Each item is {"object": "embedding", "index": <position>, "embedding": <vector>}. |
model | string | The model identifier you sent. |
usage.prompt_tokens | integer | The number of input tokens. |
usage.total_tokens | integer | The same as usage.prompt_tokens. |
{
"object": "list",
"data": [
{ "object": "embedding", "index": 0, "embedding": [0.0123, -0.0456, "..."] },
{ "object": "embedding", "index": 1, "embedding": [0.0789, 0.0012, "..."] }
],
"model": "<embedding-model>",
"usage": { "prompt_tokens": 10, "total_tokens": 10 }
}The x-inference-id response header contains the generation id.
Billing
You pay for usage.prompt_tokens as input tokens. There are no output tokens.
Errors
Errors use the same JSON shape as other endpoints: {"error": {"message": "...", "type": "..."}}.
| Status | Cause |
|---|---|
| 400 | The body is not valid: input or model is missing, input has the wrong shape, encoding_format is not float or base64, or dimensions is not a positive integer. error.fields names each bad field. |
| 400 | The model is not an embedding model. |
| 403 | Your team does not have access to the model. |
| 404 | The model does not exist. |