1use crate::get_cache_dir;
2use ort::execution_providers::ExecutionProviderDispatch;
3use std::path::PathBuf;
4
5pub trait HasMaxLength {
6 const MAX_LENGTH: usize;
7}
8
9#[derive(Debug, Clone)]
10#[non_exhaustive]
11pub struct InitOptionsWithLength<M> {
12 pub model_name: M,
13 pub execution_providers: Vec<ExecutionProviderDispatch>,
14 pub cache_dir: PathBuf,
15 pub show_download_progress: bool,
16 pub max_length: usize,
17 pub intra_threads: Option<usize>,
21}
22
23#[derive(Debug, Clone)]
24#[non_exhaustive]
25pub struct InitOptions<M> {
26 pub model_name: M,
27 pub execution_providers: Vec<ExecutionProviderDispatch>,
28 pub cache_dir: PathBuf,
29 pub show_download_progress: bool,
30 pub intra_threads: Option<usize>,
34}
35
36impl<M: Default + HasMaxLength> Default for InitOptionsWithLength<M> {
37 fn default() -> Self {
38 Self {
39 model_name: M::default(),
40 execution_providers: Default::default(),
41 cache_dir: get_cache_dir().into(),
42 show_download_progress: true,
43 max_length: M::MAX_LENGTH,
44 intra_threads: None,
45 }
46 }
47}
48
49impl<M: Default> Default for InitOptions<M> {
50 fn default() -> Self {
51 Self {
52 model_name: M::default(),
53 execution_providers: Default::default(),
54 cache_dir: get_cache_dir().into(),
55 show_download_progress: true,
56 intra_threads: None,
57 }
58 }
59}
60
61impl<M: Default + HasMaxLength> InitOptionsWithLength<M> {
62 pub fn new(model_name: M) -> Self {
64 Self {
65 model_name,
66 ..Default::default()
67 }
68 }
69
70 pub fn with_max_length(mut self, max_length: usize) -> Self {
72 self.max_length = max_length;
73 self
74 }
75
76 pub fn with_cache_dir(mut self, cache_dir: PathBuf) -> Self {
78 self.cache_dir = cache_dir;
79 self
80 }
81
82 pub fn with_execution_providers(
84 mut self,
85 execution_providers: Vec<ExecutionProviderDispatch>,
86 ) -> Self {
87 self.execution_providers = execution_providers;
88 self
89 }
90
91 pub fn with_intra_threads(mut self, intra_threads: usize) -> Self {
95 self.intra_threads = Some(intra_threads);
96 self
97 }
98
99 pub fn with_show_download_progress(mut self, show_download_progress: bool) -> Self {
101 self.show_download_progress = show_download_progress;
102 self
103 }
104}
105
106impl<M: Default> InitOptions<M> {
107 pub fn new(model_name: M) -> Self {
109 Self {
110 model_name,
111 ..Default::default()
112 }
113 }
114
115 pub fn with_cache_dir(mut self, cache_dir: PathBuf) -> Self {
117 self.cache_dir = cache_dir;
118 self
119 }
120
121 pub fn with_execution_providers(
123 mut self,
124 execution_providers: Vec<ExecutionProviderDispatch>,
125 ) -> Self {
126 self.execution_providers = execution_providers;
127 self
128 }
129
130 pub fn with_intra_threads(mut self, intra_threads: usize) -> Self {
134 self.intra_threads = Some(intra_threads);
135 self
136 }
137
138 pub fn with_show_download_progress(mut self, show_download_progress: bool) -> Self {
140 self.show_download_progress = show_download_progress;
141 self
142 }
143}
144
145#[cfg(test)]
146mod tests {
147 use super::*;
148
149 #[test]
150 fn intra_threads_defaults_none_and_builder_sets() {
151 let o = InitOptions::<crate::ImageEmbeddingModel>::default();
152 assert_eq!(o.intra_threads, None);
153 let o = o.with_intra_threads(4);
154 assert_eq!(o.intra_threads, Some(4));
155 }
156}