U
    h                     @  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)Field	SecretStr)convert_to_secret_strget_from_dict_or_env)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d fddZedddd Zddd!d"d#Zd+dd$d%ddd&d'd(Zeddd)d*Z  ZS ),YiLLMzYi large language models.zyi-largestrmodelg333333?floattemperaturegffffff?top_p<   inttimeout)default_factoryz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_internationalr   )kwargsc                   s&   t t|dd|d< t jf | d S )Nr   Z
YI_API_KEY)r   r   super__init__)selfr   	__class__ ?/tmp/pip-unpacked-wheel-9gdii04g/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)requestr'   c                 C  s  dd| j   d}g }| jdkr.| jg}n | jdkrB| jg}n| j| jg}|D ]}zZtj|||| jd}|jdkrt	
|j}|d d	 d
 d W   S |jdkr|  W qR tjk
r } z:||d krtd| ntd| d W Y qRW 5 d }~X Y qRX qRtdd S )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._postzOptional[List[str]]z"Optional[CallbackManagerForLLMRun])promptstoprun_managerr   r'   c                 K  s@   | j }d|dg|d< || | |}|d k	r<t||}|S )Nuser)roler2   messages)r)   updaterB   r   )r"   rC   rD   rE   r   r*   r8   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)NN)__name__
__module____qualname____doc__r   __annotations__r   r   r   r
   dictr   r   r   r   r   r!   propertyr)   rB   rJ   rK   __classcell__r%   r%   r#   r&   r      s&   
*  r   )
__future__r   r.   loggingtypingr   r   r   r   r   r4   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr	   Zlangchain_core.pydantic_v1r
   r   Zlangchain_core.utilsr   r   Zlangchain_community.llms.utilsr   	getLoggerrL   r<   r   r%   r%   r%   r&   <module>   s   
