
    ;3h                        d dl mZ d dlZd dlmZmZmZmZmZm	Z	 d dl
mZmZmZ ddlmZ ddlmZmZmZ  G d	 d
e      Zy)    )annotationsN)AsyncGeneratorDictIterableListOptionalUnion)ChatCompletionInputMessageChatCompletionStreamOutput	MCPClient   )PROVIDER_OR_POLICY_T   )DEFAULT_SYSTEM_PROMPTEXIT_LOOP_TOOLSMAX_NUM_TURNSc                  `     e Zd ZdZdddd	 	 	 	 	 	 	 	 	 d fdZd	dZdd	 	 	 	 	 d
dZ xZS )Agenta  
    Implementation of a Simple Agent, which is a simple while loop built right on top of an [`MCPClient`].

    <Tip warning={true}>

    This class is experimental and might be subject to breaking changes in the future without prior notice.

    </Tip>

    Args:
        model (`str`):
            The model to run inference with. Can be a model id hosted on the Hugging Face Hub, e.g. `meta-llama/Meta-Llama-3-8B-Instruct`
            or a URL to a deployed Inference Endpoint or other local or remote endpoint.
        servers (`Iterable[Dict]`):
            MCP servers to connect to. Each server is a dictionary containing a `type` key and a `config` key. The `type` key can be `"stdio"` or `"sse"`, and the `config` key is a dictionary of arguments for the server.
        provider (`str`, *optional*):
            Name of the provider to use for inference. Defaults to "auto" i.e. the first of the providers available for the model, sorted by the user's order in https://hf.co/settings/inference-providers.
            If model is a URL or `base_url` is passed, then `provider` is not used.
        api_key (`str`, *optional*):
            Token to use for authentication. Will default to the locally Hugging Face saved token if not provided. You can also use your own provider API key to interact directly with the provider's service.
        prompt (`str`, *optional*):
            The system prompt to use for the agent. Defaults to the default system prompt in `constants.py`.
    N)providerapi_keypromptc               p    t         |   |||       t        |      | _        d|xs t        dg| _        y )N)modelr   r   systemrolecontent)super__init__list_servers_cfgr   messages)selfr   serversr   r   r   	__class__s         d/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/huggingface_hub/inference/_mcp/agent.pyr   zAgent.__init__%   s=     	uxI M&*I4IJH
    c                t   K   | j                   D ]#  } | j                  |d   fi |d    d {    % y 7 w)Ntypeconfig)r!   add_mcp_server)r#   cfgs     r&   
load_toolszAgent.load_tools4   sC     $$ 	DC%$%%c&kCS]CCC	DCs   ,868)abort_eventc              R  K   | j                   j                  d|d       d}d}	 |r|j                         ry| j                  | j                   t        |dkD  xr |      2 3 d{   }| 7 
6 |dz  }| j                   d   }|j                  d	      d
k(  r@|j                  d      t        D ch c]  }|j                  j                   nc c}w c}v ry|j                  d	      d
k7  r
|t        kD  ry|j                  d	      d
k7  r|ry|j                  d	      d
k7  }w)aF  
        Run the agent with the given user input.

        Args:
            user_input (`str`):
                The user input to run the agent with.
            abort_event (`asyncio.Event`, *optional*):
                An event that can be used to abort the agent. If the event is set, the agent will stop running.
        userr   r   TN)exit_loop_toolsexit_if_first_chunk_no_toolr   r   toolname)	r"   appendis_setprocess_single_turn_with_toolsr   getfunctionr5   r   )r#   
user_inputr.   	num_turnsnext_turn_should_call_toolsitemlastts           r&   runz	Agent.run8   s'     	fDE	&*#{113"AA /-6]-Z?Z B   d
 
  NI==$Dxx6)dhhv.>\kBlWX1::??BlBl.lxx6)i-.Gxx6).I*.((6*:f*D'/ s8   A D'"A3&A1'A3*D'1A33AD'4CAD')
r   strr$   zIterable[Dict]r   zOptional[PROVIDER_OR_POLICY_T]r   Optional[str]r   rC   )returnNone)r;   rB   r.   zOptional[asyncio.Event]rD   zSAsyncGenerator[Union[ChatCompletionStreamOutput, ChatCompletionInputMessage], None])__name__
__module____qualname____doc__r   r-   rA   __classcell__)r%   s   @r&   r   r      s{    : 48!% $
 
  	

 1
 
 
D 04	+E+E -	+E
 
]+Er'   r   )
__future__r   asynciotypingr   r   r   r   r   r	   huggingface_hubr
   r   r   
_providersr   	constantsr   r   r   r    r'   r&   <module>rR      s0    "  H H ] ] - L LWEI WEr'   