a
    bg$                     @   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eedddZeddeeeef edd d!Z e!ed"d#d$Z"eed%d&d'Z#e$ed"d(d)Z%d/eeee  ee& eed*d+d,Z'd0eeee  ee( eed*d-d.Z)dS )1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+   m/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/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 ]H}||vr*||v rNt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       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_extra)r)   c                 C   s   dS )zReturn type of model.Zedenair+   )selfr+   r+   r,   	_llm_typeg   s    zEdenAI._llm_type)outputr)   c                 C   s2   | j dkr|| j d S || j d d d S 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_communityrC   rB   r+   r+   r,   get_user_agentr   s    zEdenAI.get_user_agent)promptstoprun_managerkwargsr)   c                 K   s  d}| j dur |dur tdn| j dur2| 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rtd|
j nB|
jdkrtd|
j n$|
jdkr2td|
j d|
j |
 }|| j }|ddkrn|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   s   i | ]\}}|d ur||qS Nr+   r1   kvr+   r+   r,   
<dictcomp>   r4   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&   r7   r   r   r   r   rD   r   r!   r    r"   r$   r@   r   r   poststatus_code	Exceptionr   jsonr5   rA   r   )r=   rE   rF   rG   rH   stopsrX   rW   payloadrequestresponserY   provider_responseerr_msgr?   r+   r+   r,   _callx   sd    








zEdenAI._callc              
      s   d}| j dur |dur tdn| j dur2| 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 8}	|	j|||d4 I dH  }
|
jdkrtd|
j nB|
jdkr&td|
j n$|
jdkrJt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 s0    Y  W d  I dH  q1 I dH s0    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.
        NrI   rJ   rK   rL   rN   rP   r   c                 S   s   i | ]\}}|d ur||qS rQ   r+   rR   r+   r+   r,   rU      r4   z!EdenAI._acall.<locals>.<dictcomp>rV   )rh   rW   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   )r&   r7   r   r   r   r   rD   r   r!   r    r"   r$   r@   r   r   re   ra   rg   r   rh   r5   rA   r   )r=   rE   rF   rG   rH   ri   rX   rW   rj   sessionrl   Zresponse_jsonrm   rn   r?   r+   r+   r,   _acall   sh    







zEdenAI._acall)NN)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>   rA   staticmethodrD   r	   ro   r   rq   r+   r+   r+   r,   r      sX   
  
L  
r   ) ru   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   	getLoggerrr   r8   r   r+   r+   r+   r,   <module>   s   
