o
    h%                     @   s   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 ddlmZ ddlmZ dd	lmZ eeZG d
d deeZi fdeeeef  deeef fddZejejiZG dd dZG dd deZG dd deZdddZdddZdS )    )deepcopy)Enum)DictListOptional)InferenceClient)_deprecate_method   )AutoTokenizer)Pipeline)loggingc                   @   s,   e Zd ZdZdZdZdZdZedd Z	dS )	MessageRoleuser	assistantsystemz	tool-callztool-responsec                 C   s   dd | D S )Nc                 S   s   g | ]}|j qS  )value).0rr   r   r/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/agents/llm_engine.py
<listcomp>)   s    z%MessageRole.roles.<locals>.<listcomp>r   )clsr   r   r   roles'   s   zMessageRole.rolesN)
__name__
__module____qualname__USER	ASSISTANTSYSTEM	TOOL_CALLTOOL_RESPONSEclassmethodr   r   r   r   r   r       s    r   message_listrole_conversionsc                 C   s   g }t | } | D ]U}t| ddhkstd|d }|t vr/td| dt  d||v r9|| |d< t|dkrX|d |d d krX|d d  d	|d  7  < q|| q|S )
z
    Subsequent messages with the same role will be concatenated to a single message.

    Args:
        message_list (`List[Dict[str, str]]`): List of chat messages.
    rolecontentz6Message should contain only 'role' and 'content' keys!zIncorrect role z, only z are supported for now.r   z	
=======
)r   setkeys
ValueErrorr   r   lenappend)r"   r#   final_message_listmessager$   r   r   r   get_clean_message_list,   s    r.   c                	   @   s   e Zd Zedddddee fddZdd	 Z	dd
ee	eef  deee  dee fddZ
	dd
ee	eef  deee  dee defddZdS )HfEnginez4.51.0zzSwitch to smolagents instead, with the same functionalities and similar API (https://huggingface.co/docs/smolagents/index))versionr-   Nmodel_idc              
   C   s   d | _ d | _|d u rd}td| d z	t|| _W d S  tyC } ztd| d| d td| _W Y d }~d S d }~ww )Nz#HuggingFaceTB/SmolLM2-1.7B-Instructz)Using default model for token counting: ''z#Failed to load tokenizer for model z: z$. Loading default tokenizer instead.)last_input_token_countlast_output_token_countloggerwarningr
   from_pretrained	tokenizer	Exception)selfr1   er   r   r   __init__M   s   zHfEngine.__init__c                 C   s   | j | jdS )N)input_token_countoutput_token_count)r3   r4   )r:   r   r   r   get_token_counts]   s   zHfEngine.get_token_countsmessagesstop_sequencesgrammarc                 C   s   t N)NotImplementedError)r:   r@   rA   rB   r   r   r   generatec   s   zHfEngine.generatereturnc                 C   s   t |ts	td|du rg }| |||}t| jj|dd| _t| j|| _	|D ]}|t| d |krB|dt|  }q,|S )a9  Process the input messages and return the model's response.

        This method sends a list of messages to the Hugging Face Inference API, optionally with stop sequences and grammar customization.

        Parameters:
            messages (`List[Dict[str, str]]`):
                A list of message dictionaries to be processed. Each dictionary should have the structure `{"role": "user/system", "content": "message content"}`.
            stop_sequences (`List[str]`, *optional*):
                A list of strings that will stop the generation if encountered in the model's output.
            grammar (`str`, *optional*):
                The grammar or formatting structure to use in the model's response.

        Returns:
            `str`: The text content of the model's response.

        Example:
            ```python
            >>> engine = HfApiEngine(
            ...     model="meta-llama/Meta-Llama-3.1-8B-Instruct",
            ...     token="your_hf_token_here",
            ...     max_tokens=2000
            ... )
            >>> messages = [{"role": "user", "content": "Explain quantum mechanics in simple terms."}]
            >>> response = engine(messages, stop_sequences=["END"])
            >>> print(response)
            "Quantum mechanics is the branch of physics that studies..."
            ```
        zIMessages should be a list of dictionaries with 'role' and 'content' keys.NT)tokenize)

isinstancer   r)   rE   r*   r8   apply_chat_templater3   encoder4   )r:   r@   rA   rB   responsestop_seqr   r   r   __call__h   s   
zHfEngine.__call__rC   NN)r   r   r   r   r   strr<   r?   r   r   rE   rM   r   r   r   r   r/   L   s2    


r/   c                
       s~   e Zd ZdZ				ddedee dee d	ee f fd
dZ	ddee	eef  deee  dee defddZ
  ZS )HfApiEnginea7  A class to interact with Hugging Face's Inference API for language model interaction.

    This engine allows you to communicate with Hugging Face's models using the Inference API. It can be used in both serverless mode or with a dedicated endpoint, supporting features like stop sequences and grammar customization.

    Parameters:
        model (`str`, *optional*, defaults to `"meta-llama/Meta-Llama-3.1-8B-Instruct"`):
            The Hugging Face model ID to be used for inference. This can be a path or model identifier from the Hugging Face model hub.
        token (`str`, *optional*):
            Token used by the Hugging Face API for authentication.
            If not provided, the class will use the token stored in the Hugging Face CLI configuration.
        max_tokens (`int`, *optional*, defaults to 1500):
            The maximum number of tokens allowed in the output.
        timeout (`int`, *optional*, defaults to 120):
            Timeout for the API request, in seconds.

    Raises:
        ValueError:
            If the model name is not provided.
    %meta-llama/Meta-Llama-3.1-8B-InstructN  x   modeltoken
max_tokenstimeoutc                    s0   t  j|d || _t| j||d| _|| _d S )N)r1   )rU   rW   )superr<   rT   r   clientrV   )r:   rT   rU   rV   rW   	__class__r   r   r<      s   
zHfApiEngine.__init__r@   rA   rB   rF   c                 C   sR   t |td}|d ur| jj||| j|d}n
| jj||| jd}|jd jj}|S )Nr#   )stoprV   response_format)r]   rV   r   )r.   llama_role_conversionsrY   chat_completionrV   choicesr-   r%   )r:   r@   rA   rB   rK   r   r   r   rE      s   
zHfApiEngine.generate)rQ   NrR   rS   rN   )r   r   r   __doc__rO   r   intr<   r   r   rE   __classcell__r   r   rZ   r   rP      s2    
rP   c                       sn   e Zd ZdZddedee f fddZ			ddee	eef  d	eee  d
ee de
def
ddZ  ZS )TransformersEnginezBThis engine uses a pre-initialized local text-generation pipeline.Npipeliner1   c                    s   t  | || _d S rC   )rX   r<   rf   )r:   rf   r1   rZ   r   r   r<      s   
zTransformersEngine.__init__rR   r@   rA   rB   
max_lengthrF   c                 C   sX   t |td}|d urt|dkr|}nd }| j|||| jjd}|d d d d }|S )Nr\   r   )stop_stringsrg   r8   generated_textr&   r%   )r.   r_   r*   rf   r8   )r:   r@   rA   rB   rg   rh   outputrK   r   r   r   rE      s   zTransformersEngine.generaterC   )NNrR   )r   r   r   rb   r   r   rO   r<   r   r   rc   rE   rd   r   r   rZ   r   re      s"    
re   regexzhThought: .+?\nAction:\n\{\n\s{4}"action":\s"[^"\n]+",\n\s{4}"action_input":\s"[^"\n]+"\n\}\n<end_action>)typer   zCThought: .+?\nCode:\n```(?:py|python)?\n(?:.|\s)+?\n```<end_action>N) copyr   enumr   typingr   r   r   huggingface_hubr   "huggingface_hub.utils._deprecationr    r
   pipelines.baser   utilsr   
get_loggerr   r5   rO   r   r.   r    r   r_   r/   rP   re   DEFAULT_JSONAGENT_REGEX_GRAMMARDEFAULT_CODEAGENT_REGEX_GRAMMARr   r   r   r   <module>   s,   
*J3#
