use api_huggingface::
{
Client,
environment::HuggingFaceEnvironmentImpl,
components::
{
input::InferenceParameters,
models::Models,
},
secret::Secret,
};
#[ tokio::main ]
async fn main() -> Result< (), Box< dyn std::error::Error > >
{
println!( "🤗 HuggingFace Basic Chat Example" );
println!( "==================================\n" );
let api_key = Secret::load_from_env( "HUGGINGFACE_API_KEY" )?;
println!( "✓ API key loaded successfully" );
let env = HuggingFaceEnvironmentImpl::build( api_key, None )?;
let client = Client::build( env )?;
println!( "✓ Client initialized\n" );
let params = InferenceParameters::new()
.with_temperature( 0.7 ) .with_max_new_tokens( 150 ) .with_top_p( 0.9 );
println!( "📋 Configuration:" );
println!( " Temperature : 0.7" );
println!( " Max tokens : 150" );
println!( " Top-p : 0.9\n" );
let model = Models::kimi_k2_instruct();
println!( "🤖 Model : {model}\n" );
let question = "What is artificial intelligence and how does it work?";
println!( "❓ Question : {question}\n" );
println!( "⏳ Sending request to HuggingFace API..." );
match client.inference().create_with_parameters( question, model, params ).await
{
Ok( response ) =>
{
let answer = response.extract_text_or_default( "No response generated" );
println!( "\n💬 Answer:" );
println!( "─────────────────────────────────────────" );
println!( "{answer}" );
println!( "─────────────────────────────────────────\n" );
},
Err( e ) =>
{
eprintln!( "❌ Error : {e}" );
eprintln!( "\n💡 Troubleshooting:" );
eprintln!( " • Verify your HUGGINGFACE_API_KEY is set correctly" );
eprintln!( " • Check your internet connection" );
eprintln!( " • Ensure you have access to the Inference API" );
return Err( Box::new( e ) as Box< dyn std::error::Error > );
}
}
println!( "✅ Chat example completed successfully!\n" );
Ok( () )
}