
    7|hX                        d dl Z d dlZd dlmZmZmZmZmZmZm	Z	 d dl
Z
d dlmZ d dlmZ d dlmZ d dlmZmZ d dlmZ dZe j.                   G d	 d
             Zdee   fdZ	 	 ddededededeeeeeef   f   f
dZ G d de      Zy)    N)AnyDictListMappingOptionalUnioncast)CallbackManagerForLLMRun)LLM)get_from_dict_or_env)
ConfigDictmodel_validator)enforce_stop_tokens<   c                   @    e Zd ZU dZeed<   eed<   ddZedd       Zy)	AviaryBackendzAviary backend.

    Attributes:
        backend_url: The URL for the Aviary backend.
        bearer: The bearer token for the Aviary backend.
    backend_urlbearerNc                 *    d| j                   i| _        y )NAuthorization)r   headerselfs    ^/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/aviary.py__post_init__zAviaryBackend.__post_init__   s    &4    c                     t        j                  d      }|sJ d       t        j                  dd      }|rd| nd}||j                  d      sdndz  } | ||      S )N
AVIARY_URLzAVIARY_URL must be setAVIARY_TOKEN zBearer /)osgetenvendswith)cls
aviary_urlaviary_tokenr   s       r   from_envzAviaryBackend.from_env   sd    YY|,
333zyy4-97<.)r!4!4S!9crA
:v&&r   )returnN)r)   r   )	__name__
__module____qualname____doc__str__annotations__r   classmethodr(    r   r   r   r      s-     K5 	' 	'r   r   r)   c            	         t         j                         } | j                  dz   }t        j                  || j
                  t              }	 |j                         }t        |j                         D cg c](  }d|v s|j                  d      j                  dd      * c}      }|S # t        j                  $ r!}t        d| d|j                         |d}~ww xY wc c}w )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_modelsrI   ,   s    $$&G%%
2K||KQH
 39;;=NaDAI#		tS	)NF M ## '}4Ehmm_U
	
 	Os$   B( 2	C<#C(C;CCmodelpromptuse_prompt_formatversionc                 ^   t         j                         }|j                  | j                  dd      z   dz   |z   dz   }t	        j
                  ||j                  ||dt              }	 |j                         S # t        j                  $ r!}t        d| d|j                         |d}~ww xY w)	z#Get completions from Aviary models.r!   r7   query)rK   rL   )r3   r;   r4   r5   r6   N)r   r(   r   rB   r8   postr   r:   r;   r<   r=   r>   )rJ   rK   rL   rM   rC   urlrE   rG   s           r   get_completionsrR   =   s     $$&G


c4 8
83
>
H7
RC}}5FG	H}}## 'u,=hmm_M
	s   (A8 8B,B''B,c                      e Zd ZU dZdZeed<   dZee   ed<   dZ	ee   ed<   dZ
eed<   dZee   ed	<    ed
      Z ed      ededefd              Zedeeef   fd       Zedefd       Z	 	 ddedeee      dee   dedef
dZy)Aviaryab  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/LightGPTrJ   Nr&   r'   TrL   rM   forbid)extrabefore)modevaluesr)   c                 R   t        |dd      }t        |dd      }|t        j                  d<   |t        j                  d<   	 t               }|j                  d      }|r||vrt        | d|d    d      |S # t        j
                  j                  $ r}t        |      d}~ww xY w)	z?Validate that api key and python package exists in environment.r&   r   r'   r   NrJ   z does not support model .)	r   r"   environrI   r8   
exceptionsRequestException
ValueErrorr9   )r%   rY   r&   r'   aviary_modelsrG   rJ   s          r   validate_environmentzAviary.validate_environment   s     *&,M
+FNNS $.

< %1

>"	 &LM 

7#U-/
|+CF7OCTTUVWW ""33 	 Q-	 s   
A9 9B&B!!B&c                 4    | j                   | j                  dS )zGet the identifying parameters.)
model_namer&   )rJ   r&   r   s    r   _identifying_paramszAviary._identifying_params   s     **//
 	
r   c                 @    d| j                   j                  dd       S )zReturn type of llm.zaviary-r!   -)rJ   rB   r   s    r   	_llm_typezAviary._llm_type   s#     ++C5677r   rK   stoprun_managerkwargsc                     d| j                   i}| j                  r| j                  |d<   t        d| j                  |d|}t	        t
        |d         }|rt        ||      }|S )a  Call 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.")
        rL   rM   )rJ   rK   generated_textr1   )rL   rM   rR   rJ   r	   r.   r   )r   rK   rh   ri   rj   outputr>   s          r   _callzAviary._call   st    & &t'='=><< $F9  
**
 
 C 012&tT2Dr   )NN)r*   r+   r,   r-   rJ   r.   r/   r&   r   r'   rL   boolrM   r   model_configr   r0   r   r   ra   propertyr   rd   rg   r   r
   rn   r1   r   r   rT   rT   U   s   > #E3" $J$"&L(3-&"t"!GXc]!L (#$ 3   $( 
WS#X%6 
 
 83 8 8 %):>	!! tCy!! 67	!
 ! 
!r   rT   )Tr    ) dataclassesr"   typingr   r   r   r   r   r   r	   r8   langchain_core.callbacksr
   #langchain_core.language_models.llmsr   langchain_core.utilsr   pydanticr   r   langchain_community.llms.utilsr   r:   	dataclassr   r.   rI   ro   floatintrR   rT   r1   r   r   <module>r|      s     	 B B B  = 3 5 0 >
 ' ' '6DI ( #	  	
 
#uS%_%
%&0pS pr   