U
    h,                     @   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 d dlmZmZmZ d dlmZ G dd de	Zed	kre Zeed
 dS )    )AnyDictListOptionalSequence)CallbackManagerForLLMRun)LLM)	SecretStr)convert_to_secret_strget_from_dict_or_envpre_init)enforce_stop_tokensc                   @   s  e Zd ZU 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Z/ee0 ed-< d.Z1eed/< dZ2ee ed0< d1Z3e
ed2< d3Z4e
ed4< dZ5eed5< G d6d7 d7Z6e7eed8d9d:Z8e9eeef d;d<d=Z:e9eeef d;d>d?Z;e9ed;d@dAZ<dEeeee  ee= eedBdCdDZ>dS )F
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")
    clientzluminous-basemodel@   maximum_tokensg        temperaturer   top_ktop_ppresence_penaltyfrequency_penaltyF#repetition_penalties_include_prompt#use_multiplicative_presence_penaltyN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nicec                   @   s   e Zd ZdZdS )zAlephAlpha.ConfigZforbidN)__name__
__module____qualname__extra r?   r?   H/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/aleph_alpha.pyConfig   s   rA   )valuesreturnc                 C   sz   t t|dd|d< zDddlm} ||d  |d |d |d |d |d	 d
|d< W n tk
rt   tdY nX |S )z?Validate that api key and python package exists in environment.r4   ZALEPH_ALPHA_API_KEYr   )Clientr5   r6   r7   r9   r:   )tokenr5   r6   r7   r9   r:   r   zlCould not import aleph_alpha_client python package. Please install it with `pip install aleph_alpha_client`.)r
   r   aleph_alpha_clientrD   Zget_secret_valueImportError)clsrB   rD   r?   r?   r@   validate_environment   s$    


zAlephAlpha.validate_environment)rC   c                  C   s   | j | j| j| j| j| j| j| j| j| j	| j
| j| j| j| j| j| 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.   r/   r0   r1   r2   rJ   selfr?   r?   r@   _default_params   s@    zAlephAlpha._default_paramsc                 C   s   d| j i| jS )zGet the identifying parameters.r   )r   rM   rK   r?   r?   r@   _identifying_params   s    zAlephAlpha._identifying_paramsc                 C   s   dS )zReturn type of llm.Zaleph_alphar?   rK   r?   r?   r@   	_llm_type   s    zAlephAlpha._llm_type)promptstoprun_managerkwargsrC   c                 K   s   ddl m}m} | j}| jdk	r2|dk	r2tdn| jdk	rH| j|d< n||d< ||}|f d||i|}| jj| j	|d}	|	j
d j}
|dk	s| jdk	rt|
|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.r3   rP   )r   request)rF   rT   rU   rM   r3   
ValueErrorZ	from_textr   Zcompleter   Zcompletions
completionr   )rL   rP   rQ   rR   rS   rT   rU   paramsrV   responsetextr?   r?   r@   _call   s     
zAlephAlpha._call)NN)?r;   r<   r=   __doc__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.   r/   r0   r1   r2   r3   r4   r	   r5   r6   r7   r9   r:   rA   r   rI   propertyrM   rN   rO   r   r\   r?   r?   r?   r@   r      sv   

	$  
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.pydantic_v1r	   Zlangchain_core.utilsr
   r   r   Zlangchain_community.llms.utilsr   r   r;   ZaaprintZinvoker?   r?   r?   r@   <module>   s     