Skip to content

Commit e52fb02

Browse files
committed
docs: rewrite the README for first-time readers
1 parent a66baaa commit e52fb02

1 file changed

Lines changed: 83 additions & 17 deletions

File tree

‎README.md‎

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
# @corbits/reranking
22

3-
Cross-encoder reranking for retrieval pipelines. Send a query plus candidates, get scored ids back in ranked order.
3+
Cross-encoder reranking over Hugging Face Text Embeddings Inference (TEI), Cohere/Jina and Voyage rerank endpoints, built on `@intx/inference` retries and error classification. A retrieval building block for Corbits and Interchange agents that also works standalone.
44

5-
Give `rerankDocuments` a query and your own `{id, text}` docs; it posts them to the reranker, maps the reply back to your ids, and returns `{id, score}` sorted descending.
5+
## Why @corbits/reranking?
6+
7+
1. **One call for three wire formats.** `rerankDocuments` speaks TEI, Cohere (also Jina) and Voyage. Switching providers is a config change.
8+
2. **Your ids back, sorted.** Providers score by array position. The client maps each score back to your `id` and returns `{ id, score }`, best first.
9+
3. **Interchange retry semantics.** 429s and 5xx are retried under the `@intx/inference` retry policy, and every failure is a `RerankRequestError` carrying a classified `InferenceError`.
10+
11+
## Install
12+
13+
```bash
14+
bun add @corbits/reranking @intx/inference@^0.4.0 @intx/types@^0.4.0
15+
```
16+
17+
Runs on Bun >= 1.2 or Node >= 24.
618

719
## Quickstart
820

21+
This example needs a local TEI reranker:
22+
923
```bash
10-
bun add @corbits/reranking
24+
docker run -p 8080:80 ghcr.io/huggingface/text-embeddings-inference:cpu-latest \
25+
--model-id BAAI/bge-reranker-base
1126
```
1227

1328
```ts
@@ -23,24 +38,61 @@ const ranked = await rerankDocuments("What is the capital of France?", docs, {
2338
baseURL: "http://localhost:8080",
2439
apiStyle: "tei",
2540
});
26-
27-
console.log(ranked);
28-
// [{ id: "paris", score: 0.99 }, { id: "weather", score: 0.01 }, ...]
41+
console.log(ranked[0]?.id); // paris
2942
```
3043

31-
Config takes `baseURL` and `apiStyle`, with optional `model` (required by Cohere and Voyage), `apiKey`, and `timeoutMs`. The optional fourth argument takes `deps` (defaults to global `fetch` and `createDefaultScheduler()`), `retryPolicy`, `registry`, and `signal`.
44+
## Where it fits
45+
46+
[Interchange](https://github.com/faremeter/interchange) runs AI agents with their own identity and permissions. Corbits packages add the parts an agent product needs around it.
47+
48+
- **Runs in:** any process. That can be the Interchange hub (the server that manages tenants and agents), an agent sidecar (the runtime next to each agent), or a plain script. No hub is needed.
49+
- **Plugs into:** [`@intx/inference`](https://github.com/faremeter/interchange) for retries and error types, and [`@intx/types`](https://github.com/faremeter/interchange).
50+
- **Pairs with:** [`@corbits/embedding`](https://github.com/corbitsdev/corbits-embedding) to turn text into vectors, and [`@corbits/memory`](https://github.com/corbitsdev/corbits-memory) to store and search them.
3251

33-
## How it works
52+
## Reference
3453

35-
| `apiStyle` | Endpoint | Request shape | Response shape |
54+
| Export | Description |
55+
| --------------------------------------------------------------- | ----------------------------------------------------------------- |
56+
| `rerankDocuments(query, docs, config, options?)` | Scores each doc and returns `{ id, score }`, best first. |
57+
| `RerankConfigSchema`, `RerankConfig` | Schema and type for `config`. |
58+
| `RerankOptions` | Type for `options`. |
59+
| `rerankAdapterRegistry` | The built-in formats: `tei`, `cohere` and `voyage`. |
60+
| `createRerankAdapterRegistry(adapters)` | Builds a registry with your own formats. |
61+
| `RerankRequestError` | Thrown when a request fails. |
62+
| `RerankAdapter`, `RerankRequestBuilder`, `RerankResponseParser` | Types for writing an adapter. |
63+
| `RerankRequestConfig` | Config an adapter's `buildRequest` receives. |
64+
| `RerankAdapterRegistry`, `RerankAPIStyle` | Types for registries and built-in style names. |
65+
| `RerankDoc`, `RerankResult` | Types for docs and results. |
66+
| `RequestDependencies`, `RetryAfterExtractor` | Types for `options.deps` and `RerankAdapter.extractRetryAfterMs`. |
67+
68+
### Config
69+
70+
| Field | Type | Description |
71+
| ----------- | --------- | ----------------------------------------------------------------------------------- |
72+
| `baseURL` | `string` | Server root, such as `http://localhost:8080`. |
73+
| `apiStyle` | `string` | Wire format: `tei`, `cohere` (also Jina) or `voyage`. |
74+
| `model` | `string?` | Model name. Required by `cohere` (and Jina) and `voyage`. TEI serves its own model. |
75+
| `apiKey` | `string?` | Sent as a bearer token. |
76+
| `timeoutMs` | `number?` | Time limit per attempt, so retries can exceed it. Defaults to 30000. |
77+
78+
### Options
79+
80+
| Field | Description |
81+
| ------------- | ------------------------------------------------------------------------------- |
82+
| `deps` | `{ fetch, scheduler }`. Defaults to global `fetch` and Interchange's scheduler. |
83+
| `retryPolicy` | Decides when to retry. Defaults to Interchange's policy. |
84+
| `registry` | Wire formats to choose from. Defaults to `rerankAdapterRegistry`. |
85+
| `signal` | Cancels the request. |
86+
87+
### Wire formats
88+
89+
| `apiStyle` | Endpoint | Request | Reply |
3690
| ---------- | ------------ | -------------------- | --------------------------------------- |
3791
| `tei` | `/rerank` | `{query, texts}` | `[{index, score}]` |
3892
| `cohere` | `/v2/rerank` | `{query, documents}` | `{results: [{index, relevance_score}]}` |
3993
| `voyage` | `/v1/rerank` | `{query, documents}` | `{data: [{index, relevance_score}]}` |
4094

41-
Jina's `/rerank` follows the Cohere shape and is served by the `cohere` adapter.
42-
43-
To add a house format, such as a TEI-shaped reply served at `/score`, build a registry with `createRerankAdapterRegistry` and pass it as `registry`:
95+
To add your own format, build a registry and pass it as `registry`. This one serves TEI-shaped replies at `/score`:
4496

4597
```ts
4698
import {
@@ -63,14 +115,28 @@ const registry = createRerankAdapterRegistry({
63115
});
64116
```
65117

66-
Providers reply with positions into the request array, so each score maps back to your `id` through that index.
118+
### Errors
119+
120+
- A failed request throws `RerankRequestError`. Its `reason` says what went wrong, and `url` says where. Network errors, timeouts, rate limits and server errors are retried first under the retry policy.
121+
- A bad config throws arktype's `TraversalError` before any request is sent.
122+
- With non-empty `docs`, a missing `model` for `cohere` or `voyage` throws `TraversalError`, and an `apiStyle` the registry does not know throws an `Error` that names it.
123+
- A custom `parseResponse` should throw `ProtocolMismatchError` from `@intx/inference` so the failure becomes a `RerankRequestError`. Anything else it throws is rethrown as is.
124+
125+
An empty `docs` list returns `[]` without a request. To keep search working when the reranker is down, catch the error and keep your original order.
126+
127+
## Using with Interchange
67128

68-
## Errors
129+
To share your host's retry scheduler, pass it as `options.deps.scheduler` along with `fetch`.
69130

70-
Failures throw `RerankRequestError` (`extends Error`), carrying the classified `InferenceError` from `@intx/inference` as `reason` and the request `url`. That covers transport failures, HTTP errors, and replies in the wrong shape; retryable errors are retried first under the `retryPolicy` option, which defaults to `createDefaultRetryPolicy()`. An invalid config throws arktype's `TraversalError` before any request is sent, as does a missing `model` for Cohere or Voyage once `docs` is non-empty (an empty `docs` returns `[]` without building a request); an `apiStyle` the registry does not know throws a plain `Error` naming it.
131+
## Upgrading from 0.1
71132

72-
Fallback is left to the caller: catch the error and keep your own ordering if a reranker outage should not fail search.
133+
- Install `@intx/inference` and `@intx/types` (^0.4.0) yourself. They are now peer dependencies.
134+
- `ModelRequestError` is renamed to `RerankRequestError`.
135+
- `rerankAdapters` is no longer exported. Use `rerankAdapterRegistry` or `createRerankAdapterRegistry`.
136+
- `runJSONRequest`, `extractRetryAfterMs` and `RunRequestOptions` are no longer exported.
137+
- A missing `model` for Cohere or Voyage now throws `TraversalError` instead of a plain `Error`.
138+
- Config, wire requests and results are unchanged.
73139

74140
## License
75141

76-
LGPL-2.1-only — see [`LICENSE`](LICENSE).
142+
[LGPL-2.1-only](https://github.com/corbitsdev/corbits-reranking/blob/main/LICENSE)

0 commit comments

Comments
 (0)