rerank method

Future<List<RankedResult>> rerank(
  1. String query,
  2. List<String> documents, {
  3. int? topN,
})

Score documents against query, best first.

final ranked = await RunAnywhere.rerank.rerank('cats', docs, topN: 3);
print(ranked.first.index);

Throws SDKException when no rerank model is loaded.

Implementation

Future<List<RankedResult>> rerank(
  String query,
  List<String> documents, {
  int? topN,
}) async {
  if (!DartBridge.isInitialized) {
    throw SDKException.notInitialized();
  }
  if (documents.isEmpty) return const <RankedResult>[];
  await DartBridge.ensureServicesReady();

  final snapshot = RunAnywhereModelLifecycle.shared.componentSnapshot(
    SDKComponent.SDK_COMPONENT_RERANK,
  );
  if (snapshot == null) {
    throw SDKException.componentNotReady('Rerank');
  }
  // `RerankCandidate` is deleted (idl/rerank.proto): every facade already
  // built it with `id` set to the stringified array index, so the flat
  // `documents` list carries the same information — results point back
  // at documents by `index`.
  final request = RerankRequest(
    query: query,
    documents: documents,
    options: RerankOptions(topN: topN ?? 0),
  );
  final result = DartBridgeRerank.shared.rerank(request, snapshot);
  return List<RankedResult>.unmodifiable(
    result.items.map(RankedResult.fromProto),
  );
}