autogen.ConversableAgent
ConversableAgent
(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.
Name | Description |
---|---|
name | name of the agent. Type: str |
system_message | system message for the ChatCompletion inference. Type: str | list | None Default: ‘You are a helpful AI Assistant.‘ |
is_termination_msg | 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”. Type: Callable[[dict], bool] | None Default: None |
max_consecutive_auto_reply | 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. Type: int | None Default: None |
human_input_mode | 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. Type: Literal['ALWAYS', 'NEVER', 'TERMINATE'] Default: ‘TERMINATE’ |
function_map | Mapping function names (passed to openai) to callable functions, also used for tool calls. Type: dict[str, typing.Callable] | None Default: None |
code_execution_config | 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. Default is True, which means the code will be executed in a docker container. A default list of images will be used. 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 False, the code will be executed in the current environment. We strongly recommend using docker for code execution. - timeout (Optional, int): The maximum execution time in seconds. - last_n_messages (Experimental, int or str): The number of messages to look back for code execution. If set to ‘auto’, it will scan backwards through all messages arriving since the agent last spoke, which is typically the last time execution was attempted. (Default: auto) Type: dict | Literal[False] Default: False |
llm_config | llm inference configuration. Please refer to OpenAIWrapper.create for available options. When using OpenAI or Azure OpenAI endpoints, please specify a non-empty ‘model’ either in llm_config or in each config of ‘config_list’ in llm_config .To disable llm-based auto reply, set to False. When set to None, will use self.DEFAULT_CONFIG, which defaults to False. Type: dict | Literal[False] | None Default: None |
default_auto_reply | default auto reply when no code execution or llm-based reply is generated. Type: str | dict Default: ” |
description | 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) Type: str | None Default: None |
chat_messages | the previous chat messages that this agent had in the past with other agents. Can be used to give the agent a memory by providing the chat history. This will allow the agent to resume previous had conversations. Defaults to an empty chat history. Type: dict[autogen.Agent, list[dict]] | None Default: None |
silent | (Experimental) whether to print the message sent. If None, will use the value of silent in each function. Type: bool | None Default: None |
context_variables | Context variables that provide a persistent context for the agent. Note: Will maintain a reference to the passed in context variables (enabling a shared context) Only used in Swarms at this stage: https://docs.ag2.ai/docs/reference/agentchat/contrib/swarm_agent Type: dict[str, typing.Any] | None Default: None |
Class Attributes
DEFAULT_CONFIG
DEFAULT_SUMMARY_METHOD
DEFAULT_SUMMARY_PROMPT
MAX_CONSECUTIVE_AUTO_REPLY
llm_config
Instance Attributes
chat_messages
A dictionary of conversations from agent to list of messages.
code_executor
The code executor used by this agent. Returns None if code execution is disabled.
description
Get the description of the agent.
function_map
Return the function map.
name
Get the name of the agent.
system_message
Return the system message.
use_docker
Bool value of whether to use docker to execute the code, or str value of the docker image name to use, or None when code execution is disabled.
Instance Methods
a_check_termination_and_human_reply
(async) Check if the conversation should be terminated, and if human reply is provided.
This method checks for conditions that require the conversation to be terminated, such as reaching a maximum number of consecutive auto-replies or encountering a termination message. Additionally, it prompts for and processes human input based on the configured human input mode, which can be ‘ALWAYS’, ‘NEVER’, or ‘TERMINATE’. The method also manages the consecutive auto-reply counter for the conversation and prints relevant messages based on the human input received.
Parameters:Name | Description |
---|---|
messages | A list of message dictionaries, representing the conversation history. Type: list[dict] | None Default: None |
sender | The agent object representing the sender of the message. Type: autogen.Agent | None Default: None |
config | Configuration object, defaults to the current instance if not provided. Type: Any | None Default: None |
Type | Description |
---|---|
tuple[bool, str | None] | Tuple[bool, Union[str, Dict, None]]: A tuple containing a boolean indicating if the conversation should be terminated, and a human reply which can be a string, a dictionary, or None. |
a_execute_function
Execute an async function call and return the result.
Override this function to modify the way async functions and tools are executed.
Parameters:Name | Description |
---|---|
func_call | a dictionary extracted from openai message at key “function_call” or “tool_calls” with keys “name” and “arguments”. |
call_id | a string to identify the tool call. Type: str | None Default: None |
verbose | Type: bool Default: False |
Type | Description |
---|---|
tuple[bool, dict[str, typing.Any]] | A tuple of (is_exec_success, result_dict). is_exec_success (boolean): whether the execution is successful. result_dict: a dictionary with keys “name”, “role”, and “content”. Value of “role” is “function”. “function_call” deprecated as of OpenAI API v1.1.0 See https://platform.openai.com/docs/api-reference/chat/create#chat-create-function_call |
a_generate_function_call_reply
Generate a reply using async function call.
“function_call” replaced by “tool_calls” as of OpenAI API v1.1.0 See https://platform.openai.com/docs/api-reference/chat/create#chat-create-functions
Parameters:Name | Description |
---|---|
messages | Type: list[dict] | None Default: None |
sender | Type: autogen.Agent | None Default: None |
config | Type: Any | None Default: None |
a_generate_init_message
Generate the initial message for the agent. If message is None, input() will be called to get the initial message.
Parameters:Name | Description |
---|---|
message | Type: dict | str | None |
**kwargs |
Type | Description |
---|---|
str | dict | str or dict: the processed message. |
a_generate_oai_reply
Generate a reply using autogen.oai asynchronously.
Parameters:Name | Description |
---|---|
messages | Type: list[dict] | None Default: None |
sender | Type: autogen.Agent | None Default: None |
config | Type: Any | None Default: None |
a_generate_reply
(async) Reply based on the conversation history and the sender.
Either messages or sender must be provided.
Register a reply_func with None
as one trigger for it to be activated when messages
is non-empty and sender
is None
.
Use registered auto reply functions to generate replies.
By default, the following functions are checked in order:
- check_termination_and_human_reply
- generate_function_call_reply
- generate_tool_calls_reply
- generate_code_execution_reply
- generate_oai_reply Every function returns a tuple (final, reply). When a function returns final=False, the next function will be checked. So by default, termination and human reply will be checked first. If not terminating and human reply is skipped, execute function or code and return the result. AI replies are generated only when no code execution is performed.
Name | Description |
---|---|
messages | a list of messages in the conversation history. Type: list[dict[str, typing.Any]] | None Default: None |
sender | sender of an Agent instance. Type: ForwardRef('Agent') | None Default: None |
**kwargs | Type: Any |
Type | Description |
---|---|
str | dict[str, typing.Any] | None | str or dict or None: reply. None if no reply is generated. |
a_generate_tool_calls_reply
Generate a reply using async function call.
Parameters:Name | Description |
---|---|
messages | Type: list[dict] | None Default: None |
sender | Type: autogen.Agent | None Default: None |
config | Type: Any | None Default: None |
a_get_human_input
(Async) Get human input.
Override this method to customize the way to get human input.
Parameters:Name | Description |
---|---|
prompt | prompt for the human input. Type: str |
Type | Description |
---|---|
str | str: human input. |
a_initiate_chat
(async) Initiate a chat with the recipient agent.
Reset the consecutive auto reply counter.
If clear_history
is True, the chat history with the recipient agent will be cleared.
a_generate_init_message
is called to generate the initial message for the agent.
Name | Description |
---|---|
recipient | Type: ConversableAgent |
clear_history | Type: bool Default: True |
silent | Type: bool | None Default: False |
cache | Type: autogen.cache.AbstractCache | None Default: None |
max_turns | Type: int | None Default: None |
summary_method | Type: str | Callable | None Default: ‘last_msg’ |
summary_args | Type: dict | None Default: {} |
message | Type: str | Callable | None Default: None |
**kwargs |
Type | Description |
---|---|
autogen.ChatResult | ChatResult: an ChatResult object. |
a_initiate_chats
Name | Description |
---|---|
chat_queue | Type: list[dict[str, typing.Any]] |
a_receive
(async) Receive a message from another agent.
Once a message is received, this function sends a reply to the sender or stop. The reply can be generated automatically or entered manually by a human.
Parameters:Name | Description |
---|---|
message | message from the sender. If the type is dict, it may contain the following reserved fields (either content or function_call need to be provided). 1. “content”: content of the message, can be None. 2. “function_call”: a dictionary containing the function name and arguments. (deprecated in favor of “tool_calls”) 3. “tool_calls”: a list of dictionaries containing the function name and arguments. 4. “role”: role of the message, can be “assistant”, “user”, “function”. This field is only needed to distinguish between “function” or “assistant”/“user”. 5. “name”: In most cases, this field is not needed. When the role is “function”, this field is needed to indicate the function name. 6. “context” (dict): the context of the message, which will be passed to OpenAIWrapper.create. Type: str | dict |
sender | sender of an Agent instance. Type: autogen.Agent |
request_reply | whether a reply is requested from the sender. If None, the value is determined by self.reply_at_receive[sender] .Type: bool | None Default: None |
silent | (Experimental) whether to print the message received. Type: bool | None Default: False |
a_run
Run a chat asynchronously with the agent using the given message.
A second agent will be created to represent the user, this agent will by known by the name ‘user’.
The user can terminate the conversation when prompted or, if agent’s reply contains ‘TERMINATE’, it will terminate.
Parameters:Name | Description |
---|---|
message | the message to be processed. Type: str |
tools | the tools to be used by the agent. Type: autogen.tools.Tool | Iterable[autogen.tools.Tool] | None Default: None |
executor_kwargs | the keyword arguments for the executor. Type: dict[str, typing.Any] | None Default: None |
max_turns | maximum number of turns (a turn is equivalent to both agents having replied), defaults no None which means unlimited. The original message is included. Type: int | None Default: None |
msg_to | which agent is receiving the message and will be the first to reply, defaults to the agent. Type: Literal['agent', 'user'] Default: ‘agent’ |
clear_history | whether to clear the chat history. Type: bool Default: False |
user_input | the user will be asked for input at their turn. Type: bool Default: True |
a_send
(async) Send a message to another agent.
Parameters:Name | Description |
---|---|
message | message to be sent. The message could contain the following fields: - content (str or List): Required, the content of the message. (Can be None) - function_call (str): the name of the function to be called. - name (str): the name of the function to be called. - role (str): the role of the message, any role that is not “function” will be modified to “assistant”. - context (dict): the context of the message, which will be passed to OpenAIWrapper.create. For example, one agent can send a message A as: Type: str | dict |
recipient | the recipient of the message. Type: autogen.Agent |
request_reply | whether to request a reply from the recipient. Type: bool | None Default: None |
silent | (Experimental) whether to print the message sent. Type: bool | None Default: False |
can_execute_function
Whether the agent can execute the function.
Parameters:Name | Description |
---|---|
name | Type: list[str] | str |
chat_messages_for_summary
A list of messages as a conversation to summarize.
Parameters:Name | Description |
---|---|
agent | Type: autogen.Agent |
check_termination_and_human_reply
Check if the conversation should be terminated, and if human reply is provided.
This method checks for conditions that require the conversation to be terminated, such as reaching a maximum number of consecutive auto-replies or encountering a termination message. Additionally, it prompts for and processes human input based on the configured human input mode, which can be ‘ALWAYS’, ‘NEVER’, or ‘TERMINATE’. The method also manages the consecutive auto-reply counter for the conversation and prints relevant messages based on the human input received.
Parameters:Name | Description |
---|---|
messages | Type: list[dict] | None Default: None |
sender | Type: autogen.Agent | None Default: None |
config | Type: Any | None Default: None |
Type | Description |
---|---|
tuple[bool, str | None] | - Tuple[bool, Union[str, Dict, None]]: A tuple containing a boolean indicating if the conversation should be terminated, and a human reply which can be a string, a dictionary, or None. |
clear_history
Clear the chat history of the agent.
Parameters:Name | Description |
---|---|
recipient | the agent with whom the chat history to clear. If None, clear the chat history with all agents. Type: autogen.Agent | None Default: None |
nr_messages_to_preserve | the number of newest messages to preserve in the chat history. Type: int | None Default: None |
execute_code_blocks
Execute the code blocks and return the result.
Parameters:Name | Description |
---|---|
code_blocks |
execute_function
Execute a function call and return the result.
Override this function to modify the way to execute function and tool calls.
Parameters:Name | Description |
---|---|
func_call | a dictionary extracted from openai message at “function_call” or “tool_calls” with keys “name” and “arguments”. |
call_id | a string to identify the tool call. Type: str | None Default: None |
verbose | Type: bool Default: False |
Type | Description |
---|---|
tuple[bool, dict[str, typing.Any]] | A tuple of (is_exec_success, result_dict). is_exec_success (boolean): whether the execution is successful. result_dict: a dictionary with keys “name”, “role”, and “content”. Value of “role” is “function”. “function_call” deprecated as of OpenAI API v1.1.0 See https://platform.openai.com/docs/api-reference/chat/create#chat-create-function_call |
generate_code_execution_reply
Generate a reply using code execution.
Parameters:Name | Description |
---|---|
messages | Type: list[dict] | None Default: None |
sender | Type: autogen.Agent | None Default: None |
config | Type: dict | Literal[False] | None Default: None |
generate_function_call_reply
Generate a reply using function call.
“function_call” replaced by “tool_calls” as of OpenAI API v1.1.0 See https://platform.openai.com/docs/api-reference/chat/create#chat-create-functions
Parameters:Name | Description |
---|---|
messages | Type: list[dict] | None Default: None |
sender | Type: autogen.Agent | None Default: None |
config | Type: Any | None Default: None |
generate_init_message
Generate the initial message for the agent. If message is None, input() will be called to get the initial message.
Parameters:Name | Description |
---|---|
message | the message to be processed. Type: dict | str | None |
**kwargs | any additional information. It has the following reserved fields: “carryover”: a string or a list of string to specify the carryover information to be passed to this chat. It can be a string or a list of string. If provided, we will combine this carryover with the “message” content when generating the initial chat message. |
Type | Description |
---|---|
str | dict | str or dict: the processed message. |
generate_oai_reply
Generate a reply using autogen.oai.
Parameters:Name | Description |
---|---|
messages | Type: list[dict] | None Default: None |
sender | Type: autogen.Agent | None Default: None |
config | Type: autogen.OpenAIWrapper | None Default: None |
generate_reply
Reply based on the conversation history and the sender.
Either messages or sender must be provided.
Register a reply_func with None
as one trigger for it to be activated when messages
is non-empty and sender
is None
.
Use registered auto reply functions to generate replies.
By default, the following functions are checked in order:
- check_termination_and_human_reply
- generate_function_call_reply (deprecated in favor of tool_calls)
- generate_tool_calls_reply
- generate_code_execution_reply
- generate_oai_reply Every function returns a tuple (final, reply). When a function returns final=False, the next function will be checked. So by default, termination and human reply will be checked first. If not terminating and human reply is skipped, execute function or code and return the result. AI replies are generated only when no code execution is performed.
Name | Description |
---|---|
messages | a list of messages in the conversation history. Type: list[dict[str, typing.Any]] | None Default: None |
sender | sender of an Agent instance. Type: ForwardRef('Agent') | None Default: None |
**kwargs | Type: Any |
Type | Description |
---|---|
dict | str | None | str or dict or None: reply. None if no reply is generated. |
generate_tool_calls_reply
Generate a reply using tool call.
Parameters:Name | Description |
---|---|
messages | Type: list[dict] | None Default: None |
sender | Type: autogen.Agent | None Default: None |
config | Type: Any | None Default: None |
get_actual_usage
Get the actual usage summary.
get_chat_results
A summary from the finished chats of particular agents.
Parameters:Name | Description |
---|---|
chat_index | Type: int | None Default: None |
get_context
Get a context variable by key.
Parameters:Name | Description |
---|---|
key | The key to look up Type: str |
default | Value to return if key doesn’t exist Type: Any Default: None |
Type | Description |
---|---|
Any | The value associated with the key, or default if not found |
get_human_input
Get human input.
Override this method to customize the way to get human input.
Parameters:Name | Description |
---|---|
prompt | prompt for the human input. Type: str |
Type | Description |
---|---|
str | str: human input. |
get_total_usage
Get the total usage summary.
initiate_chat
Initiate a chat with the recipient agent.
Reset the consecutive auto reply counter.
If clear_history
is True, the chat history with the recipient agent will be cleared.
Name | Description |
---|---|
recipient | the recipient agent. Type: ConversableAgent |
clear_history | whether to clear the chat history with the agent. Default is True. Type: bool Default: True |
silent | (Experimental) whether to print the messages for this conversation. Default is False. Type: bool | None Default: False |
cache | the cache client to be used for this conversation. Default is None. Type: autogen.cache.AbstractCache | None Default: None |
max_turns | the maximum number of turns for the chat between the two agents. One turn means one conversation round trip. Note that this is different from max_consecutive_auto_reply which is the maximum number of consecutive auto replies; and it is also different from max_rounds in GroupChat which is the maximum number of rounds in a group chat session. If max_turns is set to None, the chat will continue until a termination condition is met. Default is None. Type: int | None Default: None |
summary_method | a method to get a summary from the chat. Default is DEFAULT_SUMMARY_METHOD, i.e., “last_msg”. Type: str | Callable | None Default: ‘last_msg’ |
summary_args | a dictionary of arguments to be passed to the summary_method. One example key is “summary_prompt”, and value is a string of text used to prompt a LLM-based agent (the sender or recipient agent) to reflect on the conversation and extract a summary when summary_method is “reflection_with_llm”. The default summary_prompt is DEFAULT_SUMMARY_PROMPT, i.e., “Summarize takeaway from the conversation. Do not add any introductory phrases. If the intended request is NOT properly addressed, please point it out.” Another available key is “summary_role”, which is the role of the message sent to the agent in charge of summarizing. Default is “system”. Type: dict | None Default: {} |
message | the initial message to be sent to the recipient. Needs to be provided. Otherwise, input() will be called to get the initial message. - If a string or a dict is provided, it will be used as the initial message. generate_init_message is called to generate the initial message for the agent based on this string and the context.If dict, it may contain the following reserved fields (either content or tool_calls need to be provided). 1. “content”: content of the message, can be None. 2. “function_call”: a dictionary containing the function name and arguments. (deprecated in favor of “tool_calls”) 3. “tool_calls”: a list of dictionaries containing the function name and arguments. 4. “role”: role of the message, can be “assistant”, “user”, “function”. This field is only needed to distinguish between “function” or “assistant”/“user”. 5. “name”: In most cases, this field is not needed. When the role is “function”, this field is needed to indicate the function name. 6. “context” (dict): the context of the message, which will be passed to OpenAIWrapper.create. - If a callable is provided, it will be called to get the initial message in the form of a string or a dict. If the returned type is dict, it may contain the reserved fields mentioned above. Example of a callable message (returning a string): ```python Type: dict | str | Callable | None Default: None |
**kwargs | any additional information. It has the following reserved fields: - “carryover”: a string or a list of string to specify the carryover information to be passed to this chat. If provided, we will combine this carryover (by attaching a “context: ” string and the carryover content after the message content) with the “message” content when generating the initial chat message in generate_init_message .- “verbose”: a boolean to specify whether to print the message and carryover in a chat. Default is False. |
Type | Description |
---|---|
autogen.ChatResult | ChatResult: an ChatResult object. |
initiate_chats
(Experimental) Initiate chats with multiple agents.
Parameters:Name | Description |
---|---|
chat_queue | a list of dictionaries containing the information of the chats. Each dictionary should contain the input arguments for initiate_chat Type: list[dict[str, typing.Any]] |
Type | Description |
---|---|
list[autogen.ChatResult] | a list of ChatResult objects corresponding to the finished chats in the chat_queue. |
last_message
The last message exchanged with the agent.
Parameters:Name | Description |
---|---|
agent | The agent in the conversation. If None and more than one agent’s conversations are found, an error will be raised. If None and only one conversation is found, the last message of the only conversation will be returned. Type: autogen.Agent | None Default: None |
Type | Description |
---|---|
dict | None | The last message exchanged with the agent. |
max_consecutive_auto_reply
The maximum number of consecutive auto replies.
Parameters:Name | Description |
---|---|
sender | Type: autogen.Agent | None Default: None |
pop_context
Remove and return a context variable.
Parameters:Name | Description |
---|---|
key | The key to remove Type: str |
default | Value to return if key doesn’t exist Type: Any Default: None |
Type | Description |
---|---|
Any | The value that was removed, or default if key not found |
print_usage_summary
Print the usage summary.
Parameters:Name | Description |
---|---|
mode | Type: list[str] | str Default: [‘actual’, ‘total’] |
process_all_messages_before_reply
Calls any registered capability hooks to process all messages, potentially modifying the messages.
Parameters:Name | Description |
---|---|
messages | Type: list[dict] |
process_last_received_message
Calls any registered capability hooks to use and potentially modify the text of the last message, as long as the last message is not a function call or exit command.
Parameters:Name | Description |
---|---|
messages | Type: list[dict] |
receive
Receive a message from another agent.
Once a message is received, this function sends a reply to the sender or stop. The reply can be generated automatically or entered manually by a human.
Parameters:Name | Description |
---|---|
message | message from the sender. If the type is dict, it may contain the following reserved fields (either content or function_call need to be provided). 1. “content”: content of the message, can be None. 2. “function_call”: a dictionary containing the function name and arguments. (deprecated in favor of “tool_calls”) 3. “tool_calls”: a list of dictionaries containing the function name and arguments. 4. “role”: role of the message, can be “assistant”, “user”, “function”, “tool”. This field is only needed to distinguish between “function” or “assistant”/“user”. 5. “name”: In most cases, this field is not needed. When the role is “function”, this field is needed to indicate the function name. 6. “context” (dict): the context of the message, which will be passed to OpenAIWrapper.create. Type: str | dict |
sender | sender of an Agent instance. Type: autogen.Agent |
request_reply | whether a reply is requested from the sender. If None, the value is determined by self.reply_at_receive[sender] .Type: bool | None Default: None |
silent | (Experimental) whether to print the message received. Type: bool | None Default: False |
register_for_execution
Decorator factory for registering a function to be executed by an agent.
It’s return value is used to decorate a function to be registered to the agent.
Parameters:Name | Description |
---|---|
name | name of the function. If None, the function name will be used (default: None). Type: str | None Default: None |
Type | Description |
---|---|
Callable[[~F | autogen.tools.Tool], autogen.tools.Tool] | The decorator for registering a function to be used by an agent. |
register_for_llm
Decorator factory for registering a function to be used by an agent.
It’s return value is used to decorate a function to be registered to the agent. The function uses type hints to specify the arguments and return type. The function name is used as the default name for the function, but a custom name can be provided. The function description is used to describe the function in the agent’s configuration.
Parameters:Name | Description |
---|---|
name | name of the function. If None, the function name will be used (default: None). Type: str | None Default: None |
description | description of the function (default: None). It is mandatory for the initial decorator, but the following ones can omit it. Type: str | None Default: None |
api_style | (literal): the API style for function call. For Azure OpenAI API, use version 2023-12-01-preview or later. "function" style will be deprecated.For earlier version use "function" if "tool" doesn’t work.See Azure OpenAI documentation for details. Type: Literal['function', 'tool'] Default: ‘tool’ |
Type | Description |
---|---|
Callable[[~F | autogen.tools.Tool], autogen.tools.Tool] | The decorator for registering a function to be used by an agent. |
register_function
Register functions to the agent.
Parameters:Name | Description |
---|---|
function_map | a dictionary mapping function names to functions. if function_map[name] is None, the function will be removed from the function_map. Type: dict[str, Callable | None] |
register_hook
Registers a hook to be called by a hookable method, in order to add a capability to the agent. Registered hooks are kept in lists (one per hookable method), and are called in their order of registration.
Parameters:Name | Description |
---|---|
hookable_method | A hookable method name implemented by ConversableAgent. Type: str |
hook | A method implemented by a subclass of AgentCapability. Type: Callable |
register_model_client
Register a model client.
Parameters:Name | Description |
---|---|
model_client_cls | A custom client class that follows the Client interface Type: autogen.ModelClient |
**kwargs | The kwargs for the custom client class to be initialized with |
register_nested_chats
Register a nested chat reply function.
Parameters:Name | Description |
---|---|
chat_queue | a list of chat objects to be initiated. If use_async is used, then all messages in chat_queue must have a chat-id associated with them. Type: list[dict[str, typing.Any]] |
trigger | refer to register_reply for details.Type: type[autogen.Agent] | str | autogen.Agent | Callable[[autogen.Agent], bool] | list |
reply_func_from_nested_chats | the reply function for the nested chat. The function takes a chat_queue for nested chat, recipient agent, a list of messages, a sender agent and a config as input and returns a reply message. Default to “summary_from_nested_chats”, which corresponds to a built-in reply function that get summary from the nested chat_queue. ```python def reply_func_from_nested_chats( chat_queue: List[Dict], recipient: ConversableAgent, messages: Optional[List[Dict]] = None, sender: Optional[Agent] = None, config: Optional[Any] = None, Type: str | Callable Default: ‘summary_from_nested_chats’ |
position | Ref to register_reply for details.Default to 2. It means we first check the termination and human reply, then check the registered nested chat reply. Type: int Default: 2 |
use_async | Uses a_initiate_chats internally to start nested chats. If the original chat is initiated with a_initiate_chats, you may set this to true so nested chats do not run in sync. Type: bool | None Default: None |
**kwargs |
register_reply
Register a reply function.
The reply function will be called when the trigger matches the sender. The function registered later will be checked earlier by default. To change the order, set the position to a positive integer.
Both sync and async reply functions can be registered. The sync reply function will be triggered
from both sync and async chats. However, an async reply function will only be triggered from async
chats (initiated with ConversableAgent.a_initiate_chat
). If an async
reply function is registered
and a chat is initialized with a sync function, ignore_async_in_sync_chat
determines the behaviour as follows:
if ignore_async_in_sync_chat
is set to False
(default value), an exception will be raised, and
if ignore_async_in_sync_chat
is set to True
, the reply function will be ignored.
Name | Description |
---|---|
trigger | the trigger. If a class is provided, the reply function will be called when the sender is an instance of the class. If a string is provided, the reply function will be called when the sender’s name matches the string. If an agent instance is provided, the reply function will be called when the sender is the agent instance. If a callable is provided, the reply function will be called when the callable returns True. If a list is provided, the reply function will be called when any of the triggers in the list is activated. If None is provided, the reply function will be called only when the sender is None. Note: Be sure to register None as a trigger if you would like to trigger an auto-reply function with non-empty messages and sender=None .Type: type[autogen.Agent] | str | autogen.Agent | Callable[[autogen.Agent], bool] | list |
reply_func | the reply function. The function takes a recipient agent, a list of messages, a sender agent and a config as input and returns a reply message. python def reply_func( recipient: ConversableAgent, messages: Optional[List[Dict]] = None, sender: Optional[Agent] = None, config: Optional[Any] = None, ) -> Tuple[bool, Union[str, Dict, None]]: Type: Callable |
position | the position of the reply function in the reply function list. The function registered later will be checked earlier by default. To change the order, set the position to a positive integer. Type: int Default: 0 |
config | the config to be passed to the reply function. When an agent is reset, the config will be reset to the original value. Type: Any | None Default: None |
reset_config | the function to reset the config. The function returns None. Signature: def reset_config(config: Any) Type: Callable | None Default: None |
ignore_async_in_sync_chat | whether to ignore the async reply function in sync chats. If False , an exception will be raised if an async reply function is registered and a chat is initialized with a sync function.Type: bool Default: False |
remove_other_reply_funcs | whether to remove other reply functions when registering this reply function. Type: bool Default: False |
replace_reply_func
Replace a registered reply function with a new one.
Parameters:Name | Description |
---|---|
old_reply_func | the old reply function to be replaced. Type: Callable |
new_reply_func | the new reply function to replace the old one. Type: Callable |
reset
Reset the agent.
reset_consecutive_auto_reply_counter
Reset the consecutive_auto_reply_counter of the sender.
Parameters:Name | Description |
---|---|
sender | Type: autogen.Agent | None Default: None |
run
Run a chat with the agent using the given message.
A second agent will be created to represent the user, this agent will by known by the name ‘user’.
The user can terminate the conversation when prompted or, if agent’s reply contains ‘TERMINATE’, it will terminate.
Parameters:Name | Description |
---|---|
message | the message to be processed. Type: str |
tools | the tools to be used by the agent. Type: autogen.tools.Tool | Iterable[autogen.tools.Tool] | None Default: None |
executor_kwargs | the keyword arguments for the executor. Type: dict[str, typing.Any] | None Default: None |
max_turns | maximum number of turns (a turn is equivalent to both agents having replied), defaults no None which means unlimited. The original message is included. Type: int | None Default: None |
msg_to | which agent is receiving the message and will be the first to reply, defaults to the agent. Type: Literal['agent', 'user'] Default: ‘agent’ |
clear_history | whether to clear the chat history. Type: bool Default: False |
user_input | the user will be asked for input at their turn. Type: bool Default: True |
run_code
Run the code and return the result.
Override this function to modify the way to run the code.
Parameters:Name | Description |
---|---|
code | the code to be executed. |
**kwargs | other keyword arguments. |
send
Send a message to another agent.
Parameters:Name | Description |
---|---|
message | message to be sent. The message could contain the following fields: - content (str or List): Required, the content of the message. (Can be None) - function_call (str): the name of the function to be called. - name (str): the name of the function to be called. - role (str): the role of the message, any role that is not “function” will be modified to “assistant”. - context (dict): the context of the message, which will be passed to OpenAIWrapper.create. For example, one agent can send a message A as: Type: str | dict |
recipient | the recipient of the message. Type: autogen.Agent |
request_reply | whether to request a reply from the recipient. Type: bool | None Default: None |
silent | (Experimental) whether to print the message sent. Type: bool | None Default: False |
set_context
Set a context variable.
Parameters:Name | Description |
---|---|
key | The key to set Type: str |
value | The value to associate with the key Type: Any |
stop_reply_at_receive
Reset the reply_at_receive of the sender.
Parameters:Name | Description |
---|---|
sender | Type: autogen.Agent | None Default: None |
update_agent_state_before_reply
Calls any registered capability hooks to update the agent’s state. Primarily used to update context variables. Will, potentially, modify the messages.
Parameters:Name | Description |
---|---|
messages | Type: list[dict] |
update_context
Update multiple context variables at once.
Parameters:Name | Description |
---|---|
context_variables | Dictionary of variables to update/add Type: dict[str, typing.Any] |
update_function_signature
Update a function_signature in the LLM configuration for function_call.
Parameters:Name | Description |
---|---|
func_sig | description/name of the function to update/remove to the model. See: https://platform.openai.com/docs/api-reference/chat/create#chat/create-functions Type: str | dict |
is_remove | whether removing the function from llm_config with name ‘func_sig’ Type: None |
update_max_consecutive_auto_reply
Update the maximum number of consecutive auto replies.
Parameters:Name | Description |
---|---|
value | the maximum number of consecutive auto replies. Type: int |
sender | when the sender is provided, only update the max_consecutive_auto_reply for that sender. Type: autogen.Agent | None Default: None |
update_system_message
Update the system message.
Parameters:Name | Description |
---|---|
system_message | system message for the ChatCompletion inference. Type: str |
update_tool_signature
Update a tool_signature in the LLM configuration for tool_call.
Parameters:Name | Description |
---|---|
tool_sig | description/name of the tool to update/remove to the model. See: https://platform.openai.com/docs/api-reference/chat/create#chat-create-tools Type: str | dict |
is_remove | whether removing the tool from llm_config with name ‘tool_sig’ Type: bool |