use super::{
request::{RerankBody, RerankModel},
response::RerankResponse,
};
use crate::ZaiResult;
use crate::client::ZaiClient;
pub struct RerankRequest {
body: RerankBody,
}
impl RerankRequest {
pub fn new(query: impl Into<String>, documents: Vec<String>) -> Self {
let body = RerankBody::new(RerankModel::Rerank, query, documents);
Self { body }
}
pub fn with_top_n(mut self, n: usize) -> Self {
self.body = self.body.with_top_n(n);
self
}
pub fn with_return_documents(mut self, v: bool) -> Self {
self.body = self.body.with_return_documents(v);
self
}
pub fn with_return_raw_scores(mut self, v: bool) -> Self {
self.body = self.body.with_return_raw_scores(v);
self
}
pub fn with_request_id(mut self, v: impl Into<String>) -> Self {
self.body = self.body.with_request_id(v);
self
}
pub fn with_user_id(mut self, v: impl Into<String>) -> Self {
self.body = self.body.with_user_id(v);
self
}
pub fn validate(&self) -> ZaiResult<()> {
self.body
.validate_constraints()
.map_err(|e| crate::client::error::ZaiError::ApiError {
code: crate::client::error::codes::SDK_VALIDATION,
message: format!("Validation error: {e:?}"),
})
}
pub async fn send_via(&self, client: &ZaiClient) -> ZaiResult<RerankResponse> {
self.validate()?;
let route = crate::client::routes::RERANK_CREATE;
let url = client.endpoints().resolve_route(route, &[])?;
client
.send_json::<_, RerankResponse>(route.method(), url, &self.body)
.await
}
}