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

> Text vectors and cross-encoder reranking

## embeddings.embed

Returns one vector per input, in input order, each carrying its `index`.

```dart theme={null}
final vectors = await RunAnywhere.embeddings.embed(['hello', 'world']);

for (final embedding in vectors) {
  print('${embedding.index}: ${embedding.vector.length} dimensions');
}
```

An empty list returns an empty list without touching the model. Anything else loads an embedding
model first, downloading it when needed, and throws `SDKException` when none is available.

```dart theme={null}
Future<List<Embedding>> embed(
  List<String> texts, {
  String? model,
  EmbedOptions? options,
});
```

`Embedding` carries `index` (the position of the source text in your list) and `vector`, a
`Float32List`.

<Note>
  The `model` parameter is a Flutter addition. The cross-SDK contract has `embed(texts, options)`
  only; pass `model` when you want to pin an embedding model for one call.
</Note>

### EmbedOptions

```dart theme={null}
final vectors = await RunAnywhere.embeddings.embed(
  texts,
  options: EmbedOptions(
    normalize: EmbeddingsNormalizeMode.EMBEDDINGS_NORMALIZE_MODE_L2,
    pooling: EmbeddingsPoolingStrategy.EMBEDDINGS_POOLING_STRATEGY_MEAN,
  ),
);
```

| Field       | Type                        | Default | Notes                                  |
| ----------- | --------------------------- | ------- | -------------------------------------- |
| `normalize` | `EmbeddingsNormalizeMode`   | `L2`    | Applied after pooling; `NONE` skips it |
| `pooling`   | `EmbeddingsPoolingStrategy` | `MEAN`  | `MEAN`, `CLS`, or `LAST`               |

Both enums are prefixed `EMBEDDINGS_NORMALIZE_MODE_` and `EMBEDDINGS_POOLING_STRATEGY_`.

### Cosine similarity

With `L2` normalization, cosine similarity is a dot product.

```dart theme={null}
double cosine(Float32List a, Float32List b) {
  var sum = 0.0;
  for (var i = 0; i < a.length; i++) {
    sum += a[i] * b[i];
  }
  return sum;
}

final vectors = await RunAnywhere.embeddings.embed(['a cat', 'a kitten']);
print(cosine(vectors[0].vector, vectors[1].vector));
```

## rerank.rerank

A cross-encoder scores each document against the query directly, which is more accurate than
comparing embeddings but costs one forward pass per document. The usual pattern is to retrieve widely
with embeddings, then rerank the shortlist.

```dart theme={null}
final ranked = await RunAnywhere.rerank.rerank(
  'how do cats sleep',
  documents,
  topN: 3,
);

for (final item in ranked) {
  print('${documents[item.index]}  ${item.relevanceScore}');
}
```

```dart theme={null}
Future<List<RankedResult>> rerank(
  String query,
  List<String> documents, {
  int? topN,
});
```

Results come back sorted best first. `RankedResult` carries `index`, a pointer into your `documents`
list, and `relevanceScore`, which is comparable only within one result set. An empty `documents` list
returns an empty list. `topN` null returns every document.

It throws `SDKException` when the SDK is not initialized or no rerank model is loaded. Unlike the
generation verbs, `rerank` does not auto-load: load the model yourself first.

### Reranking RAG results

```dart theme={null}
final matches = await session.search(question, topK: 20);
final ranked = await RunAnywhere.rerank.rerank(
  question,
  matches.map((m) => m.text).toList(),
  topN: 5,
);

final best = [for (final item in ranked) matches[item.index]];
```

## Models

Embedding and rerank models are ONNX artifacts. Register the ONNX backend and the model, then load it.

```dart theme={null}
import 'package:runanywhere_onnx/runanywhere_onnx.dart';

await Onnx.register();
```

See [RAG](/flutter/rag) for a complete multi-file embedding registration.

## See also

<CardGroup cols={2}>
  <Card title="RAG" icon="database" href="/flutter/rag">
    Sessions that use both
  </Card>

  <Card title="Models" icon="box" href="/flutter/models">
    Registering ONNX models
  </Card>
</CardGroup>
