> ## Documentation Index
> Fetch the complete documentation index at: https://docs.samuraiapi.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Embeddings

> Convert text into vector representations for semantic search, RAG, and clustering.

## Endpoint

```http theme={null}
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

<CodeGroup>
  ```python Python theme={null}
  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
  ```

  ```javascript Node.js theme={null}
  const response = await client.embeddings.create({
    model: 'text-embedding-3-small',
    input: 'The quick brown fox jumps over the lazy dog'
  });

  const vector = response.data[0].embedding;
  console.log(`Dimensions: ${vector.length}`); // 1536
  ```

  ```bash cURL theme={null}
  curl https://api.samuraiapi.in/v1/embeddings \
    -H "Authorization: Bearer sk-samurai-YOUR_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "text-embedding-3-small",
      "input": "The quick brown fox"
    }'
  ```
</CodeGroup>

## Batch Embeddings

```python theme={null}
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

```python theme={null}
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"
```

<Tip>
  For RAG (Retrieval-Augmented Generation), combine embeddings with a vector database like Pinecone, Weaviate, or pgvector.
</Tip>
