1use super::ApiLLMModel;
2use crate::{LLMModelBase, tokenizer::Tokenizer};
3use std::sync::Arc;
4
5impl ApiLLMModel {
6 pub fn openai_model_from_model_id(model_id: &str) -> ApiLLMModel {
7 match model_id {
8 "gpt-4" => Self::gpt_4(),
9 "gpt-4-32k" => Self::gpt_4_32k(),
10 "gpt-4-turbo" => Self::gpt_4_turbo(),
11 "gpt-4o" => Self::gpt_4_o(),
12 "gpt-3.5-turbo" => Self::gpt_3_5_turbo(),
13 "gpt-4o-mini" => Self::gpt_3_5_turbo(),
14 _ => panic!("Model ID ({model_id}) not found for ApiLLMModel"),
15 }
16 }
17
18 pub fn gpt_4() -> ApiLLMModel {
19 let model_id = "gpt-4".to_string();
20 let tokenizer = model_tokenizer(&model_id);
21 ApiLLMModel {
22 model_base: LLMModelBase {
23 model_id,
24 model_ctx_size: 8192,
25 inference_ctx_size: 4096,
26 tokenizer,
27 },
28 cost_per_m_in_tokens: 30.00,
29 cost_per_m_out_tokens: 60.00,
30 tokens_per_message: 3,
31 tokens_per_name: Some(1),
32 }
33 }
34
35 pub fn gpt_4_32k() -> ApiLLMModel {
36 let model_id = "gpt-4-32k".to_string();
37 let tokenizer = model_tokenizer(&model_id);
38 ApiLLMModel {
39 model_base: LLMModelBase {
40 model_id,
41 model_ctx_size: 32768,
42 inference_ctx_size: 4096,
43 tokenizer,
44 },
45 cost_per_m_in_tokens: 60.00,
46 cost_per_m_out_tokens: 120.00,
47 tokens_per_message: 3,
48 tokens_per_name: Some(1),
49 }
50 }
51
52 pub fn gpt_4_turbo() -> ApiLLMModel {
53 let model_id = "gpt-4-turbo".to_string();
54 let tokenizer = model_tokenizer(&model_id);
55 ApiLLMModel {
56 model_base: LLMModelBase {
57 model_id,
58 model_ctx_size: 128000,
59 inference_ctx_size: 4096,
60 tokenizer,
61 },
62 cost_per_m_in_tokens: 10.00,
63 cost_per_m_out_tokens: 30.00,
64 tokens_per_message: 3,
65 tokens_per_name: Some(1),
66 }
67 }
68
69 pub fn gpt_3_5_turbo() -> ApiLLMModel {
70 let model_id = "gpt-3.5-turbo".to_string();
71 let tokenizer = model_tokenizer(&model_id);
72 ApiLLMModel {
73 model_base: LLMModelBase {
74 model_id,
75 model_ctx_size: 16385,
76 inference_ctx_size: 4096,
77 tokenizer,
78 },
79 cost_per_m_in_tokens: 0.50,
80 cost_per_m_out_tokens: 1.50,
81 tokens_per_message: 4,
82 tokens_per_name: Some(-1),
83 }
84 }
85
86 pub fn gpt_4_o_mini() -> ApiLLMModel {
87 let model_id = "gpt-4o-mini".to_string();
88 let tokenizer = model_tokenizer(&model_id);
89 ApiLLMModel {
90 model_base: LLMModelBase {
91 model_id,
92 model_ctx_size: 128000,
93 inference_ctx_size: 16384,
94 tokenizer,
95 },
96 cost_per_m_in_tokens: 0.150,
97 cost_per_m_out_tokens: 0.60,
98 tokens_per_message: 3,
99 tokens_per_name: Some(1),
100 }
101 }
102
103 pub fn gpt_4_o() -> ApiLLMModel {
104 let model_id = "gpt-4o".to_string();
105 let tokenizer = model_tokenizer(&model_id);
106 ApiLLMModel {
107 model_base: LLMModelBase {
108 model_id,
109 model_ctx_size: 128000,
110 inference_ctx_size: 4096,
111 tokenizer,
112 },
113 cost_per_m_in_tokens: 5.00,
114 cost_per_m_out_tokens: 15.00,
115 tokens_per_message: 3,
116 tokens_per_name: Some(1),
117 }
118 }
119
120 pub fn o1_mini() -> ApiLLMModel {
121 let model_id = "o1-mini".to_string();
122 let tokenizer = model_tokenizer("gpt-4o-mini");
123 ApiLLMModel {
124 model_base: LLMModelBase {
125 model_id,
126 model_ctx_size: 128000,
127 inference_ctx_size: 65536,
128 tokenizer,
129 },
130 cost_per_m_in_tokens: 3.00,
131 cost_per_m_out_tokens: 12.00,
132 tokens_per_message: 4,
133 tokens_per_name: Some(-1),
134 }
135 }
136
137 pub fn o1_preview() -> ApiLLMModel {
138 let model_id = "o1-preview".to_string();
139 let tokenizer = model_tokenizer("gpt-4o-mini");
140 ApiLLMModel {
141 model_base: LLMModelBase {
142 model_id,
143 model_ctx_size: 128000,
144 inference_ctx_size: 32768,
145 tokenizer,
146 },
147 cost_per_m_in_tokens: 15.00,
148 cost_per_m_out_tokens: 60.00,
149 tokens_per_message: 4,
150 tokens_per_name: Some(-1),
151 }
152 }
153}
154
155fn model_tokenizer(model_id: &str) -> Arc<Tokenizer> {
156 Arc::new(
157 Tokenizer::new_tiktoken(model_id)
158 .unwrap_or_else(|_| panic!("Failed to load tokenizer for {model_id}")),
159 )
160}
161
162pub trait OpenAIModelTrait {
163 fn model(&mut self) -> &mut ApiLLMModel;
164
165 fn model_id_str(mut self, model_id: &str) -> Self
167 where
168 Self: Sized,
169 {
170 *self.model() = ApiLLMModel::openai_model_from_model_id(model_id);
171 self
172 }
173
174 fn gpt_4(mut self) -> Self
176 where
177 Self: Sized,
178 {
179 *self.model() = ApiLLMModel::gpt_4();
180 self
181 }
182
183 fn gpt_4_32k(mut self) -> Self
185 where
186 Self: Sized,
187 {
188 *self.model() = ApiLLMModel::gpt_4_32k();
189 self
190 }
191
192 fn gpt_4_turbo(mut self) -> Self
194 where
195 Self: Sized,
196 {
197 *self.model() = ApiLLMModel::gpt_4_turbo();
198 self
199 }
200
201 fn gpt_4_o(mut self) -> Self
203 where
204 Self: Sized,
205 {
206 *self.model() = ApiLLMModel::gpt_4_o();
207 self
208 }
209
210 fn gpt_3_5_turbo(mut self) -> Self
212 where
213 Self: Sized,
214 {
215 *self.model() = ApiLLMModel::gpt_3_5_turbo();
216 self
217 }
218
219 fn o1_preview<T: Into<Option<bool>>>(mut self) -> Self
220 where
221 Self: Sized,
222 {
223 *self.model() = ApiLLMModel::gpt_3_5_turbo();
224 self
225 }
226}