kalosm_language_model/builder.rs
1use std::future::Future;
2
3use kalosm_model_types::ModelLoadingProgress;
4
5/// A builder that can create a model asynchronously.
6///
7/// # Example
8/// ```rust, no_run
9/// use kalosm::language::*;
10///
11/// #[tokio::main]
12/// async fn main() {
13/// let model = Llama::builder().start().await.unwrap();
14/// }
15/// ```
16pub trait ModelBuilder {
17 /// The model that this trait creates.
18 type Model;
19
20 /// An error that can occur when creating the model.
21 type Error: Send + Sync + 'static;
22
23 /// Start the model.
24 fn start(self) -> impl Future<Output = Result<Self::Model, Self::Error>>
25 where
26 Self: Sized,
27 {
28 async { self.start_with_loading_handler(|_| {}).await }
29 }
30
31 /// Start the model with a loading handler.
32 fn start_with_loading_handler(
33 self,
34 handler: impl FnMut(ModelLoadingProgress) + Send + Sync + 'static,
35 ) -> impl Future<Output = Result<Self::Model, Self::Error>>
36 where
37 Self: Sized;
38
39 /// Check if the model will need to be downloaded before use (default: false)
40 fn requires_download(&self) -> bool {
41 false
42 }
43}