1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! Provider-agnostic reranking abstractions.
//!
//! Reranking models reorder a list of documents by relevance to a query.
//! A [`Model`](crate::driver::Model) over a rerank wire calls one, and
//! [`RerankResponse`] carries both the scored results and token usage.
//!
//! ```no_run
//! use rig_core::DynModel;
//! use rig_core::operation::{Rerank, RerankRequest};
//!
//! # async fn example(model: &DynModel<Rerank>) -> Result<(), Box<dyn std::error::Error>> {
//! let request = RerankRequest {
//! query: "Rust".into(),
//! documents: vec!["A systems programming language".into()],
//! };
//! let response = model.call(request).await?;
//! # let _ = response;
//! # Ok(())
//! # }
//! ```
use crate::completion::Usage;
use serde::{Deserialize, Serialize};
/// A single reranked document result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RerankResult {
/// Index of the document in the original input list.
pub index: usize,
/// The document text, if requested via `return_documents`.
pub document: Option<String>,
/// Relevance score, with higher values more relevant within this response.
/// The range is provider-specific and may include negative values. Do not
/// interpret it as a probability or compare scores across responses.
pub relevance_score: f64,
}
/// Ranked documents and normalized provider metadata.
/// Provider-specific response data is available through [`Self::raw`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RerankResponse {
/// Reranked results sorted by relevance (highest first).
pub results: Vec<RerankResult>,
/// Provider-reported model identifier, or `None` when omitted.
#[serde(default)]
pub model: Option<String>,
/// Token usage for this rerank request; every counter is `None` when the
/// provider reported none (see [`Usage`]).
#[serde(default)]
pub usage: Usage,
/// Stable descriptor name of the provider that produced this response,
/// for example `"voyageai"`. Always populated.
pub provider: String,
/// Provider-assigned response-scoped identifier, when reported.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub response_id: Option<String>,
/// Transport request ID from HTTP headers, or `None` when unreported.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub provider_request_id: Option<String>,
/// Provider response document. Defaults to null until populated.
#[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
pub raw: serde_json::Value,
}
impl RerankResponse {
/// A response carrying `results`. The driver writes the provider, the
/// transport request id and the reply document; decoders set what the
/// provider reported.
pub fn new(results: Vec<RerankResult>) -> Self {
Self {
results,
model: None,
usage: Usage::default(),
provider: String::new(),
response_id: None,
provider_request_id: None,
raw: serde_json::Value::Null,
}
}
}