a
    bg                     @   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 d dlmZmZ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)convert_to_secret_strget_from_dict_or_envpre_init)	BaseModel
ConfigDictField	SecretStrmodel_validator)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$   o/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/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< e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ef e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?temperatureTr$   )populate_by_nameZprotected_namespaces)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"}
        r2   MOONSHOT_API_KEYr$   r"   r$   r$   r%   
lc_secrets7   s    zMoonshotCommon.lc_secretsc                 C   s   | j | j| jdS )z2Get the default parameters for calling OpenAI API.)r3   r5   r6   )r4   r5   r6   r9   r$   r$   r%   _default_params@   s    zMoonshotCommon._default_paramsc                 C   s   i d| j i| jS )Nr3   )r4   r;   r9   r$   r$   r%   _invocation_paramsI   s    z!MoonshotCommon._invocation_paramsbefore)mode)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_extraM   s    zMoonshotCommon.build_extrac                 C   s<   t t|dd|d< t|d d|v r,|d ntd|d< |S )z?Validate that api key and python package exists in environment.r2   r8   r   )r   r   r/   )r   r	   r   r,   r@   r$   r$   r%   validate_environmentV   s    


z#MoonshotCommon.validate_environmentc                 C   s   dS )zReturn type of llm.Zmoonshotr$   r9   r$   r$   r%   	_llm_typee   s    zMoonshotCommon._llm_type) r'   r(   r)   r*   r   r+   r,   r   r-   r   r2   r   r   r4   r5   intr6   floatr   model_configpropertydictr:   r   r;   r<   r   classmethodrB   r
   rC   rD   r$   r$   r$   r%   r.   '   s*   
r.   c                   @   s>   e Zd ZdZe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")
    T)r7   N)promptstoprun_managerkwargsr   c                 K   sB   | j }d|dg|d< || | j|}|d ur>t||}|S )Nuser)Zroler   messages)r<   updater/   r&   r   )r"   rL   rM   rN   rO   r   r!   r$   r$   r%   _call}   s    

zMoonshot._call)NN)r'   r(   r)   r*   r   rG   r-   r   r   r   r   rS   r$   r$   r$   r%   rK   k   s     
rK   )typingr   r   r   r   r   Zlangchain_core.callbacksr   Zlangchain_core.language_modelsr   Zlangchain_core.utilsr   r	   r
   Zpydanticr   r   r   r   r   Zlangchain_community.llms.utilsr   r,   r   r.   rK   r$   r$   r$   r%   <module>   s   D