|
| 1 | +use anyhow::Result; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | + |
| 4 | +/// Trait for embedding backends. Any model that can embed text implements this. |
| 5 | +pub trait ModelBackend { |
| 6 | + fn embed_batch(&mut self, texts: &[&str]) -> Result<Vec<Vec<f32>>>; |
| 7 | + fn embed_one(&mut self, text: &str) -> Result<Vec<f32>>; |
| 8 | + fn token_count(&self, text: &str) -> usize; |
| 9 | + fn dim(&self) -> usize; |
| 10 | + fn name(&self) -> &str; |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 14 | +pub enum ModelFormat { |
| 15 | + Onnx, |
| 16 | + Gguf, |
| 17 | + File, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Debug, Clone)] |
| 21 | +pub struct ModelSpec { |
| 22 | + pub format: ModelFormat, |
| 23 | + pub name: String, |
| 24 | + pub path: String, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 28 | +pub struct ModelRegistryEntry { |
| 29 | + pub name: String, |
| 30 | + pub format: ModelFormat, |
| 31 | + pub url: String, |
| 32 | + pub sha256: String, |
| 33 | + pub dim: usize, |
| 34 | + pub description: String, |
| 35 | +} |
| 36 | + |
| 37 | +pub struct ModelRegistry { |
| 38 | + pub entries: Vec<ModelRegistryEntry>, |
| 39 | +} |
| 40 | + |
| 41 | +impl Default for ModelRegistry { |
| 42 | + fn default() -> Self { |
| 43 | + Self { |
| 44 | + entries: vec![ModelRegistryEntry { |
| 45 | + name: "onnx:all-MiniLM-L6-v2".to_string(), |
| 46 | + format: ModelFormat::Onnx, |
| 47 | + url: "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/onnx/model.onnx".to_string(), |
| 48 | + sha256: "6fd5d72fe4589f189f8ebc006442dbb529bb7ce38f8082112682524616046452".to_string(), |
| 49 | + dim: 384, |
| 50 | + description: "Lightweight general-purpose sentence embeddings".to_string(), |
| 51 | + }], |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl ModelRegistry { |
| 57 | + pub fn get(&self, name: &str) -> Option<&ModelRegistryEntry> { |
| 58 | + self.entries.iter().find(|e| e.name == name) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +pub fn parse_model_spec(spec: &str) -> ModelSpec { |
| 63 | + if let Some(path) = spec.strip_prefix("file:") { |
| 64 | + return ModelSpec { |
| 65 | + format: ModelFormat::File, |
| 66 | + name: spec.to_string(), |
| 67 | + path: path.to_string(), |
| 68 | + }; |
| 69 | + } |
| 70 | + if let Some((format_str, name)) = spec.split_once(':') { |
| 71 | + let format = match format_str { |
| 72 | + "onnx" => ModelFormat::Onnx, |
| 73 | + "gguf" => ModelFormat::Gguf, |
| 74 | + _ => ModelFormat::Onnx, |
| 75 | + }; |
| 76 | + ModelSpec { |
| 77 | + format, |
| 78 | + name: name.to_string(), |
| 79 | + path: String::new(), |
| 80 | + } |
| 81 | + } else { |
| 82 | + ModelSpec { |
| 83 | + format: ModelFormat::Onnx, |
| 84 | + name: spec.to_string(), |
| 85 | + path: String::new(), |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#[cfg(test)] |
| 91 | +mod tests { |
| 92 | + use super::*; |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn test_model_registry_default() { |
| 96 | + let registry = ModelRegistry::default(); |
| 97 | + assert_eq!(registry.entries.len(), 1); |
| 98 | + let entry = ®istry.entries[0]; |
| 99 | + assert_eq!(entry.name, "onnx:all-MiniLM-L6-v2"); |
| 100 | + assert_eq!(entry.dim, 384); |
| 101 | + assert_eq!(entry.format, ModelFormat::Onnx); |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn test_parse_model_spec_onnx() { |
| 106 | + let spec = parse_model_spec("onnx:all-MiniLM-L6-v2"); |
| 107 | + assert_eq!(spec.format, ModelFormat::Onnx); |
| 108 | + assert_eq!(spec.name, "all-MiniLM-L6-v2"); |
| 109 | + assert!(spec.path.is_empty()); |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn test_parse_model_spec_file() { |
| 114 | + let spec = parse_model_spec("file:/path/to/model.onnx"); |
| 115 | + assert_eq!(spec.format, ModelFormat::File); |
| 116 | + assert_eq!(spec.name, "file:/path/to/model.onnx"); |
| 117 | + assert_eq!(spec.path, "/path/to/model.onnx"); |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn test_parse_model_spec_bare() { |
| 122 | + let spec = parse_model_spec("my-custom-model"); |
| 123 | + assert_eq!(spec.format, ModelFormat::Onnx); |
| 124 | + assert_eq!(spec.name, "my-custom-model"); |
| 125 | + assert!(spec.path.is_empty()); |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn test_registry_get_existing() { |
| 130 | + let registry = ModelRegistry::default(); |
| 131 | + let entry = registry.get("onnx:all-MiniLM-L6-v2"); |
| 132 | + assert!(entry.is_some()); |
| 133 | + assert_eq!(entry.unwrap().dim, 384); |
| 134 | + } |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn test_registry_get_missing() { |
| 138 | + let registry = ModelRegistry::default(); |
| 139 | + assert!(registry.get("nonexistent-model").is_none()); |
| 140 | + } |
| 141 | +} |
0 commit comments