Skip to content
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

[embeddings] extend kwargs to functions that call _encode_with_retry #400

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions caikit_nlp/modules/text_embedding/embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,7 @@ def run_embedding(

@EmbeddingTasks.taskmethod()
def run_embeddings(
self,
texts: List[str],
truncate_input_tokens: Optional[int] = 0,
self, texts: List[str], truncate_input_tokens: Optional[int] = 0, **kwargs
) -> EmbeddingResults:
"""Get embedding vectors for texts.
Args:
Expand All @@ -440,6 +438,7 @@ def run_embeddings(
texts,
truncate_input_tokens=truncate_input_tokens,
return_token_count=True,
**kwargs,
)
vectors = [Vector1D.from_vector(e) for e in embeddings]

Expand All @@ -455,6 +454,7 @@ def run_sentence_similarity(
source_sentence: str,
sentences: List[str],
truncate_input_tokens: Optional[int] = 0,
**kwargs,
) -> SentenceSimilarityResult:
"""Get similarity scores for each of sentences compared to the source_sentence.
Args:
Expand All @@ -476,11 +476,13 @@ def run_sentence_similarity(
source_sentence,
truncate_input_tokens=truncate_input_tokens,
return_token_count=True,
**kwargs,
)
embeddings, sentences_token_count = self._encode_with_retry(
sentences,
truncate_input_tokens=truncate_input_tokens,
return_token_count=True,
**kwargs,
)

input_token_count = source_token_count + sentences_token_count
Expand Down Expand Up @@ -547,6 +549,7 @@ def run_rerank_query(
return_documents: bool = True,
return_query: bool = True,
return_text: bool = True,
**kwargs,
) -> RerankResult:
"""Rerank the documents returning the most relevant top_n in order for this query.
Args:
Expand Down Expand Up @@ -598,6 +601,7 @@ def run_rerank_query(
return_documents=return_documents,
return_queries=return_query,
return_text=return_text,
**kwargs,
)

if results.results:
Expand Down Expand Up @@ -626,6 +630,7 @@ def run_rerank_queries(
return_documents: bool = True,
return_queries: bool = True,
return_text: bool = True,
**kwargs,
) -> RerankResults:
"""Rerank the documents returning the most relevant top_n in order for each of the queries.
Args:
Expand Down Expand Up @@ -690,6 +695,7 @@ def get_text(doc):
truncate_input_tokens=truncate_input_tokens,
return_token_count=True,
convert_to_tensor=True,
**kwargs,
)
doc_embeddings = normalize(doc_embeddings.to(self.model.device))

Expand All @@ -698,6 +704,7 @@ def get_text(doc):
truncate_input_tokens=truncate_input_tokens,
return_token_count=True,
convert_to_tensor=True,
**kwargs,
)
query_embeddings = normalize(query_embeddings.to(self.model.device))

Expand Down
Loading