api_huggingface 0.5.1

HuggingFace's API for accessing large language models (LLMs) and embeddings.
Documentation
//! `HuggingFace` Inference Providers API Demo โ€” Pro Plan Models
//!
//! Demonstrates the chat completions endpoint that provides access to Pro models.
//! Requires `HUGGINGFACE_API_KEY` environment variable with Pro plan access.
//!
//! ```bash
//! cargo run --example providers_api_demo --all-features
//! ```

use api_huggingface::
{
  client::Client,
  environment::HuggingFaceEnvironmentImpl,
  providers::{ ChatMessage, Providers },
  Secret,
};

/// Find a working Pro model from the list
async fn find_working_model( providers : &Providers< HuggingFaceEnvironmentImpl >, math_question : &str ) -> Option< &'static str >
{
  let pro_models = [
  "meta-llama/Meta-Llama-3-8B-Instruct",
  "meta-llama/Llama-2-7b-chat-hf",
  "mistralai/Mistral-7B-Instruct-v0.2",
  "codellama/CodeLlama-7b-Instruct-hf",
  ];

  for ( i, model ) in pro_models.iter().enumerate()
  {
  println!( "๐Ÿงช Test {}: {}", i + 1, model );
  println!( "๐Ÿ“ค Input : {math_question:?}" );

  match providers.math_completion( model, math_question ).await
  {
      Ok( response ) =>
      {
  if let Some( choice ) = response.choices.first()
  {
          println!( "โœ… SUCCESS! Model {model} is available" );
          println!( "๐Ÿ“ Response : {:?}", choice.message.content );
          println!( "๐ŸŽ‰ WORKING PRO MODEL FOUND: {model}" );
          println!( "================================================================================\n" );
          return Some( *model );
  }
  println!( "โŒ FAILED: {model} - No choices in response" );
      },
      Err( e ) =>
      {
  println!( "โŒ FAILED: {model} - {e}" );
      }
  }
  println!( "================================================================================\n" );
  }
  None
}

/// Test the working model with various scenarios
async fn test_working_model( providers : &Providers< HuggingFaceEnvironmentImpl >, model : &str )
{
  println!( "๐ŸŽŠ SUCCESS : Found working Pro model : {model}" );
  println!( "\n๐Ÿงช Testing simple chat with the working model..." );

  match providers.simple_chat( model, "Hello, how are you?" ).await
  {
  Ok( response ) =>
  {
      if let Some( choice ) = response.choices.first()
      {
  println!( "๐Ÿ“ Simple chat response : {:?}", choice.message.content );
      }
  },
  Err( e ) =>
  {
      println!( "โŒ Simple chat failed : {e}" );
  }
  }

  println!( "\n๐Ÿงช Testing conversation with context..." );

  let messages = vec![
  ChatMessage
  {
      role : "system".to_string(),
      content : "You are a helpful math assistant.".to_string(),
      tool_calls : None,
      tool_call_id : None,
  },
  ChatMessage
  {
      role : "user".to_string(),
      content : "I have x = 13".to_string(),
      tool_calls : None,
      tool_call_id : None,
  },
  ChatMessage
  {
      role : "user".to_string(),
      content : "What is x * 3?".to_string(),
      tool_calls : None,
      tool_call_id : None,
  }
  ];

  match providers.chat_completion( model, messages, Some( 100 ), Some( 0.7 ), Some( 0.9 ) ).await
  {
  Ok( response ) =>
  {
      if let Some( choice ) = response.choices.first()
      {
  println!( "๐Ÿ“ Math conversation response : {:?}", choice.message.content );
      }
  },
  Err( e ) =>
  {
      println!( "โŒ Math conversation failed : {e}" );
  }
  }
}

#[ tokio::main ]
async fn main() -> Result< (), Box< dyn std::error::Error > >
{
  println!( "๐Ÿงช HuggingFace Providers API Demo - Pro Plan Models" );
  println!( "===============================================" );

  let secret = match Secret::load_from_env( "HUGGINGFACE_API_KEY" )
  {
  Ok( s ) => s,
  Err( e ) =>
  {
      eprintln!( "โŒ Failed to load API key : {e}" );
      eprintln!( "๐Ÿ’ก Please set HUGGINGFACE_API_KEY environment variable" );
      return Err( e.into() );
  }
  };

  let env = HuggingFaceEnvironmentImpl::build( secret, None )?;
  let client = Client::build( env )?;
  let providers = client.providers();

  println!( "โœ… Client initialized successfully\n" );

  let math_question = "If x = 13, what is x * 3?";
  println!( "๐Ÿงช Testing Pro models with math question : \"{math_question}\"" );
  println!( "๐Ÿ” Using Inference Providers API (/v1/chat/completions)\n" );

  if let Some( model ) = find_working_model( &providers, math_question ).await
  {
  test_working_model( &providers, model ).await;
  }
  else
  {
  println!( "โŒ No Pro models are working. This might indicate:" );
  println!( "   1. Your HuggingFace account doesn't have Pro plan access" );
  println!( "   2. The API key doesn't have the right permissions" );
  println!( "   3. The Inference Providers API endpoint is not accessible" );
  }

  println!( "\n๐Ÿ Demo completed" );
  Ok( () )
}