use api_huggingface::
{
Client,
environment::HuggingFaceEnvironmentImpl,
components::
{
input::InferenceParameters,
models::Models,
},
secret::Secret,
};
#[ tokio::main ]
async fn main() -> Result< (), Box< dyn std::error::Error > >
{
tracing_subscriber::fmt::init();
println!( "🤗 HuggingFace Inference API Example" );
let api_key = Secret::load_from_env( "HUGGINGFACE_API_KEY" )?;
println!( "✓ API key loaded from environment" );
let env = HuggingFaceEnvironmentImpl::build( api_key, None )?;
let client = Client::build( env )?;
println!( "✓ Client initialized" );
let params = InferenceParameters::new()
.with_temperature( 0.7 )
.with_max_new_tokens( 100 )
.with_top_p( 0.9 );
let model = Models::llama_3_3_70b_instruct();
println!( "🤖 Using model : {model}" );
let prompts = [
"What is the capital of France?",
"Explain quantum computing in simple terms.",
"Write a haiku about artificial intelligence.",
];
for ( i, prompt ) in prompts.iter().enumerate()
{
println!( "\n📝 Example {}: {prompt}", i + 1 );
match client.inference().create_with_parameters( *prompt, model, params.clone() ).await
{
Ok( response ) =>
{
let text = response.extract_text_or_default( "No response generated" );
println!( "💬 Response : {text}" );
},
Err( e ) =>
{
eprintln!( "❌ Error : {e}" );
}
}
}
println!( "\n✅ Example completed!" );
Ok( () )
}