Docs
Embeddings
Embeddings
Generate vector embeddings for text using the embeddings endpoint.
The embeddings endpoint converts text into numerical vectors that capture semantic meaning. Use embeddings for similarity search, clustering, and RAG applications.
Endpoint
POST https://api.elyxir.ai/v1/embeddings
Request
Headers
| Header | Required | Description |
|---|---|---|
| Authorization | Yes | Bearer token: Bearer elyxir_xxx |
| Content-Type | Yes | Must be application/json |
Body Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| model | string | Yes | Embedding model ID |
| input | string/array | Yes | Text to embed |
| encoding_format | string | No | float (default) or base64 |
Example Request
curl -X POST https://api.elyxir.ai/v1/embeddings \
-H "Authorization: Bearer elyxir_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"model": "text-embedding-3-small",
"input": "The quick brown fox jumps over the lazy dog"
}'Response
Success (200 OK)
{
"object": "list",
"data": [
{
"object": "embedding",
"index": 0,
"embedding": [0.0023, -0.0093, 0.0152, ...]
}
],
"model": "text-embedding-3-small",
"usage": {
"prompt_tokens": 9,
"total_tokens": 9
}
}Batch Embeddings
Embed multiple texts in one request:
response = requests.post(
"https://api.elyxir.ai/v1/embeddings",
headers=headers,
json={
"model": "text-embedding-3-small",
"input": [
"First document to embed",
"Second document to embed",
"Third document to embed"
]
}
)
# Response includes embedding for each input
for item in response.json()["data"]:
print(f"Index {item['index']}: {len(item['embedding'])} dimensions")Available Models
| Model | Provider | Dimensions | Max Input |
|---|---|---|---|
| text-embedding-3-large | OpenAI | 3072 | 8191 |
| text-embedding-3-small | OpenAI | 1536 | 8191 |
| text-embedding-ada-002 | OpenAI | 1536 | 8191 |
| embed-english-v3.0 | Cohere | 1024 | 512 |
| embed-multilingual-v3.0 | Cohere | 1024 | 512 |
Use Cases
Semantic Search
Find similar documents:
import numpy as np
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
# Embed query and documents
query_embedding = get_embedding("How do I reset my password?")
doc_embeddings = [get_embedding(doc) for doc in documents]
# Find most similar
similarities = [cosine_similarity(query_embedding, doc) for doc in doc_embeddings]
most_similar_idx = np.argmax(similarities)RAG (Retrieval-Augmented Generation)
- Embed your documents and store in a vector database
- Embed the user's query
- Find similar documents using vector similarity
- Include relevant documents in the LLM prompt
Clustering
Group similar texts:
from sklearn.cluster import KMeans
# Get embeddings for all texts
embeddings = [get_embedding(text) for text in texts]
# Cluster into groups
kmeans = KMeans(n_clusters=5)
clusters = kmeans.fit_predict(embeddings)Best Practices
Text Length
- Shorter texts (1-2 sentences) work well for questions/queries
- Longer texts (paragraphs) capture more context
- Very long texts may exceed model limits
Preprocessing
- Clean text of irrelevant formatting
- Consider chunking long documents
- Normalize text (lowercase, remove special characters) if needed
Choosing a Model
| Use Case | Recommended Model |
|---|---|
| English text, high quality | text-embedding-3-large |
| English text, cost-effective | text-embedding-3-small |
| Multilingual | embed-multilingual-v3.0 |
| Legacy compatibility | text-embedding-ada-002 |
Related
- API Overview - Getting started
- Chat Completions - Text generation
- Model Hub - Browse embedding models