1use sysinfo::System;
8
9#[derive(Debug, Clone)]
11pub struct DeviceSpec {
12 pub total_ram_gb: f64,
14 pub cpu_cores: usize,
16 pub is_apple_silicon: bool,
18 pub usable_ram_gb: f64,
20}
21
22impl DeviceSpec {
23 pub fn detect() -> Self {
25 let mut sys = System::new_all();
26 sys.refresh_all();
27
28 let total_bytes = sys.total_memory();
29 let total_ram_gb = total_bytes as f64 / (1024.0 * 1024.0 * 1024.0);
30
31 let is_apple_silicon = detect_apple_silicon();
34 let os_overhead_gb = if is_apple_silicon { 3.0 } else { 4.5 };
35 let usable_ram_gb = (total_ram_gb - os_overhead_gb).max(0.5);
36
37 Self {
38 total_ram_gb,
39 cpu_cores: sys.cpus().len(),
40 is_apple_silicon,
41 usable_ram_gb,
42 }
43 }
44}
45
46#[derive(Debug, Clone)]
48pub struct ModelRecommendation {
49 pub name: &'static str,
51 pub size_gb: f64,
53 pub description: &'static str,
55 pub category: RecommendCategory,
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
61pub enum RecommendCategory {
62 BestForDevice,
64 FastAndLight,
66 MaxQuality,
68 Reasoning,
70 TooLarge,
72}
73
74impl RecommendCategory {
75 pub fn label(&self) -> &'static str {
76 match self {
77 Self::BestForDevice => "BEST FOR YOU",
78 Self::FastAndLight => "FAST & LIGHT",
79 Self::MaxQuality => "MAX QUALITY",
80 Self::Reasoning => "REASONING",
81 Self::TooLarge => "TOO LARGE (swap risk)",
82 }
83 }
84}
85
86static KNOWN_MODELS: &[(&str, f64, &str)] = &[
89 (
90 "qwen3:30b-a3b",
91 17.0,
92 "Qwen3 30B MoE (3B active) — best reasoning with low compute cost",
93 ),
94 (
95 "deepseek-r1:32b",
96 20.0,
97 "DeepSeek-R1 32B — state-of-the-art reasoning, large context",
98 ),
99 (
100 "qwen3:14b",
101 9.0,
102 "Qwen3 14B — best quality/size ratio, multilingual, tool use",
103 ),
104 (
105 "deepseek-r1:14b",
106 9.0,
107 "DeepSeek-R1 14B — chain-of-thought reasoning, 128k context",
108 ),
109 (
110 "gemma3:12b",
111 7.5,
112 "Gemma3 12B — Google's best 12B, strong at code and analysis",
113 ),
114 (
115 "qwen3:8b",
116 5.0,
117 "Qwen3 8B — fast, accurate, excellent at following instructions",
118 ),
119 (
120 "deepseek-r1:7b",
121 4.7,
122 "DeepSeek-R1 7B — reasoning specialist, great for Q&A",
123 ),
124 (
125 "gemma3:9b",
126 5.5,
127 "Gemma3 9B — well-rounded, fast, strong at code",
128 ),
129 ("qwen3:4b", 2.6, "Qwen3 4B — compact, surprisingly capable"),
130 (
131 "phi3:mini",
132 2.3,
133 "Phi-3 Mini — Microsoft's 3.8B, punches above its weight",
134 ),
135 (
136 "gemma3:2b",
137 1.8,
138 "Gemma3 2B — smallest useful model, good for simple Q&A",
139 ),
140 (
141 "qwen3:1.7b",
142 1.2,
143 "Qwen3 1.7B — ultra-light, CPU-only viable",
144 ),
145 (
146 "gemma3:1b",
147 0.8,
148 "Gemma3 1B — absolute minimum, fast even on old hardware",
149 ),
150];
151
152pub fn recommend_models(spec: &DeviceSpec) -> Vec<ModelRecommendation> {
154 let usable = spec.usable_ram_gb;
155 let mut recs: Vec<ModelRecommendation> = Vec::new();
156
157 let best_name = best_model_name(usable);
159 let fast_name = fast_model_name(usable);
160 let max_name = max_quality_model_name(usable);
161 let reason_name = reasoning_model_name(usable);
162
163 for &(name, size_gb, description) in KNOWN_MODELS {
164 if size_gb > usable + 2.0 {
165 if size_gb > usable + 6.0 {
167 continue;
168 }
169 }
170
171 let category = if name == best_name {
172 RecommendCategory::BestForDevice
173 } else if Some(name) == fast_name {
174 RecommendCategory::FastAndLight
175 } else if Some(name) == max_name && name != best_name {
176 RecommendCategory::MaxQuality
177 } else if Some(name) == reason_name && name != best_name {
178 RecommendCategory::Reasoning
179 } else if size_gb > usable {
180 RecommendCategory::TooLarge
181 } else {
182 continue; };
184
185 recs.push(ModelRecommendation {
186 name,
187 size_gb,
188 description,
189 category,
190 });
191 }
192
193 recs.sort_by_key(|r| match r.category {
195 RecommendCategory::BestForDevice => 0,
196 RecommendCategory::FastAndLight => 1,
197 RecommendCategory::MaxQuality => 2,
198 RecommendCategory::Reasoning => 3,
199 RecommendCategory::TooLarge => 4,
200 });
201
202 recs
203}
204
205pub fn best_model_name(usable_gb: f64) -> &'static str {
207 match usable_gb as u64 {
208 u if u >= 28 => "qwen3:30b-a3b", u if u >= 20 => "qwen3:14b", u if u >= 11 => "qwen3:14b", u if u >= 6 => "qwen3:8b", u if u >= 4 => "deepseek-r1:7b", u if u >= 2 => "qwen3:4b", _ => "gemma3:1b", }
216}
217
218fn fast_model_name(usable_gb: f64) -> Option<&'static str> {
219 match usable_gb as u64 {
220 u if u >= 11 => Some("qwen3:8b"),
221 u if u >= 6 => Some("deepseek-r1:7b"),
222 u if u >= 3 => Some("phi3:mini"),
223 _ => None,
224 }
225}
226
227fn max_quality_model_name(usable_gb: f64) -> Option<&'static str> {
228 match usable_gb as u64 {
229 u if u >= 28 => Some("deepseek-r1:32b"),
230 u if u >= 11 => Some("deepseek-r1:14b"),
231 u if u >= 6 => Some("gemma3:9b"),
232 _ => None,
233 }
234}
235
236fn reasoning_model_name(usable_gb: f64) -> Option<&'static str> {
237 match usable_gb as u64 {
238 u if u >= 11 => Some("deepseek-r1:14b"),
239 u if u >= 5 => Some("deepseek-r1:7b"),
240 _ => None,
241 }
242}
243
244pub fn format_setup_guide(spec: &DeviceSpec) -> String {
251 let recs = recommend_models(spec);
252 let best = best_model_name(spec.usable_ram_gb);
253
254 let mut out = String::new();
255
256 out.push_str("┌─ Ollama Setup Guide ────────────────────────────────────────────┐\n");
257 out.push_str("│ Ollama is not running. Follow these steps: │\n");
258 out.push_str("└─────────────────────────────────────────────────────────────────┘\n\n");
259
260 out.push_str(" Step 1 — Install Ollama\n");
262 out.push_str(" ────────────────────────────────────────\n");
263 out.push_str(" macOS / Linux: curl -fsSL https://ollama.ai/install.sh | sh\n");
264 out.push_str(" Windows: https://ollama.ai/download\n\n");
265
266 out.push_str(" Step 2 — Pull a model for your machine\n");
268 out.push_str(" ────────────────────────────────────────\n");
269 out.push_str(&format!(
270 " Your device: {:.0} GB RAM, {} cores{}\n",
271 spec.total_ram_gb,
272 spec.cpu_cores,
273 if spec.is_apple_silicon {
274 ", Apple Silicon (fast)"
275 } else {
276 ", Intel/AMD (CPU inference)"
277 }
278 ));
279 out.push_str(&format!(
280 " Usable for LLM: ~{:.0} GB\n\n",
281 spec.usable_ram_gb
282 ));
283
284 for rec in &recs {
285 let marker = if rec.name == best { "→" } else { " " };
286 out.push_str(&format!(
287 " {marker} [{:16}] {:<22} ~{:.0} GB {}\n",
288 rec.category.label(),
289 rec.name,
290 rec.size_gb,
291 rec.description
292 ));
293 out.push_str(&format!(" ollama pull {}\n\n", rec.name));
294 }
295
296 out.push_str(" ─────────────────────────────────────────────────────────────────\n");
298 out.push_str(" Quick-start (best model for your device):\n\n");
299 out.push_str(" ollama serve &\n");
300 out.push_str(&format!(" ollama pull {best}\n"));
301 out.push_str(" fetchium ai \"your question\"\n\n");
302
303 out.push_str(
304 " After pulling a model, re-run your command — fetchium will detect it automatically.\n",
305 );
306 out.push_str(
307 " Tip: Add `default_model = \"{best}\"` to ~/.fetchium/config.toml to always use it.\n",
308 );
309
310 out
311}
312
313pub fn format_no_models_hint(spec: &DeviceSpec) -> String {
315 let best = best_model_name(spec.usable_ram_gb);
316 format!(
317 "No models installed. Run: ollama pull {best}\n \
318 Then re-run your command. Use `fetchium doctor` for a full setup guide."
319 )
320}
321
322fn detect_apple_silicon() -> bool {
323 #[cfg(target_os = "macos")]
324 {
325 std::process::Command::new("uname")
326 .arg("-m")
327 .output()
328 .map(|o| String::from_utf8_lossy(&o.stdout).contains("arm64"))
329 .unwrap_or(false)
330 }
331 #[cfg(not(target_os = "macos"))]
332 {
333 false
334 }
335}
336
337#[cfg(test)]
338mod tests {
339 use super::*;
340
341 fn spec(ram_gb: f64) -> DeviceSpec {
342 DeviceSpec {
343 total_ram_gb: ram_gb,
344 cpu_cores: 8,
345 is_apple_silicon: false,
346 usable_ram_gb: (ram_gb - 4.5).max(0.5),
347 }
348 }
349
350 #[test]
351 fn best_model_for_64gb() {
352 assert_eq!(best_model_name(60.0), "qwen3:30b-a3b");
353 }
354
355 #[test]
356 fn best_model_for_16gb() {
357 assert_eq!(best_model_name(spec(16.0).usable_ram_gb), "qwen3:14b");
359 }
360
361 #[test]
362 fn best_model_for_8gb() {
363 let s = spec(8.0);
365 let b = best_model_name(s.usable_ram_gb);
366 assert!(b == "qwen3:4b" || b == "deepseek-r1:7b");
368 }
369
370 #[test]
371 fn best_model_for_4gb() {
372 let s = spec(4.0);
373 assert_eq!(best_model_name(s.usable_ram_gb), "gemma3:1b");
375 }
376
377 #[test]
378 fn recommend_models_nonempty_for_16gb() {
379 let recs = recommend_models(&spec(16.0));
380 assert!(!recs.is_empty());
381 assert_eq!(recs[0].category, RecommendCategory::BestForDevice);
382 }
383
384 #[test]
385 fn setup_guide_contains_ollama_install() {
386 let guide = format_setup_guide(&spec(16.0));
387 assert!(guide.contains("ollama.ai/install.sh"));
388 assert!(guide.contains("ollama pull"));
389 }
390
391 #[test]
392 fn no_models_hint_has_pull_cmd() {
393 let hint = format_no_models_hint(&spec(16.0));
394 assert!(hint.contains("ollama pull"));
395 }
396}