a
    bg                     @   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ed
ddZeeeef d
ddZeed
ddZeeedddZdddeeeeef eeef  eeeeed ef  ee eee e!f d 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   s   dS )zReturn type of chat model.zoctoai-chat selfr"   r"   t/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/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   s   dS )NFr"   )clsr"   r"   r%   is_lc_serializable=   s    zChatOctoAI.is_lc_serializable)valuesr!   c                 C   s   t |ddtd|d< tt |dd|d< t |ddtd|d< zdd	l}t r|d  |d d
}|ds|jf i |j	j
|d< |ds|jf i |j	j
|d< n&|d |d< |d  |d< |j|d< W n ty   tdY n0 |S )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   DEFAULT_API_BASEr   DEFAULT_MODELopenair   Zget_secret_valuegetZOpenAIZchatZcompletionsZAsyncOpenAIZChatCompletionImportError)r)   r+   r0   Zclient_paramsr"   r"   r%   validate_environmentA   sF    






zChatOctoAI.validate_environmentN)tool_choicestrict)autononerequiredany)toolsr4   r5   kwargsr!   c                   s    fdd|D }rt trDdvr6ddiddkrdndt trTdnTt trd	d |D }tfd
d|D std d| dntd |d< t jf d|i|S )z;Imitating bind_tool method from langchain_openai.ChatOpenAIc                    s   g | ]}t | d qS )r5   r   ).0Ztoolr<   r"   r%   
<listcomp>z   s   z)ChatOctoAI.bind_tools.<locals>.<listcomp>)r6   r7   r9   r8   functionname)typer?   r9   r8   c                 S   s   g | ]}|d  d qS )r?   r@   r"   )r=   Zformatted_toolr"   r"   r%   r>      s   c                 3   s   | ]}| d  d kV  qdS )r?   r@   Nr"   )r=   Z	tool_name)r4   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: r4   r:   )
isinstancestrbooldictr9   
ValueErrorsuperbind)r$   r:   r4   r5   r;   Zformatted_toolsZ
tool_names	__class__)r5   r4   r%   
bind_toolsn   s@    



zChatOctoAI.bind_tools)$__name__
__module____qualname____doc__r   r.   r   rE   __annotations__r   r   r/   r    propertyr&   r   r(   classmethodrF   r*   r   r3   r   r	   r   r   r   r   r   rG   r   r   r
   r   rM   __classcell__r"   r"   rK   r%   r      s.   
2
r   N)"rQ   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.   r/   r   r"   r"   r"   r%   <module>   s   (