Skip to main content

autoagents_llamacpp/
models.rs

1//! Model source definitions for llama.cpp backend.
2
3use serde::{Deserialize, Serialize};
4
5/// Source type for loading models.
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7pub enum ModelSource {
8    /// Local GGUF file path.
9    Gguf {
10        /// Path to a GGUF model file.
11        model_path: String,
12    },
13    /// HuggingFace repository ID.
14    HuggingFace {
15        /// HuggingFace repo ID (e.g. "org/model").
16        repo_id: String,
17        /// Optional GGUF filename override.
18        filename: Option<String>,
19        /// Optional MTMD mmproj filename override.
20        mmproj_filename: Option<String>,
21    },
22}
23
24impl ModelSource {
25    /// Convenience constructor for a local GGUF model.
26    pub fn gguf(model_path: impl Into<String>) -> Self {
27        Self::Gguf {
28            model_path: model_path.into(),
29        }
30    }
31
32    /// Convenience constructor for a HuggingFace model repository.
33    pub fn huggingface(repo_id: impl Into<String>) -> Self {
34        Self::HuggingFace {
35            repo_id: repo_id.into(),
36            filename: None,
37            mmproj_filename: None,
38        }
39    }
40
41    /// Convenience constructor for a HuggingFace repo with a GGUF filename override.
42    pub fn huggingface_with_filename(
43        repo_id: impl Into<String>,
44        filename: impl Into<String>,
45    ) -> Self {
46        Self::HuggingFace {
47            repo_id: repo_id.into(),
48            filename: Some(filename.into()),
49            mmproj_filename: None,
50        }
51    }
52
53    /// Convenience constructor for a HuggingFace repo with a GGUF + mmproj filename override.
54    pub fn huggingface_with_mmproj(
55        repo_id: impl Into<String>,
56        filename: impl Into<String>,
57        mmproj_filename: impl Into<String>,
58    ) -> Self {
59        Self::HuggingFace {
60            repo_id: repo_id.into(),
61            filename: Some(filename.into()),
62            mmproj_filename: Some(mmproj_filename.into()),
63        }
64    }
65
66    /// Return the model path for this source.
67    pub fn model_path(&self) -> Option<&str> {
68        match self {
69            ModelSource::Gguf { model_path } => Some(model_path),
70            ModelSource::HuggingFace { .. } => None,
71        }
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn test_model_source_path() {
81        let source = ModelSource::gguf("test.gguf");
82        assert_eq!(source.model_path(), Some("test.gguf"));
83    }
84
85    #[test]
86    fn test_model_source_hf() {
87        let source = ModelSource::huggingface("org/model");
88        assert!(source.model_path().is_none());
89        assert_eq!(
90            source,
91            ModelSource::HuggingFace {
92                repo_id: "org/model".to_string(),
93                filename: None,
94                mmproj_filename: None,
95            }
96        );
97    }
98}