use std::time::Duration;
#[derive(Debug, Clone)]
pub struct RerankConfig {
pub model: String,
pub base_url: String,
pub api_key: Option<String>,
pub top_n: Option<usize>,
pub timeout: Duration,
pub enable_chunking: bool,
pub max_tokens_per_doc: usize,
}
impl Default for RerankConfig {
fn default() -> Self {
Self {
model: "jina-reranker-v2-base-multilingual".to_string(),
base_url: "https://api.jina.ai/v1/rerank".to_string(),
api_key: None,
top_n: None,
timeout: Duration::from_secs(30),
enable_chunking: false,
max_tokens_per_doc: 480,
}
}
}
impl RerankConfig {
pub fn jina(api_key: impl Into<String>) -> Self {
Self {
model: "jina-reranker-v2-base-multilingual".to_string(),
base_url: "https://api.jina.ai/v1/rerank".to_string(),
api_key: Some(api_key.into()),
..Default::default()
}
}
pub fn cohere(api_key: impl Into<String>) -> Self {
Self {
model: "rerank-v3.5".to_string(),
base_url: "https://api.cohere.com/v2/rerank".to_string(),
api_key: Some(api_key.into()),
max_tokens_per_doc: 4096,
..Default::default()
}
}
pub fn aliyun(api_key: impl Into<String>) -> Self {
Self {
model: "gte-rerank-v2".to_string(),
base_url:
"https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank"
.to_string(),
api_key: Some(api_key.into()),
..Default::default()
}
}
pub fn with_model(mut self, model: impl Into<String>) -> Self {
self.model = model.into();
self
}
pub fn with_top_n(mut self, top_n: usize) -> Self {
self.top_n = Some(top_n);
self
}
pub fn with_chunking(mut self, enable: bool) -> Self {
self.enable_chunking = enable;
self
}
pub fn with_max_tokens(mut self, max_tokens: usize) -> Self {
self.max_tokens_per_doc = max_tokens;
self
}
}
#[derive(Debug, Clone, Copy, Default)]
pub enum ScoreAggregation {
#[default]
Max,
Mean,
First,
}