o
    RTh	                     @   s.   d dl Z dddddZd
ddZddd	ZdS )    Ni   i    )gpt-3.5-turbo-0301gpt-3.5-turbogpt-4
gpt-4-0314r   c                 C   s  zt |}W n ty   t d}Y nw |dkrEd}| D ] }|d7 }| D ]\}}|t||7 }|dkr=|d7 }q(q|d7 }|S |dksQ|d	ksQ|d
kr|d}| D ] }|d7 }| D ]\}}|t||7 }|dkrt|d7 }q_qU|d7 }|S td| d)z8Returns the number of tokens used by a list of messages.cl100k_baser   r      name   r   r   r   zBnum_tokens_from_messages() is not presently implemented for model z|.
  See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens.)tiktokenencoding_for_modelKeyErrorget_encodingitemslenencodeNotImplementedError)messagesmodelencoding
num_tokensmessagekeyvalue r   O/home/air/segue/gemini/backup/venv/lib/python3.10/site-packages/genai/tokens.pynum_tokens_from_messages   s@   
r   c                 C   sF   t | |d}|du rt| }||kr!| d t | |d}||ks| S )zGReduce the number of messages until they are below the max token limit.)r   Nr   )r   
MAX_TOKENSpop)r   r   
max_tokensr   r   r   r    trim_messages_to_fit_token_limit/   s   
r    )r   )r   N)r   r   r   r    r   r   r   r   <module>   s    

"