Skip to content

Commit

Permalink
community[patch]: add VoyageEmbeddings truncation (langchain-ai#17638)
Browse files Browse the repository at this point in the history
  • Loading branch information
baskaryan authored Feb 18, 2024
1 parent d7c26c8 commit a058c88
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion libs/community/langchain_community/embeddings/voyageai.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ class VoyageEmbeddings(BaseModel, Embeddings):
show_progress_bar: bool = False
"""Whether to show a progress bar when embedding. Must have tqdm installed if set
to True."""
truncation: Optional[bool] = None
"""Whether to truncate the input texts to fit within the context length.
If True, over-length input texts will be truncated to fit within the context
length, before vectorized by the embedding model. If False, an error will be
raised if any given text exceeds the context length. If not specified
(defaults to None), we will truncate the input text before sending it to the
embedding model if it slightly exceeds the context window length. If it
significantly exceeds the context window length, an error will be raised."""

class Config:
"""Configuration for this pydantic object."""
Expand All @@ -104,12 +113,14 @@ def _invocation_params(
self, input: List[str], input_type: Optional[str] = None
) -> Dict:
api_key = cast(SecretStr, self.voyage_api_key).get_secret_value()
params = {
params: Dict = {
"url": self.voyage_api_base,
"headers": {"Authorization": f"Bearer {api_key}"},
"json": {"model": self.model, "input": input, "input_type": input_type},
"timeout": self.request_timeout,
}
if self.truncation is not None:
params["json"]["truncation"] = self.truncation
return params

def _get_embeddings(
Expand Down

0 comments on commit a058c88

Please sign in to comment.