U
    h\                     @   s   d dl 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mZ d dlmZmZmZ G dd deZG d	d
 d
e
ZdS )    )AnyDictListOptionalcastN)CallbackManagerForLLMRun)LLM)	BaseModel	SecretStr)convert_to_secret_strget_from_dict_or_envpre_initc                   @   sZ   e Zd ZU dZ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
S )AI21PenaltyDataz!Parameters for AI21 penalty data.r   scaleTapplyToWhitespacesapplyToPunctuationsapplyToNumbersapplyToStopwordsapplyToEmojisN)__name__
__module____qualname____doc__r   int__annotations__r   boolr   r   r   r    r   r   A/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/ai21.pyr   
   s   
r   c                   @   s\  e Zd ZU dZ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< e Zeed< e Zeed< e Z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  ed< dZee ed< G dd dZeeedddZeeeef dddZeeeef dddZ eedd d!Z!d%eeee  ee" eed"d#d$Z#dS )&AI21ag  AI21 large language models.

    To use, you should have the environment variable ``AI21_API_KEY``
    set with your API key or pass it as a named parameter to the constructor.

    Example:
        .. code-block:: python

            from langchain_community.llms import AI21
            ai21 = AI21(ai21_api_key="my-api-key", model="j2-jumbo-instruct")
    zj2-jumbo-instructmodelgffffff?temperature   	maxTokensr   	minTokensg      ?topPpresencePenaltycountPenaltyfrequencyPenalty   
numResultsN	logitBiasai21_api_keystopbase_urlc                   @   s   e Zd ZdZdS )zAI21.ConfigZforbidN)r   r   r   extrar   r   r   r   ConfigG   s   r/   )valuesreturnc                 C   s   t t|dd}||d< |S )z,Validate that api key exists in environment.r+   ZAI21_API_KEY)r   r   )clsr0   r+   r   r   r   validate_environmentJ   s
    
zAI21.validate_environment)r1   c              
   C   s6   | j | j| j| j| j | j | j | j| j	d	S )z0Get the default parameters for calling AI21 API.)	r    r"   r#   r$   r%   r&   r'   r)   r*   )
r    r"   r#   r$   r%   dictr&   r'   r)   r*   selfr   r   r   _default_paramsS   s    zAI21._default_paramsc                 C   s   d| j i| jS )zGet the identifying parameters.r   )r   r7   r5   r   r   r   _identifying_paramsb   s    zAI21._identifying_paramsc                 C   s   dS )zReturn type of llm.Zai21r   r5   r   r   r   	_llm_typeg   s    zAI21._llm_type)promptr,   run_managerkwargsr1   c           
      K   s   | j dk	r|dk	rtdn| j dk	r.| j }n|dkr:g }| jdk	rL| j}n| jdkr\d}nd}| j|}tt| j| _tj	| d| j ddd	| j
  i||d
|d}|jdkr| d}td|j d| | }	|	d d d d S )ad  Call out to AI21's complete 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 = ai21("Tell me a joke.")
        Nz2`stop` found in both the input and default params.)zj1-grande-instructz+https://api.ai21.com/studio/v1/experimentalzhttps://api.ai21.com/studio/v1/z	/completeAuthorizationzBearer )r:   ZstopSequences)urlheadersjson   errorz,AI21 /complete call failed with status code z. Details: Zcompletionsr   datatext)r,   
ValueErrorr-   r   r7   r   r
   r+   requestspostZget_secret_valuestatus_coderA   get)
r6   r:   r,   r;   r<   r-   paramsresponseZoptional_detailZresponse_jsonr   r   r   _calll   s2    





z
AI21._call)NN)$r   r   r   r   r   strr   r    floatr"   r   r#   r$   r   r%   r&   r'   r)   r*   r   r   r+   r
   r,   r   r-   r/   r   r3   propertyr   r7   r8   r9   r   rM   r   r   r   r   r      s@   
  
r   )typingr   r   r   r   r   rG   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   Zlangchain_core.pydantic_v1r	   r
   Zlangchain_core.utilsr   r   r   r   r   r   r   r   r   <module>   s   