U
    h                     @   s   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mZ d dlmZmZmZ d dlmZ dZG d	d
 d
eZG dd deZG dd dee	ZdS )    )AnyDictListOptionalN)CallbackManagerForLLMRun)LLM)	BaseModelField	SecretStrroot_validator)convert_to_secret_strget_from_dict_or_envpre_init)enforce_stop_tokenszhttps://api.moonshot.cn/v1c                   @   s6   e Zd ZU dZeed< eZeed< e	e	dddZ
dS )_MoonshotClientz0An API client that talks to the Moonshot server.api_keybase_url)requestreturnc                 C   sb   dd| j   i}tj| j d||d}|jsJtd|j d|j |	 d d d	 d
 S )NAuthorizationzBearer z/chat/completions)headersjsonzHTTP z error: choicesr   messagecontent)
r   Zget_secret_valuerequestspostr   ok
ValueErrorstatus_codetextr   )selfr   r   response r#   E/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/moonshot.py
completion   s    
z_MoonshotClient.completionN)__name__
__module____qualname____doc__r
   __annotations__MOONSHOT_SERVICE_URL_BASEr   strr   r%   r#   r#   r#   r$   r      s   
r   c                   @   s  e Zd ZU dZeed< eZeed< e	dddZ
ee ed< e	dd	dZeed
< dZeed< dZeed< G dd dZ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ddeeef eeef dddZeeedddZeeddd ZdS )!MoonshotCommonz$Common parameters for Moonshot LLMs.clientr   Nr   )defaultaliasmoonshot_api_keyzmoonshot-v1-8kmodel
model_namei   
max_tokensg333333?temperaturec                   @   s   e Zd ZdZdS )zMoonshotCommon.ConfigTNr&   r'   r(   Zallow_population_by_field_namer#   r#   r#   r$   Config/   s   r7   )r   c                 C   s   ddiS )zA map of constructor argument names to secret ids.

        For example,
            {"moonshot_api_key": "MOONSHOT_API_KEY"}
        r1   MOONSHOT_API_KEYr#   r!   r#   r#   r$   
lc_secrets2   s    zMoonshotCommon.lc_secretsc                 C   s   | j | j| jdS )z2Get the default parameters for calling OpenAI API.)r2   r4   r5   )r3   r4   r5   r9   r#   r#   r$   _default_params;   s    zMoonshotCommon._default_paramsc                 C   s   d| j i| jS )Nr2   )r3   r;   r9   r#   r#   r$   _invocation_paramsD   s    z!MoonshotCommon._invocation_paramsT)pre)valuesr   c                 C   s   |S )zBuild extra parameters.
        Override the superclass method, prevent the model parameter from being
        overridden.
        r#   clsr>   r#   r#   r$   build_extraH   s    zMoonshotCommon.build_extrac                 C   s<   t t|dd|d< t|d d|kr,|d ntd|d< |S )z?Validate that api key and python package exists in environment.r1   r8   r   )r   r   r.   )r   r   r   r+   r?   r#   r#   r$   validate_environmentP   s    


z#MoonshotCommon.validate_environmentc                 C   s   dS )zReturn type of llm.Zmoonshotr#   r9   r#   r#   r$   	_llm_type_   s    zMoonshotCommon._llm_type)r&   r'   r(   r)   r   r*   r+   r   r,   r	   r1   r   r
   r3   r4   intr5   floatr7   propertydictr:   r   r   r;   r<   r   rA   r   rB   rC   r#   r#   r#   r$   r-   !   s(   
"r-   c                   @   sB   e Zd ZdZG dd dZdeeee  ee e	edddZ
dS )	Moonshotaf  Moonshot large language models.

    To use, you should have the environment variable ``MOONSHOT_API_KEY`` set with your
    API key. Referenced from https://platform.moonshot.cn/docs

    Example:
        .. code-block:: python

            from langchain_community.llms.moonshot import Moonshot

            moonshot = Moonshot(model="moonshot-v1-8k")
    c                   @   s   e Zd ZdZdS )zMoonshot.ConfigTNr6   r#   r#   r#   r$   r7   s   s   r7   N)promptstoprun_managerkwargsr   c                 K   sB   | j }d|dg|d< || | j|}|d k	r>t||}|S )Nuser)roler   messages)r<   updater.   r%   r   )r!   rI   rJ   rK   rL   r   r    r#   r#   r$   _callv   s    

zMoonshot._call)NN)r&   r'   r(   r)   r7   r,   r   r   r   r   rQ   r#   r#   r#   r$   rH   e   s     
rH   )typingr   r   r   r   r   Zlangchain_core.callbacksr   Zlangchain_core.language_modelsr   Zlangchain_core.pydantic_v1r   r	   r
   r   Zlangchain_core.utilsr   r   r   Zlangchain_community.llms.utilsr   r+   r   r-   rH   r#   r#   r#   r$   <module>   s   D