use core::time::Duration;
use crate::error::Error;
use super::ModelApi;
#[ derive( Debug ) ]
pub struct EmbeddingRequestBuilder< 'a >
{
model : &'a ModelApi< 'a >,
request : crate::models::EmbedContentRequest,
}
impl< 'a > EmbeddingRequestBuilder< 'a >
{
#[ inline ]
#[ must_use ]
pub fn new( model : &'a ModelApi< 'a > ) -> Self
{
Self {
model,
request : crate::models::EmbedContentRequest {
content : crate::models::Content {
parts : vec![],
role : "user".to_string(),
},
task_type : None,
title : None,
output_dimensionality : None,
},
}
}
#[ inline ]
#[ must_use ]
pub fn with_text( mut self, text : &str ) -> Self
{
self.request.content.parts = vec![ crate::models::Part {
text : Some( text.to_string() ),
..Default::default()
} ];
self
}
#[ inline ]
#[ must_use ]
pub fn with_task_type( mut self, task_type : &str ) -> Self
{
self.request.task_type = Some( task_type.to_string() );
self
}
#[ inline ]
#[ must_use ]
pub fn with_title( mut self, title : &str ) -> Self
{
self.request.title = Some( title.to_string() );
self
}
#[ inline ]
#[ must_use ]
pub fn with_output_dimensionality( mut self, dimensions : i32 ) -> Self
{
self.request.output_dimensionality = Some( dimensions );
self
}
#[ inline ]
pub async fn execute( self ) -> Result< crate::models::EmbedContentResponse, Error >
{
self.model.embed_content( &self.request ).await
}
#[ inline ]
pub async fn execute_vector( self ) -> Result< Vec< f32 >, Error >
{
let model_id = self.model.model_id.clone();
let response = self.execute().await?;
let values = response.embedding.values;
if values.is_empty()
{
Err( Error::ApiError(
format!( "No embedding values returned from model '{model_id}'." )
) )
} else {
Ok( values )
}
}
}
#[ derive( Debug ) ]
pub struct BatchEmbeddingRequestBuilder< 'a >
{
model : &'a ModelApi< 'a >,
texts : Option< Vec< &'a str > >,
batch_size : Option< usize >,
timeout : Option< Duration >,
}
impl< 'a > BatchEmbeddingRequestBuilder< 'a >
{
#[ inline ]
#[ must_use ]
pub fn new( model : &'a ModelApi< 'a > ) -> Self
{
Self {
model,
texts : None,
batch_size : None,
timeout : None,
}
}
#[ inline ]
#[ must_use ]
pub fn with_texts( mut self, texts : &'a [ &'a str ] ) -> Self
{
self.texts = Some( texts.to_vec() );
self
}
#[ inline ]
#[ must_use ]
pub fn with_batch_size( mut self, size : usize ) -> Self
{
self.batch_size = Some( size );
self
}
#[ inline ]
#[ must_use ]
pub fn with_timeout( mut self, timeout : Duration ) -> Self
{
self.timeout = Some( timeout );
self
}
#[ inline ]
pub async fn execute( self ) -> Result< Vec< Vec< f32 > >, Error >
{
let texts = self.texts.ok_or_else( || Error::ValidationError {
message : "No texts specified for batch embedding".to_string()
} )?;
self.model.batch_embed_texts( &texts ).await
}
}