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
//! Enhanced Embeddings Client with Intelligent Batching
//!
//! This module provides an enhanced embeddings client that automatically
//! batches requests for optimal performance and reduced API costs.

use mod_interface::mod_interface;

mod private
{
  use crate::
  {
    client ::Client,
    client_api_accessors ::ClientApiAccessors,
    error ::{ Result, OpenAIError },
    environment ::{ OpenaiEnvironment, EnvironmentInterface },
    components ::embeddings::CreateEmbeddingResponse,
    components ::embeddings_request::CreateEmbeddingRequest,
  };

  // Feature-gated imports
  #[ cfg( feature = "batching" ) ]
  use crate::request_batching::{ RequestBatcher, RequestSignature, BatchConfig, BatchMetrics };
  use std::sync::Arc;
  use core::time::Duration;

  /// Enhanced embeddings client with intelligent batching
  #[ derive( Debug ) ]
  pub struct EnhancedEmbeddings< 'client, E >
  where
    E : OpenaiEnvironment + EnvironmentInterface + Send + Sync + 'static,
  {
    client : &'client Client< E >,

    // Feature-gated batching fields
    #[ cfg( feature = "batching" ) ]
    batcher : Arc< RequestBatcher< CreateEmbeddingRequest > >,
    #[ cfg( feature = "batching" ) ]
    config : BatchConfig,
  }

  impl< 'client, E > EnhancedEmbeddings< 'client, E >
  where
    E : OpenaiEnvironment + EnvironmentInterface + Send + Sync + 'static,
  {
    /// Create new enhanced embeddings client with custom batching config
    #[ cfg( feature = "batching" ) ]
    #[ inline ]
    pub fn new( client : &'client Client< E >, config : BatchConfig ) -> Self
    {
      let batcher = Arc::new( RequestBatcher::new( config.clone() ) );

      Self
      {
        client,
        batcher,
        config,
      }
    }

    /// Create new enhanced embeddings client without batching
    #[ cfg( not( feature = "batching" ) ) ]
    #[ inline ]
    pub fn new( client : &'client Client< E >, _config : () ) -> Self
    {
      Self
      {
        client,
      }
    }

    /// Create new enhanced embeddings client with default batching
    #[ cfg( feature = "batching" ) ]
    #[ inline ]
    pub fn with_default_batching( client : &'client Client< E > ) -> Self
    {
      Self::new( client, BatchConfig::default() )
    }

    /// Create new enhanced embeddings client without batching
    #[ cfg( not( feature = "batching" ) ) ]
    #[ inline ]
    pub fn with_default_batching( client : &'client Client< E > ) -> Self
    {
      Self::new( client, () )
    }

    /// Create embeddings directly without batching (fallback method)
    ///
    /// # Errors
    ///
    /// Returns an error if the API request fails or if the response cannot be parsed.
    #[ inline ]
    pub async fn create_direct( &self, request : CreateEmbeddingRequest ) -> Result< CreateEmbeddingResponse >
    {
      // Use the client field directly for non-batched requests
      self.client.embeddings().create( request ).await
    }

    /// Create embeddings with automatic batching optimization
    ///
    /// # Errors
    ///
    /// Returns an error if the request fails, batching fails, or if the response cannot be parsed.
    #[ cfg( feature = "batching" ) ]
    #[ inline ]
    pub async fn create_batched( &self, request : CreateEmbeddingRequest ) -> Result< CreateEmbeddingResponse >
    {
      // Create request signature for batching analysis
      let request_json = serde_json::to_vec( &request )
        .map_err( | e | OpenAIError::Internal( format!( "Failed to serialize request : {e}" ) ) )?;

      let signature = RequestSignature::new( "POST", "embeddings", &request_json );

      // Submit request for batching
      let response_bytes = self.batcher.submit_request( signature, request ).await?;

      // Parse response
      let response : CreateEmbeddingResponse = serde_json::from_slice( &response_bytes )
        .map_err( | e | OpenAIError::Internal( format!( "Failed to parse response : {e}" ) ) )?;

      Ok( response )
    }

    /// Create multiple embeddings with optimal batching
    ///
    /// # Errors
    ///
    /// Returns an error if any batch request fails or if responses cannot be parsed.
    #[ inline ]
    pub async fn create_bulk( &self, texts : Vec< String >, model : String ) -> Result< Vec< CreateEmbeddingResponse > >
    {
      if texts.is_empty()
      {
        return Ok( vec![] );
      }

      // Analyze optimal batching strategy
      let optimal_batch_size = self.calculate_optimal_batch_size( texts.len() );
      let mut results = Vec::with_capacity( texts.len() );

      // Process in optimal chunks
      for chunk in texts.chunks( optimal_batch_size )
      {
        let chunk_results = self.process_text_chunk( chunk.to_vec(), model.clone() ).await?;
        results.extend( chunk_results );
      }

      Ok( results )
    }

    /// Process chunk of texts with intelligent batching
    async fn process_text_chunk( &self, texts : Vec< String >, model : String ) -> Result< Vec< CreateEmbeddingResponse > >
    {
      if texts.len() == 1
      {
        // Single request
        let request = CreateEmbeddingRequest::new_single( texts[0].clone(), model );
        let response = self.create_batched( request ).await?;
        Ok( vec![ response ] )
      }
      else
      {
        // Multiple requests - use API batch capability
        let request = CreateEmbeddingRequest::new_multiple( texts, model );
        let response = self.create_batched( request ).await?;

        // Split response based on number of inputs
        // In real implementation, this would properly handle the batch response
        Ok( vec![ response ] )
      }
    }

    /// Calculate optimal batch size based on request patterns
    fn calculate_optimal_batch_size( &self, total_requests : usize ) -> usize
    {
      // Use adaptive batch sizing based on total volume
      match total_requests
      {
        1..=10 => total_requests,
        11..=50 => 10,
        51..=200 => 25,
        201..=500 => 50,
        _ => self.config.max_batch_size,
      }
    }

    /// Get batching performance metrics
    #[ cfg( feature = "batching" ) ]
    #[ inline ]
    pub async fn get_metrics( &self ) -> BatchMetrics
    {
      self.batcher.get_metrics().await
    }

    /// Create streaming embeddings with batching
    ///
    /// # Errors
    ///
    /// Returns an error if the streaming setup fails or if channel creation fails.
    #[ inline ]
    pub fn create_streaming( &self, texts : Vec< String >, model : String ) -> Result< tokio::sync::mpsc::Receiver< core::result::Result< CreateEmbeddingResponse, OpenAIError > > >
    {
      let ( tx, rx ) = tokio::sync::mpsc::channel( 100 );
      let batcher = Arc::clone( &self.batcher );
      let batch_size = self.calculate_optimal_batch_size( texts.len() );

      tokio ::spawn( async move
      {
        for chunk in texts.chunks( batch_size )
        {
          let request = CreateEmbeddingRequest::new_multiple( chunk.to_vec(), model.clone() );
          let request_json = match serde_json::to_vec( &request )
          {
            Ok( json ) => json,
            Err( e ) =>
            {
              let _ = tx.send( Err( OpenAIError::Internal( format!( "Serialization failed : {e}" ) ) ) ).await;
              continue;
            }
          };

          let signature = RequestSignature::new( "POST", "embeddings", &request_json );

          match batcher.submit_request( signature, request ).await
          {
            Ok( response_bytes ) =>
            {
              match serde_json::from_slice::< CreateEmbeddingResponse >( &response_bytes )
              {
                Ok( response ) =>
                {
                  if tx.send( Ok( response ) ).await.is_err()
                  {
                    break; // Receiver dropped
                  }
                },
                Err( e ) =>
                {
                  let _ = tx.send( Err( OpenAIError::Internal( format!( "Parse failed : {e}" ) ) ) ).await;
                }
              }
            },
            Err( e ) =>
            {
              let _ = tx.send( Err( e ) ).await;
            }
          }
        }
      } );

      Ok( rx )
    }

  }

  /// Analyze batching potential for given requests (standalone function)
  #[ must_use ]
  #[ inline ]
  pub fn analyze_embedding_batching_potential( requests : &[ CreateEmbeddingRequest ] ) -> BatchingAnalysis
  {
    let signatures : Vec< RequestSignature > = requests.iter().map( | req |
    {
      let request_json = serde_json::to_vec( req ).unwrap_or_default();
      RequestSignature::new( "POST", "embeddings", &request_json )
    } ).collect();

    crate ::request_batching::BatchOptimizer::analyze_batching_potential( &signatures )
  }

  impl< E > EnhancedEmbeddings< '_, E >
  where
    E : OpenaiEnvironment + EnvironmentInterface + Send + Sync + 'static,
  {
    /// Flush all pending requests
    #[ inline ]
    pub async fn flush_pending( &self )
    {
      self.batcher.flush_all_pending().await;
    }
  }

  /// Configuration for enhanced embeddings
  #[ derive( Debug, Clone ) ]
  pub struct EnhancedEmbeddingsConfig
  {
    /// Batching configuration
    pub batch_config : BatchConfig,
    /// Enable automatic request optimization
    pub enable_optimization : bool,
    /// Maximum concurrent requests
    pub max_concurrent_requests : usize,
    /// Request timeout
    pub request_timeout : Duration,
  }

  impl Default for EnhancedEmbeddingsConfig
  {
    #[ inline ]
    fn default() -> Self
    {
      Self
      {
        batch_config : BatchConfig::default(),
        enable_optimization : true,
        max_concurrent_requests : 20,
        request_timeout : Duration::from_secs( 60 ),
      }
    }
  }

  /// Embedding batching analysis results
  #[ cfg( feature = "batching" ) ]
  pub use crate::request_batching::BatchingAnalysis;

  /// Smart embedding processing strategies
  #[ derive( Debug ) ]
  pub struct EmbeddingBatchProcessor;

  impl EmbeddingBatchProcessor
  {
    /// Process large text collections with optimal batching
    ///
    /// # Errors
    ///
    /// Returns an error if any batch processing fails or if responses cannot be parsed.
    #[ inline ]
    pub async fn process_document_collection< E >(
      client : &EnhancedEmbeddings< '_, E >,
      documents : Vec< String >,
      model : String,
    ) -> Result< Vec< CreateEmbeddingResponse > >
    where
      E : OpenaiEnvironment + EnvironmentInterface + Send + Sync + 'static,
    {
      // Analyze text lengths and optimize batching strategy
      let avg_text_length : usize = documents.iter().map( std::string::String::len ).sum::< usize >() / documents.len().max( 1 );

      let optimal_batch_size = match avg_text_length
      {
        0..=100 => 100,      // Short texts - large batches
        101..=500 => 50,     // Medium texts - medium batches
        501..=2000 => 25,    // Long texts - smaller batches
        _ => 10,             // Very long texts - small batches
      };

      let mut all_results = Vec::with_capacity( documents.len() );

      for chunk in documents.chunks( optimal_batch_size )
      {
        let chunk_results = client.create_bulk( chunk.to_vec(), model.clone() ).await?;
        all_results.extend( chunk_results );
      }

      Ok( all_results )
    }

    /// Process embeddings with cost optimization
    ///
    /// # Errors
    ///
    /// Returns an error if any batch request fails or if cost calculations fail.
    #[ inline ]
    pub async fn process_with_cost_optimization< E >(
      client : &EnhancedEmbeddings< '_, E >,
      texts : Vec< String >,
      model : String,
      max_cost_per_batch : f64,
    ) -> Result< Vec< CreateEmbeddingResponse > >
    where
      E : OpenaiEnvironment + EnvironmentInterface + Send + Sync + 'static,
    {
      // Estimate cost and optimize batching for budget
      let estimated_tokens_per_text : usize = texts.iter().map( | t | t.len() / 4 ).sum(); // Rough estimate
      let cost_per_token = 0.0001; // Example cost
      let estimated_total_cost = estimated_tokens_per_text as f64 * cost_per_token;

      if estimated_total_cost <= max_cost_per_batch
      {
        // Process all at once
        client.create_bulk( texts, model ).await
      }
      else
      {
        // Split into cost-effective batches
        #[ allow(clippy::cast_possible_truncation, clippy::cast_sign_loss) ]
        let texts_per_batch = ( max_cost_per_batch / cost_per_token / 250.0 ) as usize; // 250 tokens avg per text
        let batch_size = texts_per_batch.clamp( 1, 100 );

        let mut results = Vec::new();
        for chunk in texts.chunks( batch_size )
        {
          let chunk_results = client.create_bulk( chunk.to_vec(), model.clone() ).await?;
          results.extend( chunk_results );
        }
        Ok( results )
      }
    }
  }
}

mod_interface!
{
  exposed use
  {
    EnhancedEmbeddings,
    EnhancedEmbeddingsConfig,
    EmbeddingBatchProcessor,
    analyze_embedding_batching_potential,
  };

  #[ cfg( feature = "batching" ) ]
  exposed use BatchingAnalysis;
}