tektra 0.2.0

A voice-interactive AI assistant with multimodal capabilities
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
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use tauri::{AppHandle, Manager};
use tracing::{error, info, warn};
use super::inference_backend::{InferenceConfig, BackendType};
use super::inference_manager::{InferenceManager};

// Gemma-3n model information
// E2B = 2 billion parameters (faster, good for desktop)
// E4B = 4 billion parameters (more capable, needs more resources)
const GEMMA_E2B_ID: &str = "google/gemma-3n-E2B-it";
const GEMMA_E2B_GGUF_ID: &str = "unsloth/gemma-3n-E2B-it-GGUF";
const GEMMA_E2B_FILE_Q4: &str = "gemma-3n-E2B-it-Q4_K_M.gguf"; // 2.79GB
const GEMMA_E2B_FILE_Q8: &str = "gemma-3n-E2B-it-Q8_0.gguf"; // 4.79GB

// For more capable version (if user has resources)
const GEMMA_E4B_GGUF_ID: &str = "unsloth/gemma-3n-E4B-it-GGUF";
const GEMMA_E4B_FILE_Q4: &str = "gemma-3n-E4B-it-Q4_K_M.gguf"; // ~3GB estimated

// MLX format models from mlx-community (for Apple Silicon)
const GEMMA2_2B_MLX_ID: &str = "mlx-community/gemma-2-2b-it-4bit";
const GEMMA2_2B_MLX_FP16_ID: &str = "mlx-community/gemma-2-2b-it-fp16";
const GEMMA2_9B_MLX_ID: &str = "mlx-community/gemma-2-9b-it-4bit";
const GEMMA2_9B_MLX_FP16_ID: &str = "mlx-community/gemma-2-9b-it-fp16";

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelLoadProgress {
    pub progress: u32,
    pub status: String,
    pub model_name: String,
}

#[derive(Debug, Clone)]
struct GemmaChatTemplate {
    // Gemma uses a specific chat template
    bos: String,
    start_turn: String,
    end_turn: String,
    user_role: String,
    model_role: String,
}

impl Default for GemmaChatTemplate {
    fn default() -> Self {
        Self {
            // Gemma-3n uses specific tokens as per Google's format
            bos: "<bos>".to_string(),
            start_turn: "<start_of_turn>".to_string(),
            end_turn: "<end_of_turn>".to_string(),
            user_role: "user".to_string(),
            model_role: "model".to_string(), // Gemma uses "model" not "assistant"
        }
    }
}

pub struct AIManager {
    app_handle: AppHandle,
    model_loaded: bool,
    model_path: Option<PathBuf>,
    chat_template: GemmaChatTemplate,
    selected_model: String,
    inference_manager: InferenceManager,
    backend_type: BackendType,
}

impl AIManager {
    pub fn new(app_handle: AppHandle) -> Result<Self> {
        // Default to Auto backend selection
        let backend_type = BackendType::Auto;
        let inference_manager = InferenceManager::new(backend_type)?;
        
        Ok(Self {
            app_handle,
            model_loaded: false,
            model_path: None,
            chat_template: GemmaChatTemplate::default(),
            selected_model: GEMMA_E2B_GGUF_ID.to_string(), // Default to 2B model
            inference_manager,
            backend_type,
        })
    }
    
    pub fn with_backend(app_handle: AppHandle, backend_type: BackendType) -> Result<Self> {
        let inference_manager = InferenceManager::new(backend_type)?;
        
        Ok(Self {
            app_handle,
            model_loaded: false,
            model_path: None,
            chat_template: GemmaChatTemplate::default(),
            selected_model: GEMMA_E2B_GGUF_ID.to_string(),
            inference_manager,
            backend_type,
        })
    }

    pub async fn load_model(&mut self) -> Result<()> {
        self.emit_progress(0, "Starting Gemma-3n initialization...", &self.selected_model).await;
        
        // Download model if needed
        match self.download_model().await {
            Ok(path) => {
                self.model_path = Some(path.clone());
                info!("Gemma-3n model downloaded to: {:?}", path);
                
                self.emit_progress(90, "Loading Gemma-3n model into memory...", &self.selected_model).await;
                
                // Load the model into the inference engine
                match self.inference_manager.load_model(&path).await {
                    Ok(_) => {
                        self.model_loaded = true;
                        let backend_name = self.inference_manager.backend_name().await;
                        self.emit_progress(100, &format!("Gemma-3n ready with {} backend! Google's latest AI model at your service.", backend_name), &self.selected_model).await;
                        info!("Model loaded successfully using {} backend", backend_name);
                        Ok(())
                    }
                    Err(e) => {
                        error!("Failed to load model: {}", e);
                        self.emit_progress(0, &format!("Failed to load model: {}", e), &self.selected_model).await;
                        Err(e)
                    }
                }
            }
            Err(e) => {
                error!("Failed to download Gemma model: {}", e);
                Err(e)
            }
        }
    }

    async fn download_model(&self) -> Result<PathBuf> {
        // Check if we should use MLX (Apple Silicon only)
        let use_mlx = match self.backend_type {
            BackendType::Auto => {
                cfg!(target_os = "macos") && std::env::consts::ARCH == "aarch64"
            }
            BackendType::MLX => true,
            BackendType::GGUF => false,
        };
        
        if use_mlx {
            self.download_mlx_model().await
        } else {
            self.download_gguf_model().await
        }
    }
    
    async fn download_mlx_model(&self) -> Result<PathBuf> {
        self.emit_progress(10, "Preparing to download MLX model...", &self.selected_model).await;
        
        // Use Gemma-2 2B 4-bit model for efficiency
        let model_id = GEMMA2_2B_MLX_ID;
        
        // Get cache directory for MLX models
        let cache_dir = dirs::cache_dir()
            .ok_or_else(|| anyhow::anyhow!("Failed to get cache directory"))?
            .join("huggingface")
            .join("hub")
            .join(model_id.replace('/', "--"));
        
        // Check if model already exists
        if cache_dir.exists() {
            let config_path = cache_dir.join("config.json");
            let weights_path = cache_dir.join("model.safetensors");
            
            if config_path.exists() && weights_path.exists() {
                self.emit_progress(100, "Found cached MLX model", model_id).await;
                return Ok(cache_dir);
            }
        }
        
        // Download the MLX model
        self.emit_progress(20, "Downloading MLX model from mlx-community...", model_id).await;
        
        // Download files directly using URLs
        let base_url = format!("https://huggingface.co/{}/resolve/main", model_id);
        
        // Download all necessary files
        let files = vec![
            ("config.json", true),
            ("model.safetensors", true),
            ("tokenizer.json", true),
            ("tokenizer_config.json", false),
            ("special_tokens_map.json", false),
        ];
        
        std::fs::create_dir_all(&cache_dir)?;
        
        let client = reqwest::Client::new();
        
        for (idx, (file, required)) in files.iter().enumerate() {
            let progress = 20 + (idx as u32 * 15);
            self.emit_progress(progress, &format!("Downloading {}", file), model_id).await;
            
            let file_path = cache_dir.join(file);
            if !file_path.exists() {
                let url = format!("{}/{}", base_url, file);
                match client.get(&url).send().await {
                    Ok(response) => {
                        if response.status().is_success() {
                            let bytes = response.bytes().await?;
                            std::fs::write(&file_path, bytes)?;
                            info!("Downloaded {} successfully", file);
                        } else if *required {
                            return Err(anyhow::anyhow!("Failed to download {}: HTTP {}", file, response.status()));
                        } else {
                            warn!("Optional file {} not found", file);
                        }
                    }
                    Err(e) => {
                        if *required {
                            return Err(anyhow::anyhow!("Failed to download {}: {}", file, e));
                        } else {
                            warn!("Optional file {} not found: {}", file, e);
                        }
                    }
                }
            }
        }
        
        self.emit_progress(100, "MLX model downloaded successfully", model_id).await;
        Ok(cache_dir)
    }
    
    async fn download_gguf_model(&self) -> Result<PathBuf> {
        self.emit_progress(10, "Checking for GGUF model...", &self.selected_model).await;
        
        // Get cache directory
        let cache_dir = dirs::cache_dir()
            .ok_or_else(|| anyhow::anyhow!("Failed to get cache directory"))?
            .join("huggingface")
            .join("hub")
            .join(self.selected_model.replace('/', "--"));
        
        std::fs::create_dir_all(&cache_dir)?;
        
        // Choose file based on model selection
        let model_file = if self.selected_model.contains("E4B") {
            GEMMA_E4B_FILE_Q4
        } else {
            GEMMA_E2B_FILE_Q4 // Use Q4 quantization for balance of size/quality
        };
        
        let model_path = cache_dir.join(model_file);
        
        if model_path.exists() {
            let metadata = std::fs::metadata(&model_path)?;
            if metadata.len() > 100_000_000 { // At least 100MB
                self.emit_progress(70, "Found cached GGUF model", &self.selected_model).await;
                return Ok(model_path);
            } else {
                // Remove corrupted file
                let _ = std::fs::remove_file(&model_path);
            }
        }
        
        // Download the model
        let model_size = if self.selected_model.contains("E4B") {
            "~3-5GB"
        } else {
            "2.79GB"
        };
        
        self.emit_progress(
            20, 
            &format!("Downloading Gemma-3n model ({})...", model_size), 
            &self.selected_model
        ).await;
        
        let url = format!(
            "https://huggingface.co/{}/resolve/main/{}",
            self.selected_model, model_file
        );
        
        info!("Downloading from: {}", url);
        
        let client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(1200)) // 20 minutes for larger models
            .user_agent("Tektra-AI-Assistant/0.1.0")
            .build()?;
        
        let response = client.get(&url).send().await?;
        
        if !response.status().is_success() {
            return Err(anyhow::anyhow!(
                "Failed to download model: HTTP {} - Check if model file exists",
                response.status()
            ));
        }
        
        let total_size = response.content_length().unwrap_or(0);
        let mut downloaded = 0u64;
        
        // Create temporary file
        let temp_path = model_path.with_extension("tmp");
        let mut file = tokio::fs::File::create(&temp_path).await?;
        let mut stream = response.bytes_stream();
        
        use futures::StreamExt;
        use tokio::io::AsyncWriteExt;
        
        while let Some(chunk) = stream.next().await {
            let chunk = chunk?;
            file.write_all(&chunk).await?;
            downloaded += chunk.len() as u64;
            
            if total_size > 0 {
                let progress = 20 + ((downloaded as f64 / total_size as f64) * 50.0) as u32;
                self.emit_progress(
                    progress,
                    &format!(
                        "Downloading Gemma-3n ({} / {})",
                        bytesize::ByteSize(downloaded),
                        bytesize::ByteSize(total_size)
                    ),
                    &self.selected_model,
                ).await;
            }
        }
        
        file.flush().await?;
        drop(file);
        
        // Move to final location
        tokio::fs::rename(&temp_path, &model_path).await?;
        
        Ok(model_path)
    }

    pub async fn generate_response(&self, prompt: &str, max_tokens: usize) -> Result<String> {
        self.generate_response_with_system_prompt(prompt, max_tokens, None).await
    }
    
    pub async fn generate_response_with_image(&self, prompt: &str, image_data: &[u8], max_tokens: usize) -> Result<String> {
        self.generate_response_with_image_and_system_prompt(prompt, image_data, max_tokens, None).await
    }
    
    pub async fn generate_response_with_system_prompt(&self, prompt: &str, _max_tokens: usize, system_prompt: Option<String>) -> Result<String> {
        if !self.model_loaded {
            return Err(anyhow::anyhow!("Model not loaded"));
        }
        
        let system = system_prompt.unwrap_or_else(|| 
            "You are Tektra, a helpful AI assistant powered by the Gemma-3n model. You provide accurate, thoughtful, and detailed responses.".to_string()
        );
        
        // Format prompt using correct Gemma chat template
        let _formatted_prompt = if !system.is_empty() {
            format!(
                "{}{}{}\n{}\n{}{}\n{}{}",
                self.chat_template.bos,
                self.chat_template.start_turn,
                self.chat_template.user_role,
                format!("{}\n\n{}", system, prompt),
                self.chat_template.end_turn,
                self.chat_template.start_turn,
                self.chat_template.model_role,
                "\n" // Model's response will go here
            )
        } else {
            format!(
                "{}{}{}\n{}{}\n{}{}",
                self.chat_template.bos,
                self.chat_template.start_turn,
                self.chat_template.user_role,
                prompt,
                self.chat_template.end_turn,
                self.chat_template.start_turn,
                self.chat_template.model_role
            )
        };
        
        info!("Processing prompt: {}", prompt);
        info!("Formatted for Gemma: {}", _formatted_prompt);
        
        // Create inference config
        let config = InferenceConfig {
            max_tokens: _max_tokens,
            temperature: 0.7,
            top_p: 0.9,
            repeat_penalty: 1.1,
            seed: None,
            n_threads: None,
        };
        
        // Use actual inference backend
        match self.inference_manager.generate(&_formatted_prompt, &config).await {
            Ok(response) => {
                info!("Generated response: {}", response);
                Ok(response)
            }
            Err(e) => {
                error!("Inference error: {}", e);
                // Return the error instead of falling back to demo responses
                Err(anyhow::anyhow!("Model inference failed: {}", e))
            }
        }
    }
    
    pub async fn generate_response_with_image_and_system_prompt(&self, prompt: &str, _image_data: &[u8], _max_tokens: usize, system_prompt: Option<String>) -> Result<String> {
        if !self.model_loaded {
            return Err(anyhow::anyhow!("Model not loaded"));
        }
        
        let system = system_prompt.unwrap_or_else(|| 
            "You are Tektra, a helpful AI assistant powered by the Gemma-3n model. You can see and analyze images to answer questions about them.".to_string()
        );
        
        info!("Processing prompt with image: {}", prompt);
        
        // Multimodal inference is not yet implemented
        error!("Multimodal inference with images is not yet implemented");
        Err(anyhow::anyhow!("Multimodal inference with images is not yet implemented. The model can only process text prompts at this time."))
    }
    
    

    async fn emit_progress(&self, progress: u32, status: &str, model_name: &str) {
        let progress_data = ModelLoadProgress {
            progress,
            status: status.to_string(),
            model_name: model_name.to_string(),
        };
        
        if let Err(e) = self.app_handle.emit_all("model-loading-progress", &progress_data) {
            error!("Failed to emit progress: {}", e);
        }
    }

    pub fn is_loaded(&self) -> bool {
        self.model_loaded
    }
    
    pub async fn get_backend_info(&self) -> String {
        let backend_name = self.inference_manager.backend_name().await;
        let system_info = InferenceManager::get_system_info();
        
        format!(
            "Current Backend: {}\nBackend Type: {:?}\n\n{}",
            backend_name, self.backend_type, system_info
        )
    }
    
    pub async fn benchmark_backends(&self, prompt: &str, max_tokens: usize) -> Result<Vec<(String, super::inference_backend::InferenceMetrics)>> {
        let config = InferenceConfig {
            max_tokens,
            temperature: 0.7,
            top_p: 0.9,
            repeat_penalty: 1.1,
            seed: Some(42), // Fixed seed for consistent benchmarks
            n_threads: None,
        };
        
        self.inference_manager.benchmark_backends(prompt, &config).await
    }
}