Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

README.md

86 lines2.8 KB

1# Reranking (ts-react-rerank)
2​
3A small TanStack Start app that shows the `rerank()` activity reordering a fixed
4set of support articles by relevance to a query.
5​
6The corpus is listed newest-first, which is a bad answer to every query in the
7demo. The right-hand column shows what the rerank model does with it, with the
8relevance score and the document's original position.
9​
10## Tech stack
11​
12- TanStack Start (full-stack React)
13- `@tanstack/ai` — the `rerank()` activity
14- `@tanstack/ai-cohere` — `cohereRerank(...)`
15- `@tanstack/ai-openrouter` — `openRouterRerank(...)`
16​
17## Getting started
18​
19```bash
20cd examples/ts-react-rerank
21pnpm install
22cp .env.example .env
23# Add COHERE_API_KEY, OPENROUTER_API_KEY, or both
24pnpm dev
25```
26​
27Open http://localhost:3000. You only need a key for the provider you select.
28​
29- Cohere key: https://dashboard.cohere.com/api-keys
30- OpenRouter key: https://openrouter.ai/keys
31​
32## What the example shows
33​
34**One activity, two providers.** `src/lib/server-functions.ts` calls the same
35`rerank()` for both providers. Only the adapter changes:
36​
37```ts
38// Cohere
39await rerank({
40 adapter: cohereRerank('rerank-v3.5'),
41 query,
42 documents: SUPPORT_DOCS,
43 topN,
44 modelOptions: { maxTokensPerDoc: 4096 },
45})
46​
47// OpenRouter — also reaches NVIDIA rerank models through one key
48await rerank({
49 adapter: openRouterRerank('cohere/rerank-4-fast'),
50 query,
51 documents: SUPPORT_DOCS,
52 topN,
53})
54```
55​
56**Object documents stay typed.** `SUPPORT_DOCS` is `Array<SupportDoc>`, not
57`Array<string>`. `rerank()` serializes each object with `JSON.stringify` for the
58provider, then hands the original object back:
59​
60```tsx
61result.ranking[0].document.title // string — no cast, no id lookup
62result.ranking[0].score // number — relevance
63result.ranking[0].index // number — position in the input array
64```
65​
66**Keys stay on the server.** Both adapters read their key from the environment
67inside the server function, so nothing is exposed to the browser.
68​
69**Usage is reported.** Rerank bills in search units rather than tokens, so the
70result panel reads `usage.billed` (`{ quantity, unit: 'units' }`). OpenRouter
71also reports `usage.cost`.
72​
73## Files worth reading
74​
75| File | What's in it |
76| -------------------------------- | -------------------------------------------------- |
77| `src/lib/server-functions.ts` | The `rerank()` calls and adapter selection |
78| `src/lib/documents.ts` | The corpus and why its order is deliberately bad |
79| `src/lib/models.ts` | Model lists, re-exported from the adapter packages |
80| `src/components/RerankPanel.tsx` | The before/after UI |
81​
82## Learn more
83​
84- [Reranking guide](../../docs/rerank/rerank.md)
85- [Cohere adapter](../../docs/adapters/cohere.md)
86- [OpenRouter adapter](../../docs/adapters/openrouter.md)