
    Ng>                         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dS )	    N)AnyDictListMappingOptionalSet)CallbackManagerForLLMRun)LLM)Field)enforce_stop_tokensc                   R    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                      j        di | | j        pddk    r0| 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__s     Z/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/yuan2.pyr$   zYuan2.__init__I   sw    ""6"""JO!q  djoA%:%:NN,   DJJJ ! %:%:    c                     dS )NzYuan2.0r"   r'   s    r)   	_llm_typezYuan2._llm_typeT   s    yr*   c                  
    h dS )N>   r   r   r   r   r   r"   r"   r*   r)   _model_param_nameszYuan2._model_param_namesX   s    
 
 
 	
r*   c           	      h    | 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   s;    /"1IZZ+	
 	
 		
r*   c                      d j         i                                  fd j                                        D             S )zGet the identifying parameters.modelc                 H    i | ]\  }}|                                 v ||S r"   )r/   ).0kvr'   s      r)   
<dictcomp>z-Yuan2._identifying_params.<locals>.<dictcomp>t   s<       A!t?V?V?X?X:X:X1:X:X:Xr*   )r-   r2   __dict__itemsr,   s   `r)   _identifying_paramszYuan2._identifying_paramsn   sc     T^
""$$
   !%!4!4!6!6  
 	
r*   promptrun_managerc           	         | j         r5| j                            |           d                    | j                  }n|}ddi}t	          j        d|dg| j        | j        | j        | j	        | j
        d          }t                              d|           	 t          j        | j        ||          }n.# t          j        j        $ r}	t%          d	|	           d
}	~	ww xY wt                              d|            |j        dk    rt%          d|           	 |                                }
|
d         dk    r"t%          d|
d          d|
d          d          d|
v rIt)          |
d         d                   dk    r|
d         d         d         d         }nt%          d          t%          d          n6# t          j        j        $ r}	t%          d|	 d|j                   d
}	~	ww xY w|t/          ||          }| 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/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   inputrF   rG   responseerespgenerate_texts               r)   _callzYuan2._cally   s   ,  	L'''JJt|,,EEE!#56z%*E::;&*o#y!^ 	
 	
 	&...	D|DNG$OOOHH"3 	D 	D 	DBqBBCCC	D 	4(445553&&@h@@AAA	==??DI#%% ?tI ? ?'+N';? ? ?  
 D  tIx011Q66$(OH$=a$@$GMM$%CDDD !@AAA	 " "2 	 	 	/Q / /'}/ /  	 /tDDM  	/L...///000s1   B4 4CCCBF< <G/G**G/)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   r<   r	   rc   __classcell__)r(   s   @r)   r   r      s           2Is111eD444J444D(5/. E8E?   .E8C=.It !D(4.   % "D(49
"""@#%M8C=%%%&*NHUO***2It/GT#Y%K'	 	 	 	 	 	 	 	 3    X 
C 
 
 
 \


c3h 

 

 

 

 
WS#X%6 
 
 
 X
 %):>	T TT tCy!T 67	T
 T 
T T T T T T T Tr*   r   )rR   loggingtypingr   r   r   r   r   r   rU   langchain_core.callbacksr	   #langchain_core.language_models.llmsr
   pydanticr   langchain_community.llms.utilsr   	getLoggerrd   r%   r   r"   r*   r)   <module>rw      s      : : : : : : : : : : : : : : : :  = = = = = = 3 3 3 3 3 3       > > > > > >		8	$	$~ ~ ~ ~ ~C ~ ~ ~ ~ ~r*   