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,
}
impl Default for RerankInitOptionsUserDefined {
fn default() -> Self {
Self {
execution_providers: Default::default(),
max_length: DEFAULT_MAX_LENGTH,
}
}
}
impl From<RerankInitOptions> for RerankInitOptionsUserDefined {
fn from(options: RerankInitOptions) -> Self {
RerankInitOptionsUserDefined {
execution_providers: options.execution_providers,
max_length: options.max_length,
}
}
}
#[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,
}