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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! Request and response models for Ollama API client.

#[ cfg( feature = "enabled" ) ]
mod private
{
  use serde::{ Serialize, Deserialize };
  use std::collections::HashMap;

  // =====================================
  // Core Message Types
  // =====================================

  /// A message in a conversation
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct Message
  {
    /// The role of the message sender
    pub role : MessageRole,
    /// The content of the message
    pub content : String,
    /// Optional images for multimodal models
    #[ cfg( feature = "vision_support" ) ]
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub images : Option< Vec< String > >,
  }

  /// Role of the message sender in a conversation
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub enum MessageRole
  {
    /// Message from the user
    #[ serde( rename = "user" ) ]
    User,
    /// Message from the AI assistant
    #[ serde( rename = "assistant" ) ]
    Assistant,
    /// System message for context
    #[ serde( rename = "system" ) ]
    System,
  }

  /// A chat message with extended properties
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct ChatMessage
  {
    /// The role of the message sender
    pub role : MessageRole,
    /// The content of the message
    pub content : String,
    /// Optional images for vision models
    #[ cfg( feature = "vision_support" ) ]
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub images : Option< Vec< String > >,
    /// Optional tool calls for function calling models
    #[ cfg( feature = "tool_calling" ) ]
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub tool_calls : Option< Vec< ToolCall > >,
  }

  /// Tool call for function calling models
  #[ cfg( feature = "tool_calling" ) ]
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct ToolCall
  {
    /// Tool function name
    pub function : String,
    /// Tool arguments as JSON
    pub arguments : serde_json::Value,
  }

  // =====================================
  // Chat API Types
  // =====================================

  /// Request for chat completions
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct ChatRequest
  {
    /// Model name to use for the chat
    pub model : String,
    /// List of messages in the conversation
    pub messages : Vec< ChatMessage >,
    /// Enable streaming response
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub stream : Option< bool >,
    /// Sampling temperature (0.0 to 2.0)
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub temperature : Option< f32 >,
    /// Top-p nucleus sampling (0.0 to 1.0)
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub top_p : Option< f32 >,
    /// Top-k sampling (integer)
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub top_k : Option< u32 >,
    /// Maximum tokens to generate
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub max_tokens : Option< u32 >,
    /// Stop sequences
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub stop : Option< Vec< String > >,
    /// System prompt
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub system : Option< String >,
    /// Chat template
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub template : Option< String >,
    /// Additional options
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub options : Option< HashMap<  String, serde_json::Value  > >,
    /// Format for the response
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub format : Option< String >,
    /// Whether to keep the model loaded
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub keep_alive : Option< String >,
  }

  /// Response from chat completions
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct ChatResponse
  {
    /// Model that generated the response
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub model : Option< String >,
    /// Creation timestamp
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub created_at : Option< String >,
    /// The generated message
    pub message : ChatMessage,
    /// Whether the response is complete
    pub done : bool,
    /// Reason for completion
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub done_reason : Option< String >,
    /// Total processing duration in nanoseconds
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub total_duration : Option< u64 >,
    /// Model loading duration in nanoseconds
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub load_duration : Option< u64 >,
    /// Number of tokens in prompt
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub prompt_eval_count : Option< u32 >,
    /// Prompt evaluation duration in nanoseconds
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub prompt_eval_duration : Option< u64 >,
    /// Number of tokens in response
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub eval_count : Option< u32 >,
    /// Response generation duration in nanoseconds
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub eval_duration : Option< u64 >,
  }

  // =====================================
  // Generate API Types
  // =====================================

  /// Request for text generation
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct GenerateRequest
  {
    /// Model name to use for generation
    pub model : String,
    /// Input prompt
    pub prompt : String,
    /// Enable streaming response
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub stream : Option< bool >,
    /// System prompt
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub system : Option< String >,
    /// Generation template
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub template : Option< String >,
    /// Context from previous generation
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub context : Option< Vec< u8 > >,
    /// Additional generation options
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub options : Option< HashMap<  String, serde_json::Value  > >,
    /// Response format
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub format : Option< String >,
    /// Keep alive duration
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub keep_alive : Option< String >,
    /// Images for multimodal models
    #[ cfg( feature = "vision_support" ) ]
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub images : Option< Vec< String > >,
  }

  /// Response from text generation
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct GenerateResponse
  {
    /// Model that generated the response
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub model : Option< String >,
    /// Creation timestamp
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub created_at : Option< String >,
    /// Generated response text
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub response : Option< String >,
    /// Whether generation is complete
    #[ serde( default ) ]
    pub done : bool,
    /// Context for next generation
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub context : Option< Vec< u8 > >,
    /// Total processing duration in nanoseconds
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub total_duration : Option< u64 >,
    /// Model loading duration in nanoseconds
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub load_duration : Option< u64 >,
    /// Number of tokens in prompt
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub prompt_eval_count : Option< u32 >,
    /// Prompt evaluation duration in nanoseconds
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub prompt_eval_duration : Option< u64 >,
    /// Number of tokens in response
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub eval_count : Option< u32 >,
    /// Response generation duration in nanoseconds
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub eval_duration : Option< u64 >,
  }

  // =====================================
  // Model Management Types
  // =====================================

  /// Information about a model
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct ModelInfo
  {
    /// Model name
    pub name : String,
    /// Model size in bytes
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub size : Option< u64 >,
    /// Model family/architecture
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub family : Option< String >,
    /// Parameter count
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub parameter_size : Option< String >,
    /// Quantization level
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub quantization_level : Option< String >,
    /// Model details
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub details : Option< ModelDetails >,
    /// Model digest/hash
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub digest : Option< String >,
    /// Modified timestamp
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub modified_at : Option< String >,
  }

  /// Detailed model information
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct ModelDetails
  {
    /// Model format (e.g., "gguf")
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub format : Option< String >,
    /// Model family
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub family : Option< String >,
    /// Model families
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub families : Option< Vec< String > >,
    /// Parameter size
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub parameter_size : Option< String >,
    /// Quantization level
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub quantization_level : Option< String >,
  }

  /// Model entry in the list
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct ModelEntry
  {
    /// Model name
    pub name : String,
    /// Model digest
    pub digest : String,
    /// Model size in bytes
    pub size : u64,
    /// Modified timestamp
    pub modified_at : String,
    /// Model details
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub details : Option< ModelDetails >,
  }

  /// Response from listing models
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct TagsResponse
  {
    /// List of available models
    pub models : Vec< ModelEntry >,
  }

  /// Request to show model information
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct ShowModelRequest
  {
    /// Model name to show information for
    pub name : String,
    /// Whether to include verbose details
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub verbose : Option< bool >,
  }

  /// Request to pull a model
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct PullModelRequest
  {
    /// Model name to pull
    pub name : String,
    /// Enable insecure connections
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub insecure : Option< bool >,
    /// Enable streaming progress
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub stream : Option< bool >,
  }

  /// Request to push a model
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct PushModelRequest
  {
    /// Model name to push
    pub name : String,
    /// Enable insecure connections
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub insecure : Option< bool >,
    /// Enable streaming progress
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub stream : Option< bool >,
  }

  /// Request to delete a model
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct DeleteModelRequest
  {
    /// Model name to delete
    pub name : String,
  }

  // =====================================
  // Embeddings API Types
  // =====================================

  /// Request for generating embeddings
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct EmbeddingsRequest
  {
    /// Model name to use for embeddings
    pub model : String,
    /// Input text or texts
    pub prompt : String,
    /// Additional options
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub options : Option< HashMap<  String, serde_json::Value  > >,
    /// Keep alive duration
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub keep_alive : Option< String >,
  }

  /// Response from embeddings generation
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct EmbeddingsResponse
  {
    /// Generated embedding vectors
    pub embedding : Vec< f64 >,
  }

  // =====================================
  // Utility Types
  // =====================================

  /// Progress information for model operations
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct ProgressInfo
  {
    /// Status message
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub status : Option< String >,
    /// Progress digest
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub digest : Option< String >,
    /// Total size in bytes
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub total : Option< u64 >,
    /// Completed size in bytes
    #[ serde( skip_serializing_if = "Option::is_none" ) ]
    pub completed : Option< u64 >,
  }

  /// Error information from API responses
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct ApiError
  {
    /// Error message
    pub error : String,
  }

  // =====================================
  // Implementation blocks
  // =====================================

  impl ChatRequest
  {
    /// Create a new chat request with the given model and messages
    #[ inline ]
    #[ must_use ]
    pub fn new( model : String, messages : Vec< ChatMessage > ) -> Self
    {
      Self
      {
        model,
        messages,
        stream : None,
        temperature : None,
        top_p : None,
        top_k : None,
        max_tokens : None,
        stop : None,
        system : None,
        template : None,
        options : None,
        format : None,
        keep_alive : None,
      }
    }

    /// Set streaming mode
    #[ inline ]
    #[ must_use ]
    pub fn with_stream( mut self, stream : bool ) -> Self
    {
      self.stream = Some( stream );
      self
    }

    /// Set temperature for sampling
    #[ inline ]
    #[ must_use ]
    pub fn with_temperature( mut self, temperature : f32 ) -> Self
    {
      self.temperature = Some( temperature );
      self
    }

    /// Set system prompt
    #[ inline ]
    #[ must_use ]
    pub fn with_system( mut self, system : String ) -> Self
    {
      self.system = Some( system );
      self
    }
  }

  impl GenerateRequest
  {
    /// Create a new generate request with the given model and prompt
    #[ inline ]
    #[ must_use ]
    pub fn new( model : String, prompt : String ) -> Self
    {
      Self
      {
        model,
        prompt,
        stream : None,
        system : None,
        template : None,
        context : None,
        options : None,
        format : None,
        keep_alive : None,
        #[ cfg( feature = "vision_support" ) ]
        images : None,
      }
    }

    /// Set streaming mode
    #[ inline ]
    #[ must_use ]
    pub fn with_stream( mut self, stream : bool ) -> Self
    {
      self.stream = Some( stream );
      self
    }

    /// Set system prompt
    #[ inline ]
    #[ must_use ]
    pub fn with_system( mut self, system : String ) -> Self
    {
      self.system = Some( system );
      self
    }

    /// Set context from previous generation
    #[ inline ]
    #[ must_use ]
    pub fn with_context( mut self, context : Vec< u8 > ) -> Self
    {
      self.context = Some( context );
      self
    }
  }

  impl ChatMessage
  {
    /// Create a new user message
    #[ inline ]
    #[ must_use ]
    pub fn user( content : String ) -> Self
    {
      Self
      {
        role : MessageRole::User,
        content,
        #[ cfg( feature = "vision_support" ) ]
        images : None,
        #[ cfg( feature = "tool_calling" ) ]
        tool_calls : None,
      }
    }

    /// Create a new assistant message
    #[ inline ]
    #[ must_use ]
    pub fn assistant( content : String ) -> Self
    {
      Self
      {
        role : MessageRole::Assistant,
        content,
        #[ cfg( feature = "vision_support" ) ]
        images : None,
        #[ cfg( feature = "tool_calling" ) ]
        tool_calls : None,
      }
    }

    /// Create a new system message
    #[ inline ]
    #[ must_use ]
    pub fn system( content : String ) -> Self
    {
      Self
      {
        role : MessageRole::System,
        content,
        #[ cfg( feature = "vision_support" ) ]
        images : None,
        #[ cfg( feature = "tool_calling" ) ]
        tool_calls : None,
      }
    }
  }

  impl EmbeddingsRequest
  {
    /// Create a new embeddings request
    #[ inline ]
    #[ must_use ]
    pub fn new( model : String, prompt : String ) -> Self
    {
      Self
      {
        model,
        prompt,
        options : None,
        keep_alive : None,
      }
    }
  }
}

#[ cfg( feature = "enabled" ) ]
crate ::mod_interface!
{
  exposed use private::Message;
  exposed use private::MessageRole;
  exposed use private::ChatMessage;
  exposed use private::ChatRequest;
  exposed use private::ChatResponse;
  exposed use private::GenerateRequest;
  exposed use private::GenerateResponse;
  exposed use private::ModelInfo;
  exposed use private::ModelDetails;
  exposed use private::ModelEntry;
  exposed use private::TagsResponse;
  exposed use private::ShowModelRequest;
  exposed use private::PullModelRequest;
  exposed use private::PushModelRequest;
  exposed use private::DeleteModelRequest;
  exposed use private::EmbeddingsRequest;
  exposed use private::EmbeddingsResponse;
  exposed use private::ProgressInfo;
  exposed use private::ApiError;

  #[ cfg( feature = "tool_calling" ) ]
  exposed use private::ToolCall;
}