
    Ng                         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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                   >    e Zd ZU dZeed<   	 eZeed<   de	de	fdZ
dS )_MoonshotClientz0An API client that talks to the Moonshot server.api_keybase_urlrequestreturnc                     dd| j                                          i}t          j        | j         d||          }|j        st          d|j         d|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/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/moonshot.py
completionz_MoonshotClient.completion   s    "$Odl.K.K.M.M$O$OP=}///
 
 

 { 	TRX%9RR8=RRSSS}}y)!,Y7	BB    N)__name__
__module____qualname____doc__r   __annotations__MOONSHOT_SERVICE_URL_BASEr   strr   r)    r*   r(   r   r      se         ::0-Hc---	C# 	C# 	C 	C 	C 	C 	C 	Cr*   r   c                      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dS )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        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    E7DO,E0D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                     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   sp     &; );=OPP&
 &
!" +-.V## J''*	
 
 
x r*   c                     dS )zReturn type of llm.moonshotr2   rA   s    r(   	_llm_typezMoonshotCommon._llm_typee   s	     zr*   ) 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        ..KKK-Hc---,1E$i,P,P,Phy)PPPVe$4GDDDJDDDXJ/KM:t"MMML8D 8 8 8 X8 
c3h 
 
 
 X
 FDcN F F F XF _(###c3h C    [ $# $ 4    X 3    X  r*   r4   c                   p    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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)r=   Npromptstoprun_managerkwargsr   c                     | j         }d|dg|d<   |                    |           | j                            |          }|t	          ||          }|S )Nuser)roler   messages)rF   updater5   r)   r   )r&   r[   r\   r]   r^   r   r%   s          r(   _callzMoonshot._call}   sd     )(.6BBC
v{%%g.. 'tT22Dr*   )NN)r+   r,   r-   r.   r   rU   r1   r   r   r   r   rd   r2   r*   r(   rZ   rZ   k   s          :  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      so   , , , , , , , , , , , ,  = = = = = = . . . . . . V V V V V V V V V V              ? > > > > >8 C C C C Ci C C C&A A A A AY A A AH" " " " "~s " " " " "r*   