o
    ZhX                     @   s   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
 d
Zdee fddZ		ddededededeeeeeef f f
ddZG dd deZdS )    N)AnyDictListMappingOptionalUnioncast)CallbackManagerForLLMRun)LLM)get_from_dict_or_env)
ConfigDictmodel_validator)enforce_stop_tokens<   c                   @   s:   e Zd ZU dZeed< eed< d
ddZeddd	ZdS )AviaryBackendzAviary backend.

    Attributes:
        backend_url: The URL for the Aviary backend.
        bearer: The bearer token for the Aviary backend.
    backend_urlbearerreturnNc                 C   s   d| j i| _d S )NAuthorization)r   headerself r   V/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/aviary.py__post_init__   s   zAviaryBackend.__post_init__c                 C   sT   t d}|sJ dt dd}|rd| nd}||ds"dnd7 }| ||S )N
AVIARY_URLzAVIARY_URL must be setAVIARY_TOKEN zBearer /)osgetenvendswith)cls
aviary_urlaviary_tokenr   r   r   r   from_env   s   

zAviaryBackend.from_env)r   N)r   r   )	__name__
__module____qualname____doc__str__annotations__r   classmethodr%   r   r   r   r   r      s   
 
r   r   c               
   C   s~   t  } | jd }tj|| jtd}z| }W n tjy1 } zt	d| d|j
 |d}~ww tdd | D }|S )zList available modelsz-/routes)headerstimeoutError decoding JSON from . Text response: Nc                 S   s&   g | ]}d |v r| dd dqS )--r   )lstripreplace).0kr   r   r   
<listcomp>8   s   & zget_models.<locals>.<listcomp>)r   r%   r   requestsgetr   TIMEOUTjsonJSONDecodeErrorRuntimeErrortextsortedkeys)backendrequest_urlresponseresulter   r   r   
get_models,   s"   
rE   Tr   modelpromptuse_prompt_formatversionc              
   C   s~   t  }|j| dd d | d }tj||j||dtd}z| W S  tj	y> } zt
d| d|j |d}~ww )	z#Get completions from Aviary models.r   r1   query)rG   rH   )r-   r:   r.   r/   r0   N)r   r%   r   r3   r7   postr   r9   r:   r;   r<   r=   )rF   rG   rH   rI   r@   urlrB   rD   r   r   r   get_completions=   s$   
rM   c                   @   s   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
dZeddededefddZedeeef fddZedefddZ		ddedeee  dee dedef
ddZdS )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/LightGPTrF   Nr#   r$   TrH   rI   Zforbid)extrabefore)modevaluesr   c              
   C   s   t |dd}t |dd}|tjd< |tjd< zt }W n tjjy- } zt|d}~ww |d}|rE||vrEt| d|d  d|S )	z?Validate that api key and python package exists in environment.r#   r   r$   r   NrF   z does not support model .)	r   r   environrE   r7   
exceptionsRequestException
ValueErrorr8   )r"   rR   r#   r$   Zaviary_modelsrD   rF   r   r   r   validate_environment   s   



zAviary.validate_environmentc                 C   s   | j | jdS )zGet the identifying parameters.)Z
model_namer#   )rF   r#   r   r   r   r   _identifying_params   s   zAviary._identifying_paramsc                 C   s   d| j dd S )zReturn type of llm.zaviary-r   -)rF   r3   r   r   r   r   	_llm_type   s   zAviary._llm_typerG   stoprun_managerkwargsc                 K   sP   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.")
        rH   rI   )rF   rG   Zgenerated_textNr   )rH   rI   rM   rF   r   r*   r   )r   rG   r\   r]   r^   outputr=   r   r   r   _call   s   


zAviary._call)NN)r&   r'   r(   r)   rF   r*   r+   r#   r   r$   rH   boolrI   r   Zmodel_configr   r,   r   r   rX   propertyr   rY   r[   r   r	   r`   r   r   r   r   rN   U   s<   
 
rN   )Tr   ) dataclassesr   typingr   r   r   r   r   r   r   r7   Zlangchain_core.callbacksr	   Z#langchain_core.language_models.llmsr
   Zlangchain_core.utilsr   Zpydanticr   r   Zlangchain_community.llms.utilsr   r9   	dataclassr   r*   rE   ra   floatintrM   rN   r   r   r   r   <module>   s6    $
