[docs]@deprecated(since="0.3.5",removal="1.0",alternative_import="langchain_gigachat.GigaChatEmbeddings",)classGigaChatEmbeddings(BaseModel,Embeddings):"""GigaChat Embeddings models. Example: .. code-block:: python from langchain_community.embeddings.gigachat import GigaChatEmbeddings embeddings = GigaChatEmbeddings( credentials=..., scope=..., verify_ssl_certs=False ) """base_url:Optional[str]=None""" Base API URL """auth_url:Optional[str]=None""" Auth URL """credentials:Optional[str]=None""" Auth Token """scope:Optional[str]=None""" Permission scope for access token """access_token:Optional[str]=None""" Access token for GigaChat """model:Optional[str]=None"""Model name to use."""user:Optional[str]=None""" Username for authenticate """password:Optional[str]=None""" Password for authenticate """timeout:Optional[float]=600""" Timeout for request. By default it works for long requests. """verify_ssl_certs:Optional[bool]=None""" Check certificates for all requests """ca_bundle_file:Optional[str]=Nonecert_file:Optional[str]=Nonekey_file:Optional[str]=Nonekey_file_password:Optional[str]=None# Support for connection to GigaChat through SSL certificates@cached_propertydef_client(self)->Any:"""Returns GigaChat API client"""importgigachatreturngigachat.GigaChat(base_url=self.base_url,auth_url=self.auth_url,credentials=self.credentials,scope=self.scope,access_token=self.access_token,model=self.model,user=self.user,password=self.password,timeout=self.timeout,verify_ssl_certs=self.verify_ssl_certs,ca_bundle_file=self.ca_bundle_file,cert_file=self.cert_file,key_file=self.key_file,key_file_password=self.key_file_password,)
[docs]@pre_initdefvalidate_environment(cls,values:Dict)->Dict:"""Validate authenticate data in environment and python package is installed."""try:importgigachat# noqa: F401exceptImportError:raiseImportError("Could not import gigachat python package. ""Please install it with `pip install gigachat`.")fields=set(get_fields(cls).keys())diff=set(values.keys())-fieldsifdiff:logger.warning(f"Extra fields {diff} in GigaChat class")returnvalues
[docs]defembed_documents(self,texts:List[str])->List[List[float]]:"""Embed documents using a GigaChat embeddings models. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """result:List[List[float]]=[]size=0local_texts=[]embed_kwargs={}ifself.modelisnotNone:embed_kwargs["model"]=self.modelfortextintexts:local_texts.append(text)size+=len(text)ifsize>MAX_BATCH_SIZE_CHARSorlen(local_texts)>MAX_BATCH_SIZE_PARTS:forembeddinginself._client.embeddings(texts=local_texts,**embed_kwargs).data:result.append(embedding.embedding)size=0local_texts=[]# Call for last iterationiflocal_texts:forembeddinginself._client.embeddings(texts=local_texts,**embed_kwargs).data:result.append(embedding.embedding)returnresult
[docs]asyncdefaembed_documents(self,texts:List[str])->List[List[float]]:"""Embed documents using a GigaChat embeddings models. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """result:List[List[float]]=[]size=0local_texts=[]embed_kwargs={}ifself.modelisnotNone:embed_kwargs["model"]=self.modelfortextintexts:local_texts.append(text)size+=len(text)ifsize>MAX_BATCH_SIZE_CHARSorlen(local_texts)>MAX_BATCH_SIZE_PARTS:embeddings=awaitself._client.aembeddings(texts=local_texts,**embed_kwargs)forembeddinginembeddings.data:result.append(embedding.embedding)size=0local_texts=[]# Call for last iterationiflocal_texts:embeddings=awaitself._client.aembeddings(texts=local_texts,**embed_kwargs)forembeddinginembeddings.data:result.append(embedding.embedding)returnresult
[docs]defembed_query(self,text:str)->List[float]:"""Embed a query using a GigaChat embeddings models. Args: text: The text to embed. Returns: Embeddings for the text. """returnself.embed_documents(texts=[text])[0]
[docs]asyncdefaembed_query(self,text:str)->List[float]:"""Embed a query using a GigaChat embeddings models. Args: text: The text to embed. Returns: Embeddings for the text. """docs=awaitself.aembed_documents(texts=[text])returndocs[0]