CoolFace
Apppublic

aphilippov/python-server-api

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
user_proxy_agent.py97 linesDownload Raw Back to agentchat
1from typing import Callable, Dict, List, Literal, Optional, Union2 3from .conversable_agent import ConversableAgent4 5 6class UserProxyAgent(ConversableAgent):7    """(In preview) A proxy agent for the user, that can execute code and provide feedback to the other agents.8 9    UserProxyAgent is a subclass of ConversableAgent configured with `human_input_mode` to ALWAYS10    and `llm_config` to False. By default, the agent will prompt for human input every time a message is received.11    Code execution is enabled by default. LLM-based auto reply is disabled by default.12    To modify auto reply, register a method with [`register_reply`](conversable_agent#register_reply).13    To modify the way to get human input, override `get_human_input` method.14    To modify the way to execute code blocks, single code block, or function call, override `execute_code_blocks`,15    `run_code`, and `execute_function` methods respectively.16    To customize the initial message when a conversation starts, override `generate_init_message` method.17    """18 19    # Default UserProxyAgent.description values, based on human_input_mode20    DEFAULT_USER_PROXY_AGENT_DESCRIPTIONS = {21        "ALWAYS": "An attentive HUMAN user who can answer questions about the task, and can perform tasks such as running Python code or inputting command line commands at a Linux terminal and reporting back the execution results.",22        "TERMINATE": "A user that can run Python code or input command line commands at a Linux terminal and report back the execution results.",23        "NEVER": "A user that can run Python code or input command line commands at a Linux terminal and report back the execution results.",24    }25 26    def __init__(27        self,28        name: str,29        is_termination_msg: Optional[Callable[[Dict], bool]] = None,30        max_consecutive_auto_reply: Optional[int] = None,31        human_input_mode: Optional[str] = "ALWAYS",32        function_map: Optional[Dict[str, Callable]] = None,33        code_execution_config: Optional[Union[Dict, Literal[False]]] = None,34        default_auto_reply: Optional[Union[str, Dict, None]] = "",35        llm_config: Optional[Union[Dict, Literal[False]]] = False,36        system_message: Optional[Union[str, List]] = "",37        description: Optional[str] = None,38    ):39        """40        Args:41            name (str): name of the agent.42            is_termination_msg (function): a function that takes a message in the form of a dictionary43                and returns a boolean value indicating if this received message is a termination message.44                The dict can contain the following keys: "content", "role", "name", "function_call".45            max_consecutive_auto_reply (int): the maximum number of consecutive auto replies.46                default to None (no limit provided, class attribute MAX_CONSECUTIVE_AUTO_REPLY will be used as the limit in this case).47                The limit only plays a role when human_input_mode is not "ALWAYS".48            human_input_mode (str): whether to ask for human inputs every time a message is received.49                Possible values are "ALWAYS", "TERMINATE", "NEVER".50                (1) When "ALWAYS", the agent prompts for human input every time a message is received.51                    Under this mode, the conversation stops when the human input is "exit",52                    or when is_termination_msg is True and there is no human input.53                (2) When "TERMINATE", the agent only prompts for human input only when a termination message is received or54                    the number of auto reply reaches the max_consecutive_auto_reply.55                (3) When "NEVER", the agent will never prompt for human input. Under this mode, the conversation stops56                    when the number of auto reply reaches the max_consecutive_auto_reply or when is_termination_msg is True.57            function_map (dict[str, callable]): Mapping function names (passed to openai) to callable functions.58            code_execution_config (dict or False): config for the code execution.59                To disable code execution, set to False. Otherwise, set to a dictionary with the following keys:60                - work_dir (Optional, str): The working directory for the code execution.61                    If None, a default working directory will be used.62                    The default working directory is the "extensions" directory under63                    "path_to_autogen".64                - use_docker (Optional, list, str or bool): The docker image to use for code execution.65                    If a list or a str of image name(s) is provided, the code will be executed in a docker container66                    with the first image successfully pulled.67                    If None, False or empty, the code will be executed in the current environment.68                    Default is True, which will be converted into a list.69                    If the code is executed in the current environment,70                    the code must be trusted.71                - timeout (Optional, int): The maximum execution time in seconds.72                - last_n_messages (Experimental, Optional, int): The number of messages to look back for code execution. Default to 1.73            default_auto_reply (str or dict or None): the default auto reply message when no code execution or llm based reply is generated.74            llm_config (dict or False): llm inference configuration.75                Please refer to [OpenAIWrapper.create](/docs/reference/oai/client#create)76                for available options.77                Default to false, which disables llm-based auto reply.78            system_message (str or List): system message for ChatCompletion inference.79                Only used when llm_config is not False. Use it to reprogram the agent.80            description (str): a short description of the agent. This description is used by other agents81                (e.g. the GroupChatManager) to decide when to call upon this agent. (Default: system_message)82        """83        super().__init__(84            name=name,85            system_message=system_message,86            is_termination_msg=is_termination_msg,87            max_consecutive_auto_reply=max_consecutive_auto_reply,88            human_input_mode=human_input_mode,89            function_map=function_map,90            code_execution_config=code_execution_config,91            llm_config=llm_config,92            default_auto_reply=default_auto_reply,93            description=description94            if description is not None95            else self.DEFAULT_USER_PROXY_AGENT_DESCRIPTIONS[human_input_mode],96        )97