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

# Embeddings & Reranking

> Turn text into vectors, and reorder results against a query

Two namespaces that work together. `embeddings` turns text into vectors you can compare.
`rerank` takes a query and a list of candidates and reorders them by relevance.

## Embedding text

```swift theme={null}
let vectors = try await RunAnywhere.embeddings.embed(["hello", "world"])
print(vectors[0].vector.count)
```

Vectors come back in input order, so index `i` of the result matches index `i` of the input.
Batch rather than looping: one call with fifty strings is far cheaper than fifty calls.

## Options

| Field       | Default | Meaning                                                    |
| ----------- | ------- | ---------------------------------------------------------- |
| `normalize` | `true`  | Unit-length vectors, so cosine similarity is a dot product |
| `pooling`   | `mean`  | How token vectors collapse into one                        |

Leave `normalize` on unless you have a reason. With it off, similarity scores are not
comparable across texts of different lengths.

## Reranking

```swift theme={null}
let ranked = try await RunAnywhere.rerank.rerank(
    query: "refund policy",
    documents: candidates,
    topN: 5
)
```

Reranking is a different model from embedding. An embedding model encodes each text once and
compares vectors, which is fast and approximate. A reranker reads the query and the candidate
together, which is slower and much more accurate.

The usual shape is both: retrieve widely with embeddings, then rerank the top handful.

Swift also exposes helpers on the vector type:

```swift theme={null}
let similarity = a.cosineSimilarity(with: b)
let norm = a.computeNorm()
```

## Where this fits

Most people reach for these through [RAG](/swift/rag), which does retrieval, reranking, and
generation in one session. Use the namespaces directly when you want the pieces separately:
semantic search with no generation, deduplication, clustering, or classification by nearest
neighbour.

## Models

Embedding and reranking models are small, often tens of megabytes rather than gigabytes. That
makes them cheap to keep resident alongside a language model.
