use super::DEFAULT_MAX_LENGTH;
use crate::{
init::{HasMaxLength, InitOptionsWithLength},
RerankerModel, TokenizerFiles,
};
use ort::{execution_providers::ExecutionProviderDispatch, session::Session};
use std::path::PathBuf;
use tokenizers::Tokenizer;
#[derive(Debug)]
pub struct TextRerank {
pub tokenizer: Tokenizer,
pub(crate) session: Session,
pub(crate) need_token_type_ids: bool,
}
impl HasMaxLength for RerankerModel {
const MAX_LENGTH: usize = DEFAULT_MAX_LENGTH;
}
pub type RerankInitOptions = InitOptionsWithLength<RerankerModel>;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct RerankInitOptionsUserDefined {
pub execution_providers: Vec<ExecutionProviderDispatch>,
pub max_length: usize,
pub intra_threads: Option<usize>,
}
impl Default for RerankInitOptionsUserDefined {
fn default() -> Self {
Self {
execution_providers: Default::default(),
max_length: DEFAULT_MAX_LENGTH,
intra_threads: None,
}
}
}
impl RerankInitOptionsUserDefined {
pub fn new() -> Self {
Self::default()
}
pub fn with_execution_providers(
mut self,
execution_providers: Vec<ExecutionProviderDispatch>,
) -> Self {
self.execution_providers = execution_providers;
self
}
pub fn with_max_length(mut self, max_length: usize) -> Self {
self.max_length = max_length;
self
}
pub fn with_intra_threads(mut self, intra_threads: usize) -> Self {
self.intra_threads = Some(intra_threads);
self
}
}
impl From<RerankInitOptions> for RerankInitOptionsUserDefined {
fn from(options: RerankInitOptions) -> Self {
RerankInitOptionsUserDefined {
execution_providers: options.execution_providers,
max_length: options.max_length,
intra_threads: options.intra_threads,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OnnxSource {
Memory(Vec<u8>),
File(PathBuf),
}
impl From<Vec<u8>> for OnnxSource {
fn from(bytes: Vec<u8>) -> Self {
OnnxSource::Memory(bytes)
}
}
impl From<PathBuf> for OnnxSource {
fn from(path: PathBuf) -> Self {
OnnxSource::File(path)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct UserDefinedRerankingModel {
pub onnx_source: OnnxSource,
pub tokenizer_files: TokenizerFiles,
}
impl UserDefinedRerankingModel {
pub fn new(onnx_source: impl Into<OnnxSource>, tokenizer_files: TokenizerFiles) -> Self {
Self {
onnx_source: onnx_source.into(),
tokenizer_files,
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct RerankResult {
pub document: Option<String>,
pub score: f32,
pub index: usize,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn userdefined_builders_set_fields() {
let o = RerankInitOptionsUserDefined::new()
.with_max_length(128)
.with_intra_threads(2);
assert_eq!(o.max_length, 128);
assert_eq!(o.intra_threads, Some(2));
}
}