
    7|h                         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
e      Z G d de      Z G d dee	      Zy)    )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                   <    e Zd ZU dZeed<   	 eZeed<   de	de	fdZ
y)_MoonshotClientz0An API client that talks to the Moonshot server.api_keybase_urlrequestreturnc                 $   dd| j                   j                          i}t        j                  | j                   d||      }|j
                  s%t        d|j                   d|j                         |j                         d   d   d	   d
   S )NAuthorizationzBearer z/chat/completions)headersjsonzHTTP z error: choicesr   messagecontent)
r   get_secret_valuerequestspostr   ok
ValueErrorstatus_codetextr   )selfr   r   responses       `/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/moonshot.py
completionz_MoonshotClient.completion   s    "gdll.K.K.M-N$OP==}}o./

 {{uX%9%9$:(8==/RSS}}y)!,Y7	BB    N)__name__
__module____qualname____doc__r   __annotations__MOONSHOT_SERVICE_URL_BASEr   strr   r)    r*   r(   r   r      s.    :0-Hc-	C# 	C# 	Cr*   r   c                   f   e Zd ZU dZeed<   eZeed<    e	dd      Z
ee   ed<   	  e	dd	      Zeed
<   	 dZeed<   	 dZeed<   	  edd      Zedefd       Zedeeef   fd       Zedeeef   fd       Z ed      edeeef   defd              Zededefd       Zedefd       Zy)MoonshotCommonz$Common parameters for Moonshot LLMs.clientr   Nr   )defaultaliasmoonshot_api_keyzmoonshot-v1-8kmodel
model_namei   
max_tokensg333333?temperatureTr2   )populate_by_nameprotected_namespacesr   c                 
    ddiS )zA map of constructor argument names to secret ids.

        For example,
            {"moonshot_api_key": "MOONSHOT_API_KEY"}
        r8   MOONSHOT_API_KEYr2   r&   s    r(   
lc_secretszMoonshotCommon.lc_secrets7   s     #$677r*   c                 J    | j                   | j                  | j                  dS )z2Get the default parameters for calling OpenAI API.)r9   r;   r<   )r:   r;   r<   rA   s    r(   _default_paramszMoonshotCommon._default_params@   s%     __//++
 	
r*   c                 :    i d| j                   i| j                  S )Nr9   )r:   rD   rA   s    r(   _invocation_paramsz!MoonshotCommon._invocation_paramsI   s!    E7DOO,E0D0DEEr*   before)modevaluesc                     |S )zBuild extra parameters.
        Override the superclass method, prevent the model parameter from being
        overridden.
        r2   clsrI   s     r(   build_extrazMoonshotCommon.build_extraM   s	     r*   c                 x    t        t        |dd            |d<   t        |d   d|v r|d   nt              |d<   |S )z?Validate that api key and python package exists in environment.r8   r@   r   )r   r   r5   )r	   r
   r   r0   rK   s     r(   validate_environmentz#MoonshotCommon.validate_environmentV   sX     &; );=OP&
!" +-.V# J'*	
x r*   c                      y)zReturn type of llm.moonshotr2   rA   s    r(   	_llm_typezMoonshotCommon._llm_typee   s     r*   ) r+   r,   r-   r.   r   r/   r0   r   r1   r   r8   r   r   r:   r;   intr<   floatr   model_configpropertydictrB   r   rD   rF   r   classmethodrM   r   rO   rR   r2   r*   r(   r4   r4   '   s?   .K-Hc-,1$i,Phy)PV$4GDJDXJ/KMt"ML8D 8 8 
c3h 
 
 FDcN F F (#c3h C   $ $ 4   3  r*   r4   c                   V    e Zd ZdZ ed      Z	 	 ddedeee      dee	   de
d	ef
d
Zy)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)r=   Npromptstoprun_managerkwargsr   c                     | j                   }d|dg|d<   |j                  |       | j                  j                  |      }|t	        ||      }|S )Nuser)roler   messages)rF   updater5   r)   r   )r&   r[   r\   r]   r^   r   r%   s          r(   _callzMoonshot._call}   sZ     ))(.6BC
v{{%%g. 'tT2Dr*   )NN)r+   r,   r-   r.   r   rU   r1   r   r   r   r   rd   r2   r*   r(   rZ   rZ   k   sb     L %):>	 tCy! 67	
  
r*   rZ   )typingr   r   r   r   r    langchain_core.callbacksr   langchain_core.language_modelsr   langchain_core.utilsr	   r
   r   pydanticr   r   r   r   r   langchain_community.llms.utilsr   r0   r   r4   rZ   r2   r*   r(   <module>rk      sZ    , ,  = . V V  ?8 Ci C&AY AH"~s "r*   