aichat 0.30.0

All-in-one LLM CLI Tool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use super::{
    list_all_models, list_client_names,
    message::{Message, MessageContent, MessageContentPart},
    ApiPatch, MessageContentToolCalls, RequestPatch,
};

use crate::config::Config;
use crate::utils::{estimate_token_length, strip_think_tag};

use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt::Display;

const PER_MESSAGES_TOKENS: usize = 5;
const BASIS_TOKENS: usize = 2;

#[derive(Debug, Clone)]
pub struct Model {
    client_name: String,
    data: ModelData,
}

impl Default for Model {
    fn default() -> Self {
        Model::new("", "")
    }
}

impl Model {
    pub fn new(client_name: &str, name: &str) -> Self {
        Self {
            client_name: client_name.into(),
            data: ModelData::new(name),
        }
    }

    pub fn from_config(client_name: &str, models: &[ModelData]) -> Vec<Self> {
        models
            .iter()
            .map(|v| Model {
                client_name: client_name.to_string(),
                data: v.clone(),
            })
            .collect()
    }

    pub fn retrieve_model(config: &Config, model_id: &str, model_type: ModelType) -> Result<Self> {
        let models = list_all_models(config);
        let (client_name, model_name) = match model_id.split_once(':') {
            Some((client_name, model_name)) => {
                if model_name.is_empty() {
                    (client_name, None)
                } else {
                    (client_name, Some(model_name))
                }
            }
            None => (model_id, None),
        };
        match model_name {
            Some(model_name) => {
                if let Some(model) = models.iter().find(|v| v.id() == model_id) {
                    if model.model_type() == model_type {
                        return Ok((*model).clone());
                    } else {
                        bail!("Model '{model_id}' is not a {model_type} model")
                    }
                }
                if list_client_names(config)
                    .into_iter()
                    .any(|v| *v == client_name)
                    && model_type.can_create_from_name()
                {
                    let mut new_model = Self::new(client_name, model_name);
                    new_model.data.model_type = model_type.to_string();
                    return Ok(new_model);
                }
            }
            None => {
                if let Some(found) = models
                    .iter()
                    .find(|v| v.client_name == client_name && v.model_type() == model_type)
                {
                    return Ok((*found).clone());
                }
            }
        };
        bail!("Unknown {model_type} model '{model_id}'")
    }

    pub fn id(&self) -> String {
        if self.data.name.is_empty() {
            self.client_name.to_string()
        } else {
            format!("{}:{}", self.client_name, self.data.name)
        }
    }

    pub fn client_name(&self) -> &str {
        &self.client_name
    }

    pub fn name(&self) -> &str {
        &self.data.name
    }

    pub fn real_name(&self) -> &str {
        self.data.real_name.as_deref().unwrap_or(&self.data.name)
    }

    pub fn model_type(&self) -> ModelType {
        if self.data.model_type.starts_with("embed") {
            ModelType::Embedding
        } else if self.data.model_type.starts_with("rerank") {
            ModelType::Reranker
        } else {
            ModelType::Chat
        }
    }

    pub fn data(&self) -> &ModelData {
        &self.data
    }

    pub fn data_mut(&mut self) -> &mut ModelData {
        &mut self.data
    }

    pub fn description(&self) -> String {
        match self.model_type() {
            ModelType::Chat => {
                let ModelData {
                    max_input_tokens,
                    max_output_tokens,
                    input_price,
                    output_price,
                    supports_vision,
                    supports_function_calling,
                    ..
                } = &self.data;
                let max_input_tokens = stringify_option_value(max_input_tokens);
                let max_output_tokens = stringify_option_value(max_output_tokens);
                let input_price = stringify_option_value(input_price);
                let output_price = stringify_option_value(output_price);
                let mut capabilities = vec![];
                if *supports_vision {
                    capabilities.push('👁');
                };
                if *supports_function_calling {
                    capabilities.push('');
                };
                let capabilities: String = capabilities
                    .into_iter()
                    .map(|v| format!("{v} "))
                    .collect::<Vec<String>>()
                    .join("");
                format!(
                    "{max_input_tokens:>8} / {max_output_tokens:>8}  |  {input_price:>6} / {output_price:>6}  {capabilities:>6}"
                )
            }
            ModelType::Embedding => {
                let ModelData {
                    input_price,
                    max_tokens_per_chunk,
                    max_batch_size,
                    ..
                } = &self.data;
                let max_tokens = stringify_option_value(max_tokens_per_chunk);
                let max_batch = stringify_option_value(max_batch_size);
                let price = stringify_option_value(input_price);
                format!("max-tokens:{max_tokens};max-batch:{max_batch};price:{price}")
            }
            ModelType::Reranker => String::new(),
        }
    }

    pub fn patch(&self) -> Option<&Value> {
        self.data.patch.as_ref()
    }

    pub fn max_input_tokens(&self) -> Option<usize> {
        self.data.max_input_tokens
    }

    pub fn max_output_tokens(&self) -> Option<isize> {
        self.data.max_output_tokens
    }

    pub fn no_stream(&self) -> bool {
        self.data.no_stream
    }

    pub fn no_system_message(&self) -> bool {
        self.data.no_system_message
    }

    pub fn system_prompt_prefix(&self) -> Option<&str> {
        self.data.system_prompt_prefix.as_deref()
    }

    pub fn max_tokens_per_chunk(&self) -> Option<usize> {
        self.data.max_tokens_per_chunk
    }

    pub fn default_chunk_size(&self) -> usize {
        self.data.default_chunk_size.unwrap_or(1000)
    }

    pub fn max_batch_size(&self) -> Option<usize> {
        self.data.max_batch_size
    }

    pub fn max_tokens_param(&self) -> Option<isize> {
        if self.data.require_max_tokens {
            self.data.max_output_tokens
        } else {
            None
        }
    }

    pub fn set_max_tokens(
        &mut self,
        max_output_tokens: Option<isize>,
        require_max_tokens: bool,
    ) -> &mut Self {
        match max_output_tokens {
            None | Some(0) => self.data.max_output_tokens = None,
            _ => self.data.max_output_tokens = max_output_tokens,
        }
        self.data.require_max_tokens = require_max_tokens;
        self
    }

    pub fn messages_tokens(&self, messages: &[Message]) -> usize {
        let messages_len = messages.len();
        messages
            .iter()
            .enumerate()
            .map(|(i, v)| match &v.content {
                MessageContent::Text(text) => {
                    if v.role.is_assistant() && i != messages_len - 1 {
                        estimate_token_length(&strip_think_tag(text))
                    } else {
                        estimate_token_length(text)
                    }
                }
                MessageContent::Array(list) => list
                    .iter()
                    .map(|v| match v {
                        MessageContentPart::Text { text } => estimate_token_length(text),
                        MessageContentPart::ImageUrl { .. } => 0,
                    })
                    .sum(),
                MessageContent::ToolCalls(MessageContentToolCalls {
                    tool_results, text, ..
                }) => {
                    estimate_token_length(text)
                        + tool_results
                            .iter()
                            .map(|v| {
                                serde_json::to_string(v)
                                    .map(|v| estimate_token_length(&v))
                                    .unwrap_or_default()
                            })
                            .sum::<usize>()
                }
            })
            .sum()
    }

    pub fn total_tokens(&self, messages: &[Message]) -> usize {
        if messages.is_empty() {
            return 0;
        }
        let num_messages = messages.len();
        let message_tokens = self.messages_tokens(messages);
        if messages[num_messages - 1].role.is_user() {
            num_messages * PER_MESSAGES_TOKENS + message_tokens
        } else {
            (num_messages - 1) * PER_MESSAGES_TOKENS + message_tokens
        }
    }

    pub fn guard_max_input_tokens(&self, messages: &[Message]) -> Result<()> {
        let total_tokens = self.total_tokens(messages) + BASIS_TOKENS;
        if let Some(max_input_tokens) = self.data.max_input_tokens {
            if total_tokens >= max_input_tokens {
                bail!("Exceed max_input_tokens limit")
            }
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ModelData {
    pub name: String,
    #[serde(default = "default_model_type", rename = "type")]
    pub model_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub real_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_input_tokens: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input_price: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output_price: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub patch: Option<Value>,

    // chat-only properties
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_output_tokens: Option<isize>,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub require_max_tokens: bool,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub supports_vision: bool,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub supports_function_calling: bool,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    no_stream: bool,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    no_system_message: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    system_prompt_prefix: Option<String>,

    // embedding-only properties
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens_per_chunk: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_chunk_size: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_batch_size: Option<usize>,
}

impl ModelData {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            model_type: default_model_type(),
            ..Default::default()
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderModels {
    pub provider: String,
    pub models: Vec<ModelData>,
}

fn default_model_type() -> String {
    "chat".into()
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ModelType {
    Chat,
    Embedding,
    Reranker,
}

impl Display for ModelType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ModelType::Chat => write!(f, "chat"),
            ModelType::Embedding => write!(f, "embedding"),
            ModelType::Reranker => write!(f, "reranker"),
        }
    }
}

impl ModelType {
    pub fn can_create_from_name(self) -> bool {
        match self {
            ModelType::Chat => true,
            ModelType::Embedding => false,
            ModelType::Reranker => true,
        }
    }

    pub fn api_name(self) -> &'static str {
        match self {
            ModelType::Chat => "chat_completions",
            ModelType::Embedding => "embeddings",
            ModelType::Reranker => "rerank",
        }
    }

    pub fn extract_patch(self, patch: &RequestPatch) -> Option<&ApiPatch> {
        match self {
            ModelType::Chat => patch.chat_completions.as_ref(),
            ModelType::Embedding => patch.embeddings.as_ref(),
            ModelType::Reranker => patch.rerank.as_ref(),
        }
    }
}

fn stringify_option_value<T>(value: &Option<T>) -> String
where
    T: std::fmt::Display,
{
    match value {
        Some(value) => value.to_string(),
        None => "-".to_string(),
    }
}