Endpoint
POST https://api.samuraiapi.in/v1/embeddings
Request Body
| Parameter | Type | Required | Description |
|---|
model | string | ✅ | Embedding model ID |
input | string / array | ✅ | Text or array of texts to embed |
encoding_format | string | — | "float" (default) or "base64" |
dimensions | integer | — | Output dimensions (model-dependent) |
Code Examples
from openai import OpenAI
client = OpenAI(
api_key="sk-samurai-YOUR_KEY",
base_url="https://api.samuraiapi.in/v1"
)
response = client.embeddings.create(
model="text-embedding-3-small",
input="The quick brown fox jumps over the lazy dog"
)
vector = response.data[0].embedding
print(f"Vector dimensions: {len(vector)}") # 1536
Batch Embeddings
texts = [
"Machine learning is fascinating",
"Natural language processing is powerful",
"Vector databases enable semantic search"
]
response = client.embeddings.create(
model="text-embedding-3-small",
input=texts
)
vectors = [item.embedding for item in response.data]
print(f"Got {len(vectors)} embeddings")
Available Models
| Model | Dimensions | Best For |
|---|
text-embedding-3-small | 1536 | Fast, cheap, great quality |
text-embedding-3-large | 3072 | Highest accuracy |
text-embedding-ada-002 | 1536 | Legacy compatibility |
Semantic Search Example
import numpy as np
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
# Embed your knowledge base
docs = ["Paris is the capital of France", "Berlin is in Germany", "Tokyo is in Japan"]
doc_embeddings = client.embeddings.create(model="text-embedding-3-small", input=docs).data
# Embed the query
query = "What is the capital of France?"
query_vec = client.embeddings.create(model="text-embedding-3-small", input=query).data[0].embedding
# Find most similar
scores = [cosine_similarity(query_vec, d.embedding) for d in doc_embeddings]
best = docs[np.argmax(scores)]
print(f"Most relevant: {best}")
# => "Paris is the capital of France"
For RAG (Retrieval-Augmented Generation), combine embeddings with a vector database like Pinecone, Weaviate, or pgvector.