Body Request
Timeout permintaan sinkron: endpoint non-chat ini menunggu model yang dirutekan selesai. Input besar, audio panjang, atau batch besar dapat melebihi default client umum 30s, jadi atur timeout HTTP client Anda minimal 120s.
ID model embedding yang akan digunakan (mis. text-embedding-3-small).
Teks input yang akan di-embed. Dapat berupa string atau array string.
Format untuk embedding: float atau base64.
Jumlah dimensi untuk output (spesifik model).
Pengidentifikasi unik yang merepresentasikan end-user Anda untuk pemantauan penyalahgunaan.
Model yang Tersedia
| Model | Dimensi | Deskripsi |
|---|
text-embedding-3-large | 3072 | Kualitas terbaik |
text-embedding-3-small | 1536 | Seimbang |
text-embedding-ada-002 | 1536 | Legacy |
Respons
Array objek embedding.Setiap objek berisi:
object (string): embedding
index (integer): Indeks dalam array input
embedding (array): Vektor embedding
Penggunaan token dengan prompt_tokens dan total_tokens.
curl -X POST "https://api.tokenlab.sh/v1/embeddings" \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "text-embedding-3-small",
"input": "The quick brown fox jumps over the lazy dog"
}'
from openai import OpenAI
client = OpenAI(
api_key="sk-your-api-key",
base_url="https://api.tokenlab.sh/v1"
)
response = client.embeddings.create(
model="text-embedding-3-small",
input="The quick brown fox jumps over the lazy dog"
)
embedding = response.data[0].embedding
print(f"Embedding dimension: {len(embedding)}")
print(f"First 5 values: {embedding[:5]}")
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'sk-your-api-key',
baseURL: 'https://api.tokenlab.sh/v1'
});
const response = await client.embeddings.create({
model: 'text-embedding-3-small',
input: 'The quick brown fox jumps over the lazy dog'
});
console.log(response.data[0].embedding.slice(0, 5));
<?php
$ch = curl_init('https://api.tokenlab.sh/v1/embeddings');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Bearer sk-your-api-key'
],
CURLOPT_POSTFIELDS => json_encode([
'model' => 'text-embedding-3-small',
'input' => 'The quick brown fox jumps over the lazy dog'
])
]);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
print_r(array_slice($data['data'][0]['embedding'], 0, 5));
{
"object": "list",
"data": [
{
"object": "embedding",
"index": 0,
"embedding": [0.0023, -0.0194, 0.0081, ...]
}
],
"model": "text-embedding-3-small",
"usage": {
"prompt_tokens": 9,
"total_tokens": 9
}
}
Embedding Batch
# Embed multiple texts at once
response = client.embeddings.create(
model="text-embedding-3-small",
input=[
"First document text",
"Second document text",
"Third document text"
]
)
for i, data in enumerate(response.data):
print(f"Document {i}: {len(data.embedding)} dimensions")