o
    Zh$                     @   s   d Z ddlZddlmZmZmZmZmZ ddlm	Z	 ddl
mZmZ ddlmZ ddlmZmZ ddlmZ dd	lmZmZmZ dd
lmZ ddlmZ eeZG dd deZdS )z'Wrapper around EdenAI's Generation API.    N)AnyDictListLiteralOptional)ClientSession)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LLM)get_from_dict_or_envpre_init)
get_fields)
ConfigDictFieldmodel_validator)enforce_stop_tokens)Requestsc                   @   s  e Zd ZU dZdZeed< dZee ed< dZ	e
d ed< 	 d	Ze
d	 ed
< 	 eed< 	 dZee ed< 	 eddddZee ed< edddZee ed< dZee
d  ed< eedZeeef ed< 	 eedZeeef ed< 	 dZeee  ed< 	 eddZededefddZedd edeeef defd!d"Z e!defd#d$Z"d%edefd&d'Z#e$defd(d)Z%		d2d*ed+eee  d,ee& d-edef
d.d/Z'		d2d*ed+eee  d,ee( d-edef
d0d1Z)dS )3EdenAIa  EdenAI models.

    To use, you should have
    the environment variable ``EDENAI_API_KEY`` set with your API token.
    You can find your token here: https://app.edenai.run/admin/account/settings

    `feature` and `subfeature` are required, but any other model parameters can also be
    passed in with the format params={model_param: value, ...}

    for api reference check edenai documentation: http://docs.edenai.co.
    zhttps://api.edenai.run/v2base_urlNedenai_api_keytext)r   imagefeatureZ
generation
subfeatureprovidermodelr      )defaultgeletemperature)r   r   
max_tokens)Z256x256Z512x512Z	1024x1024
resolution)default_factoryparamsmodel_kwargsstop_sequencesZforbid)extravaluesreturnc                 C   s   t |dd|d< |S )z,Validate that api key exists in environment.r   ZEDENAI_API_KEY)r   )clsr(    r+   V/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/edenai.pyvalidate_environmentL   s   zEdenAI.validate_environmentbefore)modec                 C   s   dd t |  D }|di }t|D ]$}||vr9||v r'td| dt| d| d ||||< q||d< |S )z>Build extra kwargs from additional params that were passed in.c                 S   s   h | ]}|j qS r+   )alias).0fieldr+   r+   r,   	<setcomp>X   s    z%EdenAI.build_extra.<locals>.<setcomp>r%   zFound z supplied twice.zJ was transferred to model_kwargs.
                    Please confirm that z is what you intended.)r   r(   getlist
ValueErrorloggerwarningpop)r*   r(   Zall_required_field_namesr'   
field_namer+   r+   r,   build_extraT   s   zEdenAI.build_extrac                 C   s   dS )zReturn type of model.Zedenair+   )selfr+   r+   r,   	_llm_typeg   s   zEdenAI._llm_typeoutputc                 C   s.   | j dkr|| j d S || j d d d S )Nr   Zgenerated_textitemsr   r   )r   r   )r<   r>   r+   r+   r,   _format_outputl   s   
zEdenAI._format_outputc                  C   s   ddl m}  d|  S )Nr   __version__z
langchain/)Zlangchain_communityrB   rA   r+   r+   r,   get_user_agentr   s   
zEdenAI.get_user_agentpromptstoprun_managerkwargsc                 K   s  d}| j dur|durtd| j dur| j }n|}| j d| j d| j }d| j |  d}| j|| j| j	| j
d| j|ddi}d	d
 | D }| jdur]| j| ji|d< t|d}	|	j||d}
|
jdkrvtd|
j |
jdkrtd|
j |
jdkrtd|
j d|
j |
 }|| j }|ddkr|di d}t|| |}|durt||}|S )zCall out to EdenAI's text generation endpoint.

        Args:
            prompt: The prompt to pass into the model.

        Returns:
            json formatted str response.
        N:stop sequences found in both the input and default params./Bearer Authorizationz
User-Agent	providersr   r!   r    r"   
num_imagesr   c                 S      i | ]\}}|d ur||qS Nr+   r1   kvr+   r+   r,   
<dictcomp>       z EdenAI._call.<locals>.<dictcomp>settings)headers)urldata  EdenAI Server: Error   $EdenAI received an invalid payload:    3EdenAI returned an unexpected response with status : statusfailerrormessage)r&   r6   r   r   r   r   rC   r   r!   r    r"   r$   r?   r   r   poststatus_code	Exceptionr   jsonr4   r@   r   )r<   rD   rE   rF   rG   stopsrY   rX   payloadrequestresponserZ   provider_responseerr_msgr>   r+   r+   r,   _callx   sd   









zEdenAI._callc              
      s
  d}| j dur|durtd| j dur| j }n|}| j d| j d| j }d| j |  d}| j|| j| j	| j
d| j|ddi}d	d
 | D }| jdur^| j| ji|d< t 4 I dH }	|	j|||d4 I dH p}
|
jdkrtd|
j |
jdkrtd|
j |
jdkrtd|
j d|
j |
 I dH }|| j }|ddkr|di d}t|| |}|durt||}|W  d  I dH  W  d  I dH  S 1 I dH sw   Y  W d  I dH  dS 1 I dH sw   Y  dS )aI  Call EdenAi model to get predictions based on the prompt.

        Args:
            prompt: The prompt to pass into the model.
            stop: A list of stop words (optional).
            run_manager: A callback manager for async interaction with LLMs.

        Returns:
            The string generated by the model.
        NrH   rI   rJ   rK   rM   rO   r   c                 S   rP   rQ   r+   rR   r+   r+   r,   rU      rV   z!EdenAI._acall.<locals>.<dictcomp>rW   )ri   rX   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   )r&   r6   r   r   r   r   rC   r   r!   r    r"   r$   r?   r   r   rf   rb   rh   r   ri   r4   r@   r   )r<   rD   rE   rF   rG   rj   rY   rX   rk   sessionrm   Zresponse_jsonrn   ro   r>   r+   r+   r,   _acall   sr   









.zEdenAI._acall)NN)*__name__
__module____qualname____doc__r   str__annotations__r   r   r   r   r   r   r   r    floatr!   intr"   dictr$   r   r   r%   r&   r   r   Zmodel_configr   r-   r   classmethodr;   propertyr=   r@   staticmethodrC   r	   rp   r   rr   r+   r+   r+   r,   r      sx   
 

L
r   ) rv   loggingtypingr   r   r   r   r   Zaiohttpr   Zlangchain_core.callbacksr   r	   Z#langchain_core.language_models.llmsr
   Zlangchain_core.utilsr   r   Zlangchain_core.utils.pydanticr   Zpydanticr   r   r   Zlangchain_community.llms.utilsr   Z&langchain_community.utilities.requestsr   	getLoggerrs   r7   r   r+   r+   r+   r,   <module>   s    
