llm_interface 0.0.3

llm_interface: The backend for the llm_client crate
Documentation
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// Internal imports
use super::*;
use llm_models::tokenizer::LlmTokenizer;
use std::{collections::HashMap, sync::Arc};

#[derive(Clone, Default)]
pub struct LogitBias {
    pub base_logit_bias: Option<HashMap<u32, f32>>,
    pub built_llama_cpp_bias: LlamaCppLogitBias,
    pub built_openai_bias: OpenAiLogitBias,
    from_token_ids: FromTokenIds,
    from_chars: FromChars,
    from_words: FromWords,
    from_texts: FromTexts,
}

impl LogitBias {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn add_token_id(&mut self, token_id: u32, bias: f32) -> &mut Self {
        self.from_token_ids.add_token_id(token_id, bias);
        self.clear_built();
        self
    }

    pub fn add_token_ids(&mut self, logit_bias: HashMap<u32, f32>) -> &mut Self {
        self.from_token_ids.add_token_ids(logit_bias);
        self.clear_built();
        self
    }

    pub fn add_from_char(&mut self, char: char, bias: f32) -> &mut Self {
        self.from_chars.add_char(char, bias);
        self.clear_built();
        self
    }

    pub fn add_from_word(&mut self, word: &str, bias: f32) -> &mut Self {
        self.from_words.add_word(word, bias);
        self.clear_built();
        self
    }

    pub fn add_from_text(&mut self, text: &str, bias: f32) -> &mut Self {
        self.from_texts.add_text(text, bias);
        self.clear_built();
        self
    }

    pub fn clear_logit_bias(&mut self) -> &mut Self {
        self.from_token_ids.clear();
        self.from_chars.clear();
        self.from_words.clear();
        self.from_texts.clear();
        self.clear_built();
        self
    }

    pub(crate) fn build_llama(&mut self, tokenizer: &Arc<LlmTokenizer>) -> crate::Result<()> {
        if !self.built_llama_cpp_bias.is_none() {
            return Ok(());
        }
        if self.base_logit_bias.is_none() {
            self.build_base(tokenizer)?;
        }
        if let Some(base_logit_bias) = &self.base_logit_bias {
            self.built_llama_cpp_bias.build(base_logit_bias);
        }
        Ok(())
    }

    pub(crate) fn build_openai(&mut self, tokenizer: &Arc<LlmTokenizer>) -> crate::Result<()> {
        if !self.built_openai_bias.is_none() {
            return Ok(());
        }
        if self.base_logit_bias.is_none() {
            self.build_base(tokenizer)?;
        }
        if let Some(base_logit_bias) = &self.base_logit_bias {
            self.built_openai_bias.build(base_logit_bias);
        }
        Ok(())
    }

    pub(crate) fn get_openai(&self) -> Option<HashMap<String, serde_json::Value>> {
        self.built_openai_bias.get()
    }

    pub(crate) fn get_llama_cpp(&self) -> Option<Vec<Vec<serde_json::Value>>> {
        self.built_llama_cpp_bias.get()
    }

    fn build_base(&mut self, tokenizer: &Arc<LlmTokenizer>) -> crate::Result<()> {
        if self.from_token_ids.is_none()
            && self.from_chars.is_none()
            && self.from_words.is_none()
            && self.from_texts.is_none()
        {
            return Ok(());
        }
        let validated_logit_bias = self.from_token_ids.get(tokenizer)?;
        self.from_token_ids.clear();

        let validated_logit_bias = Self::merge_logit_biases(vec![
            &validated_logit_bias,
            &self.from_chars.get(tokenizer)?,
        ]);
        self.from_chars.clear();

        let validated_logit_bias = Self::merge_logit_biases(vec![
            &validated_logit_bias,
            &self.from_words.get(tokenizer)?,
        ]);
        self.from_words.clear();

        let validated_logit_bias = Self::merge_logit_biases(vec![
            &validated_logit_bias,
            &self.from_texts.get(tokenizer)?,
        ]);
        self.from_texts.clear();

        if !validated_logit_bias.is_empty() {
            Self::validate_logit_bias_values(&validated_logit_bias)?;
            self.base_logit_bias = Some(validated_logit_bias);
        }
        Ok(())
    }

    fn clear_built(&mut self) -> &mut Self {
        self.base_logit_bias = None;
        self.built_llama_cpp_bias.clear();
        self.built_openai_bias.clear();
        self
    }

    /// Validates the logit bias values by checking if they are within the range of -100.0 to 100.0.
    ///
    /// # Arguments
    ///
    /// * `logit_bias` - A reference to the `HashMap` containing the logit biases with token IDs as keys and bias values as values.
    ///
    /// # Returns
    ///
    /// Returns `Result<(), anyhow::Error>` indicating success or an error if any of the bias values are out of range.
    fn validate_logit_bias_values(logit_bias: &HashMap<u32, f32>) -> crate::Result<()> {
        for value in logit_bias.values() {
            if *value > 100.0 || *value < -100.0 {
                return Err(crate::anyhow!(
                    "logit_bias value must be between -100.0 and 100.0. Given value: {}",
                    value
                ));
            }
        }
        Ok(())
    }

    /// Merges multiple logit biases into a single `HashMap` of token IDs and bias values.
    ///
    /// # Arguments
    ///
    /// * `logit_biases` - A vector of references to `HashMap`s containing logit biases with token IDs as keys and bias values as values.
    ///
    /// # Returns
    ///
    /// Returns a `HashMap<u32, f32>` containing the merged logit biases.
    fn merge_logit_biases(logit_biases: Vec<&HashMap<u32, f32>>) -> HashMap<u32, f32> {
        let mut merged_logit_bias: HashMap<u32, f32> = HashMap::new();
        for logit_bias in logit_biases {
            for (token_id, bias) in logit_bias {
                merged_logit_bias.insert(*token_id, *bias);
            }
        }
        merged_logit_bias
    }
}

#[derive(Clone, Default)]
struct FromTokenIds {
    pub token_ids: Option<HashMap<u32, f32>>,
}

impl FromTokenIds {
    fn is_none(&self) -> bool {
        self.token_ids.is_none()
    }

    fn clear(&mut self) {
        self.token_ids = None;
    }

    fn get(&self, tokenizer: &Arc<LlmTokenizer>) -> crate::Result<HashMap<u32, f32>> {
        if let Some(token_ids) = &self.token_ids {
            for token_id in token_ids.keys() {
                tokenizer.try_from_single_token_id(*token_id)?;
            }
            Ok(token_ids.clone())
        } else {
            Ok(HashMap::new())
        }
    }

    fn add_token_id(&mut self, token_id: u32, bias: f32) {
        self.token_ids
            .get_or_insert_with(HashMap::new)
            .entry(token_id)
            .or_insert(bias);
    }

    fn add_token_ids(&mut self, logit_bias: HashMap<u32, f32>) {
        self.token_ids
            .get_or_insert_with(HashMap::new)
            .extend(logit_bias);
    }
}

#[derive(Clone, Default)]
struct FromChars {
    pub chars: Option<HashMap<char, f32>>,
}

impl FromChars {
    fn is_none(&self) -> bool {
        self.chars.is_none()
    }

    fn clear(&mut self) {
        self.chars = None;
    }

    fn get(&self, tokenizer: &Arc<LlmTokenizer>) -> crate::Result<HashMap<u32, f32>> {
        if let Some(chars) = &self.chars {
            let mut token_logit_bias: HashMap<u32, f32> = HashMap::new();
            for (char, bias) in chars {
                let token_id = tokenizer.try_into_single_token(&char.to_string())?;
                token_logit_bias.insert(token_id, *bias);
            }
            Ok(token_logit_bias)
        } else {
            Ok(HashMap::new())
        }
    }

    fn add_char(&mut self, char: char, bias: f32) {
        self.chars
            .get_or_insert_with(HashMap::new)
            .entry(char)
            .or_insert(bias);
    }
}

#[derive(Clone, Default)]
struct FromWords {
    pub words: Option<HashMap<String, f32>>,
}

impl FromWords {
    fn is_none(&self) -> bool {
        self.words.is_none()
    }

    fn clear(&mut self) {
        self.words = None;
    }

    fn get(&self, tokenizer: &Arc<LlmTokenizer>) -> crate::Result<HashMap<u32, f32>> {
        if let Some(words) = &self.words {
            let mut token_logit_bias: HashMap<u32, f32> = HashMap::new();
            for (word_maybe, bias) in words {
                let mut words_maybe: Vec<String> = word_maybe
                    .trim()
                    .split_ascii_whitespace()
                    .map(|s| s.trim().to_string())
                    .collect();
                let word = if words_maybe.is_empty() {
                    return Err(crate::anyhow!(
                        "logit_bias contains an empty word. Given word: {}",
                        word_maybe
                    ));
                } else if words_maybe.len() > 1 {
                    return Err(crate::anyhow!(
                        "logit_bias contains a word seperated by whitespace. Given word: {}",
                        word_maybe
                    ));
                } else {
                    words_maybe.remove(0)
                };
                let token_ids = tokenizer.tokenize(&word);
                for id in token_ids {
                    if id == tokenizer.white_space_token_id {
                        panic!(
                            "logit_bias contains a whitespace token. Given word: {}",
                            word
                        )
                    }
                    token_logit_bias.insert(id, *bias);
                }
            }
            Ok(token_logit_bias)
        } else {
            Ok(HashMap::new())
        }
    }

    fn add_word(&mut self, word: &str, bias: f32) {
        self.words
            .get_or_insert_with(HashMap::new)
            .entry(word.to_owned())
            .or_insert(bias);
    }
}

#[derive(Clone, Default)]
struct FromTexts {
    pub texts: Option<HashMap<String, f32>>,
}

impl FromTexts {
    fn is_none(&self) -> bool {
        self.texts.is_none()
    }

    fn clear(&mut self) {
        self.texts = None;
    }

    fn get(&self, tokenizer: &Arc<LlmTokenizer>) -> crate::Result<HashMap<u32, f32>> {
        if let Some(texts) = &self.texts {
            let mut token_logit_bias: HashMap<u32, f32> = HashMap::new();
            for (text, bias) in texts {
                let token_ids = tokenizer.tokenize(text);
                for id in token_ids {
                    if id == tokenizer.white_space_token_id {
                        continue;
                    }
                    token_logit_bias.insert(id, *bias);
                }
            }
            Ok(token_logit_bias)
        } else {
            Ok(HashMap::new())
        }
    }

    fn add_text(&mut self, text: &str, bias: f32) {
        self.texts
            .get_or_insert_with(HashMap::new)
            .entry(text.to_owned())
            .or_insert(bias);
    }
}

#[derive(Clone, Default)]
pub struct OpenAiLogitBias {
    pub built_logit_bias: Option<HashMap<String, serde_json::Value>>,
}

impl OpenAiLogitBias {
    fn is_none(&self) -> bool {
        self.built_logit_bias.is_none()
    }

    fn clear(&mut self) {
        self.built_logit_bias = None;
    }

    fn build(&mut self, logit_bias: &HashMap<u32, f32>) {
        let mut openai_logit_bias: HashMap<String, serde_json::Value> = HashMap::new();
        for (token_id, value) in logit_bias {
            openai_logit_bias.insert(
                token_id.to_string(),
                serde_json::Value::Number(serde_json::Number::from(value.ceil() as i32)),
            );
        }
    }

    fn get(&self) -> Option<HashMap<String, serde_json::Value>> {
        self.built_logit_bias.clone()
    }
}

#[derive(Clone, Default)]
pub struct LlamaCppLogitBias {
    pub built_logit_bias: Option<Vec<Vec<serde_json::Value>>>,
}

impl LlamaCppLogitBias {
    fn is_none(&self) -> bool {
        self.built_logit_bias.is_none()
    }

    fn clear(&mut self) {
        self.built_logit_bias = None;
    }

    fn build(&mut self, logit_bias: &HashMap<u32, f32>) {
        let mut llama_logit_bias: Vec<Vec<serde_json::Value>> = Vec::new();
        for (token_id, value) in logit_bias {
            llama_logit_bias.push(vec![
                serde_json::Value::Number(serde_json::Number::from(*token_id)),
                serde_json::Value::Number(
                    serde_json::Number::from_f64((*value).into()).expect("Invalid float value"),
                ),
            ]);
        }
        self.built_logit_bias = Some(llama_logit_bias);
    }

    fn get(&self) -> Option<Vec<Vec<serde_json::Value>>> {
        self.built_logit_bias.clone()
    }
}

impl std::fmt::Display for LogitBias {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f)?;
        writeln!(f, "LogitBias: {:?}", self.base_logit_bias)?;
        Ok(())
    }
}

pub trait LogitBiasTrait: RequestConfigTrait {
    fn lb_mut(&mut self) -> &mut Option<LogitBias>;

    fn logit_bias(&mut self) -> &mut LogitBias {
        if self.lb_mut().is_none() {
            *self.lb_mut() = Some(LogitBias::default());
        }
        self.lb_mut().as_mut().unwrap()
    }

    /// Adds a logit bias for a specific token ID. In the case you have your own tokenizer or other situations where you have token IDs.
    ///
    /// # Arguments
    ///
    /// * `token_id` - The token ID.
    /// * `bias` - The bias value.
    fn add_logit_bias_token_id(&mut self, token_id: u32, bias: f32) -> &mut Self {
        self.logit_bias().add_token_id(token_id, bias);
        self
    }

    /// Adds multiple logit biases for token IDs. In the case you have your own tokenizer or other situations where you have token IDs.
    ///
    /// # Arguments
    ///
    /// * `logit_bias` - A `HashMap` containing token IDs as keys and bias values as values.
    fn add_logit_bias_token_ids(&mut self, logit_bias: HashMap<u32, f32>) -> &mut Self {
        self.logit_bias().add_token_ids(logit_bias);
        self
    }

    /// Adds a logit bias for a specific character.
    /// Not very useful as it does not necessarily remove all instances of that character as the character may be part of other tokens.
    ///
    /// # Arguments
    ///
    /// * `char` - The character.
    /// * `bias` - The bias value.
    fn add_logit_bias_from_char(&mut self, char: char, bias: f32) -> &mut Self {
        self.logit_bias().add_from_char(char, bias);
        self
    }

    /// Adds a logit bias for a specific word. If a word is more than one token, it will be split into multiple tokens.
    /// Errors if the word is empty or contains whitespace.
    ///
    /// # Arguments
    ///
    /// * `word` - The word.
    /// * `bias` - The bias value.
    fn add_logit_bias_from_word(&mut self, word: &str, bias: f32) -> &mut Self {
        self.logit_bias().add_from_word(word, bias);
        self
    }

    /// Adds a logit bias for a specific text. Splits the text into tokens and applies the bias to each token. It does not add the logit bias value to the whitespace token.
    ///
    /// # Arguments
    ///
    /// * `text` - The text.
    /// * `bias` - The bias value.
    fn add_logit_bias_from_text(&mut self, text: &str, bias: f32) -> &mut Self {
        self.logit_bias().add_from_text(text, bias);
        self
    }

    /// Clearss the logit bias configuration. To reuse the request object for another request. Mostly for testing.
    fn clear_logit_bias(&mut self) -> &mut Self {
        self.logit_bias().clear_logit_bias();
        self
    }
}