Rerank
Use rerank when you already have candidate documents and want the model to score them for a query.
import SwiftAISDK
@mainstruct RerankExample { static func main() async throws { let provider = try AIProviders.cohere() let model = try provider.rerankingModel("rerank-v3.5")
let result = try await model.rerank( query: "Which document explains provider options?", documents: [ "Provider options carry provider-specific settings.", "Streaming text emits lifecycle parts.", "Embeddings convert strings into vectors.", ], topK: 2 )
for item in result.results { print("index: \(item.index), score: \(item.score)") } }}Use the request initializer when your provider supports structured document objects.
let request = RerankingRequest( query: "Find Swift examples", documents: [ ["title": "Quickstart", "body": "Generate text with a provider."], ["title": "Tools", "body": "Execute typed Swift tools."], ], topK: 1)
let result = try await AI.rerank(model: model, request: request)Options
Section titled “Options”queryis the search or ranking intent.documentsare candidate strings or structured document objects.topKlimits the returned matches.providerOptionscarries provider-specific scoring settings.extraBody,headers,abortSignal,retryPolicy, andtelemetryfollow the shared facade behavior.
Results
Section titled “Results”RerankingResult returns scored document references:
resultswarningsproviderMetadatarequestMetadataresponseMetadata