
    dhP                     t    S SK JrJrJrJrJr  S SKrS SKJr  S SK	J
r
Jr  S SKJrJr  SrSr " S S	\\5      rg)
    )AnyDictListMappingOptionalN)
Embeddings)get_from_dict_or_envpre_init)	BaseModel
ConfigDictz#sentence-transformers/clip-ViT-B-32i   c                   B   \ rS rSr% Sr\r\\S'    Sr	\
\S'    Sr\\S'    Sr\\S	'    S
r\\   \S'    S
r\\   \S'    \r\\S'    \" SSS9r\S\S\4S j5       r\S\\\4   4S j5       rS\\   S\\\      4S jrS\\   S\\\      4S jr S\S\\   4S jr!Sr"g
)DeepInfraEmbeddings   am  Deep Infra's embedding inference service.

To use, you should have the
environment variable ``DEEPINFRA_API_TOKEN`` set with your API token, or pass
it as a named parameter to the constructor.
There are multiple embeddings models available,
see https://deepinfra.com/models?type=embeddings.

Example:
    .. code-block:: python

        from langchain_community.embeddings import DeepInfraEmbeddings
        deepinfra_emb = DeepInfraEmbeddings(
            model_id="sentence-transformers/clip-ViT-B-32",
            deepinfra_api_token="my-api-key"
        )
        r1 = deepinfra_emb.embed_documents(
            [
                "Alpha is the first letter of Greek alphabet",
                "Beta is the second letter of Greek alphabet",
            ]
        )
        r2 = deepinfra_emb.embed_query(
            "What is the second letter of Greek alphabet"
        )

model_idF	normalizez	passage: embed_instructionzquery: query_instructionNmodel_kwargsdeepinfra_api_token
batch_sizeforbid )extraprotected_namespacesvaluesreturnc                 (    [        USS5      nX!S'   U$ )z?Validate that api key and python package exists in environment.r   DEEPINFRA_API_TOKEN)r	   )clsr   r   s      `/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/embeddings/deepinfra.pyvalidate_environment(DeepInfraEmbeddings.validate_environment;   s(     3)+@
 )<$%    c                     SU R                   0$ )zGet the identifying parameters.r   )r   )selfs    r    _identifying_params'DeepInfraEmbeddings._identifying_paramsD   s     DMM**r#   inputc                 <   U R                   =(       d    0 nSU R                   3SS.n [        R                  " SU R                   3UXR
                  S.UES9nUR                  S:w  a'  [        S	UR                  < S
UR                  < 35      e UR                  5       nUS   nU$ ! [        R                  R                   a  n[        SU 35      eS nAff = f! [        R                  R                   a   n[        SU SUR                   35      eS nAff = f)Nzbearer zapplication/json)AuthorizationzContent-Typez'https://api.deepinfra.com/v1/inference/)inputsr   )headersjsonz$Error raised by inference endpoint:    z)Error raised by inference API HTTP code: z, 
embeddingszError raised by inference API: z.
Response: )r   r   requestspostr   r   
exceptionsRequestException
ValueErrorstatus_codetextr-   JSONDecodeError)r%   r(   _model_kwargsr,   resetr/   s           r    _embedDeepInfraEmbeddings._embedI   s"   ))/R  't'?'?&@A.

	I--9$--I %NNTmTC ??c!??CHH. 	
A<J ! ""33 	ICA3GHH	I ""22 	1!M#((L 	s/   1B) C )CCCD;DDtextsc                    / nU Vs/ sH  o0R                    U 3PM     nn[        S[        U5      U R                  5       Vs/ sH  nXEXPR                  -    PM     nnU H  nX R	                  U5      -  nM     U$ s  snf s  snf )a  Embed documents using a Deep Infra deployed embedding model.
For larger batches, the input list of texts is chunked into smaller
batches to avoid exceeding the maximum request size.

Args:
    texts: The list of texts to embed.

Returns:
    List of embeddings, one for each text.
r   )r   rangelenr   r<   )r%   r>   r/   r6   instruction_pairsichunkschunks           r    embed_documents#DeepInfraEmbeddings.embed_documentsi   s     
KPQ54 6 67v>5Q 1c"34dooF
F !oo"56F 	 
 E++e,,J   R
s   A<Br6   c                 P    U R                    U 3nU R                  U/5      S   nU$ )zEmbed a query using a Deep Infra deployed embedding model.

Args:
    text: The text to embed.

Returns:
    Embeddings for the text.
r   )r   r<   )r%   r6   instruction_pair	embeddings       r    embed_queryDeepInfraEmbeddings.embed_query   s7     #445dV<KK!1 23A6	r#   )#__name__
__module____qualname____firstlineno____doc__DEFAULT_MODEL_IDr   str__annotations__r   boolr   r   r   r   dictr   MAX_BATCH_SIZEr   intr   model_configr
   r   r!   propertyr   r   r&   r   floatr<   rF   rK   __static_attributes__r   r#   r    r   r      s   8 %Hc$"It6(s(.&s&.#'L(4.'")-#-D$J$,H2FL$ 4   +WS#X%6 + +DI $tE{*; @T#Y 4U3D 0 U r#   r   )typingr   r   r   r   r   r0   langchain_core.embeddingsr   langchain_core.utilsr	   r
   pydanticr   r   rR   rW   r   r   r#   r    <module>ra      s2    5 5  0 ? *8 @)Z @r#   