ironmonitor 7.1.0

IronMonitor: hardware monitoring for CPUs, GPUs, NPUs, memory, I/O and network silicon on Linux, Windows and macOS, which reports what it cannot read as well as what it can
Documentation
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Ollama Client - Local LLM Server Integration
//!
//! Ollama is a popular tool for running large language models locally.
//! This module provides a client for the Ollama HTTP API.
//!
//! # Requirements
//!
//! - Ollama must be installed and running: <https://ollama.ai>
//! - Default endpoint: http://localhost:11434
//!
//! # Supported API Endpoints
//!
//! - `/api/generate` - Generate completion
//! - `/api/chat` - Chat completion (conversational)
//! - `/api/tags` - List available models
//! - `/api/show` - Get model information
//!
//! # Example
//!
//! ```no_run
//! use ironmonitor::agent::local::{OllamaClient, InferenceRequest, LocalInferenceClient};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let client = OllamaClient::new("http://localhost:11434")?;
//!
//! let request = InferenceRequest {
//!     model: "llama3".to_string(),
//!     prompt: "What is the current GPU temperature?".to_string(),
//!     ..Default::default()
//! };
//!
//! let response = client.generate(request).await?;
//! println!("{}", response.text);
//! # Ok(())
//! # }
//! ```

use super::{InferenceRequest, InferenceResponse, LocalInferenceClient, ModelInfo};
use crate::error::{IronError, Result};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
#[allow(unused_imports)]
use std::time::Instant;

/// Ollama API client
#[derive(Debug, Clone)]
pub struct OllamaClient {
    #[allow(dead_code)]
    endpoint: String,
    #[cfg(feature = "remote-backends")]
    client: reqwest::Client,
}

impl OllamaClient {
    /// Create new Ollama client
    #[allow(unused_variables)]
    pub fn new(endpoint: &str) -> Result<Self> {
        #[cfg(feature = "remote-backends")]
        {
            let client = reqwest::Client::builder()
                .timeout(std::time::Duration::from_secs(60))
                .build()
                .map_err(|e| IronError::Network(e.to_string()))?;

            Ok(Self {
                endpoint: endpoint.trim_end_matches('/').to_string(),
                client,
            })
        }

        #[cfg(not(feature = "remote-backends"))]
        {
            Err(IronError::NotImplemented(
                "Ollama client requires 'remote-backends' feature".to_string(),
            ))
        }
    }

    /// Create default Ollama client (localhost:11434)
    ///
    /// Not `Default::default`: constructing a client can fail, and that trait has no
    /// way to report it. Renaming would break callers for no gain.
    #[allow(clippy::should_implement_trait)]
    pub fn default() -> Result<Self> {
        Self::new("http://localhost:11434")
    }

    /// Generate text (non-streaming)
    #[cfg(feature = "remote-backends")]
    pub async fn generate_simple(&self, model: &str, prompt: &str) -> Result<String> {
        let request = InferenceRequest {
            model: model.to_string(),
            prompt: prompt.to_string(),
            ..Default::default()
        };

        let response = self.generate(request).await?;
        Ok(response.text)
    }

    /// Chat completion (conversational format)
    #[cfg(feature = "remote-backends")]
    pub async fn chat(&self, model: &str, messages: Vec<ChatMessage>) -> Result<InferenceResponse> {
        let start = Instant::now();

        let request_body = OllamaChatRequest {
            model: model.to_string(),
            messages,
            stream: false,
            options: None,
        };

        let url = format!("{}/api/chat", self.endpoint);
        let response = self
            .client
            .post(&url)
            .json(&request_body)
            .send()
            .await
            .map_err(|e| IronError::Network(e.to_string()))?;

        if !response.status().is_success() {
            return Err(IronError::Agent(format!(
                "Ollama API error: {}",
                response.status()
            )));
        }

        let ollama_response: OllamaChatResponse = response
            .json()
            .await
            .map_err(|e| IronError::Agent(format!("Failed to parse response: {}", e)))?;

        Ok(InferenceResponse {
            text: ollama_response.message.content,
            model: ollama_response.model,
            tokens_generated: ollama_response.eval_count.map(|n| n as usize),
            duration_ms: start.elapsed().as_millis() as u64,
            truncated: ollama_response
                .done_reason
                .as_deref()
                .map(|reason| reason == "length"),
        })
    }

    /// Pull/download a model
    #[cfg(feature = "remote-backends")]
    pub async fn pull_model(&self, model: &str) -> Result<()> {
        let url = format!("{}/api/pull", self.endpoint);

        let request_body = serde_json::json!({
            "name": model,
            "stream": false,
        });

        let response = self
            .client
            .post(&url)
            .json(&request_body)
            .send()
            .await
            .map_err(|e| IronError::Network(e.to_string()))?;

        if !response.status().is_success() {
            return Err(IronError::Agent(format!(
                "Failed to pull model: {}",
                response.status()
            )));
        }

        Ok(())
    }
}

#[async_trait]
impl LocalInferenceClient for OllamaClient {
    fn name(&self) -> &str {
        "Ollama"
    }

    async fn is_available(&self) -> bool {
        #[cfg(feature = "remote-backends")]
        {
            // Any HTTP answer used to count. An unrelated server on the port was then
            // reported as Ollama, so the listing's shape is required too.
            let url = format!("{}/api/tags", self.endpoint);
            match self.client.get(&url).send().await {
                Ok(response) if response.status().is_success() => response
                    .json::<serde_json::Value>()
                    .await
                    .ok()
                    .is_some_and(|body| body.get("models").is_some_and(|m| m.is_array())),
                _ => false,
            }
        }

        #[cfg(not(feature = "remote-backends"))]
        false
    }

    async fn list_models(&self) -> Result<Vec<ModelInfo>> {
        #[cfg(feature = "remote-backends")]
        {
            let url = format!("{}/api/tags", self.endpoint);
            let response = self
                .client
                .get(&url)
                .send()
                .await
                .map_err(|e| IronError::Network(e.to_string()))?;

            if !response.status().is_success() {
                return Err(IronError::Agent(
                    "Failed to list models from Ollama".to_string(),
                ));
            }

            let tags: OllamaTagsResponse = response
                .json()
                .await
                .map_err(|e| IronError::Agent(format!("Failed to parse models: {}", e)))?;

            Ok(tags
                .models
                .into_iter()
                .map(|m| ModelInfo {
                    name: m.name,
                    size: Some(m.size),
                    family: m.details.family,
                    parameter_count: m.details.parameter_size,
                    quantization: m.details.quantization_level,
                })
                .collect())
        }

        #[cfg(not(feature = "remote-backends"))]
        Err(IronError::NotImplemented(
            "Ollama client requires 'remote-backends' feature".to_string(),
        ))
    }

    #[allow(unused_variables)]
    async fn generate(&self, request: InferenceRequest) -> Result<InferenceResponse> {
        #[cfg(feature = "remote-backends")]
        {
            let start = Instant::now();

            let mut options = OllamaOptions::default();
            if let Some(temp) = request.temperature {
                options.temperature = Some(temp);
            }
            if let Some(tokens) = request.max_tokens {
                options.num_predict = Some(tokens as i32);
            }
            if let Some(top_p) = request.top_p {
                options.top_p = Some(top_p);
            }

            let request_body = OllamaGenerateRequest {
                model: request.model.clone(),
                prompt: request.prompt.clone(),
                system: request.system,
                stream: false,
                options: Some(options),
            };

            let url = format!("{}/api/generate", self.endpoint);
            let response = self
                .client
                .post(&url)
                .json(&request_body)
                .send()
                .await
                .map_err(|e| IronError::Network(e.to_string()))?;

            if !response.status().is_success() {
                return Err(IronError::Agent(format!(
                    "Ollama API error: {}",
                    response.status()
                )));
            }

            let ollama_response: OllamaGenerateResponse = response
                .json()
                .await
                .map_err(|e| IronError::Agent(format!("Failed to parse response: {}", e)))?;

            // `done` is true for every completed generation, including a normal stop,
            // so `truncated` used to be inverted: every complete answer read as cut
            // off. The stop reason is `done_reason`, "length" when the token limit hit.
            // A missing eval_count used to become a measured zero tokens; it is now
            // unknown.
            Ok(InferenceResponse {
                text: ollama_response.response,
                model: ollama_response.model,
                tokens_generated: ollama_response.eval_count.map(|n| n as usize),
                duration_ms: start.elapsed().as_millis() as u64,
                truncated: ollama_response
                    .done_reason
                    .as_deref()
                    .map(|reason| reason == "length"),
            })
        }

        #[cfg(not(feature = "remote-backends"))]
        Err(IronError::NotImplemented(
            "Ollama client requires 'remote-backends' feature".to_string(),
        ))
    }

    #[allow(unused_variables)]
    async fn model_info(&self, model_name: &str) -> Result<ModelInfo> {
        #[cfg(feature = "remote-backends")]
        {
            let url = format!("{}/api/show", self.endpoint);

            let request_body = serde_json::json!({
                "name": model_name,
            });

            let response = self
                .client
                .post(&url)
                .json(&request_body)
                .send()
                .await
                .map_err(|e| IronError::Network(e.to_string()))?;

            if !response.status().is_success() {
                return Err(IronError::Agent(format!("Model {} not found", model_name)));
            }

            let show_response: OllamaShowResponse = response
                .json()
                .await
                .map_err(|e| IronError::Agent(format!("Failed to parse model info: {}", e)))?;

            Ok(ModelInfo {
                name: model_name.to_string(),
                size: Some(show_response.size),
                family: show_response.details.family,
                parameter_count: show_response.details.parameter_size,
                quantization: show_response.details.quantization_level,
            })
        }

        #[cfg(not(feature = "remote-backends"))]
        Err(IronError::NotImplemented(
            "Ollama client requires 'remote-backends' feature".to_string(),
        ))
    }
}

// Ollama API Types
// These are used when the remote-backends feature is enabled
#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
struct OllamaGenerateRequest {
    model: String,
    prompt: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    system: Option<String>,
    stream: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    options: Option<OllamaOptions>,
}

#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
struct OllamaGenerateResponse {
    model: String,
    response: String,
    done: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    done_reason: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    eval_count: Option<i32>,
}

#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
struct OllamaChatRequest {
    model: String,
    messages: Vec<ChatMessage>,
    stream: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    options: Option<OllamaOptions>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatMessage {
    pub role: String, // "system", "user", "assistant"
    pub content: String,
}

#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
struct OllamaChatResponse {
    model: String,
    message: ChatMessage,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    done_reason: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    eval_count: Option<i32>,
}

#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
struct OllamaOptions {
    #[serde(skip_serializing_if = "Option::is_none")]
    temperature: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    num_predict: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    top_p: Option<f32>,
}

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
struct OllamaTagsResponse {
    models: Vec<OllamaModel>,
}

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
struct OllamaModel {
    name: String,
    size: u64,
    details: OllamaModelDetails,
}

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
struct OllamaModelDetails {
    #[serde(skip_serializing_if = "Option::is_none")]
    family: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    parameter_size: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    quantization_level: Option<String>,
}

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
struct OllamaShowResponse {
    size: u64,
    details: OllamaModelDetails,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ollama_client_creation() {
        let result = OllamaClient::new("http://localhost:11434");
        #[cfg(feature = "remote-backends")]
        assert!(result.is_ok());
        #[cfg(not(feature = "remote-backends"))]
        assert!(result.is_err());
    }

    /// A completed generation reports `done: true` whether it stopped naturally or
    /// hit the token limit, so `done` says nothing about truncation. The stop reason
    /// is `done_reason`, and a response without it must not be read as a stop.
    #[test]
    fn stop_reason_comes_from_done_reason_not_done() {
        let natural: OllamaGenerateResponse = serde_json::from_str(
            r#"{"model":"m","response":"ok","done":true,"done_reason":"stop","eval_count":12}"#,
        )
        .expect("captured-shape response parses");
        assert!(natural.done);
        assert_eq!(natural.done_reason.as_deref(), Some("stop"));
        assert_eq!(natural.eval_count, Some(12));

        let cut_off: OllamaGenerateResponse = serde_json::from_str(
            r#"{"model":"m","response":"partial","done":true,"done_reason":"length"}"#,
        )
        .expect("length-stopped response parses");
        assert_eq!(cut_off.done_reason.as_deref(), Some("length"));
        // No eval_count in the body: the token count is unknown, not zero.
        assert_eq!(cut_off.eval_count, None);

        let unreported: OllamaGenerateResponse =
            serde_json::from_str(r#"{"model":"m","response":"x","done":true}"#)
                .expect("response without a stop reason parses");
        assert_eq!(unreported.done_reason, None);
    }

    #[test]
    fn test_default_client() {
        let result = OllamaClient::default();
        #[cfg(feature = "remote-backends")]
        assert!(result.is_ok());
        #[cfg(not(feature = "remote-backends"))]
        assert!(result.is_err());
    }
}