[docs]classVoyageAIEmbeddings(BaseModel,Embeddings):"""VoyageAIEmbeddings embedding model. Example: .. code-block:: python from langchain_voyageai import VoyageAIEmbeddings model = VoyageAIEmbeddings() """_client:voyageai.Client=PrivateAttr()_aclient:voyageai.client_async.AsyncClient=PrivateAttr()model:strbatch_size:intoutput_dimension:Optional[Literal[256,512,1024,2048]]=Noneshow_progress_bar:bool=Falsetruncation:bool=Truevoyage_api_key:SecretStr=Field(alias="api_key",default_factory=secret_from_env("VOYAGE_API_KEY",error_message="Must set `VOYAGE_API_KEY` environment variable or ""pass `api_key` to VoyageAIEmbeddings constructor.",),)model_config=ConfigDict(extra="forbid",populate_by_name=True,)@model_validator(mode="before")@classmethoddefdefault_values(cls,values:dict)->Any:"""Set default batch size based on model"""model=values.get("model")batch_size=values.get("batch_size")ifbatch_sizeisNone:values["batch_size"]=(DEFAULT_VOYAGE_2_BATCH_SIZEifmodelin["voyage-2","voyage-02"]else(DEFAULT_VOYAGE_3_LITE_BATCH_SIZEifmodel=="voyage-3-lite"else(DEFAULT_VOYAGE_3_BATCH_SIZEifmodel=="voyage-3"elseDEFAULT_BATCH_SIZE)))returnvalues@model_validator(mode="after")defvalidate_environment(self)->Self:"""Validate that VoyageAI credentials exist in environment."""api_key_str=self.voyage_api_key.get_secret_value()self._client=voyageai.Client(api_key=api_key_str)self._aclient=voyageai.client_async.AsyncClient(api_key=api_key_str)returnselfdef_get_batch_iterator(self,texts:List[str])->Iterable:ifself.show_progress_bar:try:fromtqdm.autoimporttqdm# type: ignoreexceptImportErrorase:raiseImportError("Must have tqdm installed if `show_progress_bar` is set to True. ""Please install with `pip install tqdm`.")frome_iter=tqdm(range(0,len(texts),self.batch_size))else:_iter=range(0,len(texts),self.batch_size)# type: ignorereturn_iter