use crate::{
common::{TokenizerFiles, DEFAULT_CACHE_DIR},
pooling::Pooling,
EmbeddingModel, QuantizationMode,
};
use ort::{execution_providers::ExecutionProviderDispatch, session::Session};
use std::path::{Path, PathBuf};
use tokenizers::Tokenizer;
use super::{DEFAULT_EMBEDDING_MODEL, DEFAULT_MAX_LENGTH};
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct InitOptions {
pub model_name: EmbeddingModel,
pub execution_providers: Vec<ExecutionProviderDispatch>,
pub max_length: usize,
pub cache_dir: PathBuf,
pub show_download_progress: bool,
}
impl InitOptions {
pub fn new(model_name: EmbeddingModel) -> Self {
Self {
model_name,
..Default::default()
}
}
pub fn with_max_length(mut self, max_length: usize) -> Self {
self.max_length = max_length;
self
}
pub fn with_cache_dir(mut self, cache_dir: PathBuf) -> Self {
self.cache_dir = cache_dir;
self
}
pub fn with_execution_providers(
mut self,
execution_providers: Vec<ExecutionProviderDispatch>,
) -> Self {
self.execution_providers = execution_providers;
self
}
pub fn with_show_download_progress(mut self, show_download_progress: bool) -> Self {
self.show_download_progress = show_download_progress;
self
}
}
impl Default for InitOptions {
fn default() -> Self {
Self {
model_name: DEFAULT_EMBEDDING_MODEL,
execution_providers: Default::default(),
max_length: DEFAULT_MAX_LENGTH,
cache_dir: Path::new(DEFAULT_CACHE_DIR).to_path_buf(),
show_download_progress: true,
}
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct InitOptionsUserDefined {
pub execution_providers: Vec<ExecutionProviderDispatch>,
pub max_length: usize,
}
impl InitOptionsUserDefined {
pub fn new() -> Self {
Self {
..Default::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
}
}
impl Default for InitOptionsUserDefined {
fn default() -> Self {
Self {
execution_providers: Default::default(),
max_length: DEFAULT_MAX_LENGTH,
}
}
}
impl From<InitOptions> for InitOptionsUserDefined {
fn from(options: InitOptions) -> Self {
InitOptionsUserDefined {
execution_providers: options.execution_providers,
max_length: options.max_length,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct UserDefinedEmbeddingModel {
pub onnx_file: Vec<u8>,
pub tokenizer_files: TokenizerFiles,
pub pooling: Option<Pooling>,
pub quantization: QuantizationMode,
}
impl UserDefinedEmbeddingModel {
pub fn new(onnx_file: Vec<u8>, tokenizer_files: TokenizerFiles) -> Self {
Self {
onnx_file,
tokenizer_files,
quantization: QuantizationMode::None,
pooling: None,
}
}
pub fn with_quantization(mut self, quantization: QuantizationMode) -> Self {
self.quantization = quantization;
self
}
pub fn with_pooling(mut self, pooling: Pooling) -> Self {
self.pooling = Some(pooling);
self
}
}
pub struct TextEmbedding {
pub tokenizer: Tokenizer,
pub(crate) pooling: Option<Pooling>,
pub(crate) session: Session,
pub(crate) need_token_type_ids: bool,
pub(crate) quantization: QuantizationMode,
}