rerank()
Rerank a set of documents based on their relevance to a query using a reranking model.
This is ideal for improving search relevance by reordering documents, emails, or other content based on semantic understanding of the query and documents.
import { cohere } from '@ai-sdk/cohere';import { rerank } from 'ai';
const { ranking } = await rerank({ model: cohere.reranking('rerank-v3.5'), documents: ['sunny day at the beach', 'rainy afternoon in the city'], query: 'talk about rain',});Import
import { rerank } from "ai"API Signature
Parameters
model:
RerankingModel
documents:
Array<VALUE>
query:
string
topN?:
number
maxRetries?:
number
abortSignal?:
AbortSignal
headers?:
Record<string, string>
providerOptions?:
ProviderOptions
experimental_telemetry?:
TelemetrySettings
TelemetrySettings
isEnabled?:
boolean
recordInputs?:
boolean
recordOutputs?:
boolean
functionId?:
string
metadata?:
Record<string, string | number | boolean | Array<null | undefined | string> | Array<null | undefined | number> | Array<null | undefined | boolean>>
tracer?:
Tracer
Returns
originalDocuments:
Array<VALUE>
rerankedDocuments:
Array<VALUE>
ranking:
Array<RankingItem<VALUE>>
RankingItem<VALUE>
originalIndex:
number
score:
number
document:
VALUE
response:
Response
Response
id?:
string
timestamp:
Date
modelId:
string
headers?:
Record<string, string>
body?:
unknown
providerMetadata?:
ProviderMetadata | undefined
Examples
String Documents
import { cohere } from '@ai-sdk/cohere';import { rerank } from 'ai';
const { ranking, rerankedDocuments } = await rerank({ model: cohere.reranking('rerank-v3.5'), documents: [ 'sunny day at the beach', 'rainy afternoon in the city', 'snowy night in the mountains', ], query: 'talk about rain', topN: 2,});
console.log(rerankedDocuments);// ['rainy afternoon in the city', 'sunny day at the beach']
console.log(ranking);// [// { originalIndex: 1, score: 0.9, document: 'rainy afternoon...' },// { originalIndex: 0, score: 0.3, document: 'sunny day...' }// ]Object Documents
import { cohere } from '@ai-sdk/cohere';import { rerank } from 'ai';
const documents = [ { from: 'Paul Doe', subject: 'Follow-up', text: 'We are happy to give you a discount of 20%.', }, { from: 'John McGill', subject: 'Missing Info', text: 'Here is the pricing from Oracle: $5000/month', },];
const { ranking } = await rerank({ model: cohere.reranking('rerank-v3.5'), documents, query: 'Which pricing did we get from Oracle?', topN: 1,});
console.log(ranking[0].document);// { from: 'John McGill', subject: 'Missing Info', ... }With Provider Options
import { cohere } from '@ai-sdk/cohere';import { rerank } from 'ai';
const { ranking } = await rerank({ model: cohere.reranking('rerank-v3.5'), documents: ['sunny day at the beach', 'rainy afternoon in the city'], query: 'talk about rain', providerOptions: { cohere: { maxTokensPerDoc: 1000, }, },});