kalosm-learning 0.4.0

A simplified machine learning library for building off of pretrained models.
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
use candle_core::Device;
use kalosm_language_model::{Embedder, EmbedderExt, Embedding};

use crate::{
    Class, ClassificationDataset, ClassificationDatasetBuilder, Classifier, ClassifierConfig,
    ClassifierOutput,
};

use super::ClassifierProgress;

/// A builder for [`TextClassifier`].
///
/// # Example
/// ```rust, no_run
/// # use kalosm_learning::*;
/// # use rbert::*;
/// # use std::collections::HashMap;
/// # #[derive(Debug, Copy, Clone, PartialEq, Eq, Class)]
/// # enum MyClass {
/// #     Person,
/// #     Thing,
/// # }
/// # #[tokio::main]
/// # async fn main() -> anyhow::Result<()> {
/// // Create a dataset for the classifier
/// let bert = Bert::new().await?;
/// let mut dataset = TextClassifierDatasetBuilder::<MyClass, _>::new(&bert);
/// for question in ["What is the author's name?", "What is the author's age?"] {
///     dataset.add(question, MyClass::Person).await?;
/// }
/// for question in [
///     "What is the capital of France?",
///     "What is the capital of England?",
/// ] {
///     dataset.add(question, MyClass::Thing).await?;
/// }
/// # Ok::<(), anyhow::Error>(())
/// # }
/// ```
pub struct TextClassifierDatasetBuilder<'a, T: Class, E: Embedder> {
    dataset: ClassificationDatasetBuilder<T>,
    embedder: &'a E,
}

impl<'a, T: Class, E: Embedder> TextClassifierDatasetBuilder<'a, T, E> {
    /// Creates a new [`TextClassifierDatasetBuilder`].
    pub fn new(embedder: &'a E) -> Self {
        Self {
            dataset: ClassificationDatasetBuilder::new(),
            embedder,
        }
    }

    /// Adds a new example to the dataset.
    pub async fn add(&mut self, text: impl ToString, class: T) -> Result<(), E::Error> {
        let embedding = self.embedder.embed(text).await?;
        self.dataset
            .add(embedding.vector().to_vec().into_boxed_slice(), class);
        Ok(())
    }

    /// Add many examples to the dataset at once. This may be faster than adding each example individually depending on the embedding model.
    ///
    /// # Example
    /// ```rust, no_run
    /// # use kalosm_learning::*;
    /// # use rbert::*;
    /// # use std::collections::HashMap;
    /// # #[derive(Debug, Copy, Clone, PartialEq, Eq, Class)]
    /// # enum MyClass {
    /// #     Person,
    /// #     Thing,
    /// # }
    /// # #[tokio::main]
    /// # async fn main() -> anyhow::Result<()> {
    /// // Create a dataset for the classifier
    /// let bert = Bert::new().await?;
    /// let mut dataset = TextClassifierDatasetBuilder::<MyClass, _>::new(&bert);
    /// dataset
    ///     .extend(
    ///         ["What is the author's name?", "What is the author's age?"]
    ///             .into_iter()
    ///             .map(|q| (q, MyClass::Person)),
    ///     )
    ///     .await?;
    /// dataset
    ///     .extend(
    ///         [
    ///             "What is the capital of France?",
    ///             "What is the capital of England?",
    ///         ]
    ///         .into_iter()
    ///         .map(|q| (q, MyClass::Thing)),
    ///     )
    ///     .await?;
    /// # Ok::<(), anyhow::Error>(())
    /// # }
    /// ```
    pub async fn extend(
        &mut self,
        examples: impl IntoIterator<Item = (impl ToString, T)>,
    ) -> Result<(), E::Error> {
        let (texts, classes): (Vec<_>, Vec<_>) = examples.into_iter().unzip();
        let embeddings = self.embedder.embed_batch(texts).await?;
        for (embedding, class) in embeddings.into_iter().zip(classes) {
            self.dataset
                .add(embedding.vector().to_vec().into_boxed_slice(), class);
        }
        Ok(())
    }

    /// Builds the dataset.
    pub fn build(self, device: &Device) -> candle_core::Result<ClassificationDataset> {
        self.dataset.build(device)
    }
}

/// A text classifier.
///
/// # Example
///
/// ```rust, no_run
/// use candle_core::Device;
/// use kalosm_language_model::{Embedder, EmbedderExt};
/// use kalosm_learning::{
///     Class, Classifier, ClassifierConfig, TextClassifier, TextClassifierDatasetBuilder,
/// };
/// use rbert::Bert;
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
///     #[derive(Debug, Copy, Clone, PartialEq, Eq, Class)]
///     enum MyClass {
///         Person,
///         Thing,
///     }
///
///     let mut bert = Bert::builder().build().await?;
///
///     let dev = Device::cuda_if_available(0)?;
///     let person_questions = vec![
///         "What is the author's name?",
///         "What is the author's age?",
///         "Who is the queen of England?",
///         "Who is the president of the United States?",
///         "Who is the president of France?",
///         "Tell me about the CEO of Apple.",
///         "Who is the CEO of Google?",
///         "Who is the CEO of Microsoft?",
///         "What person invented the light bulb?",
///         "What person invented the telephone?",
///         "What is the name of the person who invented the light bulb?",
///         "Who wrote the book 'The Lord of the Rings'?",
///         "Who wrote the book 'The Hobbit'?",
///         "How old is the author of the book 'The Lord of the Rings'?",
///         "How old is the author of the book 'The Hobbit'?",
///         "Who is the best soccer player in the world?",
///         "Who is the best basketball player in the world?",
///         "Who is the best tennis player in the world?",
///         "Who is the best soccer player in the world right now?",
///         "Who is the leader of the United States?",
///         "Who is the leader of France?",
///         "What is the name of the leader of the United States?",
///         "What is the name of the leader of France?",
///     ];
///     let thing_sentences = vec![
///         "What is the capital of France?",
///         "What is the capital of England?",
///         "What is the name of the biggest city in the world?",
///         "What tool do you use to cut a tree?",
///         "What tool do you use to cut a piece of paper?",
///         "What is a good book to read?",
///         "What is a good movie to watch?",
///         "What is a good song to listen to?",
///         "What is the best tool to use to create a website?",
///         "What is the best tool to use to create a mobile app?",
///         "How long does it take to fly from Paris to New York?",
///         "How do you make a cake?",
///         "How do you make a pizza?",
///         "How can you make a website?",
///         "What is the best way to learn a new language?",
///         "What is the best way to learn a new programming language?",
///         "What is a framework?",
///         "What is a library?",
///         "What is a good way to learn a new language?",
///         "What is a good way to learn a new programming language?",
///         "What is the city with the most people in the world?",
///         "What is the most spoken language in the world?",
///         "What is the most spoken language in the United States?",
///     ];
///
///     let mut dataset = TextClassifierDatasetBuilder::<MyClass, _>::new(&mut bert);
///
///     for question in &person_questions {
///         dataset.add(question, MyClass::Person).await?;
///     }
///
///     for sentence in &thing_sentences {
///         dataset.add(sentence, MyClass::Thing).await?;
///     }
///
///     let dataset = dataset.build(&dev)?;
///
///     let mut classifier;
///     let layers = vec![5, 8, 5];
///
///     loop {
///         classifier = TextClassifier::<MyClass>::new(Classifier::new(
///             &dev,
///             ClassifierConfig::new().layers_dims(layers.clone()),
///         )?);
///         if let Err(error) = classifier.train(&dataset, 100, 0.05, 3, |_| {}) {
///             println!("Error: {:?}", error);
///         } else {
///             break;
///         }
///         println!("Retrying...");
///     }
///
///     let config = classifier.config();
///     classifier.save("classifier.safetensors")?;
///     let classifier = Classifier::<MyClass>::load("classifier.safetensors", &dev, config)?;
///
///     let tests = [
///         "Who is the president of Russia?",
///         "What is the capital of Russia?",
///         "Who invented the TV?",
///         "What is the best way to learn a how to ride a bike?",
///     ];
///
///     for test in &tests {
///         let input = bert.embed(test).await?;
///         let class = classifier.run(input.vector())?;
///         println!();
///         println!("{test}");
///         println!("{:?} {:?}", &input.vector()[..5], class);
///     }
///
///     Ok(())
/// }
/// ```
pub struct TextClassifier<T: Class> {
    model: Classifier<T>,
}

impl<T: Class> TextClassifier<T> {
    /// Creates a new [`TextClassifier`].
    pub fn new(model: Classifier<T>) -> Self {
        Self { model }
    }

    /// Runs the classifier on the given input.
    pub fn run(&self, input: Embedding) -> candle_core::Result<ClassifierOutput<T>> {
        self.model.run(input.vector())
    }

    /// Trains the classifier on the given dataset.
    pub fn train(
        &self,
        dataset: &ClassificationDataset,
        epochs: usize,
        learning_rate: f64,
        batch_size: usize,
        progress: impl FnMut(ClassifierProgress),
    ) -> candle_core::Result<f32> {
        self.model
            .train(dataset, epochs, learning_rate, batch_size, progress)
    }

    /// Get the configuration of the classifier.
    pub fn config(&self) -> ClassifierConfig {
        self.model.config()
    }

    /// Saves the classifier to the given path.
    pub fn save<P: AsRef<std::path::Path>>(&self, path: P) -> candle_core::Result<()> {
        self.model.save(path)
    }

    /// Loads a classifier from the given path.
    pub fn load<P: AsRef<std::path::Path>>(
        path: P,
        device: &Device,
        config: ClassifierConfig,
    ) -> candle_core::Result<Self> {
        let model = Classifier::load(path, device, config)?;
        Ok(Self::new(model))
    }
}

#[cfg(test)]
#[tokio::test]
async fn simplified() -> Result<(), Box<dyn std::error::Error>> {
    use crate::{Class, Classifier, ClassifierConfig};
    use rbert::{Bert, BertSource};

    #[derive(Debug, Copy, Clone, PartialEq, Eq, Class)]
    enum MyClass {
        Person,
        Thing,
    }

    let bert = Bert::builder()
        .with_source(BertSource::snowflake_arctic_embed_extra_small())
        .build()
        .await?;

    let dev = kalosm_common::accelerated_device_if_available()?;
    let person_questions = [
        "What is the author's name?",
        "What is the author's age?",
        "Who is the queen of England?",
        "Who is the president of the United States?",
        "Who is the president of France?",
        "Tell me about the CEO of Apple.",
        "Who is the CEO of Google?",
        "Who is the CEO of Microsoft?",
        "What person invented the light bulb?",
        "What person invented the telephone?",
        "What is the name of the person who invented the light bulb?",
        "Who wrote the book 'The Lord of the Rings'?",
        "Who wrote the book 'The Hobbit'?",
        "How old is the author of the book 'The Lord of the Rings'?",
        "How old is the author of the book 'The Hobbit'?",
        "Who is the best soccer player in the world?",
        "Who is the best basketball player in the world?",
        "Who is the best tennis player in the world?",
        "Who is the best soccer player in the world right now?",
        "Who is the leader of the United States?",
        "Who is the leader of France?",
        "What is the name of the leader of the United States?",
        "What is the name of the leader of France?",
    ];
    let thing_sentences = [
        "What is the capital of France?",
        "What is the capital of England?",
        "What is the name of the biggest city in the world?",
        "What tool do you use to cut a tree?",
        "What tool do you use to cut a piece of paper?",
        "What is a good book to read?",
        "What is a good movie to watch?",
        "What is a good song to listen to?",
        "What is the best tool to use to create a website?",
        "What is the best tool to use to create a mobile app?",
        "How long does it take to fly from Paris to New York?",
        "How do you make a cake?",
        "How do you make a pizza?",
        "How can you make a website?",
        "What is the best way to learn a new language?",
        "What is the best way to learn a new programming language?",
        "What is a framework?",
        "What is a library?",
        "What is a good way to learn a new language?",
        "What is a good way to learn a new programming language?",
        "What is the city with the most people in the world?",
        "What is the most spoken language in the world?",
        "What is the most spoken language in the United States?",
    ];

    let mut dataset = TextClassifierDatasetBuilder::<MyClass, _>::new(&bert);

    for question in &person_questions {
        dataset.add(question, MyClass::Person).await?;
    }

    for sentence in &thing_sentences {
        dataset.add(sentence, MyClass::Thing).await?;
    }

    let dataset = dataset.build(&dev)?;

    let mut classifier;
    let layers = vec![5, 8, 5];

    loop {
        classifier = TextClassifier::<MyClass>::new(Classifier::new(
            &dev,
            ClassifierConfig::new().layers_dims(layers.clone()),
        )?);
        println!("Training...");
        if let Err(error) = classifier.train(&dataset, 100, 0.05, 100, |_| {}) {
            println!("Error: {:?}", error);
        } else {
            break;
        }
        println!("Retrying...");
    }

    let config = classifier.model.config();
    classifier.save("classifier.safetensors")?;
    let classifier = Classifier::<MyClass>::load("classifier.safetensors", &dev, config)?;

    let tests = [
        "Who is the president of Russia?",
        "What is the capital of Russia?",
        "Who invented the TV?",
        "What is the best way to learn a how to ride a bike?",
    ];

    for test in &tests {
        let input = bert.embed(test).await?;
        let class = classifier.run(input.vector())?;
        println!();
        println!("{test}");
        println!("{:?} {:?}", &input.vector()[..5], class);
    }

    Ok(())
}