
    ih                        d Z ddlmZ ddlZddlZddlZddlmZ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mZ ddlmZmZ dd	lmZ dd
lmZmZ erddlZ ej@                  e!      Z"dZ#dZ$ G d de      Z%y)z>EverlyAI Endpoints chat wrapper. Relies heavily on ChatOpenAI.    )annotationsN)	TYPE_CHECKINGAnyCallableDictOptionalSequenceSetTypeUnion)BaseMessage)BaseTool)convert_to_secret_strget_from_dict_or_env)Fieldmodel_validator)convert_message_to_dict)
ChatOpenAI_import_tiktokenzhttps://everlyai.xyz/hostedmeta-llama/Llama-2-7b-chat-hfc                       e Zd ZU dZedd       Zedd       Zedd       ZdZ	de
d<   	  eed	
      Zde
d<   	 eZde
d<   	 dZde
d<   	 edd       Z ed      edd              ZddZ	 d	 	 	 	 	 d fdZ xZS )ChatEverlyAIaQ  `EverlyAI` Chat large language models.

    To use, you should have the ``openai`` python package installed, and the
    environment variable ``EVERLYAI_API_KEY`` set with your API key.
    Alternatively, you can use the everlyai_api_key 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 ChatEverlyAI
            chat = ChatEverlyAI(model_name="meta-llama/Llama-2-7b-chat-hf")
    strc                     y)zReturn type of chat model.zeverlyai-chat selfs    f/var/www/html/dev/engine/venv/lib/python3.12/site-packages/langchain_community/chat_models/everlyai.py	_llm_typezChatEverlyAI._llm_type:   s         c                
    ddiS )Neverlyai_api_keyEVERLYAI_API_KEYr   r   s    r   
lc_secretszChatEverlyAI.lc_secrets?   s    "$677r    c                     y)NFr   )clss    r   is_lc_serializablezChatEverlyAI.is_lc_serializableC   s    r    NzOptional[str]r"   model)defaultalias
model_nameeverlyai_api_basezOptional[Set[str]]available_modelsc                     t        ddg      S )z'Get available models from EverlyAI API.r   z(meta-llama/Llama-2-13b-chat-hf-quantized)setr   r    r   get_available_modelsz!ChatEverlyAI.get_available_modelsP   s     /:
 	
r    before)modec                ~   t        t        |dd            |d<   t        |d<   	 ddl}	 |j
                  |d<   d
|j                         vr	t        |d
<   |d
   }| j                         }||vrt        d| d| d      ||d<   |S # t        $ r}t	        d      |d}~ww xY w# t        $ r}t        d	      |d}~ww xY w)z?Validate that api key and python package exists in environment.r"   r#   openai_api_keyopenai_api_baser   NzTCould not import openai python package. Please install it with `pip install openai`.clientz`openai` has no `ChatCompletion` attribute, this is likely due to an old version of the openai package. Try upgrading it with `pip install --upgrade openai`.r+   zModel name z  not found in available models: .r-   )r   r   DEFAULT_API_BASEopenaiImportErrorChatCompletionAttributeError
ValueErrorkeysDEFAULT_MODELr0   )r&   valuesr9   eexcr+   r-   s          r   validate_environment_overridez*ChatEverlyAI.validate_environment_override[   s    $9 ""$
  %5 !		%44F8 v{{},#0F< L)
335--j\)I#$A' 
 &6!";  	? 	  	7 		s.   B B" 	BBB"	B<+B77B<c                    t               }| j                  | j                  }n| j                  }	 |j                  d      }||fS # t        $ r. t
        j                  d       d}|j                  |      }Y ||fS w xY w)Nzgpt-3.5-turbo-0301z5Warning: model not found. Using cl100k_base encoding.cl100k_base)r   tiktoken_model_namer+   encoding_for_modelKeyErrorloggerwarningget_encoding)r   	tiktoken_r(   encodings       r   _get_encoding_modelz ChatEverlyAI._get_encoding_model   s    $&	##/,,EOOE	5 334HIH
 h	  	5NNRS!E --e4Hh		5s   A 1A=<A=c           
        |t        j                  d       t        j                  d   dk  rt        |   |      S | j                         \  }}d}d}d}|D cg c]  }t        |       }	}|	D ]P  }
||z  }|
j                         D ]6  \  }}|t        |j                  t        |                  z  }|dk(  s2||z  }8 R |dz  }|S c c}w )zCalculate num tokens with tiktoken package.

        Official documentation: https://github.com/openai/openai-cookbook/blob/
        main/examples/How_to_format_inputs_to_ChatGPT_models.ipynbzECounting tokens in tool schemas is not yet supported. Ignoring tools.         r   name)warningswarnsysversion_infosuperget_num_tokens_from_messagesrN   r   itemslenencoder   )r   messagestoolsr(   rM   tokens_per_messagetokens_per_name
num_tokensmmessages_dictmessagekeyvalue	__class__s                r   rY   z)ChatEverlyAI.get_num_tokens_from_messages   s     MMW A!#77AA224x
=EF03FF$ 	2G,,J%mmo 2
U c(//#e*"=>>
&=/1J2	2 	a
 Gs   C)returnr   )rh   zDict[str, str])rh   bool)rh   zSet[str])r@   dictrh   r   )rh   ztuple[str, tiktoken.Encoding])N)r]   zlist[BaseMessage]r^   zCOptional[Sequence[Union[Dict[str, Any], Type, Callable, BaseTool]]]rh   int)__name__
__module____qualname____doc__propertyr   r$   classmethodr'   r"   __annotations__r   r?   r+   r8   r,   r-   staticmethodr0   r   rC   rN   rY   __classcell__)rg   s   @r   r   r   )   s        8 8   '+m*&MAJA-s-)+/(/-
 
 (#+  $+Z(  # 
  
   r    r   )&ro   
__future__r   loggingrV   rT   typingr   r   r   r   r   r	   r
   r   r   langchain_core.messagesr   langchain_core.toolsr   langchain_core.utilsr   r   pydanticr   r   #langchain_community.adapters.openair   &langchain_community.chat_models.openair   r   tiktoken	getLoggerrl   rI   r8   r?   r   r   r    r   <module>r      sn    D "  
 
 
 
 0 ) L + G
 			8	$ 1 /P: Pr    