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

rerank.md

66 lines1.4 KB

1---
2id: rerank
3title: rerank
4---
5​
6```ts
7function rerank<TAdapter, TDocument>(options): Promise<RerankResult<TDocument>>;
8```
9​
10Defined in: packages/ai/src/activities/rerank/index.ts:152
11​
12Rerank activity - reorders documents by relevance to a query.
13​
14## Type Parameters
15​
16### TAdapter
17​
18`TAdapter` *extends* [`RerankAdapter`](../interfaces/RerankAdapter.md)\<`string`, `RerankProviderOptions`\<`TAdapter`\>\>
19​
20### TDocument
21​
22`TDocument` *extends* `string` \| `object` = `string`
23​
24## Parameters
25​
26### options
27​
28`RerankActivityOptions`\<`TAdapter`, `TDocument`\>
29​
30## Returns
31​
32`Promise`\<[`RerankResult`](../interfaces/RerankResult.md)\<`TDocument`\>\>
33​
34## Examples
35​
36**Basic reranking**
37​
38```ts
39import { rerank } from '@tanstack/ai'
40import { cohereRerank } from '@tanstack/ai-cohere'
41​
42const { ranking, rerankedDocuments } = await rerank({
43 adapter: cohereRerank('rerank-v3.5'),
44 query: 'talk about rain',
45 documents: ['sunny day at the beach', 'rainy afternoon in the city'],
46 topN: 2,
47})
48​
49console.log(rerankedDocuments[0]) // 'rainy afternoon in the city'
50```
51​
52**Reranking object documents**
53​
54```ts
55const { ranking } = await rerank({
56 adapter: cohereRerank('rerank-v3.5'),
57 query: 'best laptop for travel',
58 documents: [
59 { id: 1, text: 'A heavy gaming desktop' },
60 { id: 2, text: 'A lightweight ultrabook with all-day battery' },
61 ],
62})
63​
64// ranking[0].document is the original object, fully typed.
65console.log(ranking[0].document.id)
66```