o
    Zh,                     @   s   d dl mZmZmZmZmZ d dlmZ d dlm	Z	 d dl
mZmZmZ d dlmZmZ d dlmZ G dd de	Zed	krJe Zeed
 dS dS )    )AnyDictListOptionalSequence)CallbackManagerForLLMRun)LLM)convert_to_secret_strget_from_dict_or_envpre_init)
ConfigDict	SecretStr)enforce_stop_tokensc                   @   s*  e Zd ZU dZdZeed< dZee	 ed< 	 dZ
eed< 	 dZeed	< 	 d
Zeed< 	 dZeed< 	 dZeed< 	 dZeed< 	 dZee ed< 	 dZee ed< 	 dZee	 ed< 	 dZee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ef  ed< 	 dZee ed< 	 dZee ed< 	 dZee ed< d
Z ee ed< 	 dZ!eed< 	 dZ"eed< dZ#eed< d Z$eed!< dZ%eed"< dZ&ee'e	  ed#< dZ(eed$< dZ)ee'e	  ed%< 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d+< 	 dZ/eee	  ed,< 	 dZ0ee1 ed-< 	 d.Z2e	ed/< 	 dZ3ee	 ed0< 	 d1Z4eed2< 	 d3Z5eed4< 	 dZ6eed5< 	 e7d6d7Z8e9d8ed9efd:d;Z:e;d9ee	ef fd<d=Z<e;d9ee	ef fd>d?Z=e;d9e	fd@dAZ>		dHdBe	dCeee	  dDee? dEed9e	f
dFdGZ@dS )I
AlephAlphaam  Aleph Alpha large language models.

    To use, you should have the ``aleph_alpha_client`` python package installed, and the
    environment variable ``ALEPH_ALPHA_API_KEY`` set with your API key, or pass
    it as a named parameter to the constructor.

    Parameters are explained more in depth here:
    https://github.com/Aleph-Alpha/aleph-alpha-client/blob/c14b7dd2b4325c7da0d6a119f6e76385800e097b/aleph_alpha_client/completion.py#L10

    Example:
        .. code-block:: python

            from langchain_community.llms import AlephAlpha
            aleph_alpha = AlephAlpha(aleph_alpha_api_key="my-api-key")
    Nclientzluminous-basemodel@   maximum_tokensg        temperaturer   top_ktop_ppresence_penaltyfrequency_penaltyF#repetition_penalties_include_prompt#use_multiplicative_presence_penaltypenalty_biaspenalty_exceptions)penalty_exceptions_include_stop_sequencesbest_of   n
logit_bias	log_probstokensdisable_optimizationsminimum_tokensecho$use_multiplicative_frequency_penaltysequence_penalty   sequence_penalty_min_length#use_multiplicative_sequence_penaltycompletion_bias_inclusion*completion_bias_inclusion_first_token_onlycompletion_bias_exclusion*completion_bias_exclusion_first_token_onlycontextual_control_thresholdTcontrol_log_additive'repetition_penalties_include_completionraw_completionstop_sequencesaleph_alpha_api_keyzhttps://api.aleph-alpha.comhosthostingi1  request_timeout_seconds   total_retriesniceZforbid)extravaluesreturnc                 C   sr   t t|dd|d< z#ddlm} ||d  |d |d |d |d |d	 d
|d< W |S  ty8   tdw )z?Validate that api key and python package exists in environment.r5   ZALEPH_ALPHA_API_KEYr   )Clientr6   r7   r8   r:   r;   )tokenr6   r7   r8   r:   r;   r   zlCould not import aleph_alpha_client python package. Please install it with `pip install aleph_alpha_client`.)r	   r
   aleph_alpha_clientr?   Zget_secret_valueImportError)clsr=   r?    rD   [/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/aleph_alpha.pyvalidate_environment   s&   

zAlephAlpha.validate_environmentc                 C   s   i d| j d| jd| jd| jd| jd| jd| jd| jd	| jd
| j	d| j
d| jd| jd| jd| jd| jd| j| j| j| j| j| j| j| j| j| j| j| j| j| j| jdS )z;Get the default parameters for calling the Aleph Alpha API.r   r   r   r   r   r   r    r   r   r   r   r   r   r!   r"   r#   r$   )r%   r&   r'   r(   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   )r   r   r   r   r   r   r    r   r   r   r   r   r   r!   r"   r#   r$   r%   r&   r'   r(   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   selfrD   rD   rE   _default_params   sd   	
zAlephAlpha._default_paramsc                 C   s   i d| j i| jS )zGet the identifying parameters.r   )r   rI   rG   rD   rD   rE   _identifying_params   s   zAlephAlpha._identifying_paramsc                 C   s   dS )zReturn type of llm.Zaleph_alpharD   rG   rD   rD   rE   	_llm_type   s   zAlephAlpha._llm_typepromptstoprun_managerkwargsc                 K   s   ddl m}m} | j}| jdur|durtd| jdur#| j|d< n||d< i ||}|dd||i|}| jj| j	|d}	|	j
d j}
|dusQ| jdurXt|
|d }
|
S )	at  Call out to Aleph Alpha's completion endpoint.

        Args:
            prompt: The prompt to pass into the model.
            stop: Optional list of stop words to use when generating.

        Returns:
            The string generated by the model.

        Example:
            .. code-block:: python

                response = aleph_alpha("Tell me a joke.")
        r   )CompletionRequestPromptNz:stop sequences found in both the input and default params.r4   rL   )r   requestrD   )rA   rP   rQ   rI   r4   
ValueErrorZ	from_textr   Zcompleter   Zcompletions
completionr   )rH   rL   rM   rN   rO   rP   rQ   paramsrR   responsetextrD   rD   rE   _call   s    
zAlephAlpha._call)NN)A__name__
__module____qualname____doc__r   r   __annotations__r   r   strr   intr   floatr   r   r   r   r   boolr   r   r   r   r   r   r    r!   r   r"   r#   r$   r%   r&   r'   r(   r*   r+   r,   r   r-   r.   r/   r0   r1   r2   r3   r4   r5   r   r6   r7   r8   r:   r;   r   Zmodel_configr   rF   propertyrI   rJ   rK   r   rX   rD   rD   rD   rE   r      s   
 	$
r   __main__zHow are you?N)typingr   r   r   r   r   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   Zlangchain_core.utilsr	   r
   r   Zpydanticr   r   Zlangchain_community.llms.utilsr   r   rY   ZaaprintZinvokerD   rD   rD   rE   <module>   s      