-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
improvements to FastEmbed integration #558
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
from typing import ClassVar, Dict, List, Optional | ||
|
||
from tqdm import tqdm | ||
|
||
from fastembed import TextEmbedding | ||
|
||
|
||
|
@@ -39,7 +41,12 @@ def __init__( | |
): | ||
self.model = TextEmbedding(model_name=model_name, cache_dir=cache_dir, threads=threads) | ||
|
||
def embed(self, data: List[List[str]], **kwargs) -> List[List[float]]: | ||
def embed(self, data: List[str], progress_bar=True, **kwargs) -> List[List[float]]: | ||
# the embed method returns a Iterable[np.ndarray], so we convert it to a list of lists | ||
embeddings = [np_array.tolist() for np_array in self.model.embed(data, **kwargs)] | ||
embeddings = [] | ||
embeddings_iterable = self.model.embed(data, **kwargs) | ||
for np_array in tqdm( | ||
embeddings_iterable, disable=not progress_bar, desc="Calculating embeddings", total=len(data) | ||
): | ||
embeddings.append(np_array.tolist()) | ||
Comment on lines
+44
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since the original library does not provide this feature, |
||
return embeddings |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,6 @@ def __init__( | |
threads: Optional[int] = None, | ||
prefix: str = "", | ||
suffix: str = "", | ||
batch_size: int = 256, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the text embedder only accepts a single string, |
||
progress_bar: bool = True, | ||
parallel: Optional[int] = None, | ||
): | ||
|
@@ -47,7 +46,6 @@ def __init__( | |
Can be set using the `FASTEMBED_CACHE_PATH` env variable. | ||
Defaults to `fastembed_cache` in the system's temp directory. | ||
:param threads: The number of threads single onnxruntime session can use. Defaults to None. | ||
:param batch_size: Number of strings to encode at once. | ||
:param prefix: A string to add to the beginning of each text. | ||
:param suffix: A string to add to the end of each text. | ||
:param progress_bar: If true, displays progress bar during embedding. | ||
|
@@ -62,7 +60,6 @@ def __init__( | |
self.threads = threads | ||
self.prefix = prefix | ||
self.suffix = suffix | ||
self.batch_size = batch_size | ||
self.progress_bar = progress_bar | ||
self.parallel = parallel | ||
|
||
|
@@ -80,7 +77,6 @@ def to_dict(self) -> Dict[str, Any]: | |
threads=self.threads, | ||
prefix=self.prefix, | ||
suffix=self.suffix, | ||
batch_size=self.batch_size, | ||
progress_bar=self.progress_bar, | ||
parallel=self.parallel, | ||
) | ||
|
@@ -119,8 +115,7 @@ def run(self, text: str): | |
embedding = list( | ||
self.embedding_backend.embed( | ||
text_to_embed, | ||
batch_size=self.batch_size, | ||
show_progress_bar=self.progress_bar, | ||
progress_bar=self.progress_bar, | ||
parallel=self.parallel, | ||
)[0] | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
show coverage