> ## 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

```ts theme={null}
const vectors = await RunAnywhere.embeddings.embed(['hello', 'world'])
console.log(vectors[0].vector.length)
```

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.

## Comparison helpers

Electron is the only SDK with these on the namespace:

```ts theme={null}
const similarity = await RunAnywhere.embeddings.cosineSimilarity(a, b)
const norm = await RunAnywhere.embeddings.computeNorm(vector)
```

Both accept an `Embedding` or a raw `Float32Array`. Elsewhere you write these yourself, or use
helpers on the vector type.

## 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

```ts theme={null}
const ranked = await RunAnywhere.rerank.rerank('refund policy', candidates, 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.

## Indexing a folder

A desktop app can embed a real corpus rather than a handful of pasted strings:

```ts theme={null}
import { readdir, readFile } from 'node:fs/promises'
import { join } from 'node:path'

const names = await readdir(folder)
const texts = await Promise.all(names.map((n) => readFile(join(folder, n), 'utf8')))

const vectors = await RunAnywhere.embeddings.embed(texts)
```

Batch in chunks rather than passing thousands of documents at once, and persist the vectors so
a relaunch does not re-embed everything.

## Where this fits

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

## Models

Embedding and reranking models are small, often tens of megabytes rather than gigabytes, so
keeping both resident alongside a language model is reasonable on a desktop.
