o
    Zh>                     @   s   d dl Z d dlZd dlmZmZ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 d dlmZ eeZG dd deZdS )	    N)AnyDictListMappingOptionalSet)CallbackManagerForLLMRun)LLM)Field)enforce_stop_tokensc                       s  e Zd ZU dZdZeed< 	 edddZe	ed< 	 dZ
ee ed	< 	 d
Zee ed< 	 dZee	 ed< 	 dZeed< 	 dZee ed< 	 g Zeee  ed< 	 dZee	 ed< 	 dZee ed< 	 dZeed< 	 g Zee ed< 	 dZeed< 	 deddf fddZedefddZedee fd d!Zde eef fd"d#Z!ede"eef fd$d%Z#		d*d&edeee  d'ee$ dedef
d(d)Z%  Z&S )+Yuan2un  Yuan2.0 language models.

    Example:
        .. code-block:: python

            yuan_llm = Yuan2(
                infer_api="http://127.0.0.1:8000/yuan",
                max_tokens=1024,
                temp=1.0,
                top_p=0.9,
                top_k=40,
            )
            print(yuan_llm)
            print(yuan_llm.invoke("你是谁？"))
    zhttp://127.0.0.1:8000/yuan	infer_apii   Z	max_token)alias
max_tokensgffffff?tempg?top_pr   top_kF	do_sampleechostop@   repeat_last_ngzG?repeat_penalty	streaminghistoryuse_historykwargsreturnNc                    sJ   t  jdi | | jpddkr!| jpddkr#td d| _dS dS dS )zInitialize the Yuan2 class.r   zGtop_p and top_k cannot be set simultaneously. set top_k to 0 instead...N )super__init__r   r   loggerwarning)selfr   	__class__r   U/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/yuan2.pyr    I   s   
zYuan2.__init__c                 C   s   dS )NzYuan2.0r   r#   r   r   r&   	_llm_typeT   s   zYuan2._llm_typec                   C   s   h dS )N>   r   r   r   r   r   r   r   r   r   r&   _model_param_namesX   s   zYuan2._model_param_namesc              	   C   s&   | j | j| j| j| j| j| j| jdS )Nr   r   r   r   r   r   r   r   r*   r'   r   r   r&   _default_paramsb   s   zYuan2._default_paramsc                    s*   d j i   fdd j D S )zGet the identifying parameters.modelc                    s"   i | ]\}}|   v r||qS r   )r)   ).0kvr'   r   r&   
<dictcomp>t   s    z-Yuan2._identifying_params.<locals>.<dictcomp>)r(   r+   __dict__itemsr'   r   r'   r&   _identifying_paramsn   s   
zYuan2._identifying_paramspromptrun_managerc              
   K   s  | j r| j| d| j}n|}ddi}td|dg| j| j| j| j	| j
d}td| ztj| j||d}W n tjjyQ }	 ztd	|	 d
}	~	ww td|  |jdkrftd| z<| }
|
d dkrtd|
d  d|
d  dd|
v rt|
d d dkr|
d d d d }ntdtdW n tjjy }	 ztd|	 d|j d
}	~	ww |d
urt||}| j r| j| td| j  |S )ux  Call out to a Yuan2.0 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 = yuan_llm.invoke("你能做什么?")
        z<n>zContent-Typezapplication/jsonZ000)idZques)Z	ques_listZtokens_to_generateZtemperaturer   r   r   zYuan2.0 prompt:)headersdatazError raised by inference api: NzYuan2.0 response:    zFailed with response: ZerrCode0zFailed with error code [z], error message: [ZexceptionMsg]ZresDataoutputr   ZanszNo output found in response.zNo resData found in response.z:Error raised during decoding response from inference api: z.
Response: z	history: )r   r   appendjoinjsondumpsr   r   r   r   r   r!   debugrequestsputr   
exceptionsRequestException
ValueErrorstatus_codelenJSONDecodeErrortextr   )r#   r4   r   r5   r   inputr7   r8   responseerespZgenerate_textr   r   r&   _cally   sh   


zYuan2._call)NN)'__name__
__module____qualname____doc__r   str__annotations__r
   r   intr   r   floatr   r   r   boolr   r   r   r   r   r   r   r   r   r    propertyr(   staticmethodr   r)   r   r+   r   r3   r   rO   __classcell__r   r   r$   r&   r      sb   
 	
r   )r?   loggingtypingr   r   r   r   r   r   rB   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr	   Zpydanticr
   Zlangchain_community.llms.utilsr   	getLoggerrP   r!   r   r   r   r   r&   <module>   s     
