rerank
API Reference for 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
import { rerank } from "ai"
API Signature
Parameters
model:
RerankingModel
The reranking model to use. Example: cohere.reranking('rerank-v4.0-pro')
documents:
Array<VALUE>
The documents to rerank. Can be an array of strings or JSON objects.
query:
string
The search query to rank documents against.
topN?:
number
Maximum number of top documents to return. If not specified, all documents are returned.
maxRetries?:
number
Maximum number of retries. Set to 0 to disable retries. Default: 2.
abortSignal?:
AbortSignal
An optional abort signal that can be used to cancel the call.
headers?:
Record<string, string>
Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
providerOptions?:
ProviderOptions
Provider-specific options for the reranking request.
runtimeContext?:
RUNTIME_CONTEXT
User-defined runtime context passed to lifecycle callbacks. Defaults to an empty object. Telemetry integrations only receive top-level properties explicitly included with telemetry.includeRuntimeContext.
telemetry?:
TelemetryOptions<RUNTIME_CONTEXT>
Telemetry configuration.
TelemetryOptions
isEnabled?:
boolean
Enable or disable telemetry. Enabled by default. Set to
false to opt out.recordInputs?:
boolean
Enable or disable input recording. Enabled by default.
recordOutputs?:
boolean
Enable or disable output recording. Enabled by default.
functionId?:
string
Identifier for this function. Used to group telemetry data by function.
includeRuntimeContext?:
{ [KEY in keyof RUNTIME_CONTEXT]?: boolean }
Top-level runtime context properties to include in telemetry. Only properties set to true are included. All properties are excluded by default. User callbacks still receive the full context.
integrations?:
Telemetry | Telemetry[]
Per-call telemetry integrations that receive lifecycle events. When provided, these replace any globally registered integrations for this call.
onStart?:
(event: RerankStartEvent<RUNTIME_CONTEXT>) => void | Promise<void>
Called when the rerank operation begins, before the reranking model is called. The event includes the full, unfiltered runtimeContext.
onEnd?:
(event: RerankEndEvent<RUNTIME_CONTEXT>) => void | Promise<void>
Called when the rerank operation completes, after the reranking model returns. The event includes the full, unfiltered runtimeContext.
Returns
originalDocuments:
Array<VALUE>
The original documents array in their original order.
rerankedDocuments:
Array<VALUE>
The documents sorted by relevance score (descending).
ranking:
Array<RankingItem<VALUE>>
Array of ranking items with scores and indices.
RankingItem<VALUE>
originalIndex:
number
The index of the document in the original documents array.
score:
number
The relevance score for the document (typically 0-1, where higher is more relevant).
document:
VALUE
The document itself.
response:
Response
Response data.
Response
id?:
string
The response ID from the provider.
timestamp:
Date
The timestamp of the response.
modelId:
string
The model ID used for reranking.
headers?:
Record<string, string>
Response headers.
body?:
unknown
The raw response body.
providerMetadata?:
ProviderMetadata | undefined
Optional metadata from the provider. The outer key is the provider name. The inner values are the metadata. Details depend on the provider.