o
    Zh                     @   s   d dl Z d dlZd dlmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZmZmZmZmZ d dlmZ d dlmZ eeZdd	iZd
ZdedefddZG dd de
ZdS )    N)AnyListOptionalUnion)CallbackManagerForLLMRun)LLM)	AIMessageBaseMessageFunctionMessageHumanMessageSystemMessage)Field)enforce_stop_tokenszContent-Typezapplication/json   messagereturnc                 C   sv   t | trd| jd}|S t | trd| jd}|S t | tr'd| jd}|S t | tr4d| jd}|S td|  )Nuser)ZrolecontentZ	assistantsystemfunctionzGot unknown type )
isinstancer   r   r   r   r
   
ValueError)r   Zmessage_dict r   X/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/chatglm3.py_convert_message_to_dict   s   
	


r   c                   @   s.  e Zd ZU dZedddZeed< dZeed< 	 dZ	e
e ed	< 	 d
Zeed< 	 dZeed< 	 dZeed< 	 eedZee ed< 	 dZeed< 	 dZeedf ed< eZeed< edefddZedefddZedefddZdedefddZ 		d%ded e
ee  d!e
e! d"edef
d#d$Z"dS )&ChatGLM3z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                 C   s   dS )NZ
chat_glm_3r   )selfr   r   r   	_llm_type:   s   zChatGLM3._llm_typec                 C   s,   | j | j| j| j| jd}i || jpi S )z,Get the parameters used to invoke the model.)r   r#   r"   r$   stream)r   r#   r"   r$   r'   r!   )r*   paramsr   r   r   _invocation_params>   s   zChatGLM3._invocation_paramsc                 C   s   dd l }| jp|j| jdS )Nr   )r)   )httpxr(   ZClientr)   )r*   r/   r   r   r   clientJ   s   zChatGLM3.clientpromptc                 C   s4   | j }| jt|dg }|ddd |D i |S )N)r   messagesc                 S   s   g | ]}t |qS r   )r   ).0mr   r   r   
<listcomp>U   s    z)ChatGLM3._get_payload.<locals>.<listcomp>)r.   r&   r   update)r*   r1   r-   r2   r   r   r   _get_payloadP   s   zChatGLM3._get_payloadstoprun_managerkwargsc              
   K   s4  ddl }| |}td|  z| jj| jt|d}W n |jy1 } zt	d| d}~ww td|  |j
dkrFt	d| z0| }	t|	trnd	}
|
|	v rg|	|
 }t|rf|d d
 d }nt	d|	 t	d|	 W n tjy } zt	d| d|j d}~ww |durt||}|S )ax  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/   r7   loggerdebugr0   postr    HEADERSZNetworkErrorr   status_coder<   r   dictlenJSONDecodeErrortextr   )r*   r1   r8   r9   r:   r/   payloadresponseeZparsed_responseZcontent_keysr>   rG   r   r   r   _callZ   sJ   




zChatGLM3._call)NN)#__name__
__module____qualname____doc__r   r   str__annotations__r    r!   r   rD   r"   intr#   floatr$   listr&   r   r	   r'   boolr(   r   r   DEFAULT_TIMEOUTr)   propertyr+   r.   r0   r7   r   rK   r   r   r   r   r   %   sN   
 
r   )r<   loggingtypingr   r   r   r   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   Zlangchain_core.messagesr   r	   r
   r   r   Zpydanticr   Zlangchain_community.llms.utilsr   	getLoggerrL   r?   rB   rV   rD   r   r   r   r   r   r   <module>   s    
