use serde::{ Deserialize, Serialize };
#[ derive( Debug, Clone, Serialize, Deserialize ) ]
pub struct EmbeddingRequest
{
pub inputs : EmbeddingInput,
#[ serde( skip_serializing_if = "Option::is_none" ) ]
pub options : Option< EmbeddingOptions >,
}
impl EmbeddingRequest
{
#[ inline ]
#[ must_use ]
pub fn new( input : impl Into< String > ) -> Self
{
Self
{
inputs : EmbeddingInput::Single( input.into() ),
options : None,
}
}
#[ inline ]
#[ must_use ]
pub fn new_batch( inputs : Vec< String > ) -> Self
{
Self
{
inputs : EmbeddingInput::Batch( inputs ),
options : None,
}
}
#[ inline ]
#[ must_use ]
pub fn with_options( mut self, options : EmbeddingOptions ) -> Self
{
self.options = Some( options );
self
}
}
#[ derive( Debug, Clone, Serialize, Deserialize ) ]
#[ serde( untagged ) ]
pub enum EmbeddingInput
{
Single( String ),
Batch( Vec< String > ),
}
#[ derive( Debug, Clone, Serialize, Deserialize ) ]
pub struct EmbeddingOptions
{
#[ serde( skip_serializing_if = "Option::is_none" ) ]
pub use_cache : Option< bool >,
#[ serde( skip_serializing_if = "Option::is_none" ) ]
pub wait_for_model : Option< bool >,
#[ serde( skip_serializing_if = "Option::is_none" ) ]
pub normalize : Option< bool >,
#[ serde( skip_serializing_if = "Option::is_none" ) ]
pub pooling : Option< PoolingStrategy >,
}
impl Default for EmbeddingOptions
{
#[ inline ]
fn default() -> Self
{
Self::recommended()
}
}
impl EmbeddingOptions
{
#[ inline ]
#[ must_use ]
pub fn recommended() -> Self
{
Self
{
use_cache : Some( true ), wait_for_model : Some( true ), normalize : Some( true ), pooling : Some( PoolingStrategy::Mean ), }
}
#[ inline ]
#[ must_use ]
pub fn empty() -> Self
{
Self
{
use_cache : None,
wait_for_model : None,
normalize : None,
pooling : None,
}
}
#[ inline ]
#[ must_use ]
pub fn conservative() -> Self
{
Self
{
use_cache : Some( false ), wait_for_model : Some( false ), normalize : Some( false ), pooling : Some( PoolingStrategy::Cls ), }
}
#[ inline ]
#[ must_use ]
pub fn with_use_cache( mut self, use_cache : bool ) -> Self
{
self.use_cache = Some( use_cache );
self
}
#[ inline ]
#[ must_use ]
pub fn with_wait_for_model( mut self, wait_for_model : bool ) -> Self
{
self.wait_for_model = Some( wait_for_model );
self
}
#[ inline ]
#[ must_use ]
pub fn with_normalize( mut self, normalize : bool ) -> Self
{
self.normalize = Some( normalize );
self
}
#[ inline ]
#[ must_use ]
pub fn with_pooling( mut self, pooling : PoolingStrategy ) -> Self
{
self.pooling = Some( pooling );
self
}
}
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq ) ]
#[ serde( rename_all = "lowercase" ) ]
pub enum PoolingStrategy
{
Mean,
Max,
Cls,
}
#[ derive( Debug, Clone, Serialize, Deserialize ) ]
#[ serde( untagged ) ]
pub enum EmbeddingResponse
{
Single( Vec< Vec< f32 > > ),
Batch( Vec< Vec< Vec< f32 > > > ),
}