class_docstr: (In preview) A class for generic conversable agents which can be configured as assistant or user proxy.
  
  After receiving each message, the agent will send a reply to the sender unless the msg is a termination msg.
  For example, AssistantAgent and UserProxyAgent are subclasses of this class,
  configured with different default settings.
  
  To modify auto reply, override `generate_reply` method.
  To disable/enable human response in every turn, set `human_input_mode` to "NEVER" or "ALWAYS".
  To modify the way to get human input, override `get_human_input` method.
  To modify the way to execute code blocks, single code block, or function call, override `execute_code_blocks`,
  `run_code`, and `execute_function` methods respectively.
  To customize the initial message when a conversation starts, override `generate_init_message` method.
class_docstr_tok: ["(","In","preview",")","A","class","for","generic","conversable","agents","which","can","be","configured","as","assistant","or","user","proxy",".","After","receiving","each","message",",","the","agent","will","send","a","reply","to","the","sender","unless","the","msg","is","a","termination","msg",".","For","example",",","AssistantAgent","and","UserProxyAgent","are","subclasses","of","this","class",",","configured","with","different","default","settings",".","To","modify","auto","reply",",","override","`","generate_reply","`","method",".","To","disable\/enable","human","response","in","every","turn",",","set","`","human_input_mode","`","to","``","NEVER","''","or","``","ALWAYS","''",".","To","modify","the","way","to","get","human","input",",","override","`","get_human_input","`","method",".","To","modify","the","way","to","execute","code","blocks",",","single","code","block",",","or","function","call",",","override","`","execute_code_blocks","`",",","`","run_code","`",",","and","`","execute_function","`","methods","respectively",".","To","customize","the","initial","message","when","a","conversation","starts",",","override","`","generate_init_message","`","method","."]
docstr: Args:
      name (str): name of the agent.
      system_message (str or list): system message for the ChatCompletion inference.
      is_termination_msg (function): a function that takes a message in the form of a dictionary
          and returns a boolean value indicating if this received message is a termination message.
          The dict can contain the following keys: "content", "role", "name", "function_call".
      max_consecutive_auto_reply (int): the maximum number of consecutive auto replies.
          default to None (no limit provided, class attribute MAX_CONSECUTIVE_AUTO_REPLY will be used as the limit in this case).
          When set to 0, no auto reply will be generated.
      human_input_mode (str): whether to ask for human inputs every time a message is received.
          Possible values are "ALWAYS", "TERMINATE", "NEVER".
          (1) When "ALWAYS", the agent prompts for human input every time a message is received.
              Under this mode, the conversation stops when the human input is "exit",
              or when is_termination_msg is True and there is no human input.
          (2) When "TERMINATE", the agent only prompts for human input only when a termination message is received or
              the number of auto reply reaches the max_consecutive_auto_reply.
          (3) When "NEVER", the agent will never prompt for human input. Under this mode, the conversation stops
              when the number of auto reply reaches the max_consecutive_auto_reply or when is_termination_msg is True.
      function_map (dict[str, callable]): Mapping function names (passed to openai) to callable functions, also used for tool calls.
      code_execution_config (dict or False): config for the code execution.
          To disable code execution, set to False. Otherwise, set to a dictionary with the following keys:
          - work_dir (Optional, str): The working directory for the code execution.
              If None, a default working directory will be used.
              The default working directory is the "extensions" directory under
              "path_to_autogen".
          - use_docker (Optional, list, str or bool): The docker image to use for code execution.
              If a list or a str of image name(s) is provided, the code will be executed in a docker container
              with the first image successfully pulled.
              If None, False or empty, the code will be executed in the current environment.
              Default is True when the docker python package is installed.
              When set to True, a default list will be used.
              We strongly recommend using docker for code execution.
          - timeout (Optional, int): The maximum execution time in seconds.
          - last_n_messages (Experimental, Optional, int or str): The number of messages to look back for code execution. Default to 1. If set to 'auto', it will scan backwards through all messages arriving since the agent last spoke (typically this is the last time execution was attempted).
      llm_config (dict or False): llm inference configuration.
          Please refer to [OpenAIWrapper.create](/docs/reference/oai/client#create)
          for available options.
          To disable llm-based auto reply, set to False.
      default_auto_reply (str or dict or None): default auto reply when no code execution or llm-based reply is generated.
      description (str): a short description of the agent. This description is used by other agents
          (e.g. the GroupChatManager) to decide when to call upon this agent. (Default: system_message)
docstr_tok: ["Args",":","name","(","str",")",":","name","of","the","agent",".","system_message","(","str","or","list",")",":","system","message","for","the","ChatCompletion","inference",".","is_termination_msg","(","function",")",":","a","function","that","takes","a","message","in","the","form","of","a","dictionary","and","returns","a","boolean","value","indicating","if","this","received","message","is","a","termination","message",".","The","dict","can","contain","the","following","keys",":","``","content","''",",","``","role","''",",","``","name","''",",","``","function_call","''",".","max_consecutive_auto_reply","(","int",")",":","the","maximum","number","of","consecutive","auto","replies",".","default","to","None","(","no","limit","provided",",","class","attribute","MAX_CONSECUTIVE_AUTO_REPLY","will","be","used","as","the","limit","in","this","case",")",".","When","set","to","0",",","no","auto","reply","will","be","generated",".","human_input_mode","(","str",")",":","whether","to","ask","for","human","inputs","every","time","a","message","is","received",".","Possible","values","are","``","ALWAYS","''",",","``","TERMINATE","''",",","``","NEVER","''",".","(","1",")","When","``","ALWAYS","''",",","the","agent","prompts","for","human","input","every","time","a","message","is","received",".","Under","this","mode",",","the","conversation","stops","when","the","human","input","is","``","exit","''",",","or","when","is_termination_msg","is","True","and","there","is","no","human","input",".","(","2",")","When","``","TERMINATE","''",",","the","agent","only","prompts","for","human","input","only","when","a","termination","message","is","received","or","the","number","of","auto","reply","reaches","the","max_consecutive_auto_reply",".","(","3",")","When","``","NEVER","''",",","the","agent","will","never","prompt","for","human","input",".","Under","this","mode",",","the","conversation","stops","when","the","number","of","auto","reply","reaches","the","max_consecutive_auto_reply","or","when","is_termination_msg","is","True",".","function_map","(","dict","[","str",",","callable","]",")",":","Mapping","function","names","(","passed","to","openai",")","to","callable","functions",",","also","used","for","tool","calls",".","code_execution_config","(","dict","or","False",")",":","config","for","the","code","execution",".","To","disable","code","execution",",","set","to","False",".","Otherwise",",","set","to","a","dictionary","with","the","following","keys",":","-","work_dir","(","Optional",",","str",")",":","The","working","directory","for","the","code","execution",".","If","None",",","a","default","working","directory","will","be","used",".","The","default","working","directory","is","the","``","extensions","''","directory","under","``","path_to_autogen","''",".","-","use_docker","(","Optional",",","list",",","str","or","bool",")",":","The","docker","image","to","use","for","code","execution",".","If","a","list","or","a","str","of","image","name","(","s",")","is","provided",",","the","code","will","be","executed","in","a","docker","container","with","the","first","image","successfully","pulled",".","If","None",",","False","or","empty",",","the","code","will","be","executed","in","the","current","environment",".","Default","is","True","when","the","docker","python","package","is","installed",".","When","set","to","True",",","a","default","list","will","be","used",".","We","strongly","recommend","using","docker","for","code","execution",".","-","timeout","(","Optional",",","int",")",":","The","maximum","execution","time","in","seconds",".","-","last_n_messages","(","Experimental",",","Optional",",","int","or","str",")",":","The","number","of","messages","to","look","back","for","code","execution",".","Default","to","1",".","If","set","to","'auto","'",",","it","will","scan","backwards","through","all","messages","arriving","since","the","agent","last","spoke","(","typically","this","is","the","last","time","execution","was","attempted",")",".","llm_config","(","dict","or","False",")",":","llm","inference","configuration",".","Please","refer","to","[","OpenAIWrapper.create","]","(","\/docs\/reference\/oai\/client","#","create",")","for","available","options",".","To","disable","llm-based","auto","reply",",","set","to","False",".","default_auto_reply","(","str","or","dict","or","None",")",":","default","auto","reply","when","no","code","execution","or","llm-based","reply","is","generated",".","description","(","str",")",":","a","short","description","of","the","agent",".","This","description","is","used","by","other","agents","(","e.g",".","the","GroupChatManager",")","to","decide","when","to","call","upon","this","agent",".","(","Default",":","system_message",")"]
code: def __init__(
      self,
      name: str,
      system_message: Optional[
          Union[str, List]
      ] = "You are a helpful AI Assistant.",
      is_termination_msg: Optional[Callable[[Dict], bool]] = None,
      max_consecutive_auto_reply: Optional[int] = None,
      human_input_mode: Optional[str] = "TERMINATE",
      function_map: Optional[Dict[str, Callable]] = None,
      code_execution_config: Optional[
          Union[Dict, Literal[False]]
      ] = None,
      llm_config: Optional[Union[Dict, Literal[False]]] = None,
      default_auto_reply: Optional[Union[str, Dict, None]] = "",
      description: Optional[str] = None,
  ):
      """
      Args:
          name (str): name of the agent.
          system_message (str or list): system message for the ChatCompletion inference.
          is_termination_msg (function): a function that takes a message in the form of a dictionary
              and returns a boolean value indicating if this received message is a termination message.
              The dict can contain the following keys: "content", "role", "name", "function_call".
          max_consecutive_auto_reply (int): the maximum number of consecutive auto replies.
              default to None (no limit provided, class attribute MAX_CONSECUTIVE_AUTO_REPLY will be used as the limit in this case).
              When set to 0, no auto reply will be generated.
          human_input_mode (str): whether to ask for human inputs every time a message is received.
              Possible values are "ALWAYS", "TERMINATE", "NEVER".
              (1) When "ALWAYS", the agent prompts for human input every time a message is received.
                  Under this mode, the conversation stops when the human input is "exit",
                  or when is_termination_msg is True and there is no human input.
              (2) When "TERMINATE", the agent only prompts for human input only when a termination message is received or
                  the number of auto reply reaches the max_consecutive_auto_reply.
              (3) When "NEVER", the agent will never prompt for human input. Under this mode, the conversation stops
                  when the number of auto reply reaches the max_consecutive_auto_reply or when is_termination_msg is True.
          function_map (dict[str, callable]): Mapping function names (passed to openai) to callable functions, also used for tool calls.
          code_execution_config (dict or False): config for the code execution.
              To disable code execution, set to False. Otherwise, set to a dictionary with the following keys:
              - work_dir (Optional, str): The working directory for the code execution.
                  If None, a default working directory will be used.
                  The default working directory is the "extensions" directory under
                  "path_to_autogen".
              - use_docker (Optional, list, str or bool): The docker image to use for code execution.
                  If a list or a str of image name(s) is provided, the code will be executed in a docker container
                  with the first image successfully pulled.
                  If None, False or empty, the code will be executed in the current environment.
                  Default is True when the docker python package is installed.
                  When set to True, a default list will be used.
                  We strongly recommend using docker for code execution.
              - timeout (Optional, int): The maximum execution time in seconds.
              - last_n_messages (Experimental, Optional, int or str): The number of messages to look back for code execution. Default to 1. If set to 'auto', it will scan backwards through all messages arriving since the agent last spoke (typically this is the last time execution was attempted).
          llm_config (dict or False): llm inference configuration.
              Please refer to [OpenAIWrapper.create](/docs/reference/oai/client#create)
              for available options.
              To disable llm-based auto reply, set to False.
          default_auto_reply (str or dict or None): default auto reply when no code execution or llm-based reply is generated.
          description (str): a short description of the agent. This description is used by other agents
              (e.g. the GroupChatManager) to decide when to call upon this agent. (Default: system_message)
      """
      super().__init__(name)
      # a dictionary of conversations, default value is list
      self._oai_messages = defaultdict(list)
      self._oai_system_message = [
          {"content": system_message, "role": "system"}
      ]
      self.description = (
          description if description is not None else system_message
      )
      self._is_termination_msg = (
          is_termination_msg
          if is_termination_msg is not None
          else (lambda x: content_str(x.get("content")) == "TERMINATE")
      )
  
      if llm_config is False:
          self.llm_config = False
          self.client = None
      else:
          self.llm_config = self.DEFAULT_CONFIG.copy()
          if isinstance(llm_config, dict):
              self.llm_config.update(llm_config)
          self.client = OpenAIWrapper(**self.llm_config)
  
      self._code_execution_config: Union[Dict, Literal[False]] = (
          {} if code_execution_config is None else code_execution_config
      )
      self.human_input_mode = human_input_mode
      self._max_consecutive_auto_reply = (
          max_consecutive_auto_reply
          if max_consecutive_auto_reply is not None
          else self.MAX_CONSECUTIVE_AUTO_REPLY
      )
      self._consecutive_auto_reply_counter = defaultdict(int)
      self._max_consecutive_auto_reply_dict = defaultdict(
          self.max_consecutive_auto_reply
      )
      self._function_map = (
          {}
          if function_map is None
          else {
              name: callable
              for name, callable in function_map.items()
              if self._assert_valid_name(name)
          }
      )
      self._default_auto_reply = default_auto_reply
      self._reply_func_list = []
      self._ignore_async_func_in_sync_chat_list = []
      self.reply_at_receive = defaultdict(bool)
      self.register_reply(
          [Agent, None], ConversableAgent.generate_oai_reply
      )
      self.register_reply(
          [Agent, None],
          ConversableAgent.a_generate_oai_reply,
          ignore_async_in_sync_chat=True,
      )
      self.register_reply(
          [Agent, None], ConversableAgent.generate_code_execution_reply
      )
      self.register_reply(
          [Agent, None], ConversableAgent.generate_tool_calls_reply
      )
      self.register_reply(
          [Agent, None],
          ConversableAgent.a_generate_tool_calls_reply,
          ignore_async_in_sync_chat=True,
      )
      self.register_reply(
          [Agent, None], ConversableAgent.generate_function_call_reply
      )
      self.register_reply(
          [Agent, None],
          ConversableAgent.a_generate_function_call_reply,
          ignore_async_in_sync_chat=True,
      )
      self.register_reply(
          [Agent, None],
          ConversableAgent.check_termination_and_human_reply,
      )
      self.register_reply(
          [Agent, None],
          ConversableAgent.a_check_termination_and_human_reply,
          ignore_async_in_sync_chat=True,
      )
  
      # Registered hooks are kept in lists, indexed by hookable method, to be called in their order of registration.
      # New hookable methods should be added to this list as required to support new agent capabilities.
      self.hook_lists = {self.process_last_message: []}
  
imports: import asyncio
  import copy
  import functools
  import inspect
  import json
  import logging
  import re
  from collections import defaultdict
  from typing import Any, Awaitable, Callable, Dict, List, Literal, Optional, Tuple, Type, TypeVar, Union
  from ..None import OpenAIWrapper
  from ..code_utils import DEFAULT_MODEL, UNKNOWN, content_str, execute_code, extract_code, infer_lang
  from ..function_utils import get_function_schema, load_basemodels_if_needed, serialize_to_str
  from .agent import Agent
  from .._pydantic import model_dump
label_desc: Use image node_id 1 to create a new ConversableAgent object from inherited base classes: Agent with example: obj = ConversableAgent(name, system_message, is_termination_msg, max_consecutive_auto_reply, human_input_mode, function_map, code_execution_config, llm_config, default_auto_reply, description)