o
    Zh                     @   s   d Z ddlmZmZmZmZmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ ddlmZmZmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZ dZdZ G dd deZ!dS )z<OctoAI Endpoints chat wrapper. Relies heavily on ChatOpenAI.    )AnyCallableDictLiteralOptionalSequenceTypeUnion)LanguageModelInput)BaseMessage)Runnable)BaseTool)convert_to_secret_strget_from_dict_or_envpre_initconvert_to_openai_tool)Field	SecretStr)
ChatOpenAI)is_openai_v1zhttps://text.octoai.run/v1/zllama-2-13b-chatc                       s
  e Zd ZU dZeedZeed< ee	dddZ
e	ed< eeddZeed	< ed
efddZed
eeef fddZed
efddZeded
efddZddddeeeeef eeef  deeeeed ef  dee ded
ee e!f f
 fddZ"  Z#S )
ChatOctoAIaz  OctoAI Chat large language models.

    See https://octo.ai/ for information about OctoAI.

    To use, you should have the ``openai`` python package installed and the
    environment variable ``OCTOAI_API_TOKEN`` set with your API token.
    Alternatively, you can use the octoai_api_token keyword argument.

    Any parameters that are valid to be passed to the `openai.create` call can be passed
    in, even if not explicitly saved on this class.

    Example:
        .. code-block:: python

            from langchain_community.chat_models import ChatOctoAI
            chat = ChatOctoAI(model_name="mixtral-8x7b-instruct")
    defaultoctoai_api_base api_key)r   aliasoctoai_api_tokenmodel
model_namereturnc                 C      dS )zReturn type of chat model.zoctoai-chat selfr#   r#   ]/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/chat_models/octoai.py	_llm_type4   s   zChatOctoAI._llm_typec                 C   s   ddiS )Nr   OCTOAI_API_TOKENr#   r$   r#   r#   r&   
lc_secrets9   s   zChatOctoAI.lc_secretsc                 C   r"   )NFr#   )clsr#   r#   r&   is_lc_serializable=   s   zChatOctoAI.is_lc_serializablevaluesc                 C   s   t |ddtd|d< tt |dd|d< t |ddtd|d< zPdd	l}t rY|d  |d d
}|dsB|jdi |j	j
|d< |dsV|jdi |j	j
|d< W |S W |S |d |d< |d  |d< |j|d< W |S  tyy   tdw )z?Validate that api key and python package exists in environment.r   ZOCTOAI_API_BASEr   r   r(   r    Z
MODEL_NAMEr   N)r   base_urlclientZasync_clientZopenai_api_baseZopenai_api_keyzTCould not import openai python package. Please install it with `pip install openai`.r#   )r   DEFAULT_API_BASEr   DEFAULT_MODELopenair   Zget_secret_valuegetZOpenAIZchatZcompletionsZAsyncOpenAIZChatCompletionImportError)r*   r,   r1   Zclient_paramsr#   r#   r&   validate_environmentA   sR   





zChatOctoAI.validate_environmentN)tool_choicestricttoolsr5   )autononerequiredanyr6   kwargsc                   s    fdd|D }rXt tr"dvrddiddkr!dn2t tr*dn*t trMd	d |D }tfd
d|D sLtd d| dntd |d< t jdd|i|S )z;Imitating bind_tool method from langchain_openai.ChatOpenAIc                    s   g | ]}t | d qS )r6   r   ).0Ztoolr=   r#   r&   
<listcomp>z   s    z)ChatOctoAI.bind_tools.<locals>.<listcomp>)r8   r9   r;   r:   functionname)typer@   r;   r:   c                 S   s   g | ]}|d  d qS )r@   rA   r#   )r>   Zformatted_toolr#   r#   r&   r?      s    
c                 3   s     | ]}| d  d kV  qdS )r@   rA   Nr#   )r>   Z	tool_name)r5   r#   r&   	<genexpr>   s
    
z(ChatOctoAI.bind_tools.<locals>.<genexpr>zTool choice z1 was specified, but the only provided tools were .zEUnrecognized tool_choice type. Expected str, bool or dict. Received: r5   r7   Nr#   )
isinstancestrbooldictr;   
ValueErrorsuperbind)r%   r7   r5   r6   r<   Zformatted_toolsZ
tool_names	__class__)r6   r5   r&   
bind_toolsn   sD   



	zChatOctoAI.bind_tools)$__name__
__module____qualname____doc__r   r/   r   rF   __annotations__r   r   r0   r    propertyr'   r   r)   classmethodrG   r+   r   r4   r   r	   r   r   r   r   r   rH   r   r   r
   r   rN   __classcell__r#   r#   rL   r&   r      s8   
 2
	r   N)"rR   typingr   r   r   r   r   r   r   r	   Zlangchain_core.language_modelsr
   Zlangchain_core.messagesr   Zlangchain_core.runnablesr   Zlangchain_core.toolsr   Zlangchain_core.utilsr   r   r   Z%langchain_core.utils.function_callingr   Zpydanticr   r   Z&langchain_community.chat_models.openair   Z langchain_community.utils.openair   r/   r0   r   r#   r#   r#   r&   <module>   s    (