api_ollama 0.2.0

Ollama local LLM runtime API client for HTTP communication.
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! Input validation framework for request types.
//!
//! Provides comprehensive validation to catch invalid requests before network calls,
//! improving error messages and preventing injection attacks.

#[ cfg( feature = "input_validation" ) ]
mod private
{
  use std::fmt;

  /// Validation error with detailed context
  #[ derive( Debug, Clone, PartialEq, Eq ) ]
  pub struct ValidationError
  {
    /// Field name that failed validation
    pub field : String,
    /// Human-readable error message
    pub message : String,
    /// The invalid value (may be truncated for large inputs)
    pub value : String,
    /// The constraint that was violated
    pub constraint : String,
  }

  impl fmt::Display for ValidationError
  {
    #[ inline ]
    fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
    {
      write!(
        f,
        "Validation error in field '{}': {}. Value : '{}', Constraint : '{}'",
        self.field, self.message, self.value, self.constraint
      )
    }
  }

  impl std::error::Error for ValidationError {}

  /// Result type for validation operations
  pub type ValidationResult = Result< (), Vec< ValidationError > >;

  /// Trait for types that can be validated
  pub trait Validate
  {
    /// Validate the instance, returning all validation errors
    ///
    /// # Errors
    ///
    /// Returns a vector of validation errors if any fields fail validation
    fn validate( &self ) -> ValidationResult;
  }

  /// Specific validators for common field types
  pub mod validators
  {

    /// Validate model name (non-empty, max 256 chars, alphanumeric+dash+underscore+colon)
    ///
    /// # Errors
    ///
    /// Returns error if model name is invalid
    #[ inline ]
    pub fn validate_model_name( name : &str ) -> Result< (), String >
    {
      if name.is_empty()
      {
        return Err( "Model name cannot be empty".to_string() );
      }

      if name.len() > 256
      {
        return Err( format!( "Model name too long : {} chars (max 256)", name.len() ) );
      }

      // Ollama model names : alphanumeric + dash + underscore + colon + slash + dot
      let valid_chars = name.chars().all( | c |
        c.is_alphanumeric() || c == '-' || c == '_' || c == ':' || c == '/' || c == '.'
      );

      if !valid_chars
      {
        return Err( "Model name contains invalid characters (allowed : alphanumeric, -, _, :, /, .)".to_string() );
      }

      Ok( () )
    }

    /// Validate temperature (0.0 to 2.0)
    ///
    /// # Errors
    ///
    /// Returns error if temperature is out of range
    #[ inline ]
    pub fn validate_temperature( temp : f32 ) -> Result< (), String >
    {
      if !( 0.0..=2.0 ).contains( &temp )
      {
        return Err( format!( "Temperature {} out of range [0.0, 2.0]", temp ) );
      }

      if temp.is_nan() || temp.is_infinite()
      {
        return Err( format!( "Temperature must be a finite number, got {}", temp ) );
      }

      Ok( () )
    }

    /// Validate top_p (0.0 to 1.0)
    ///
    /// # Errors
    ///
    /// Returns error if top_p is out of range
    #[ inline ]
    pub fn validate_top_p( top_p : f32 ) -> Result< (), String >
    {
      if !( 0.0..=1.0 ).contains( &top_p )
      {
        return Err( format!( "top_p {} out of range [0.0, 1.0]", top_p ) );
      }

      if top_p.is_nan() || top_p.is_infinite()
      {
        return Err( format!( "top_p must be a finite number, got {}", top_p ) );
      }

      Ok( () )
    }

    /// Validate top_k (must be positive)
    ///
    /// # Errors
    ///
    /// Returns error if top_k is invalid
    #[ inline ]
    pub fn validate_top_k( top_k : i32 ) -> Result< (), String >
    {
      if top_k <= 0
      {
        return Err( format!( "top_k must be positive, got {}", top_k ) );
      }

      Ok( () )
    }

    /// Validate repeat penalty (typically 0.0 to 2.0)
    ///
    /// # Errors
    ///
    /// Returns error if repeat penalty is invalid
    #[ inline ]
    pub fn validate_repeat_penalty( penalty : f32 ) -> Result< (), String >
    {
      if penalty < 0.0
      {
        return Err( format!( "repeat_penalty must be non-negative, got {}", penalty ) );
      }

      if penalty.is_nan() || penalty.is_infinite()
      {
        return Err( format!( "repeat_penalty must be a finite number, got {}", penalty ) );
      }

      Ok( () )
    }

    /// Validate base64 image data (basic format check)
    ///
    /// # Errors
    ///
    /// Returns error if image data is invalid
    #[ inline ]
    #[ cfg( feature = "vision_support" ) ]
    pub fn validate_base64_image( data : &str ) -> Result< (), String >
    {
      if data.is_empty()
      {
        return Err( "Image data cannot be empty".to_string() );
      }

      // Check if it looks like base64 (only contains valid base64 chars)
      let valid_chars = data.chars().all( | c |
        c.is_alphanumeric() || c == '+' || c == '/' || c == '='
      );

      if !valid_chars
      {
        return Err( "Image data contains invalid base64 characters".to_string() );
      }

      // Base64 length must be multiple of 4
      if !data.len().is_multiple_of( 4 )
      {
        return Err( format!( "Invalid base64 length : {} (must be multiple of 4)", data.len() ) );
      }

      // Check reasonable size limits (max 10MB base64 ~ 13.3MB decoded)
      const MAX_BASE64_SIZE : usize = 13_333_333;
      if data.len() > MAX_BASE64_SIZE
      {
        return Err( format!( "Image data too large : {} bytes (max ~10MB)", data.len() ) );
      }

      Ok( () )
    }

    /// Validate max tokens (must be positive)
    ///
    /// # Errors
    ///
    /// Returns error if max tokens is invalid
    #[ inline ]
    pub fn validate_max_tokens( max_tokens : i32 ) -> Result< (), String >
    {
      if max_tokens <= 0
      {
        return Err( format!( "max_tokens must be positive, got {}", max_tokens ) );
      }

      // Reasonable upper limit (Ollama models typically have context windows up to 128k)
      const MAX_CONTEXT : i32 = 131_072;
      if max_tokens > MAX_CONTEXT
      {
        return Err( format!( "max_tokens {} exceeds reasonable limit ({})", max_tokens, MAX_CONTEXT ) );
      }

      Ok( () )
    }

    /// Validate messages list (must not be empty)
    ///
    /// # Errors
    ///
    /// Returns error if messages list is invalid
    #[ inline ]
    pub fn validate_messages< T >( messages : &[ T ] ) -> Result< (), String >
    {
      if messages.is_empty()
      {
        return Err( "Messages list cannot be empty".to_string() );
      }

      Ok( () )
    }

    /// Validate prompt (must not be empty)
    ///
    /// # Errors
    ///
    /// Returns error if prompt is invalid
    #[ inline ]
    pub fn validate_prompt( prompt : &str ) -> Result< (), String >
    {
      if prompt.is_empty()
      {
        return Err( "Prompt cannot be empty".to_string() );
      }

      // Reasonable length check (most models have context limits)
      const MAX_PROMPT_LENGTH : usize = 500_000; // ~500k chars
      if prompt.len() > MAX_PROMPT_LENGTH
      {
        return Err( format!( "Prompt too long : {} chars (max {})", prompt.len(), MAX_PROMPT_LENGTH ) );
      }

      Ok( () )
    }

    /// Validate audio format
    ///
    /// # Errors
    ///
    /// Returns error if audio format is invalid
    #[ inline ]
    #[ cfg( feature = "audio_processing" ) ]
    pub fn validate_audio_format( format : &str ) -> Result< (), String >
    {
      const VALID_FORMATS : &[ &str ] = &[ "wav", "mp3", "ogg", "flac", "m4a" ];

      if !VALID_FORMATS.contains( &format )
      {
        return Err( format!(
          "Invalid audio format '{}' (valid : {})",
          format,
          VALID_FORMATS.join( ", " )
        ));
      }

      Ok( () )
    }
  }

  // Implementation of Validate trait for request types
  impl Validate for crate::ChatRequest
  {
    #[ inline ]
    fn validate( &self ) -> ValidationResult
    {
      let mut errors = Vec::new();

      // Validate model name
      if let Err( e ) = validators::validate_model_name( &self.model )
      {
        errors.push( ValidationError
        {
          field : "model".to_string(),
          message : e,
          value : truncate_value( &self.model, 50 ),
          constraint : "non-empty, max 256 chars, alphanumeric+-_:/.".to_string(),
        });
      }

      // Validate messages
      if let Err( e ) = validators::validate_messages( &self.messages )
      {
        errors.push( ValidationError
        {
          field : "messages".to_string(),
          message : e,
          value : format!( "{} messages", self.messages.len() ),
          constraint : "at least 1 message".to_string(),
        });
      }

      // Validate options if present (check it's a valid object)
      if let Some( ref options ) = self.options
      {
        if !options.is_object() && !options.is_null()
        {
          errors.push( ValidationError
          {
            field : "options".to_string(),
            message : "Options must be a JSON object".to_string(),
            value : truncate_value( &options.to_string(), 50 ),
            constraint : "JSON object".to_string(),
          });
        }

        // Validate specific option fields if present
        if let Some( obj ) = options.as_object()
        {
          if let Some( temp ) = obj.get( "temperature" ).and_then( | v | v.as_f64() )
          {
            if let Err( e ) = validators::validate_temperature( temp as f32 )
            {
              errors.push( ValidationError
              {
                field : "options.temperature".to_string(),
                message : e,
                value : format!( "{}", temp ),
                constraint : "[0.0, 2.0]".to_string(),
              });
            }
          }

          if let Some( top_p ) = obj.get( "top_p" ).and_then( | v | v.as_f64() )
          {
            if let Err( e ) = validators::validate_top_p( top_p as f32 )
            {
              errors.push( ValidationError
              {
                field : "options.top_p".to_string(),
                message : e,
                value : format!( "{}", top_p ),
                constraint : "[0.0, 1.0]".to_string(),
              });
            }
          }

          if let Some( top_k ) = obj.get( "top_k" ).and_then( | v | v.as_i64() )
          {
            if let Err( e ) = validators::validate_top_k( top_k as i32 )
            {
              errors.push( ValidationError
              {
                field : "options.top_k".to_string(),
                message : e,
                value : format!( "{}", top_k ),
                constraint : "positive integer".to_string(),
              });
            }
          }

          if let Some( penalty ) = obj.get( "repeat_penalty" ).and_then( | v | v.as_f64() )
          {
            if let Err( e ) = validators::validate_repeat_penalty( penalty as f32 )
            {
              errors.push( ValidationError
              {
                field : "options.repeat_penalty".to_string(),
                message : e,
                value : format!( "{}", penalty ),
                constraint : "non-negative".to_string(),
              });
            }
          }
        }
      }

      if errors.is_empty() { Ok( () ) } else { Err( errors ) }
    }
  }

  impl Validate for crate::GenerateRequest
  {
    #[ inline ]
    fn validate( &self ) -> ValidationResult
    {
      let mut errors = Vec::new();

      // Validate model name
      if let Err( e ) = validators::validate_model_name( &self.model )
      {
        errors.push( ValidationError
        {
          field : "model".to_string(),
          message : e,
          value : truncate_value( &self.model, 50 ),
          constraint : "non-empty, max 256 chars, alphanumeric+-_:/.".to_string(),
        });
      }

      // Validate prompt
      if let Err( e ) = validators::validate_prompt( &self.prompt )
      {
        errors.push( ValidationError
        {
          field : "prompt".to_string(),
          message : e,
          value : truncate_value( &self.prompt, 100 ),
          constraint : "non-empty, max 500k chars".to_string(),
        });
      }

      // Validate options if present
      if let Some( ref options ) = self.options
      {
        if !options.is_object() && !options.is_null()
        {
          errors.push( ValidationError
          {
            field : "options".to_string(),
            message : "Options must be a JSON object".to_string(),
            value : truncate_value( &options.to_string(), 50 ),
            constraint : "JSON object".to_string(),
          });
        }

        // Validate specific option fields if present
        if let Some( obj ) = options.as_object()
        {
          if let Some( temp ) = obj.get( "temperature" ).and_then( | v | v.as_f64() )
          {
            if let Err( e ) = validators::validate_temperature( temp as f32 )
            {
              errors.push( ValidationError
              {
                field : "options.temperature".to_string(),
                message : e,
                value : format!( "{}", temp ),
                constraint : "[0.0, 2.0]".to_string(),
              });
            }
          }

          if let Some( top_p ) = obj.get( "top_p" ).and_then( | v | v.as_f64() )
          {
            if let Err( e ) = validators::validate_top_p( top_p as f32 )
            {
              errors.push( ValidationError
              {
                field : "options.top_p".to_string(),
                message : e,
                value : format!( "{}", top_p ),
                constraint : "[0.0, 1.0]".to_string(),
              });
            }
          }
        }
      }

      if errors.is_empty() { Ok( () ) } else { Err( errors ) }
    }
  }

  #[ cfg( feature = "embeddings" ) ]
  impl Validate for crate::EmbeddingsRequest
  {
    #[ inline ]
    fn validate( &self ) -> ValidationResult
    {
      let mut errors = Vec::new();

      // Validate model name
      if let Err( e ) = validators::validate_model_name( &self.model )
      {
        errors.push( ValidationError
        {
          field : "model".to_string(),
          message : e,
          value : truncate_value( &self.model, 50 ),
          constraint : "non-empty, max 256 chars, alphanumeric+-_:/.".to_string(),
        });
      }

      // Validate prompt
      if let Err( e ) = validators::validate_prompt( &self.prompt )
      {
        errors.push( ValidationError
        {
          field : "prompt".to_string(),
          message : e,
          value : truncate_value( &self.prompt, 100 ),
          constraint : "non-empty, max 500k chars".to_string(),
        });
      }

      if errors.is_empty() { Ok( () ) } else { Err( errors ) }
    }
  }

  /// Truncate value for display in error messages
  #[ inline ]
  fn truncate_value( s : &str, max_len : usize ) -> String
  {
    if s.len() <= max_len
    {
      s.to_string()
    }
    else
    {
      format!( "{}... ({} chars total)", &s[ ..max_len ], s.len() )
    }
  }
}

#[ cfg( feature = "input_validation" ) ]
crate ::mod_interface!
{
  exposed use private::ValidationError;
  exposed use private::ValidationResult;
  exposed use private::Validate;
  exposed use private::validators;
}