api_huggingface 0.4.1

HuggingFace's API for accessing large language models (LLMs) and embeddings.
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
//! Synchronous API wrappers for `HuggingFace` client
//!
//! This module provides blocking wrappers around the async `HuggingFace` API,
//! allowing usage in synchronous contexts without explicit async/await.
//!
//! # Features
//!
//! - **Blocking Operations**: All async operations wrapped as blocking calls
//! - **Thread Safety**: Client is Send + Sync and can be shared across threads
//! - **Runtime Management**: Tokio runtime automatically managed internally
//! - **Zero Magic**: Simple blocking wrappers with no automatic behavior
//!
//! # Usage Example
//!
//! ```no_run
//! use api_huggingface::sync::SyncClient;
//!
//! let client = SyncClient::new( "your_api_key".to_string() )
//!   .expect( "Failed to create client" );
//!
//! // Blocking call - no async/await needed
//! let result = client.inference().create(
//!   "What is 2+2?",
//!   "mistralai/Mistral-7B-Instruct-v0.3"
//! );
//!
//! match result
//! {
//!   Ok( response ) => println!( "Response : {}", response.extract_text_or_default( "" ) ),
//!   Err( e ) => eprintln!( "Error : {}", e ),
//! }
//! ```

use crate::
{
  Client,
  environment::HuggingFaceEnvironmentImpl,
  secret::Secret,
  error::{ Result, HuggingFaceError },
  components::
  {
  input::InferenceParameters,
  inference_shared::InferenceResponse,
  },
  token_counter::{ TokenCounter, CountingStrategy },
  cache::{ Cache, CacheConfig },
};

use std::sync::Arc;
use tokio::sync::mpsc;

/// Synchronous streaming iterator
///
/// Wraps an async stream and provides blocking iteration over stream tokens.
/// Each call to `next()` blocks until the next token is available or the stream ends.
///
/// # Examples
///
/// ```no_run
/// # use api_huggingface::{sync::SyncClient, components::input::InferenceParameters};
/// # fn example() -> Result< (), Box< dyn std::error::Error > > {
/// # let client = SyncClient::new("hf_test".to_string())?;
/// # let params = InferenceParameters::new();
/// let stream = client.inference().create_stream( "Hello", "model", params )?;
///
/// for token_result in stream
/// {
///   match token_result
///   {
///     Ok( token ) => print!( "{token}" ),
///     Err( e ) => eprintln!( "Error : {e}" ),
///   }
/// }
/// # Ok(())
/// # }
/// ```
pub struct SyncStream
{
  receiver : mpsc::Receiver< Result< String > >,
  runtime : Arc< tokio::runtime::Runtime >,
}

impl Iterator for SyncStream
{
  type Item = Result< String >;

  #[ inline ]
  fn next( &mut self ) -> Option< Self::Item >
  {
  self.runtime.block_on( async
  {
      self.receiver.recv().await
  } )
  }
}

impl core::fmt::Debug for SyncStream
{
  #[ inline ]
  fn fmt( &self, f : &mut core::fmt::Formatter< '_ > ) -> core::fmt::Result
  {
  f.debug_struct( "SyncStream" )
      .field( "receiver", &"< Receiver >" )
      .field( "runtime", &"< Runtime >" )
      .finish()
  }
}

/// Synchronous cache wrapper
///
/// Wraps an async `Cache` and provides blocking methods for cache operations.
///
/// # Examples
///
/// ```no_run
/// # use api_huggingface::sync::SyncClient;
/// # fn example() -> Result< (), Box< dyn std::error::Error > > {
/// let client = SyncClient::new( "api_key".to_string() )?;
/// let cache = client.cache();
///
/// cache.insert( "key", "value", None );
/// if let Some( value ) = cache.get( &"key" )
/// {
///   println!( "Cached : {value}" );
/// }
/// # Ok(())
/// # }
/// ```
pub struct SyncCache< K, V >
{
  cache : Cache< K, V >,
  runtime : Arc< tokio::runtime::Runtime >,
}

impl< K, V > SyncCache< K, V >
where
  K : Eq + core::hash::Hash + Clone,
  V : Clone,
{
  /// Insert a value into the cache
  #[ inline ]
  pub fn insert( &self, key : K, value : V, ttl : Option< core::time::Duration > )
  {
  let cache = self.cache.clone();
  self.runtime.block_on( async move
  {
      cache.insert( key, value, ttl ).await;
  } );
  }

  /// Get a value from the cache
  #[ inline ]
  pub fn get( &self, key : &K ) -> Option< V >
  {
  let cache = self.cache.clone();
  let key = key.clone();
  self.runtime.block_on( async move
  {
      cache.get( &key ).await
  } )
  }

  /// Check if key exists in cache
  #[ inline ]
  pub fn contains_key( &self, key : &K ) -> bool
  {
  let cache = self.cache.clone();
  let key = key.clone();
  self.runtime.block_on( async move
  {
      cache.contains_key( &key ).await
  } )
  }

  /// Remove entry from cache
  #[ inline ]
  pub fn remove( &self, key : &K ) -> Option< V >
  {
  let cache = self.cache.clone();
  let key = key.clone();
  self.runtime.block_on( async move
  {
      cache.remove( &key ).await
  } )
  }

  /// Clear all entries from cache
  #[ inline ]
  pub fn clear( &self )
  {
  let cache = self.cache.clone();
  self.runtime.block_on( async move
  {
      cache.clear().await;
  } );
  }

  /// Get current cache size
  #[ inline ]
  #[ must_use ]
  pub fn len( &self ) -> usize
  {
  let cache = self.cache.clone();
  self.runtime.block_on( async move
  {
      cache.len().await
  } )
  }

  /// Check if cache is empty
  #[ inline ]
  #[ must_use ]
  pub fn is_empty( &self ) -> bool
  {
  let cache = self.cache.clone();
  self.runtime.block_on( async move
  {
      cache.is_empty().await
  } )
  }
}

impl< K, V > core::fmt::Debug for SyncCache< K, V >
{
  #[ inline ]
  fn fmt( &self, f : &mut core::fmt::Formatter< '_ > ) -> core::fmt::Result
  {
  f.debug_struct( "SyncCache" )
      .field( "cache", &"< Cache >" )
      .field( "runtime", &"< Runtime >" )
      .finish()
  }
}

/// Synchronous wrapper around the async `HuggingFace` client
///
/// This client uses an internal Tokio runtime to execute async operations
/// as blocking calls. The runtime is created once when the client is built
/// and reused for all operations.
///
/// # Thread Safety
///
/// This client is Send + Sync and can be safely shared across threads using Arc.
///
/// # Runtime Management
///
/// The internal Tokio runtime is automatically cleaned up when the client is dropped.
#[ derive( Debug ) ]
pub struct SyncClient
{
  client : Client< HuggingFaceEnvironmentImpl >,
  runtime : Arc< tokio::runtime::Runtime >,
}

impl SyncClient
{
  /// Create a new synchronous client from an API key
  ///
  /// # Arguments
  ///
  /// * `api_key` - `HuggingFace` API key
  ///
  /// # Errors
  ///
  /// Returns error if:
  /// - API key is invalid
  /// - Environment cannot be initialized
  /// - Tokio runtime cannot be created
  ///
  /// # Examples
  ///
  /// ```no_run
  /// use api_huggingface::sync::SyncClient;
  ///
  /// let client = SyncClient::new( "hf_...".to_string() )
  ///   .expect( "Failed to create client" );
  /// ```
  #[ inline ]
  pub fn new( api_key : String ) -> Result< Self >
  {
  let secret = Secret::new( api_key );
  let env = HuggingFaceEnvironmentImpl::build( secret, None )?;
  let client = Client::build( env )?;

  let runtime = tokio::runtime::Runtime::new()
      .map_err( |e| HuggingFaceError::Generic( format!( "Failed to create runtime : {e}" ) ) )?;

  Ok( Self
  {
      client,
      runtime : Arc::new( runtime ),
  } )
  }

  /// Create a synchronous client with a custom base URL
  ///
  /// # Arguments
  ///
  /// * `api_key` - `HuggingFace` API key
  /// * `base_url` - Custom base URL for the API
  ///
  /// # Errors
  ///
  /// Returns error if:
  /// - API key is invalid
  /// - Base URL is invalid
  /// - Environment cannot be initialized
  /// - Tokio runtime cannot be created
  #[ inline ]
  pub fn with_base_url( api_key : String, base_url : String ) -> Result< Self >
  {
  let secret = Secret::new( api_key );
  let env = HuggingFaceEnvironmentImpl::build( secret, Some( base_url ) )?;
  let client = Client::build( env )?;

  let runtime = tokio::runtime::Runtime::new()
      .map_err( |e| HuggingFaceError::Generic( format!( "Failed to create runtime : {e}" ) ) )?;

  Ok( Self
  {
      client,
      runtime : Arc::new( runtime ),
  } )
  }

  /// Get a synchronous inference API interface
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use api_huggingface::sync::SyncClient;
  /// # fn example() -> Result< (), Box< dyn std::error::Error > > {
  /// let client = SyncClient::new( "api_key".to_string() )?;
  /// let _result = client.inference().create( "Hello", "model" );
  /// # Ok(())
  /// # }
  /// ```
  #[ inline ]
  #[ must_use ]
  pub fn inference( &self ) -> SyncInference
  {
  SyncInference
  {
      client : self.client.clone(),
      runtime : Arc::clone( &self.runtime ),
  }
  }

  /// Get a token counter instance
  ///
  /// Returns a new `TokenCounter` with the default estimation strategy.
  /// The token counter is already synchronous, so no runtime wrapping is needed.
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use api_huggingface::sync::SyncClient;
  /// # fn example() -> Result< (), Box< dyn std::error::Error > > {
  /// let client = SyncClient::new( "api_key".to_string() )?;
  /// let counter = client.token_counter();
  /// let count = counter.count_tokens( "Hello world" );
  /// println!( "Tokens : {}", count.total );
  /// # Ok(())
  /// # }
  /// ```
  #[ inline ]
  #[ must_use ]
  pub fn token_counter( &self ) -> TokenCounter
  {
  TokenCounter::new( CountingStrategy::Estimation )
  }

  /// Get a token counter with a specific strategy
  ///
  /// # Arguments
  ///
  /// * `strategy` - The counting strategy to use
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use api_huggingface::{sync::SyncClient, token_counter::CountingStrategy};
  /// # fn example() -> Result< (), Box< dyn std::error::Error > > {
  /// let client = SyncClient::new( "api_key".to_string() )?;
  /// let counter = client.token_counter_with_strategy( CountingStrategy::CharacterBased );
  /// let count = counter.count_tokens( "Hello world" );
  /// # let _ = count;
  /// # Ok(())
  /// # }
  /// ```
  #[ inline ]
  #[ must_use ]
  pub fn token_counter_with_strategy( &self, strategy : CountingStrategy ) -> TokenCounter
  {
  TokenCounter::new( strategy )
  }

  /// Create a new cache with default configuration
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use api_huggingface::sync::SyncClient;
  /// # fn example() -> Result< (), Box< dyn std::error::Error > > {
  /// let client = SyncClient::new( "api_key".to_string() )?;
  /// let cache = client.cache::< String, String >();
  ///
  /// cache.insert( "key".to_string(), "value".to_string(), None );
  /// let _value = cache.get( &"key".to_string() );
  /// # Ok(())
  /// # }
  /// ```
  #[ inline ]
  #[ must_use ]
  pub fn cache< K, V >( &self ) -> SyncCache< K, V >
  where
  K : Eq + core::hash::Hash + Clone,
  V : Clone,
  {
  SyncCache
  {
      cache : Cache::new( CacheConfig::default() ),
      runtime : Arc::clone( &self.runtime ),
  }
  }

  /// Create a cache with custom configuration
  ///
  /// # Arguments
  ///
  /// * `config` - Cache configuration
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use api_huggingface::{sync::SyncClient, cache::CacheConfig};
  /// # use std::time::Duration;
  /// # fn example() -> Result< (), Box< dyn std::error::Error > > {
  /// let client = SyncClient::new( "api_key".to_string() )?;
  /// let config = CacheConfig
  /// {
  ///   max_entries : 100,
  ///   default_ttl : Some( Duration::from_secs( 60 ) ),
  /// };
  /// let _cache = client.cache_with_config::< String, String >( config );
  /// # Ok(())
  /// # }
  /// ```
  #[ inline ]
  #[ must_use ]
  pub fn cache_with_config< K, V >( &self, config : CacheConfig ) -> SyncCache< K, V >
  where
  K : Eq + core::hash::Hash + Clone,
  V : Clone,
  {
  SyncCache
  {
      cache : Cache::new( config ),
      runtime : Arc::clone( &self.runtime ),
  }
  }
}

/// Synchronous inference API interface
///
/// Provides blocking wrappers around inference operations.
#[ derive( Debug ) ]
pub struct SyncInference
{
  client : Client< HuggingFaceEnvironmentImpl >,
  runtime : Arc< tokio::runtime::Runtime >,
}

impl SyncInference
{
  /// Create a blocking text generation inference request
  ///
  /// This is a simple synchronous wrapper around the async inference API.
  ///
  /// # Arguments
  ///
  /// * `inputs` - Input text or prompt
  /// * `model` - Model identifier
  ///
  /// # Errors
  ///
  /// Returns error if the API call fails
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use api_huggingface::sync::SyncClient;
  /// # fn example() -> Result< (), Box< dyn std::error::Error > > {
  /// # let client = SyncClient::new("hf_test".to_string())?;
  /// let _result = client.inference().create(
  ///   "What is 2+2?",
  ///   "mistralai/Mistral-7B-Instruct-v0.3"
  /// )?;
  /// # Ok(())
  /// # }
  /// ```
  #[ inline ]
  pub fn create< I, M >( &self, inputs : I, model : M ) -> Result< InferenceResponse >
  where
  I : Into< String >,
  M : AsRef< str >,
  {
  let inference = self.client.inference();
  self.runtime.block_on( async move
  {
      inference.create( inputs, model ).await
  })
  }

  /// Create a blocking text generation inference request with parameters
  ///
  /// # Arguments
  ///
  /// * `inputs` - Input text or prompt
  /// * `model` - Model identifier
  /// * `parameters` - Inference parameters (temperature, `max_tokens`, etc.)
  ///
  /// # Errors
  ///
  /// Returns error if the API call fails
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use api_huggingface::{sync::SyncClient, components::input::InferenceParameters};
  /// # fn example() -> Result< (), Box< dyn std::error::Error > > {
  /// # let client = SyncClient::new("hf_test".to_string())?;
  /// let params = InferenceParameters::new()
  ///   .with_temperature( 0.7 )
  ///   .with_max_new_tokens( 150 );
  ///
  /// let _result = client.inference().create_with_parameters(
  ///   "Explain quantum computing",
  ///   "mistralai/Mistral-7B-Instruct-v0.3",
  ///   params
  /// )?;
  /// # Ok(())
  /// # }
  /// ```
  #[ inline ]
  pub fn create_with_parameters< I, M >(
  &self,
  inputs : I,
  model : M,
  parameters : InferenceParameters
  ) -> Result< InferenceResponse >
  where
  I : Into< String >,
  M : AsRef< str >,
  {
  let inference = self.client.inference();
  self.runtime.block_on( async move
  {
      inference.create_with_parameters( inputs, model, parameters ).await
  })
  }

  /// Create a blocking streaming inference request
  ///
  /// Returns an iterator that yields tokens as they become available.
  /// Each iteration blocks until the next token arrives or the stream ends.
  ///
  /// # Arguments
  ///
  /// * `inputs` - Input text or prompt
  /// * `model` - Model identifier
  /// * `parameters` - Inference parameters (will be modified to enable streaming)
  ///
  /// # Errors
  ///
  /// Returns error if the API call fails to initialize
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use api_huggingface::{sync::SyncClient, components::input::InferenceParameters};
  /// # fn example() -> Result< (), Box< dyn std::error::Error > > {
  /// # let client = SyncClient::new("hf_test".to_string())?;
  /// let params = InferenceParameters::new()
  ///   .with_temperature( 0.7 )
  ///   .with_max_new_tokens( 150 );
  ///
  /// let stream = client.inference().create_stream(
  ///   "Tell me a story",
  ///   "mistralai/Mistral-7B-Instruct-v0.3",
  ///   params
  /// )?;
  ///
  /// for token_result in stream
  /// {
  ///   match token_result
  ///   {
  ///     Ok( token ) => print!( "{token}" ),
  ///     Err( e ) => eprintln!( "Error : {e}" ),
  ///   }
  /// }
  /// # Ok(())
  /// # }
  /// ```
  #[ inline ]
  pub fn create_stream< I, M >(
  &self,
  inputs : I,
  model : M,
  parameters : InferenceParameters
  ) -> Result< SyncStream >
  where
  I : Into< String >,
  M : AsRef< str >,
  {
  let inference = self.client.inference();
  let runtime = Arc::clone( &self.runtime );

  let receiver = self.runtime.block_on( async move
  {
      inference.create_stream( inputs, model, parameters ).await
  } )?;

  Ok( SyncStream
  {
      receiver,
      runtime,
  } )
  }
}

// SyncClient is automatically Send + Sync because:
// - Client is Send + Sync
// - Arc< Runtime > is Send + Sync
// No unsafe impl needed