
    7|h>                         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j$                  e      Z G d de      Zy)	    N)AnyDictListMappingOptionalSet)CallbackManagerForLLMRun)LLM)Field)enforce_stop_tokensc                       e Zd ZU dZdZeed<   	  e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Zedefd       Zedee   fd       Zde eef   fdZ!ede"eef   fd        Z#	 	 d$d!edeee      d"ee$   dedef
d#Z% x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   	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                     t        |   di | | j                  xs ddkD  r1| j                  xs ddkD  rt        j                  d       d| _        yyy)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__s     ]/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/yuan2.pyr$   zYuan2.__init__I   sT    "6"JJO!q djjoA%:NN, DJ &;     c                      y)NzYuan2.0r"   r'   s    r)   	_llm_typezYuan2._llm_typeT   s    r*   c                  
    h dS )N>   r   r   r   r   r   r"   r"   r*   r)   _model_param_nameszYuan2._model_param_namesX   s    
 	
r*   c           	          | j                   | j                  | j                  | j                  | j                  | j
                  | j                  | j                  dS )Nr   r   r   r   r   r   r   r   r1   r,   s    r)   _default_paramszYuan2._default_paramsb   sH    //"11IIZZZZ++	
 		
r*   c                     d| j                   i| j                         | j                  j                         D ci c]  \  }}|| j	                         v s|| c}}S c c}}w )zGet the identifying parameters.model)r-   r2   __dict__itemsr/   )r'   kvs      r)   _identifying_paramszYuan2._identifying_paramsn   sl     T^^
""$
 "&!4!4!6A!t?V?V?X:X1
 	
s   A A promptrun_managerc           	         | j                   r7| j                  j                  |       dj                  | j                        }n|}ddi}t	        j
                  d|dg| j                  | j                  | j                  | j                  | j                  d      }t        j                  d|       	 t        j                  | j                  ||      }t        j                  d|        |j&                  dk7  rt%        d|       	 |j	                         }
|
d   dk7  rt%        d|
d    d|
d    d      d|
v r.t)        |
d   d         dk\  r|
d   d   d   d   }nt%        d      t%        d      	 |t/        ||      }| j                   r| j                  j                  |       t        j                  d| j                          |S # t        j                   j"                  $ r}	t%        d	|	       d
}	~	ww xY w# t        j                   j*                  $ r }	t%        d|	 d|j,                         d
}	~	ww xY w)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/json000)idques)	ques_listtokens_to_generatetemperaturer   r   r   zYuan2.0 prompt:)headersdatazError raised by inference api: NzYuan2.0 response:    zFailed with response: errCode0zFailed with error code [z], error message: [exceptionMsg]resDataoutputr   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'   r:   r   r;   r   inputrC   rD   responseerespgenerate_texts               r)   _callzYuan2._cally   sO   , LL'JJt||,EE!#56zz%*E:;&*oo#yy!^^	
 	&.	D||DNNG$OH 	)(453&5hZ@AA	==?DI#% .tI.? @''+N';&<A? 
 D tIx01Q6$(OH$=a$@$GM$%CDD !@AA	 " /tDM LL.y/0M ""33 	D>qcBCC	D0 ""22 	LQC P'}}o/ 	s1   3"G A-H H/G==HI"H==I)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   r2   r   r9   r	   r`   __classcell__)r(   s   @r)   r   r      s     2Is1D4J4D(5/. E8E? .E8C=.It !D(4. % "D(49
"@#%M8C=%&*NHUO*2It/GT#Y%K'	 	 	 3   
C 
 


c3h 

 
WS#X%6 
 
 %):>	TT tCy!T 67	T
 T 
Tr*   r   )rO   loggingtypingr   r   r   r   r   r   rR   langchain_core.callbacksr	   #langchain_core.language_models.llmsr
   pydanticr   langchain_community.llms.utilsr   	getLoggerra   r%   r   r"   r*   r)   <module>rt      s@      : :  = 3  >			8	$~C ~r*   