a
    bgh                     @   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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)convert_to_secret_strget_from_dict_or_envpre_init)	BaseModel
ConfigDict	SecretStrc                   @   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   k/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/ai21.pyr   
   s   
r   c                   @   sX  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< e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_urlZforbid)extra)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_environmentK   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_paramsT   s    zAI21._default_paramsc                 C   s   i d| j i| jS )zGet the identifying parameters.r    )r    r7   r5   r   r   r   _identifying_paramsc   s    zAI21._identifying_paramsc                 C   s   dS )zReturn type of llm.Zai21r   r5   r   r   r   	_llm_typeh   s    zAI21._llm_type)promptr-   run_managerkwargsr1   c           
      K   s   | j dur|durtdn| j dur.| j }n|du r:g }| jdurL| j}n| jdv r\d}nd}i | 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   _callm   s6    





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   Zmodel_configr   r3   propertyr   r7   r8   r9   r   rM   r   r   r   r   r      sD   
  
r   )typingr   r   r   r   r   rG   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   Zlangchain_core.utilsr	   r
   r   Zpydanticr   r   r   r   r   r   r   r   r   <module>   s   