use serde::{ Deserialize, Serialize };
use std::collections::HashMap;
use crate::
{
error::{ HuggingFaceError, Result },
validation::
{
validate_temperature,
validate_max_new_tokens,
validate_top_p,
validate_repetition_penalty,
validate_stop_sequences,
},
};
#[ derive( Debug, Serialize ) ]
pub( crate ) struct BinaryClassificationInput
{
pub( crate ) inputs : String,
}
#[ derive( Debug, Clone, Serialize, Deserialize ) ]
pub struct InferenceParameters
{
#[ serde( skip_serializing_if = "Option::is_none" ) ]
pub temperature : Option< f32 >,
#[ serde( skip_serializing_if = "Option::is_none" ) ]
pub max_new_tokens : Option< u32 >,
#[ serde( skip_serializing_if = "Option::is_none" ) ]
pub top_p : Option< f32 >,
#[ serde( skip_serializing_if = "Option::is_none" ) ]
pub top_k : Option< u32 >,
#[ serde( skip_serializing_if = "Option::is_none" ) ]
pub repetition_penalty : Option< f32 >,
#[ serde( skip_serializing_if = "Option::is_none" ) ]
pub return_full_text : Option< bool >,
#[ serde( skip_serializing_if = "Option::is_none" ) ]
pub stop : Option< Vec< String > >,
#[ serde( skip_serializing_if = "Option::is_none" ) ]
pub stream : Option< bool >,
#[ serde( flatten ) ]
pub additional : HashMap< String, serde_json::Value >,
}
impl Default for InferenceParameters
{
#[ inline ]
fn default() -> Self
{
Self::recommended()
}
}
impl InferenceParameters
{
#[ inline ]
#[ must_use ]
pub fn recommended() -> Self
{
Self
{
temperature : Some( 0.7 ), max_new_tokens : Some( 512 ), top_p : Some( 0.9 ), top_k : None, repetition_penalty : Some( 1.1 ), return_full_text : Some( false ), stop : None, stream : Some( false ), additional : HashMap::new(),
}
}
#[ inline ]
#[ must_use ]
pub fn new() -> Self
{
Self::recommended()
}
#[ inline ]
#[ must_use ]
pub fn empty() -> Self
{
Self
{
temperature : None,
max_new_tokens : None,
top_p : None,
top_k : None,
repetition_penalty : None,
return_full_text : None,
stop : None,
stream : None,
additional : HashMap::new(),
}
}
#[ inline ]
#[ must_use ]
pub fn conservative() -> Self
{
Self
{
temperature : Some( 0.3 ), max_new_tokens : Some( 256 ), top_p : Some( 0.8 ), top_k : Some( 50 ), repetition_penalty : Some( 1.2 ), return_full_text : Some( false ),
stop : None,
stream : Some( false ),
additional : HashMap::new(),
}
}
#[ inline ]
#[ must_use ]
pub fn with_temperature( mut self, temperature : f32 ) -> Self
{
self.temperature = Some( temperature );
self
}
#[ inline ]
#[ must_use ]
pub fn with_max_new_tokens( mut self, max_tokens : u32 ) -> Self
{
self.max_new_tokens = Some( max_tokens );
self
}
#[ inline ]
#[ must_use ]
pub fn with_top_p( mut self, top_p : f32 ) -> Self
{
self.top_p = Some( top_p );
self
}
#[ inline ]
#[ must_use ]
pub fn with_top_k( mut self, top_k : u32 ) -> Self
{
self.top_k = Some( top_k );
self
}
#[ inline ]
#[ must_use ]
pub fn with_repetition_penalty( mut self, penalty : f32 ) -> Self
{
self.repetition_penalty = Some( penalty );
self
}
#[ inline ]
#[ must_use ]
pub fn with_return_full_text( mut self, return_full : bool ) -> Self
{
self.return_full_text = Some( return_full );
self
}
#[ inline ]
#[ must_use ]
pub fn with_streaming( mut self, stream : bool ) -> Self
{
self.stream = Some( stream );
self
}
#[ inline ]
#[ must_use ]
pub fn with_stop_sequences( mut self, stop : Vec< String > ) -> Self
{
self.stop = Some( stop );
self
}
#[ inline ]
pub fn validate( &self ) -> Result< () >
{
let mut errors = Vec::new();
if let Some( temperature ) = self.temperature
{
if let Err( e ) = validate_temperature( temperature )
{
errors.push( e.to_string() );
}
}
if let Some( max_tokens ) = self.max_new_tokens
{
if let Err( e ) = validate_max_new_tokens( max_tokens )
{
errors.push( e.to_string() );
}
}
if let Some( top_p ) = self.top_p
{
if let Err( e ) = validate_top_p( top_p )
{
errors.push( e.to_string() );
}
}
if let Some( penalty ) = self.repetition_penalty
{
if let Err( e ) = validate_repetition_penalty( penalty )
{
errors.push( e.to_string() );
}
}
if let Some( ref stop_sequences ) = self.stop
{
if let Err( e ) = validate_stop_sequences( stop_sequences )
{
errors.push( e.to_string() );
}
}
if !errors.is_empty()
{
return Err( HuggingFaceError::Validation(
format!( "Parameter validation failed : {}", errors.join( "; " ) )
) );
}
Ok( () )
}
}