use api_huggingface::
{
client::Client,
environment::HuggingFaceEnvironmentImpl,
providers::{ ChatMessage, Providers },
Secret,
};
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
}
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( () )
}