
    h                        d dl mZ d dlZd dlmZmZmZmZ d dlm	Z	 d dl
mZmZmZ d dlmZmZmZmZ  ej&                  e      Z G d dee	      Zy)	    )annotationsN)AnyDictListOptional)
Embeddings)convert_to_secret_strget_from_dict_or_envpre_init)	BaseModel
ConfigDictField	SecretStrc                      e Zd ZU dZ edd      Zded<   	  edd      Zded<   	 d	Zd
ed<   	  ed      Z	ded<   	 dZ
ded<   	 dZded<   	  ee      Zded<   	  ee      Zded<   	  ed      Zedd       Zd dZd!dZd dZd!dZy)"QianfanEmbeddingsEndpointa  Baidu Qianfan Embeddings embedding models.

    Setup:
        To use, you should have the ``qianfan`` python package installed, and set
        environment variables ``QIANFAN_AK``, ``QIANFAN_SK``.

        .. code-block:: bash

            pip install qianfan
            export QIANFAN_AK="your-api-key"
            export QIANFAN_SK="your-secret_key"

    Instantiate:
        .. code-block:: python

            from langchain_community.embeddings import QianfanEmbeddingsEndpoint

            embeddings = QianfanEmbeddingsEndpoint()

     Embed:
        .. code-block:: python

            # embed the documents
            vectors = embeddings.embed_documents([text1, text2, ...])

            # embed the query
            vectors = embeddings.embed_query(text)

            # embed the documents with async
            vectors = await embeddings.aembed_documents([text1, text2, ...])

            # embed the query with async
            vectors = await embeddings.aembed_query(text)
    Napi_key)defaultaliaszOptional[SecretStr]
qianfan_ak
secret_key
qianfan_sk   int
chunk_sizer   zOptional[str]model strendpointr   client)default_factoryzDict[str, Any]init_kwargsmodel_kwargs )protected_namespacesc                   t        t        |ddd            |d<   t        t        |ddd            |d<   	 ddl}i |j                  d	i       d
|d
   i}|d   j	                         dk7  r|d   j	                         |d<   |d   j	                         dk7  r|d   j	                         |d<   |d   |d   dk7  r|d   |d<    |j
                  di ||d<   |S # t        $ r t        d      w xY w)a3  
        Validate whether qianfan_ak and qianfan_sk in the environment variables or
        configuration file are available or not.

        init qianfan embedding client with `ak`, `sk`, `model`, `endpoint`

        Args:

            values: a dictionary containing configuration information, must include the
            fields of qianfan_ak and qianfan_sk
        Returns:

            a dictionary containing configuration information. If qianfan_ak and
            qianfan_sk are not provided in the environment variables or configuration
            file,the original values will be returned; otherwise, values containing
            qianfan_ak and qianfan_sk will be returned.
        Raises:

            ValueError: qianfan package not found, please install it with `pip install
            qianfan`
        r   
QIANFAN_AKr   r   r   
QIANFAN_SKr   Nr"   r   akskr   r    zGqianfan package not found, please install it with `pip install qianfan`r$   )r	   r
   qianfangetget_secret_value	EmbeddingImportError)clsvaluesr+   paramss       s/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/embeddings/baidu_qianfan_endpoint.pyvalidate_environmentz.QianfanEmbeddingsEndpoint.validate_environmentV   sB   .  5 	 
|  5 	 
|	**]B/F l#446"<%l3DDFtl#446"<%l3DDFtj!-&2D2J%+J%7z"0w00:6:F8   	( 	s   B!C C0c                0    | j                  |g      }|d   S Nr   )embed_documents)selftextresps      r3   embed_queryz%QianfanEmbeddingsEndpoint.embed_query   s    ##TF+Aw    c                @   t        dt        |      | j                        D cg c]  }|||| j                  z     }}g }|D ]O  } | j                  j                  dd|i| j
                  }|j                  |d   D cg c]  }|d   	 c}       Q |S c c}w c c}w )a_  
        Embeds a list of text documents using the AutoVOT algorithm.

        Args:
            texts (List[str]): A list of text documents to embed.

        Returns:
            List[List[float]]: A list of embeddings for each document in the input list.
                            Each embedding is represented as a list of float values.
        r   textsdata	embeddingr$   )rangelenr   r    dor#   extendr8   r>   itext_in_chunkslstchunkr:   ress           r3   r7   z)QianfanEmbeddingsEndpoint.embed_documents   s     1c%j$//:
 !a$//)*
 
 # 	CE!4;;>>CC1B1BCDJJDLASK(AB	C 

 Bs   B?B
c                L   K   | j                  |g       d {   }|d   S 7 	wr6   )aembed_documents)r8   r9   
embeddingss      r3   aembed_queryz&QianfanEmbeddingsEndpoint.aembed_query   s*     00$88
!} 9s   $"
$c                F  K   t        dt        |      | j                        D cg c]  }|||| j                  z     }}g }|D ]Q  } | j                  j                  dd|i| j
                   d {   }|d   D ]  }|j                  |d   g        S |S c c}w 7 ,w)Nr   r>   r?   r@   r$   )rA   rB   r   r    ador#   rD   rE   s           r3   rL   z*QianfanEmbeddingsEndpoint.aembed_documents   s      1c%j$//:
 !a$//)*
 
 # 	/E(JuJ8I8IJJDF| /

C,-./	/ 

 Ks   #B!B4B!2B3-B!)r1   r   returnr   )r9   r   rQ   zList[float])r>   z	List[str]rQ   zList[List[float]])__name__
__module____qualname____doc__r   r   __annotations__r   r   r   r   r    dictr"   r#   r   model_configr   r4   r;   r7   rN   rL   r$   r<   r3   r   r      s    !F ',D	&JJ#J$&+D&MJ#M'J2 .E=.
 HcKFC"'"=K=@ $)#>L.>826L: :x*
r<   r   )
__future__r   loggingtypingr   r   r   r   langchain_core.embeddingsr   langchain_core.utilsr	   r
   r   pydanticr   r   r   r   	getLoggerrR   loggerr   r$   r<   r3   <module>ra      sB    "  , , 0 V V < <			8	$m	: mr<   