o
    Zh                     @  s   d Z ddlmZ ddlZddl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mZmZ dd	lmZ eeZG d
d deZG dd deZG dd deeZdS )zWrapper around Minimax APIs.    )annotationsN)AnyDictListOptional)CallbackManagerForLLMRun)LLM)convert_to_secret_strget_from_dict_or_envpre_init)	BaseModel
ConfigDictField	SecretStrmodel_validator)enforce_stop_tokensc                   @  sT   e Zd ZU dZded< ded< ded< ded< edd	edddZdddZdS )_MinimaxEndpointClientz(API client for the Minimax LLM endpoint.strhostgroup_idr   api_keyapi_urlbefore)modevaluesDict[str, Any]returnr   c                 C  s2   d|vr|d }|d }| d| }||d< |S )Nr   r   r   z /v1/text/chatcompletion?GroupId= )clsr   r   r   r   r   r   W/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/minimax.pyset_api_url"   s   z"_MinimaxEndpointClient.set_api_urlrequestc                 C  s   dd| j   i}tj| j||d}|js"td|j d|j |	 d d dkrBtd	|	 d d  d|	 d d
  |	 d S )NAuthorizationzBearer )headersjsonzHTTP z error: Z	base_respstatus_coder   zAPI Z
status_msgZreply)
r   Zget_secret_valuerequestspostr   ok
ValueErrorr%   textr$   )selfr!   r#   responser   r   r   r'   ,   s   z_MinimaxEndpointClient.postN)r   r   r   r   )r!   r   r   r   )	__name__
__module____qualname____doc____annotations__r   classmethodr    r'   r   r   r   r   r      s   
 r   c                   @  s   e Zd ZU dZeddZded< dZded< 	 d	Zd
ed< 	 dZ	ded< 	 dZ
ded< 	 eedZded< 	 dZded< dZded< dZded< ed%ddZed&dd Zed&d!d"Zed'd#d$ZdS )(MinimaxCommonz4Common parameters for Minimax large language models.r   )Zprotected_namespacesr   _clientzabab5.5-chatr   model   int
max_tokensgffffff?floattemperaturegffffff?top_p)default_factoryr   model_kwargsNzOptional[str]minimax_api_hostminimax_group_idzOptional[SecretStr]minimax_api_keyr   r   r   c                 C  sZ   t t|dd|d< t|dd|d< t|dddd|d< t|d |d |d d	|d
< |S )z?Validate that api key and python package exists in environment.r@   ZMINIMAX_API_KEYr?   ZMINIMAX_GROUP_IDr>   ZMINIMAX_API_HOSTzhttps://api.minimax.chat)default)r   r   r   r4   )r	   r
   r   )r   r   r   r   r   validate_environmentN   s$   


z"MinimaxCommon.validate_environmentc                 C  s   | j | j| j| jd| jS )z2Get the default parameters for calling OpenAI API.)r5   Ztokens_to_generater:   r;   )r5   r8   r:   r;   r=   r+   r   r   r   _default_paramse   s   zMinimaxCommon._default_paramsc                 C  s   i d| j i| jS )zGet the identifying parameters.r5   )r5   rD   rC   r   r   r   _identifying_paramsp   s   z!MinimaxCommon._identifying_paramsc                 C  s   dS )zReturn type of llm.Zminimaxr   rC   r   r   r   	_llm_typeu   s   zMinimaxCommon._llm_type)r   r   r   r   )r   r   )r   r   )r-   r.   r/   r0   r   Zmodel_configr1   r5   r8   r:   r;   r   dictr=   r>   r?   r@   r   rB   propertyrD   rE   rF   r   r   r   r   r3   :   s2   
 

r3   c                   @  s    e Zd ZdZ		ddddZdS )Minimaxa  Minimax large language models.

    To use, you should have the environment variable
    ``MINIMAX_API_KEY`` and ``MINIMAX_GROUP_ID`` set with your API key,
    or pass them as a named parameter to the constructor.
    Example:
     . code-block:: python
         from langchain_community.llms.minimax import Minimax
         minimax = Minimax(model="<model_name>", minimax_api_key="my-api-key",
          minimax_group_id="my-group-id")
    Npromptr   stopOptional[List[str]]run_manager"Optional[CallbackManagerForLLMRun]kwargsr   r   c                 K  sB   | j }d|dg|d< || | j|}|durt||}|S )a)  Call out to Minimax's completion endpoint to chat
        Args:
            prompt: The prompt to pass into the model.
        Returns:
            The string generated by the model.
        Example:
            .. code-block:: python
                response = minimax("Tell me a joke.")
        USER)Zsender_typer*   messagesN)rD   updater4   r'   r   )r+   rJ   rK   rM   rO   r!   r*   r   r   r   _call   s   

zMinimax._call)NN)
rJ   r   rK   rL   rM   rN   rO   r   r   r   )r-   r.   r/   r0   rS   r   r   r   r   rI   {   s
    rI   ) r0   
__future__r   loggingtypingr   r   r   r   r&   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   Zlangchain_core.utilsr	   r
   r   Zpydanticr   r   r   r   r   Zlangchain_community.llms.utilsr   	getLoggerr-   loggerr   r3   rI   r   r   r   r   <module>   s    
 A