api_openai 0.3.0

OpenAI's API for accessing large language models (LLMs).
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
//! Model Tuning Module
//!
//! This module provides stateless model tuning and fine-tuning utilities for `OpenAI` API models.
//! Following the "Thin Client, Rich API" principle, this module offers training management
//! patterns and optimization tools without automatic behaviors or persistent state management.

#![ allow( clippy::missing_inline_in_public_items, clippy::unused_async ) ]

mod private
{
  use std::
  {
    collections ::HashMap,
    time ::SystemTime,
  };
  use core::time::Duration;
  use serde::{ Deserialize, Serialize };
  use tokio::sync::mpsc;

  /// Fine-tuning job status
  #[ derive( Debug, Clone, PartialEq, Serialize, Deserialize ) ]
  pub enum TuningStatus
  {
    /// Job is being prepared and validated
    Validating,
    /// Job is queued for execution
    Queued,
    /// Training is in progress
    Running,
    /// Job completed successfully
    Succeeded,
    /// Job failed due to error
    Failed( String ),
    /// Job was cancelled by user
    Cancelled,
  }

  /// Training objective type
  #[ derive( Debug, Clone, PartialEq, Serialize, Deserialize ) ]
  pub enum TrainingObjective
  {
    /// Standard language modeling objective
    LanguageModeling,
    /// Supervised fine-tuning with custom objectives
    SupervisedFineTuning,
    /// Reinforcement learning from human feedback
    RLHF,
    /// Custom objective with specific parameters
    Custom
    {
      /// Objective name
      name : String,
      /// Objective-specific parameters
      parameters : HashMap<  String, String  >,
    },
  }

  /// Parameter-efficient fine-tuning method
  #[ derive( Debug, Clone, PartialEq, Serialize, Deserialize ) ]
  pub enum FineTuningMethod
  {
    /// Full model fine-tuning
    Full,
    /// Low-Rank Adaptation (`LoRA`)
    LoRA
    {
      /// Rank of adaptation matrices
      rank : u32,
      /// Alpha scaling parameter
      alpha : f64,
      /// Dropout rate for `LoRA` layers
      dropout : f64,
    },
    /// Adapter-based fine-tuning
    Adapter
    {
      /// Adapter hidden dimension
      hidden_dim : u32,
      /// Number of adapter layers
      num_layers : u32,
    },
    /// Prefix tuning
    PrefixTuning
    {
      /// Number of prefix tokens
      prefix_length : u32,
      /// Prefix embedding dimension
      embedding_dim : u32,
    },
  }

  /// Training hyperparameters
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct HyperParameters
  {
    /// Learning rate for optimization
    pub learning_rate : f64,
    /// Batch size for training
    pub batch_size : u32,
    /// Number of training epochs
    pub epochs : u32,
    /// Warmup steps for learning rate schedule
    pub warmup_steps : u32,
    /// Weight decay for regularization
    pub weight_decay : f64,
    /// Gradient clipping threshold
    pub gradient_clip_norm : f64,
    /// Learning rate schedule type
    pub lr_schedule : String,
    /// Custom hyperparameters
    pub custom_params : HashMap<  String, String  >,
  }

  impl Default for HyperParameters
  {
    fn default() -> Self
    {
      Self
      {
        learning_rate : 1e-4,
        batch_size : 32,
        epochs : 3,
        warmup_steps : 100,
        weight_decay : 0.01,
        gradient_clip_norm : 1.0,
        lr_schedule : "linear".to_string(),
        custom_params : HashMap::new(),
      }
    }
  }

  /// Training data configuration
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct TrainingDataConfig
  {
    /// Training dataset file path or identifier
    pub training_file : String,
    /// Validation dataset file path or identifier
    pub validation_file : Option< String >,
    /// Data format specification
    pub data_format : String,
    /// Maximum sequence length
    pub max_sequence_length : u32,
    /// Data preprocessing options
    pub preprocessing : HashMap<  String, String  >,
  }

  /// Model checkpoint information
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct ModelCheckpoint
  {
    /// Checkpoint identifier
    pub checkpoint_id : String,
    /// Training step at checkpoint
    pub step : u64,
    /// Training loss at checkpoint
    pub loss : f64,
    /// Validation metrics at checkpoint
    pub validation_metrics : HashMap<  String, f64  >,
    /// Checkpoint creation timestamp
    pub created_at : SystemTime,
    /// Checkpoint file path
    pub file_path : String,
  }

  /// Training progress metrics
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct TrainingMetrics
  {
    /// Current training step
    pub step : u64,
    /// Current epoch
    pub epoch : u32,
    /// Training loss
    pub training_loss : f64,
    /// Validation loss
    pub validation_loss : Option< f64 >,
    /// Learning rate
    pub learning_rate : f64,
    /// Throughput (tokens per second)
    pub throughput : f64,
    /// Estimated time remaining
    pub eta_seconds : Option< u64 >,
    /// Custom metrics
    pub custom_metrics : HashMap<  String, f64  >,
  }

  /// Model tuning job configuration
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct TuningJobConfig
  {
    /// Job name/identifier
    pub job_name : String,
    /// Base model to fine-tune
    pub base_model : String,
    /// Training data configuration
    pub training_data : TrainingDataConfig,
    /// Hyperparameters for training
    pub hyperparameters : HyperParameters,
    /// Fine-tuning method
    pub method : FineTuningMethod,
    /// Training objective
    pub objective : TrainingObjective,
    /// Resource requirements
    pub resource_requirements : TuningResourceRequirements,
    /// Checkpointing configuration
    pub checkpointing : CheckpointConfig,
    /// Environment variables for training
    pub env_vars : HashMap<  String, String  >,
  }

  /// Resource requirements for training
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct TuningResourceRequirements
  {
    /// Number of GPUs required
    pub gpu_count : u32,
    /// GPU type preference
    pub gpu_type : Option< String >,
    /// Memory requirements in GB
    pub memory_gb : u64,
    /// CPU cores required
    pub cpu_cores : u32,
    /// Storage requirements in GB
    pub storage_gb : u64,
  }

  impl Default for TuningResourceRequirements
  {
    fn default() -> Self
    {
      Self
      {
        gpu_count : 1,
        gpu_type : None,
        memory_gb : 16,
        cpu_cores : 4,
        storage_gb : 100,
      }
    }
  }

  /// Checkpointing configuration
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct CheckpointConfig
  {
    /// Enable automatic checkpointing
    pub enabled : bool,
    /// Checkpoint save interval (in steps)
    pub save_interval : u64,
    /// Maximum number of checkpoints to keep
    pub max_checkpoints : u32,
    /// Save directory for checkpoints
    pub save_directory : String,
  }

  impl Default for CheckpointConfig
  {
    fn default() -> Self
    {
      Self
      {
        enabled : true,
        save_interval : 1000,
        max_checkpoints : 5,
        save_directory : "./checkpoints".to_string(),
      }
    }
  }

  /// Model tuning job instance
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct TuningJob
  {
    /// Job configuration
    pub config : TuningJobConfig,
    /// Current job status
    pub status : TuningStatus,
    /// Job creation timestamp
    pub created_at : SystemTime,
    /// Last update timestamp
    pub updated_at : SystemTime,
    /// Current training metrics
    pub current_metrics : Option< TrainingMetrics >,
    /// Saved checkpoints
    pub checkpoints : Vec< ModelCheckpoint >,
    /// Job execution history
    pub execution_log : Vec< TuningEvent >,
  }

  impl TuningJob
  {
    /// Create a new tuning job
    #[ must_use ]
    pub fn new( config : TuningJobConfig ) -> Self
    {
      let now = SystemTime::now();
      Self
      {
        config,
        status : TuningStatus::Validating,
        created_at : now,
        updated_at : now,
        current_metrics : None,
        checkpoints : Vec::new(),
        execution_log : Vec::new(),
      }
    }

    /// Update job status
    pub fn update_status( &mut self, status : TuningStatus )
    {
      let event = TuningEvent
      {
        event_type : TuningEventType::StatusChanged
        {
          from : self.status.clone(),
          to : status.clone(),
        },
        message : format!( "Status changed from {:?} to {:?}", self.status, status ),
        timestamp : SystemTime::now(),
      };

      self.status = status;
      self.updated_at = SystemTime::now();
      self.execution_log.push( event );
    }

    /// Update training metrics
    pub fn update_metrics( &mut self, metrics : TrainingMetrics )
    {
      let event = TuningEvent
      {
        event_type : TuningEventType::MetricsUpdated
        {
          step : metrics.step,
          loss : metrics.training_loss,
        },
        message : format!( "Metrics updated at step {}", metrics.step ),
        timestamp : SystemTime::now(),
      };

      self.current_metrics = Some( metrics );
      self.updated_at = SystemTime::now();
      self.execution_log.push( event );
    }

    /// Add a checkpoint
    pub fn add_checkpoint( &mut self, checkpoint : ModelCheckpoint )
    {
      let event = TuningEvent
      {
        event_type : TuningEventType::CheckpointSaved
        {
          checkpoint_id : checkpoint.checkpoint_id.clone(),
          step : checkpoint.step,
        },
        message : format!( "Checkpoint saved at step {}", checkpoint.step ),
        timestamp : SystemTime::now(),
      };

      self.checkpoints.push( checkpoint );
      self.updated_at = SystemTime::now();
      self.execution_log.push( event );
    }

    /// Calculate job duration
    #[ must_use ]
    pub fn duration( &self ) -> Duration
    {
      self.updated_at.duration_since( self.created_at ).unwrap_or( Duration::from_secs( 0 ) )
    }
  }

  /// Tuning event for logging
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct TuningEvent
  {
    /// Event type
    pub event_type : TuningEventType,
    /// Event message
    pub message : String,
    /// Event timestamp
    pub timestamp : SystemTime,
  }

  /// Types of tuning events
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub enum TuningEventType
  {
    /// Status change event
    StatusChanged
    {
      /// Previous status
      from : TuningStatus,
      /// New status
      to : TuningStatus,
    },
    /// Metrics update event
    MetricsUpdated
    {
      /// Training step
      step : u64,
      /// Current loss
      loss : f64,
    },
    /// Checkpoint saved event
    CheckpointSaved
    {
      /// Checkpoint identifier
      checkpoint_id : String,
      /// Training step
      step : u64,
    },
  }

  /// Model tuning manager
  #[ derive( Debug ) ]
  pub struct TuningManager
  {
    /// Active tuning jobs
    jobs : HashMap<  String, TuningJob  >,
  }

  impl TuningManager
  {
    /// Create a new tuning manager
    #[ must_use ]
    pub fn new() -> Self
    {
      Self
      {
        jobs : HashMap::new(),
      }
    }

    /// Create a new tuning job
    ///
    /// # Errors
    /// Returns an error if a job with the same name already exists.
    pub async fn create_job( &mut self, config : TuningJobConfig ) -> Result< String, String >
    {
      let job_name = config.job_name.clone();

      if self.jobs.contains_key( &job_name )
      {
        return Err( format!( "Job with name '{job_name}' already exists" ) );
      }

      let job = TuningJob::new( config );
      self.jobs.insert( job_name.clone(), job );

      Ok( job_name )
    }

    /// Get a tuning job by name
    pub async fn get_job( &self, job_name : &str ) -> Option< &TuningJob >
    {
      self.jobs.get( job_name )
    }

    /// Get a mutable reference to a tuning job
    pub async fn get_job_mut( &mut self, job_name : &str ) -> Option< &mut TuningJob >
    {
      self.jobs.get_mut( job_name )
    }

    /// List all tuning jobs
    pub async fn list_jobs( &self ) -> Vec< &TuningJob >
    {
      self.jobs.values().collect()
    }

    /// Update job status
    ///
    /// # Errors
    /// Returns an error if the job is not found.
    pub async fn update_job_status( &mut self, job_name : &str, status : TuningStatus ) -> Result< (), String >
    {
      match self.jobs.get_mut( job_name )
      {
        Some( job ) =>
        {
          job.update_status( status );
          Ok( () )
        }
        None => Err( format!( "Job '{job_name}' not found" ) ),
      }
    }

    /// Cancel a tuning job
    ///
    /// # Errors
    /// Returns an error if the job is not found.
    pub async fn cancel_job( &mut self, job_name : &str ) -> Result< (), String >
    {
      self.update_job_status( job_name, TuningStatus::Cancelled ).await
    }

    /// Delete a tuning job
    ///
    /// # Errors
    /// Returns an error if the job is not found.
    pub async fn delete_job( &mut self, job_name : &str ) -> Result< (), String >
    {
      match self.jobs.remove( job_name )
      {
        Some( _ ) => Ok( () ),
        None => Err( format!( "Job '{job_name}' not found" ) ),
      }
    }

    /// Get tuning statistics
    #[ must_use ]
    pub fn tuning_stats( &self ) -> TuningStats
    {
      let mut stats = TuningStats
      {
        total : self.jobs.len(),
        validating : 0,
        queued : 0,
        running : 0,
        succeeded : 0,
        failed : 0,
        cancelled : 0,
      };

      for job in self.jobs.values()
      {
        match job.status
        {
          TuningStatus::Validating => stats.validating += 1,
          TuningStatus::Queued => stats.queued += 1,
          TuningStatus::Running => stats.running += 1,
          TuningStatus::Succeeded => stats.succeeded += 1,
          TuningStatus::Failed( _ ) => stats.failed += 1,
          TuningStatus::Cancelled => stats.cancelled += 1,
        }
      }

      stats
    }
  }

  impl Default for TuningManager
  {
    #[ inline ]
    fn default() -> Self
    {
      Self::new()
    }
  }

  /// Tuning statistics
  #[ derive( Debug, Clone ) ]
  pub struct TuningStats
  {
    /// Total number of jobs
    pub total : usize,
    /// Number of validating jobs
    pub validating : usize,
    /// Number of queued jobs
    pub queued : usize,
    /// Number of running jobs
    pub running : usize,
    /// Number of succeeded jobs
    pub succeeded : usize,
    /// Number of failed jobs
    pub failed : usize,
    /// Number of cancelled jobs
    pub cancelled : usize,
  }

  /// Tuning event notification
  #[ derive( Debug, Clone ) ]
  pub struct TuningNotification
  {
    /// Job name
    pub job_name : String,
    /// Current status
    pub status : TuningStatus,
    /// Current metrics (if available)
    pub metrics : Option< TrainingMetrics >,
    /// Notification timestamp
    pub timestamp : SystemTime,
  }

  /// Tuning event sender
  #[ derive( Debug ) ]
  pub struct TuningEventSender
  {
    /// Internal sender
    pub sender : mpsc::UnboundedSender< TuningNotification >,
  }

  /// Tuning event receiver
  #[ derive( Debug ) ]
  pub struct TuningEventReceiver
  {
    /// Internal receiver
    pub receiver : mpsc::UnboundedReceiver< TuningNotification >,
  }

  /// Model tuning utilities
  #[ derive( Debug ) ]
  pub struct ModelTuningUtils;

  impl ModelTuningUtils
  {
    /// Create a tuning event notifier
    #[ must_use ]
    pub fn create_event_notifier() -> ( TuningEventSender, TuningEventReceiver )
    {
      let ( tx, rx ) = mpsc::unbounded_channel();
      ( TuningEventSender { sender : tx }, TuningEventReceiver { receiver : rx } )
    }

    /// Validate tuning job configuration
    ///
    /// # Errors
    /// Returns a vector of validation error messages if the configuration is invalid.
    pub fn validate_config( config : &TuningJobConfig ) -> Result< (), Vec< String > >
    {
      let mut errors = Vec::new();

      if config.job_name.is_empty()
      {
        errors.push( "Job name cannot be empty".to_string() );
      }

      if config.base_model.is_empty()
      {
        errors.push( "Base model cannot be empty".to_string() );
      }

      if config.training_data.training_file.is_empty()
      {
        errors.push( "Training file cannot be empty".to_string() );
      }

      if config.hyperparameters.learning_rate <= 0.0
      {
        errors.push( "Learning rate must be positive".to_string() );
      }

      if config.hyperparameters.batch_size == 0
      {
        errors.push( "Batch size must be positive".to_string() );
      }

      if config.hyperparameters.epochs == 0
      {
        errors.push( "Number of epochs must be positive".to_string() );
      }

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

    /// Estimate training time
    #[ must_use ]
    pub fn estimate_training_time( config : &TuningJobConfig, dataset_size : u64 ) -> Duration
    {
      let tokens_per_epoch = dataset_size;
      let total_tokens = tokens_per_epoch * u64::from( config.hyperparameters.epochs );
      let tokens_per_second = 1000; // Estimated throughput

      let total_seconds = total_tokens / tokens_per_second;
      Duration::from_secs( total_seconds )
    }

    /// Estimate resource cost
    #[ must_use ]
    pub fn estimate_training_cost( config : &TuningJobConfig, duration : Duration ) -> f64
    {
      let hours = duration.as_secs_f64() / 3600.0;
      let gpu_cost_per_hour = match config.resource_requirements.gpu_type.as_deref()
      {
        Some( "A100" ) => 4.0,
        Some( "V100" ) => 2.0,
        Some( "T4" ) => 0.5,
        _ => 1.0,
      };

      hours * gpu_cost_per_hour * f64::from( config.resource_requirements.gpu_count )
    }

    /// Optimize hyperparameters
    #[ must_use ]
    pub fn suggest_hyperparameters( method : &FineTuningMethod, dataset_size : u64 ) -> HyperParameters
    {
      let mut params = HyperParameters::default();

      match method
      {
        FineTuningMethod::Full =>
        {
          params.learning_rate = if dataset_size > 100_000 { 1e-5 } else { 5e-5 };
          params.batch_size = if dataset_size > 50000 { 16 } else { 32 };
        }
        FineTuningMethod::LoRA { .. } =>
        {
          params.learning_rate = 1e-4;
          params.batch_size = 64;
          params.epochs = 5;
        }
        FineTuningMethod::Adapter { .. } =>
        {
          params.learning_rate = 5e-4;
          params.batch_size = 32;
          params.epochs = 10;
        }
        FineTuningMethod::PrefixTuning { .. } =>
        {
          params.learning_rate = 1e-3;
          params.batch_size = 16;
          params.epochs = 20;
        }
      }

      params
    }
  }

}

crate ::mod_interface!
{
  exposed use private::TuningStatus;
  exposed use private::TrainingObjective;
  exposed use private::FineTuningMethod;
  exposed use private::HyperParameters;
  exposed use private::TrainingDataConfig;
  exposed use private::ModelCheckpoint;
  exposed use private::TrainingMetrics;
  exposed use private::TuningJobConfig;
  exposed use private::TuningResourceRequirements;
  exposed use private::CheckpointConfig;
  exposed use private::TuningJob;
  exposed use private::TuningEvent;
  exposed use private::TuningEventType;
  exposed use private::TuningManager;
  exposed use private::TuningStats;
  exposed use private::TuningNotification;
  exposed use private::TuningEventSender;
  exposed use private::TuningEventReceiver;
  exposed use private::ModelTuningUtils;
}