o
    Zh                     @  s   d dl mZ d dlZd dlZd dl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mZ d dlmZmZ d dlmZ eeZG d	d
 d
eZdS )    )annotationsN)AnyDictListLiteralOptional)CallbackManagerForLLMRun)LLM)convert_to_secret_strget_from_dict_or_env)Field	SecretStr)enforce_stop_tokensc                      s   e Zd ZU dZdZded< dZded< dZded	< d
Zded< e	e
dZded< dZded< dZded< dZded< dZded< d- fddZed.dd Zd/d"d#Z		d0d1d)d*Zed2d+d,Z  ZS )3YiLLMzYi large language models.zyi-largestrmodelg333333?floattemperaturegffffff?top_p<   inttimeout)default_factoryDict[str, Any]model_kwargsNzOptional[SecretStr]
yi_api_keyautoz,Literal['auto', 'domestic', 'international']regionz/https://api.lingyiwanwu.com/v1/chat/completionsyi_api_url_domesticz%https://api.01.ai/v1/chat/completionsyi_api_url_internationalkwargsr   c                   s*   t t|dd|d< t jdi | d S )Nr   Z
YI_API_KEY )r
   r   super__init__)selfr    	__class__r!   R/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/yi.pyr#       s   
zYiLLM.__init__returnc                 C  s   | j | j| jd| jS )N)r   r   r   )r   r   r   r   r$   r!   r!   r'   _default_params&   s   zYiLLM._default_paramsrequestc                 C  s  dd| j   d}g }| jdkr| jg}n| jdkr!| jg}n| j| jg}|D ]X}z-tj|||| jd}|jdkrNt	
|j}|d d	 d
 d W   S |jdkrW|  W q) tjy } z||d krntd| td| d W Y d }~q)d }~ww td)Nzapplication/jsonzBearer )zContent-TypeAuthorizationZdomesticinternational)headersjsonr      choicesr   messagecontenti  zAn error has occurred: zFailed to connect to z, trying next URLz'Failed to connect to all available URLs)r   Zget_secret_valuer   r   r   requestspostr   status_coder/   loadstextraise_for_statusRequestException
ValueErrorloggerwarning)r$   r+   r.   urlsurlresponseZparsed_jsoner!   r!   r'   _post/   s>   





zYiLLM._postpromptstopOptional[List[str]]run_manager"Optional[CallbackManagerForLLMRun]c                 K  s@   | j }d|dg|d< || | |}|d urt||}|S )Nuser)Zroler3   messages)r*   updaterC   r   )r$   rD   rE   rG   r    r+   r9   r!   r!   r'   _callV   s   


zYiLLM._callc                 C  s   dS )zReturn type of chat_model.zyi-llmr!   r)   r!   r!   r'   	_llm_typee   s   zYiLLM._llm_type)r    r   )r(   r   )r+   r   r(   r   )NN)
rD   r   rE   rF   rG   rH   r    r   r(   r   )r(   r   )__name__
__module____qualname____doc__r   __annotations__r   r   r   r   dictr   r   r   r   r   r#   propertyr*   rC   rL   rM   __classcell__r!   r!   r%   r'   r      s(   
 
*r   )
__future__r   r/   loggingtypingr   r   r   r   r   r5   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr	   Zlangchain_core.utilsr
   r   Zpydanticr   r   Zlangchain_community.llms.utilsr   	getLoggerrN   r=   r   r!   r!   r!   r'   <module>   s    
