# Embeddings and rerank

Vectors for search, and ordering documents by relevance.

## Embeddings

`POST /v1/embeddings`, compatible with the OpenAI SDKs. Pass one string or a list in `input`; each comes back as a vector in `data`, in the same order. Add `"encoding_format": "base64"` to get each vector as base64-encoded float32 instead of a list of numbers, as in the OpenAI API.

cURL:

```bash
curl https://api.ewest.ai/v1/embeddings \
  -H "Authorization: Bearer $EWEST_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "ewest/gemini-embedding", "input": ["How do I reset my password?", "Billing questions"]}'

# → {"data": [{"embedding": [0.012, -0.034, …], "index": 0}, …], "usage": {…}}
```

Python:

```python
import os
from openai import OpenAI

client = OpenAI(base_url="https://api.ewest.ai/v1", api_key=os.environ["EWEST_KEY"])
res = client.embeddings.create(model="ewest/gemini-embedding", input=["How do I reset my password?"])
print(len(res.data[0].embedding))
```

JavaScript:

```javascript
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://api.ewest.ai/v1", apiKey: process.env.EWEST_KEY });
const res = await client.embeddings.create({ model: "ewest/gemini-embedding", input: ["How do I reset my password?"] });
console.log(res.data[0].embedding.length);
```

## Rerank

`POST /v1/rerank` takes a `query` and a list of `documents` (strings) and returns `results` ordered by `relevance_score`, each with the `index` of the document. Use `top_n` to keep only the best matches.

cURL:

```bash
curl https://api.ewest.ai/v1/rerank \
  -H "Authorization: Bearer $EWEST_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "ewest/semantic-ranker", "query": "How do I get a refund?", "top_n": 2,
       "documents": ["Refunds are issued within 5 days.", "Our office is in Yerevan.", "Contact support to cancel an order."]}'

# → {"results": [{"index": 0, "relevance_score": 0.91}, {"index": 2, "relevance_score": 0.42}]}
```

Python:

```python
import os, requests

res = requests.post(
    "https://api.ewest.ai/v1/rerank",
    headers={"Authorization": f"Bearer {os.environ['EWEST_KEY']}"},
    json={"model": "ewest/semantic-ranker", "query": "How do I get a refund?", "top_n": 2,
          "documents": ["Refunds are issued within 5 days.", "Our office is in Yerevan."]},
).json()
for r in res["results"]:
    print(r["index"], r["relevance_score"])
```

JavaScript:

```javascript
const res = await (await fetch("https://api.ewest.ai/v1/rerank", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.EWEST_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({
    model: "ewest/semantic-ranker", query: "How do I get a refund?", top_n: 2,
    documents: ["Refunds are issued within 5 days.", "Our office is in Yerevan."],
  }),
})).json();
console.log(res.results);
```

Source: https://ewest.ai/docs/embeddings
