Skip to content

Commit eff9acb

Browse files
authored
refactor: Updated embed(...) and rerank() signatures (#201)
Signed-off-by: Anush008 <[email protected]>
1 parent 7acb3e2 commit eff9acb

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

src/image_embedding/impl.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,16 @@ impl ImageEmbedding {
157157
Ok(output)
158158
}
159159

160-
/// Method to generate image embeddings for a Vec of image path
161-
// Generic type to accept String, &str, OsString, &OsStr
160+
/// Method to generate image embeddings for a collection of image paths.
161+
///
162+
/// Accepts anything that can be referenced as a slice of elements implementing
163+
/// [`AsRef<Path>`], such as `Vec<String>`, `Vec<PathBuf>`, `&[&str]`, or `&[&Path]`.
162164
pub fn embed<S: AsRef<Path> + Send + Sync>(
163165
&mut self,
164-
images: Vec<S>,
166+
images: impl AsRef<[S]>,
165167
batch_size: Option<usize>,
166168
) -> anyhow::Result<Vec<Embedding>> {
169+
let images = images.as_ref();
167170
// Determine the batch size, default if not specified
168171
let batch_size = batch_size.unwrap_or(DEFAULT_BATCH_SIZE);
169172

src/reranking/impl.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,16 @@ impl TextRerank {
122122
}
123123

124124
/// Rerank documents using the reranker model and returns the results sorted by score in descending order.
125+
///
126+
/// Accepts a query and a collection of documents implementing [`AsRef<str>`].
125127
pub fn rerank<S: AsRef<str> + Send + Sync>(
126128
&mut self,
127129
query: S,
128-
documents: Vec<S>,
130+
documents: impl AsRef<[S]>,
129131
return_documents: bool,
130132
batch_size: Option<usize>,
131133
) -> Result<Vec<RerankResult>> {
134+
let documents = documents.as_ref();
132135
let batch_size = batch_size.unwrap_or(DEFAULT_BATCH_SIZE);
133136
let q = query.as_ref();
134137

src/sparse_text_embedding/impl.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,16 @@ impl SparseTextEmbedding {
104104
.expect("Model not found in supported models list. This is a bug - please report it.")
105105
}
106106

107-
/// Method to generate sentence embeddings for a Vec of texts
108-
// Generic type to accept String, &str, OsString, &OsStr
107+
/// Method to generate sentence embeddings for a collection of texts.
108+
///
109+
/// Accepts anything that can be referenced as a slice of elements implementing
110+
/// [`AsRef<str>`], such as `Vec<String>`, `Vec<&str>`, `&[String]`, or `&[&str]`.
109111
pub fn embed<S: AsRef<str> + Send + Sync>(
110112
&mut self,
111-
texts: Vec<S>,
113+
texts: impl AsRef<[S]>,
112114
batch_size: Option<usize>,
113115
) -> Result<Vec<SparseEmbedding>> {
116+
let texts = texts.as_ref();
114117
// Determine the batch size, default if not specified
115118
let batch_size = batch_size.unwrap_or(DEFAULT_BATCH_SIZE);
116119

src/text_embedding/impl.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,10 @@ impl TextEmbedding {
265265
/// embeddings with your custom output type.
266266
pub fn transform<S: AsRef<str> + Send + Sync>(
267267
&mut self,
268-
texts: Vec<S>,
268+
texts: impl AsRef<[S]>,
269269
batch_size: Option<usize>,
270270
) -> Result<EmbeddingOutput> {
271+
let texts = texts.as_ref();
271272
// Determine the batch size according to the quantization method used.
272273
// Default if not specified
273274
let batch_size = match self.quantization {
@@ -360,10 +361,10 @@ impl TextEmbedding {
360361
Ok(EmbeddingOutput::new(batches))
361362
}
362363

363-
/// Method to generate sentence embeddings for a Vec of texts.
364+
/// Method to generate sentence embeddings for a collection of texts.
364365
///
365-
/// Accepts a [`Vec`] consisting of elements of either [`String`], &[`str`],
366-
/// [`std::ffi::OsString`], &[`std::ffi::OsStr`].
366+
/// Accepts anything that can be referenced as a slice of elements implementing
367+
/// [`AsRef<str>`], such as `Vec<String>`, `Vec<&str>`, `&[String]`, or `&[&str]`.
367368
///
368369
/// The output is a [`Vec`] of [`Embedding`]s.
369370
///
@@ -373,10 +374,10 @@ impl TextEmbedding {
373374
/// the default output precedence and array transformer for the [`TextEmbedding`] model.
374375
pub fn embed<S: AsRef<str> + Send + Sync>(
375376
&mut self,
376-
texts: Vec<S>,
377+
texts: impl AsRef<[S]>,
377378
batch_size: Option<usize>,
378379
) -> Result<Vec<Embedding>> {
379-
let batches = self.transform(texts, batch_size)?;
380+
let batches = self.transform(texts.as_ref(), batch_size)?;
380381
if let Some(output_key) = &self.output_key {
381382
batches.export_with_transformer(output::transformer_with_precedence(
382383
output_key,

0 commit comments

Comments
 (0)