
    dhX                        S SK r 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  S SKJr  S SKJr  S SKJrJr  S SKJr  Sr\ R.                   " S	 S
5      5       rS\\   4S jr  SS\S\S\S\S\\\\\\4   4   4
S jjr " S S\5      rg)    N)AnyDictListMappingOptionalUnioncast)CallbackManagerForLLMRun)LLM)get_from_dict_or_env)
ConfigDictmodel_validator)enforce_stop_tokens<   c                   L    \ rS rSr% Sr\\S'   \\S'   S	S jr\S
S j5       r	Sr
g)AviaryBackend   zAviary backend.

Attributes:
    backend_url: The URL for the Aviary backend.
    bearer: The bearer token for the Aviary backend.
backend_urlbearerNc                 *    SU R                   0U l        g )NAuthorization)r   headerselfs    W/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/aviary.py__post_init__AviaryBackend.__post_init__   s    &4    c                     [         R                  " S5      nU(       d   S5       e[         R                  " SS5      nU(       a  SU 3OSnXR                  S5      (       d  SOS-  nU " X5      $ )N
AVIARY_URLzAVIARY_URL must be setAVIARY_TOKEN zBearer /)osgetenvendswith)cls
aviary_urlaviary_tokenr   s       r   from_envAviaryBackend.from_env   sc    YY|,
333zyy4-97<.)r!4!4S!9!9crA
:&&r   )r   )returnN)r,   r   )__name__
__module____qualname____firstlineno____doc__str__annotations__r   classmethodr*   __static_attributes__ r   r   r   r      s-     K5 	' 	'r   r   r,   c            	         [         R                  5       n U R                  S-   n[        R                  " XR
                  [        S9n UR                  5       n[        UR                  5        Vs/ sH,  nSU;   d  M  UR                  S5      R                  SS5      PM.     sn5      nU$ ! [        R                   a!  n[        SU SUR                   35      UeSnAff = fs  snf )zList available modelsz-/routes)headerstimeoutError decoding JSON from . Text response: N--r#   )r   r*   r   requestsgetr   TIMEOUTjsonJSONDecodeErrorRuntimeErrortextsortedkeyslstripreplace)backendrequest_urlresponseresulteks         r   
get_modelsrN   ,   s    $$&G%%
2K||KQH
 39;;=N=aDAI	)#		tS	)=NF M ## '}4Ehmm_U
	
 	Os$   B* /	C"<%C"*C>CCmodelpromptuse_prompt_formatversionc                 Z   [         R                  5       nUR                  U R                  SS5      -   S-   U-   S-   n[        R
                  " UUR                  XS.[        S9n UR                  5       $ ! [        R                   a!  n[        SU SUR                   35      UeSnAff = f)	z#Get completions from Aviary models.r#   r<   query)rP   rQ   )r8   r@   r9   r:   r;   N)r   r*   r   rG   r=   postr   r?   r@   rA   rB   rC   )rO   rP   rQ   rR   rH   urlrJ   rL   s           r   get_completionsrW   =   s     $$&G


c4 8
83
>
H7
RC}}G	H}}## 'u,=hmm_M
	s   %A5 5B*	B%%B*c                      \ rS rSr% SrSr\\S'   Sr\	\   \S'   Sr
\	\   \S'   Sr\\S	'   Sr\	\   \S
'   \" SS9r\" SS9\S\S\4S j5       5       r\S\\\4   4S j5       r\S\4S j5       r  SS\S\	\\      S\	\   S\S\4
S jjrSrg)AviaryU   a  Aviary hosted models.

Aviary is a backend for hosted models. You can
find out more about aviary at
http://github.com/ray-project/aviary

To get a list of the models supported on an
aviary, follow the instructions on the website to
install the aviary CLI and then use:
`aviary models`

AVIARY_URL and AVIARY_TOKEN environment variables must be set.

Attributes:
    model: The name of the model to use. Defaults to "amazon/LightGPT".
    aviary_url: The URL for the Aviary backend. Defaults to None.
    aviary_token: The bearer token for the Aviary backend. Defaults to None.
    use_prompt_format: If True, the prompt template for the model will be ignored.
        Defaults to True.
    version: API version to use for Aviary. Defaults to None.

Example:
    .. code-block:: python

        from langchain_community.llms import Aviary
        os.environ["AVIARY_URL"] = "<URL>"
        os.environ["AVIARY_TOKEN"] = "<TOKEN>"
        light = Aviary(model='amazon/LightGPT')
        output = light('How do you make fried rice?')
zamazon/LightGPTrO   Nr(   r)   TrQ   rR   forbid)extrabefore)modevaluesr,   c                 `   [        USS5      n[        USS5      nU[        R                  S'   U[        R                  S'    [        5       nUR                  S5      nU(       a  Xd;  a  [        U SUS    S35      eU$ ! [        R
                  R                   a  n[        U5      eSnAff = f)	z?Validate that api key and python package exists in environment.r(   r    r)   r!   NrO   z does not support model .)	r   r$   environrN   r=   
exceptionsRequestException
ValueErrorr>   )r'   r_   r(   r)   aviary_modelsrL   rO   s          r   validate_environmentAviary.validate_environment   s     *&,M
+FNNS $.

< %1

>"	 &LM 

7#U/
|+CF7OCTTUVWW ""33 	 Q-	 s   
A? ?B-B((B-c                 4    U R                   U R                  S.$ )zGet the identifying parameters.)
model_namer(   )rO   r(   r   s    r   _identifying_paramsAviary._identifying_params   s     **//
 	
r   c                 @    SU R                   R                  SS5       3$ )zReturn type of llm.zaviary-r#   -)rO   rG   r   s    r   	_llm_typeAviary._llm_type   s#     ++C5677r   rP   stoprun_managerkwargsc                     SU R                   0nU R                  (       a  U R                  US'   [        SU R                  US.UD6n[	        [
        US   5      nU(       a  [        Xb5      nU$ )zCall out to Aviary
Args:
    prompt: The prompt to pass into the model.

Returns:
    The string generated by the model.

Example:
    .. code-block:: python

        response = aviary("Tell me a joke.")
rQ   rR   )rO   rP   generated_textr6   )rQ   rR   rW   rO   r	   r2   r   )r   rP   rq   rr   rs   outputrC   s          r   _callAviary._call   sr    & &t'='=><< $F9  
**
 
 C 012&t2Dr   r6   )NN)r-   r.   r/   r0   r1   rO   r2   r3   r(   r   r)   rQ   boolrR   r   model_configr   r4   r   r   rg   propertyr   rk   ro   r   r
   rw   r5   r6   r   r   rY   rY   U   s   > #E3" $J$"&L(3-&"t"!GXc]!L (#$ 3   $( 
WS#X%6 
 
 83 8 8 %):>	!! tCy!! 67	!
 ! 
! !r   rY   )Tr"   ) dataclassesr$   typingr   r   r   r   r   r   r	   r=   langchain_core.callbacksr
   #langchain_core.language_models.llmsr   langchain_core.utilsr   pydanticr   r   langchain_community.llms.utilsr   r?   	dataclassr   r2   rN   ry   floatintrW   rY   r6   r   r   <module>r      s     	 B B B  = 3 5 0 >
 ' ' '6DI ( #	  	
 
#uS%_%
%&0pS pr   