
    dh                         S SK r S SKrS SKJrJrJrJr  S SKJr  S SK	J
r
  S SKJrJrJrJrJr  S SKJr  S SKJr  \R*                  " \5      rSS	0rS
rS\S\4S jr " S S\
5      rg)    N)AnyListOptionalUnion)CallbackManagerForLLMRun)LLM)	AIMessageBaseMessageFunctionMessageHumanMessageSystemMessage)Field)enforce_stop_tokenszContent-Typezapplication/json   messagereturnc                 N   [        U [        5      (       a  SU R                  S.nU$ [        U [        5      (       a  SU R                  S.nU$ [        U [        5      (       a  SU R                  S.nU$ [        U [
        5      (       a  SU R                  S.nU$ [        SU  35      e)Nuser)rolecontent	assistantsystemfunctionzGot unknown type )
isinstancer   r   r	   r   r   
ValueError)r   message_dicts     Y/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/llms/chatglm3.py_convert_message_to_dictr      s    '<(( &7??C  
GY	'	' +H  
G]	+	+ (W__E
 	 
G_	-	- *wG  ,WI677    c                   n   \ rS rSr% Sr\" SSS9r\\S'   Sr	\\S'    S	r
\\   \S
'    Sr\\S'    Sr\\S'    Sr\\S'    \" \S9r\\   \S'    Sr\\S'    S	r\\S	4   \S'   \r\\S'   \S\4S j5       r\S\4S j5       r\S\4S j5       r S\S\4S jr!  S"S\S\\\      S\\"   S\S\4
S  jjr#S!r$g	)#ChatGLM3%   zChatGLM3 LLM service.zchatglm3-6bmodel)defaultalias
model_namez)http://127.0.0.1:8000/v1/chat/completionsendpoint_urlNmodel_kwargsi N  
max_tokensg?temperaturegffffff?top_p)default_factoryprefix_messagesF	streaminghttp_clienttimeoutr   c                     g)N
chat_glm_3 )selfs    r   	_llm_typeChatGLM3._llm_type:   s    r   c                     U R                   U R                  U R                  U R                  U R                  S.n0 UEU R
                  =(       d    0 E$ )z,Get the parameters used to invoke the model.)r#   r*   r)   r+   stream)r&   r*   r)   r+   r.   r(   )r4   paramss     r   _invocation_paramsChatGLM3._invocation_params>   sM     __++//ZZnn
 7&6T..4"66r   c                 b    SS K nU R                  =(       d    UR                  U R                  S9$ )Nr   )r0   )httpxr/   Clientr0   )r4   r=   s     r   clientChatGLM3.clientJ   s%    E5<<<#EEr   promptc           	          U R                   nU R                  [        US9/-   nUR                  SU Vs/ sH  n[	        U5      PM     sn05        U$ s  snf )N)r   messages)r:   r-   r   updater   )r4   rA   r9   rC   ms        r   _get_payloadChatGLM3._get_payloadP   s]    ((''<+G*HH(K(Q5a8(K	

  Ls   A
stoprun_managerkwargsc                    SSK nU R                  U5      n[        R                  SU 35         U R                  R                  U R                  [        US9n[        R                  SU 35        UR                  S:w  a  [        SU 35      e UR                  5       n	[        U	[        5      (       a5  S	n
X;   a   X   n[        U5      (       a  US   S
   S   nO[        SU	 35      e[        SU	 35      eUb  [#        WU5      nW$ ! UR                   a  n[        SU 35      eSnAff = f! [        R                   a   n[        SU SUR                    35      eSnAff = f)a0  Call out to a ChatGLM3 LLM inference endpoint.

Args:
    prompt: The prompt to pass into the model.
    stop: Optional list of stop words to use when generating.

Returns:
    The string generated by the model.

Example:
    .. code-block:: python

        response = chatglm_llm.invoke("Who are you?")
r   NzChatGLM3 payload: )headersjsonz$Error raised by inference endpoint: zChatGLM3 response:    zFailed with response: choicesr   r   zNo content in response : zUnexpected response type: z?Error raised during decoding response from inference endpoint: z.
Response: )r=   rF   loggerdebugr?   postr'   HEADERSNetworkErrorr   status_coderM   r   dictlenJSONDecodeErrortextr   )r4   rA   rH   rI   rJ   r=   payloadresponseeparsed_responsecontent_keysrO   rY   s                r   _callChatGLM3._callZ   s   * 	##F+)'34	I{{''!!7 ( H 	*8*563&5hZ@AA	&mmoO/400(2-;G7||&qz)4Y?$'@@Q%RSS #=o=N!OPP &tT2D? !! 	ICA3GHH	I, ## 	QRSQT U'}}o/ 	s6   )D AD. D. D+D&&D+.E"EE"r3   )NN)%__name__
__module____qualname____firstlineno____doc__r   r&   str__annotations__r'   r(   r   rV   r)   intr*   floatr+   listr-   r   r
   r.   boolr/   r   r   DEFAULT_TIMEOUTr0   propertyr5   r:   r?   rF   r   r_   __static_attributes__r3   r   r   r!   r!   %   sR   MAJACL#C#'L(4.'1J1K-E50).t)DOT+&D,It/$(KsDy!("GS"3   	7D 	7 	7 F F F
3 4  %):>	== tCy!= 67	=
 = 
= =r   r!   )rM   loggingtypingr   r   r   r   langchain_core.callbacksr   #langchain_core.language_models.llmsr   langchain_core.messagesr	   r
   r   r   r   pydanticr   langchain_community.llms.utilsr   	getLoggerra   rP   rS   rl   rV   r   r!   r3   r   r   <module>rw      sj      - - = 3   >			8	$-
.k d rs rr   