
    dh                         S SK r S SKJrJrJrJrJrJrJr  S SK	r	S SK
JrJr  S SKJr  S SKJr  S SKJrJr  S SKJr  S SKJr  S	r " S
 S\5      rS\\   S\\   4S jrS\	R:                  S\\   4S jrS\S\\   4S jrS\S\\   4S jr g)    N)AnyAsyncIteratorDictIteratorListMappingOptional)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LLM)GenerationChunk)get_from_dict_or_envpre_init)
ConfigDict)Requestsz$meta-llama/Meta-Llama-3-70B-Instructc                      \ rS rSr% Sr\r\\S'   Sr	\
\   \S'   Sr\
\   \S'   \" SS9r\S	\S
\4S j5       r\S
\\\4   4S j5       r\S
\4S j5       rS
\4S jrS
\4S jrS\S\S
\4S jrS\S\S
S4S jr  SS\S\
\\      S\
\   S\S
\4
S jjr  SS\S\
\\      S\
\   S\S
\4
S jjr  SS\S\
\\      S\
\   S\S
\ \!   4
S jjr"  SS\S\
\\      S\
\   S\S
\#\!   4
S jjr$S\S
S4S jr%Sr&g) 	DeepInfra   a  DeepInfra models.

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.

Only supports `text-generation` and `text2text-generation` for now.

Example:
    .. code-block:: python

        from langchain_community.llms import DeepInfra
        di = DeepInfra(model_id="google/flan-t5-xl",
                            deepinfra_api_token="my-api-key")
model_idNmodel_kwargsdeepinfra_api_tokenforbid)extra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      Z/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/deepinfra.pyvalidate_environmentDeepInfra.validate_environment-   s(     3)+@
 )<$%    c                 >    0 SU R                   0ESU R                  0E$ )zGet the identifying parameters.r   r   )r   r   selfs    r   _identifying_paramsDeepInfra._identifying_params6   s0    
4==)
t001
 	
r"   c                     g)zReturn type of llm.	deepinfra r$   s    r   	_llm_typeDeepInfra._llm_type>   s     r"   c                      SU R                    3$ )Nz'https://api.deepinfra.com/v1/inference/)r   r$   s    r   _urlDeepInfra._urlC   s    8HHr"   c                 &    SU R                    3SS.$ )Nzbearer zapplication/json)AuthorizationzContent-Type)r   r$   s    r   _headersDeepInfra._headersF   s     &t'?'?&@A.
 	
r"   promptkwargsc                 D    U R                   =(       d    0 n0 UEUEnSU0UE$ )Ninput)r   )r%   r4   r5   r   s       r   _bodyDeepInfra._bodyL   s:    ((.B1,1&1 V

 	
r"   codetextc                 $   US:  a  [        SU 35      eUS:X  a  [        S5      eUS:X  a  [        S5      eUS:X  a  [        SU R                   35      eUS:X  a  [        S	5      eUS
:  a  [        SU 35      eUS:w  a  [        SU SU 35      eg )Ni  zDeepInfra Server: Error i  zDeepInfra Server: Unauthorizedi  i  z"DeepInfra Server: Model not found i  z%DeepInfra Server: Rate limit exceededi  z'DeepInfra received an invalid payload:    z6DeepInfra returned an unexpected response with status z: )	Exceptionr   
ValueError)r%   r:   r;   s      r   _handle_statusDeepInfra._handle_statusU   s    3;6tf=>>S[<==S[<==S[@PQQS[CDDS[FtfMNNS[HbQUPVW  r"   stoprun_managerc                    [        U R                  5       S9nUR                  U R                  5       U R	                  X5      S9nU R                  UR                  UR                  5        UR                  5       nUS   S   S   $ )a$  Call out to DeepInfra's inference API endpoint.

Args:
    prompt: The prompt to pass into the model.
    stop: Optional list of stop words to use when generating.

Returns:
    The string generated by the model.

Example:
    .. code-block:: python

        response = di("Tell me a joke.")
headersurldataresultsr   generated_text)	r   r2   postr.   r8   r@   status_coder;   jsonr%   r4   rB   rC   r5   requestresponserI   s           r   _callDeepInfra._callg   so    , 4==?3<<DIIKdjj6P<QH00(--@}}Iq!"233r"   c                   #    [        U R                  5       S9nUR                  U R                  5       U R	                  X5      S9 IS h  vN nU R                  UR                  UR                  5        UR                  5       I S h  vN nUS   S   S   sS S S 5      IS h  vN   $  N^ N" N	! , IS h  vN  (       d  f       g = f7f)NrE   rG   rJ   r   rK   )	r   r2   apostr.   r8   r@   statusr;   rN   rO   s           r   _acallDeepInfra._acall   s      4==?3==		$**V"< ! 
 
?!(D	?1%&67
 
 
 )	
 
 
 
sZ   A	C
B*C
:B0	B,
B0C
$B.%C
,B0.C
0C6B97CC
c           	   +     #    [        U R                  5       S9nUR                  U R                  5       U R	                  U0 UESS0E5      S9nUR
                  nU R                  U5        U R                  UR                  UR
                  5        [        UR                  5       5       H=  n[        U5      n	U	(       d  M  U(       a  UR                  U	R
                  5        U	v   M?     g 7fNrE   streamTrG   )r   r2   rL   r.   r8   r;   _handle_body_errorsr@   rM   _parse_stream
iter_lines_handle_sse_lineon_llm_new_token
r%   r4   rB   rC   r5   rP   rQ   response_textlinechunks
             r   _streamDeepInfra._stream   s      4==?3<<		$**V5O5O$5O"P   
 !  /H00(--@!("5"5"78D$T*Eu00< 9s   B;C,+C,c           	       #    [        U R                  5       S9nUR                  U R                  5       U R	                  U0 UESS0E5      S9 IS h  vN nUR                  5       I S h  vN nU R                  U5        U R                  UR                  UR
                  5        [        UR                  5        S h  vN n[        U5      n	U	(       d  M  U(       a#  UR                  U	R
                  5      I S h  vN   U	7v   MN   N N NM N
 S S S 5      IS h  vN    g ! , IS h  vN  (       d  f       g = f7frZ   )r   r2   rU   r.   r8   r;   r\   r@   rV   _parse_stream_asynccontentr_   r`   ra   s
             r   _astreamDeepInfra._astream   s     4==?3==		$**V5O5O$5O"P ! 
 
"*--/1M$$]3?1(2B2BC  d(.5")::5::FFFK
 2  G	 D
 
 
 
 
 
s   AD>D	D>D$*D+AD$;D?D DD$%D$=D>D$	D>D$DD$D$D>D D>$D;*D-+D;7D>bodyc                    SU;   a[   UR                  S5      (       a  U[        S5      S n[        R                  " U5      nUR	                  SS5      n[        SU 35      eg! [        R                   a    [        SU 35      ef = f)zk
Example error response:
data: {"error_type": "validation_error",
"error_message": "ConnectionError: ..."}
errorzdata:Nerror_messagezUnknown errorzDeepInfra Server Error: zDeepInfra Server: )
startswithlenrN   loadsgetr>   JSONDecodeError)r%   rl   
error_dataro   s       r   r\   DeepInfra._handle_body_errors   s     d?	=??7++G/D!ZZ-
 * P":=/ JKK  '' ="4TF ;<<=s   AA# ##Br*   )NN)'__name__
__module____qualname____firstlineno____doc__DEFAULT_MODEL_IDr   str__annotations__r   r	   r   r   r   model_configr   r    propertyr   r   r&   r+   r.   r2   r8   intr@   r   r   rR   r
   rW   r   r   re   r   rj   r\   __static_attributes__r*   r"   r   r   r      s:     %Hc$#'L(4.')-#-L $ 4   
WS#X%6 
 
 3  Ic I
$ 

C 
 
 
3 c d * %):>	44 tCy!4 67	4
 4 
4B %)?C	88 tCy!8 ;<	8
 8 
8$ %):>	 tCy! 67	
  
/	"0 %)?C	   tCy!  ;<	 
   
	' *= = =r"   r   rbodyr   c              #   F   #    U  H  n[        U5      nUc  M  Uv   M     g 7fN_parse_stream_helperr   rc   _lines      r   r]   r]      s$     $T*K s   !	!c                V   #    U   S h  vN n[        U5      nUc  M  U7v   M    N
 g 7fr   r   r   s      r   rh   rh      s+      d$T*Kes    )'%'))')rc   c                     U (       ao  U R                  S5      (       aY  U R                  S5      (       a  U [        S5      S  n OU [        S5      S  n U R                  5       S:X  a  g U R                  S5      $ g )Ns   data:s   data: s   [DONE]zutf-8)rp   rq   stripdecode)rc   s    r   r   r      sh    ))??9%%I()DH(D::<9$ ;;w''r"   c                      [         R                  " U 5      n[        UR                  S0 5      R                  S5      S9$ ! [         a     g f = f)Ntokenr;   )r;   )rN   rr   r   rs   r>   )rc   objs     r   r_   r_      sO    jj"%))&1
 	
  s   =A   
AA)!rN   typingr   r   r   r   r   r   r	   aiohttplangchain_core.callbacksr
   r   #langchain_core.language_models.llmsr   langchain_core.outputsr   langchain_core.utilsr   r   pydanticr   &langchain_community.utilities.requestsr   r|   r   bytesr}   r]   StreamReaderrh   r   r_   r*   r"   r   <module>r      s     N N N  4 2 ?  ;9 {= {=|% Xc] W%9%9 mC>P u #  3 8O#< r"   