TimeReplyAgent
autogen.agents.contrib.TimeReplyAgent #
TimeReplyAgent(date_time_format='%Y-%m-%d %H:%M:%S', output_prefix='Tick, tock, the current date/time is ', **kwargs)
Bases: ConversableAgent
A simple agent that returns the current time.
Use it is as a reference for creating new agents with a reply-based approach (as opposed to tool-based).
This agent will return the date and time whenever it needs to reply.
Initialize the TimeReplyAgent.
| PARAMETER | DESCRIPTION |
|---|---|
date_time_format | The format in which the date and time should be returned. TYPE: |
output_prefix | The prefix to add to the output message. TYPE: |
**kwargs | Additional parameters to pass to the base TYPE: |
Source code in autogen/agents/contrib/time/time_reply_agent.py
DEFAULT_SUMMARY_PROMPT class-attribute instance-attribute #
DEFAULT_SUMMARY_PROMPT = 'Summarize the takeaway from the conversation. Do not add any introductory phrases.'
context_variables instance-attribute #
context_variables = context_variables if context_variables is not None else ContextVariables()
hook_lists instance-attribute #
hook_lists = {'process_last_received_message': [], 'process_all_messages_before_reply': [], 'process_message_before_send': [], 'update_agent_state': [], 'safeguard_tool_inputs': [], 'safeguard_tool_outputs': [], 'safeguard_llm_inputs': [], 'safeguard_llm_outputs': [], 'safeguard_human_inputs': []}
code_executor property #
The code executor used by this agent. Returns None if code execution is disabled.
chat_messages property #
A dictionary of conversations from agent to list of messages.
use_docker property #
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.
tools property #
Get the agent's tools (registered for LLM)
Note this is a copy of the tools list, use add_tool and remove_tool to modify the tools list.
DEFAULT_SYSTEM_MESSAGE class-attribute instance-attribute #
send #
Send a message to another agent.
| PARAMETER | 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: |
{
"content": lambda context: context["use_tool_msg"],
"context": {"use_tool_msg": "Use tool X if they are relevant."},
}
| RAISES | DESCRIPTION |
|---|---|
ValueError | if the message can't be converted into a valid ChatCompletion message. |
Source code in autogen/agentchat/conversable_agent.py
a_send async #
(async) Send a message to another agent.
| PARAMETER | 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: |
{
"content": lambda context: context["use_tool_msg"],
"context": {"use_tool_msg": "Use tool X if they are relevant."},
}
| RAISES | DESCRIPTION |
|---|---|
ValueError | if the message can't be converted into a valid ChatCompletion message. |
Source code in autogen/agentchat/conversable_agent.py
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.
| PARAMETER | 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. |
sender | sender of an Agent instance. TYPE: |
request_reply | whether a reply is requested from the sender. If None, the value is determined by TYPE: |
silent | (Experimental) whether to print the message received. TYPE: |
| RAISES | DESCRIPTION |
|---|---|
ValueError | if the message can't be converted into a valid ChatCompletion message. |
Source code in autogen/agentchat/conversable_agent.py
a_receive async #
(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.
| PARAMETER | 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. |
sender | sender of an Agent instance. TYPE: |
request_reply | whether a reply is requested from the sender. If None, the value is determined by TYPE: |
silent | (Experimental) whether to print the message received. TYPE: |
| RAISES | DESCRIPTION |
|---|---|
ValueError | if the message can't be converted into a valid ChatCompletion message. |
Source code in autogen/agentchat/conversable_agent.py
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: 1. check_termination_and_human_reply 2. generate_function_call_reply (deprecated in favor of tool_calls) 3. generate_tool_calls_reply 4. generate_code_execution_reply 5. 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.
| PARAMETER | DESCRIPTION |
|---|---|
messages | a list of messages in the conversation history. |
sender | sender of an Agent instance. |
exclude | A list of reply functions to exclude from the reply generation process. Functions in this list will be skipped even if they would normally be triggered. |
| RETURNS | DESCRIPTION |
|---|---|
str | dict[str, Any] | None | str or dict or None: reply. None if no reply is generated. |
Source code in autogen/agentchat/conversable_agent.py
3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 | |
a_generate_reply async #
(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: 1. check_termination_and_human_reply 2. generate_function_call_reply 3. generate_tool_calls_reply 4. generate_code_execution_reply 5. 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.
| PARAMETER | DESCRIPTION |
|---|---|
messages | a list of messages in the conversation history. |
sender | sender of an Agent instance. |
exclude | A list of reply functions to exclude from the reply generation process. Functions in this list will be skipped even if they would normally be triggered. |
| RETURNS | DESCRIPTION |
|---|---|
str | dict[str, Any] | None | str or dict or None: reply. None if no reply is generated. |
Source code in autogen/agentchat/conversable_agent.py
3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 | |
set_ui_tools #
Set the UI tools for the agent.
| PARAMETER | DESCRIPTION |
|---|---|
tools | a list of tools to be set. |
Source code in autogen/agentchat/conversable_agent.py
unset_ui_tools #
Unset the UI tools for the agent.
| PARAMETER | DESCRIPTION |
|---|---|
tools | a list of tools to be unset. |
update_system_message #
Update the system message.
| PARAMETER | DESCRIPTION |
|---|---|
system_message | system message for the ChatCompletion inference. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
register_reply #
register_reply(trigger, reply_func, position=0, config=None, reset_config=None, *, ignore_async_in_sync_chat=False, remove_other_reply_funcs=False)
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.
| PARAMETER | 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 TYPE: |
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. TYPE: |
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: |
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: |
reset_config | the function to reset the config. The function returns None. Signature: TYPE: |
ignore_async_in_sync_chat | whether to ignore the async reply function in sync chats. If TYPE: |
remove_other_reply_funcs | whether to remove other reply functions when registering this reply function. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
replace_reply_func #
Replace a registered reply function with a new one.
| PARAMETER | DESCRIPTION |
|---|---|
old_reply_func | the old reply function to be replaced. TYPE: |
new_reply_func | the new reply function to replace the old one. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
register_nested_chats #
register_nested_chats(chat_queue, trigger, reply_func_from_nested_chats='summary_from_nested_chats', position=2, use_async=None, **kwargs)
Register a nested chat reply function.
| PARAMETER | 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: |
trigger | refer to TYPE: |
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. |
position | Ref to TYPE: |
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: |
kwargs | Ref to TYPE: |
Source code in autogen/agentchat/conversable_agent.py
update_max_consecutive_auto_reply #
Update the maximum number of consecutive auto replies.
| PARAMETER | DESCRIPTION |
|---|---|
value | the maximum number of consecutive auto replies. TYPE: |
sender | when the sender is provided, only update the max_consecutive_auto_reply for that sender. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
max_consecutive_auto_reply #
The maximum number of consecutive auto replies.
Source code in autogen/agentchat/conversable_agent.py
chat_messages_for_summary #
last_message #
The last message exchanged with the agent.
| PARAMETER | 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: |
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | None | The last message exchanged with the agent. |
Source code in autogen/agentchat/conversable_agent.py
initiate_chat #
initiate_chat(recipient, clear_history=True, silent=False, cache=None, max_turns=None, summary_method=DEFAULT_SUMMARY_METHOD, summary_args={}, message=None, **kwargs)
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.
| PARAMETER | DESCRIPTION |
|---|---|
recipient | the recipient agent. TYPE: |
clear_history | whether to clear the chat history with the agent. Default is True. TYPE: |
silent | (Experimental) whether to print the messages for this conversation. Default is False. TYPE: |
cache | the cache client to be used for this conversation. Default is None. TYPE: |
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 TYPE: |
summary_method | a method to get a summary from the chat. Default is DEFAULT_SUMMARY_METHOD, i.e., "last_msg". Supported strings are "last_msg" and "reflection_with_llm": - when set to "last_msg", it returns the last message of the dialog as the summary. - when set to "reflection_with_llm", it returns a summary extracted using an llm client. A callable summary_method should take the recipient and sender agent in a chat as input and return a string of summary. E.g., TYPE: |
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: |
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.
|
**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 TYPE: |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError | if any async reply functions are registered and not ignored in sync chat. |
| RETURNS | DESCRIPTION |
|---|---|
ChatResult | an ChatResult object. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 | |
run #
run(recipient=None, clear_history=True, silent=False, cache=None, max_turns=None, summary_method=DEFAULT_SUMMARY_METHOD, summary_args={}, message=None, executor_kwargs=None, tools=None, user_input=False, msg_to='agent', **kwargs)
Run a chat with an optional recipient agent, returning a response that can be processed or iterated over.
This method starts a chat in a background thread and returns immediately with a RunResponse object that provides access to events as they occur.
For step-by-step execution with control over each event, use run_iter() instead.
| PARAMETER | DESCRIPTION |
|---|---|
recipient | The recipient agent to chat with. If None, creates a temporary executor agent for single-agent mode. TYPE: |
clear_history | Whether to clear the chat history with the agent. Default is True. TYPE: |
silent | Whether to suppress console output. Default is False. TYPE: |
cache | Cache client for this conversation. Default is None. TYPE: |
max_turns | Maximum number of conversation turns. One turn is one round trip. If None, chat continues until termination condition is met. TYPE: |
summary_method | Method to summarize chat. Default is "last_msg". Options: "last_msg", "reflection_with_llm", or a callable. TYPE: |
summary_args | Arguments passed to summary_method. |
message | Initial message to send. Can be a string, dict, or callable. TYPE: |
executor_kwargs | Kwargs for executor agent (single-agent mode only). |
tools | Tools to register with the executor (single-agent mode only). |
user_input | Whether to enable user input mode. Default is False. TYPE: |
msg_to | Direction of initial message - "agent" or "user". Default is "agent". TYPE: |
**kwargs | Additional arguments passed to initiate_chat. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
RunResponseProtocol | RunResponseProtocol |
Source code in autogen/agentchat/conversable_agent.py
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 | |
run_iter #
run_iter(recipient=None, clear_history=True, silent=False, cache=None, max_turns=None, summary_method=DEFAULT_SUMMARY_METHOD, summary_args={}, message=None, executor_kwargs=None, tools=None, user_input=False, msg_to='agent', yield_on=None, **kwargs)
Run a chat with iterator-based stepped execution.
Iterate over events as they occur. The background thread blocks after each event until you advance to the next iteration.
Example
for event in agent.run_iter(message="Hello"): if isinstance(event, TextEvent): print(event.content.content) if should_abort(event): break # Cleanup happens automatically
| PARAMETER | DESCRIPTION |
|---|---|
recipient | The recipient agent to chat with. If None, creates a temporary executor agent for single-agent mode. TYPE: |
clear_history | Whether to clear the chat history with the agent. Default is True. TYPE: |
silent | Whether to suppress console output. Default is False. TYPE: |
cache | Cache client for this conversation. Default is None. TYPE: |
max_turns | Maximum number of conversation turns. One turn is one round trip. If None, chat continues until termination condition is met. TYPE: |
summary_method | Method to summarize chat. Default is "last_msg". Options: "last_msg", "reflection_with_llm", or a callable. TYPE: |
summary_args | Arguments passed to summary_method. |
message | Initial message to send. Can be a string, dict, or callable. TYPE: |
executor_kwargs | Kwargs for executor agent (single-agent mode only). |
tools | Tools to register with the executor (single-agent mode only). |
user_input | Whether to enable user input mode. Default is False. TYPE: |
msg_to | Direction of initial message - "agent" or "user". Default is "agent". TYPE: |
yield_on | List of event types to yield. If None, yields all events. Example: [TextEvent, TerminationEvent] to only yield text messages. |
**kwargs | Additional arguments passed to initiate_chat. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
RunIterResponse | An iterator that yields events as they occur. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 | |
a_initiate_chat async #
a_initiate_chat(recipient, clear_history=True, silent=False, cache=None, max_turns=None, summary_method=DEFAULT_SUMMARY_METHOD, summary_args={}, message=None, **kwargs)
(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.
Args: Please refer to initiate_chat.
| RETURNS | DESCRIPTION |
|---|---|
ChatResult | an ChatResult object. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 | |
a_run async #
a_run(recipient=None, clear_history=True, silent=False, cache=None, max_turns=None, summary_method=DEFAULT_SUMMARY_METHOD, summary_args={}, message=None, executor_kwargs=None, tools=None, user_input=False, msg_to='agent', **kwargs)
Async version of run().
For step-by-step execution with control over each event, use a_run_iter() instead.
Source code in autogen/agentchat/conversable_agent.py
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 | |
a_run_iter #
a_run_iter(recipient=None, clear_history=True, silent=False, cache=None, max_turns=None, summary_method=DEFAULT_SUMMARY_METHOD, summary_args={}, message=None, executor_kwargs=None, tools=None, user_input=False, msg_to='agent', yield_on=None, **kwargs)
Async version of run_iter() for async contexts.
Iterate over events as they occur using async for. The background thread blocks after each event until you advance to the next iteration.
Example
async for event in agent.a_run_iter(message="Hello"): if isinstance(event, TextEvent): print(event.content.content) if should_abort(event): break # Cleanup happens automatically
| PARAMETER | DESCRIPTION |
|---|---|
recipient | The recipient agent to chat with. If None, creates a temporary executor agent for single-agent mode. TYPE: |
clear_history | Whether to clear the chat history with the agent. Default is True. TYPE: |
silent | Whether to suppress console output. Default is False. TYPE: |
cache | Cache client for this conversation. Default is None. TYPE: |
max_turns | Maximum number of conversation turns. One turn is one round trip. If None, chat continues until termination condition is met. TYPE: |
summary_method | Method to summarize chat. Default is "last_msg". Options: "last_msg", "reflection_with_llm", or a callable. TYPE: |
summary_args | Arguments passed to summary_method. |
message | Initial message to send. Can be a string, dict, or callable. TYPE: |
executor_kwargs | Kwargs for executor agent (single-agent mode only). |
tools | Tools to register with the executor (single-agent mode only). |
user_input | Whether to enable user input mode. Default is False. TYPE: |
msg_to | Direction of initial message - "agent" or "user". Default is "agent". TYPE: |
yield_on | List of event types to yield. If None, yields all events. Example: [TextEvent, TerminationEvent] to only yield text messages. |
**kwargs | Additional arguments passed to initiate_chat. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
AsyncRunIterResponse | An async iterator that yields events as they occur. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 | |
initiate_chats #
(Experimental) Initiate chats with multiple agents.
| PARAMETER | DESCRIPTION |
|---|---|
chat_queue | a list of dictionaries containing the information of the chats. Each dictionary should contain the input arguments for TYPE: |
Returns: a list of ChatResult objects corresponding to the finished chats in the chat_queue.
Source code in autogen/agentchat/conversable_agent.py
sequential_run #
(Experimental) Initiate chats with multiple agents sequentially.
| PARAMETER | DESCRIPTION |
|---|---|
chat_queue | a list of dictionaries containing the information of the chats. Each dictionary should contain the input arguments for TYPE: |
Returns: a list of ChatResult objects corresponding to the finished chats in the chat_queue.
Source code in autogen/agentchat/conversable_agent.py
2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 | |
a_initiate_chats async #
Source code in autogen/agentchat/conversable_agent.py
a_sequential_run async #
(Experimental) Initiate chats with multiple agents sequentially.
| PARAMETER | DESCRIPTION |
|---|---|
chat_queue | a list of dictionaries containing the information of the chats. Each dictionary should contain the input arguments for TYPE: |
Returns: a list of ChatResult objects corresponding to the finished chats in the chat_queue.
Source code in autogen/agentchat/conversable_agent.py
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 | |
get_chat_results #
A summary from the finished chats of particular agents.
Source code in autogen/agentchat/conversable_agent.py
reset #
Reset the agent.
Source code in autogen/agentchat/conversable_agent.py
stop_reply_at_receive #
Reset the reply_at_receive of the sender.
reset_consecutive_auto_reply_counter #
Reset the consecutive_auto_reply_counter of the sender.
Source code in autogen/agentchat/conversable_agent.py
clear_history #
Clear the chat history of the agent.
| PARAMETER | DESCRIPTION |
|---|---|
recipient | the agent with whom the chat history to clear. If None, clear the chat history with all agents. TYPE: |
nr_messages_to_preserve | the number of newest messages to preserve in the chat history. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
generate_oai_reply #
Generate a reply using autogen.oai.
Source code in autogen/agentchat/conversable_agent.py
a_generate_oai_reply async #
Generate a reply using autogen.oai asynchronously.
Source code in autogen/agentchat/conversable_agent.py
generate_code_execution_reply #
Generate a reply using code execution.
Source code in autogen/agentchat/conversable_agent.py
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
Source code in autogen/agentchat/conversable_agent.py
a_generate_function_call_reply async #
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
Source code in autogen/agentchat/conversable_agent.py
generate_tool_calls_reply #
Generate a reply using tool call.
Source code in autogen/agentchat/conversable_agent.py
2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 | |
a_generate_tool_calls_reply async #
Generate a reply using async function call.
Source code in autogen/agentchat/conversable_agent.py
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.
| PARAMETER | DESCRIPTION |
|---|---|
messages | A list of message dictionaries, representing the conversation history. TYPE: |
sender | The agent object representing the sender of the message. |
config | Configuration object, defaults to the current instance if not provided. |
iostream | The IOStream object to use for sending messages. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
bool | A tuple containing a boolean indicating if the conversation |
str | None | should be terminated, and a human reply which can be a string, a dictionary, or None. |
Source code in autogen/agentchat/conversable_agent.py
2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 | |
a_check_termination_and_human_reply async #
(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.
| PARAMETER | DESCRIPTION |
|---|---|
messages | A list of message dictionaries, representing the conversation history. TYPE: |
sender | The agent object representing the sender of the message. |
config | Configuration object, defaults to the current instance if not provided. |
iostream | The AsyncIOStreamProtocol object to use for sending messages. TYPE: |
Returns: 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.
Source code in autogen/agentchat/conversable_agent.py
3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 | |
get_human_input #
Get human input.
Override this method to customize the way to get human input.
| PARAMETER | DESCRIPTION |
|---|---|
prompt | prompt for the human input. TYPE: |
iostream | The InputStream object to use for sending messages. TYPE: |
Returns: str: human input.
Source code in autogen/agentchat/conversable_agent.py
a_get_human_input async #
(Async) Get human input.
Override this method to customize the way to get human input.
| PARAMETER | DESCRIPTION |
|---|---|
prompt | prompt for the human input. TYPE: |
iostream | The AsyncInputStream object to use for sending messages. TYPE: |
Returns: str: human input.
Source code in autogen/agentchat/conversable_agent.py
run_code #
Run the code and return the result.
Override this function to modify the way to run the code.
| PARAMETER | DESCRIPTION |
|---|---|
code | the code to be executed. TYPE: |
**kwargs | other keyword arguments. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
int | A tuple of (exitcode, logs, image). |
exitcode | the exit code of the code execution. TYPE: |
logs | the logs of the code execution. TYPE: |
image | the docker image used for the code execution. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
execute_code_blocks #
Execute the code blocks and return the result.
Source code in autogen/agentchat/conversable_agent.py
execute_function #
Execute a function call and return the result.
Override this function to modify the way to execute function and tool calls.
| PARAMETER | 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: |
verbose | Whether to send messages about the execution details to the output stream. When True, both the function call arguments and the execution result will be displayed. Defaults to False. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
bool | A tuple of (is_exec_success, result_dict). |
is_exec_success | whether the execution is successful. TYPE: |
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
Source code in autogen/agentchat/conversable_agent.py
3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 | |
a_execute_function async #
Execute an async function call and return the result.
Override this function to modify the way async functions and tools are executed.
| PARAMETER | 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: |
verbose | Whether to send messages about the execution details to the output stream. When True, both the function call arguments and the execution result will be displayed. Defaults to False. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
bool | A tuple of (is_exec_success, result_dict). |
is_exec_success | whether the execution is successful. TYPE: |
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
Source code in autogen/agentchat/conversable_agent.py
3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 | |
generate_init_message #
Generate the initial message for the agent. If message is None, input() will be called to get the initial message.
| PARAMETER | DESCRIPTION |
|---|---|
message | the message to be processed. TYPE: |
**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: |
| RETURNS | DESCRIPTION |
|---|---|
str | dict[str, Any] | str or dict: the processed message. |
Source code in autogen/agentchat/conversable_agent.py
a_generate_init_message async #
Generate the initial message for the agent. If message is None, input() will be called to get the initial message.
| PARAMETER | DESCRIPTION |
|---|---|
message | the message to be processed. TYPE: |
**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: |
| RETURNS | DESCRIPTION |
|---|---|
str | dict[str, Any] | str or dict: the processed message. |
Source code in autogen/agentchat/conversable_agent.py
remove_tool_for_llm #
Remove a tool (register for LLM tool)
Source code in autogen/agentchat/conversable_agent.py
register_function #
Register functions to the agent.
| PARAMETER | 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. |
silent_override | whether to print warnings when overriding functions. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
update_function_signature #
Update a function_signature in the LLM configuration for function_call.
| PARAMETER | 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 |
is_remove | whether removing the function from llm_config with name 'func_sig' TYPE: |
silent_override | whether to print warnings when overriding functions. TYPE: |
Deprecated as of OpenAI API v1.1.0 See https://platform.openai.com/docs/api-reference/chat/create#chat-create-function_call
Source code in autogen/agentchat/conversable_agent.py
update_tool_signature #
Update a tool_signature in the LLM configuration for tool_call.
| PARAMETER | 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 |
is_remove | whether removing the tool from llm_config with name 'tool_sig' TYPE: |
silent_override | whether to print warnings when overriding functions. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
can_execute_function #
Whether the agent can execute the function.
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.
| PARAMETER | DESCRIPTION |
|---|---|
name | name of the function. If None, the function name will be used (default: None). TYPE: |
description | description of the function (default: None). It is mandatory for the initial decorator, but the following ones can omit it. TYPE: |
api_style | (literal): the API style for function call. For Azure OpenAI API, use version 2023-12-01-preview or later. TYPE: |
silent_override | whether to suppress any override warning messages. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Callable[[F | Tool], Tool] | The decorator for registering a function to be used by an agent. |
Examples:
@user_proxy.register_for_execution()
@agent2.register_for_llm()
@agent1.register_for_llm(description="This is a very useful function")
def my_function(a: Annotated[str, "description of a parameter"] = "a", b: int, c=3.14) -> str:
return a + str(b * c)
For Azure OpenAI versions prior to 2023-12-01-preview, set api_style to "function" if "tool" doesn't work:
@agent2.register_for_llm(api_style="function")
def my_function(a: Annotated[str, "description of a parameter"] = "a", b: int, c=3.14) -> str:
return a + str(b * c)
Source code in autogen/agentchat/conversable_agent.py
3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 | |
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.
| PARAMETER | DESCRIPTION |
|---|---|
name | name of the function. If None, the function name will be used (default: None). TYPE: |
description | description of the function (default: None). TYPE: |
serialize | whether to serialize the return value TYPE: |
silent_override | whether to suppress any override warning messages TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Callable[[Tool | F], Tool] | The decorator for registering a function to be used by an agent. |
Examples:
@user_proxy.register_for_execution()
@agent2.register_for_llm()
@agent1.register_for_llm(description="This is a very useful function")
def my_function(a: Annotated[str, "description of a parameter"] = "a", b: int, c=3.14):
return a + str(b * c)
Source code in autogen/agentchat/conversable_agent.py
register_model_client #
Register a model client.
| PARAMETER | DESCRIPTION |
|---|---|
model_client_cls | A custom client class that follows the Client interface TYPE: |
**kwargs | The kwargs for the custom client class to be initialized with TYPE: |
Source code in autogen/agentchat/conversable_agent.py
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.
| PARAMETER | DESCRIPTION |
|---|---|
hookable_method | A hookable method name implemented by ConversableAgent. TYPE: |
hook | A method implemented by a subclass of AgentCapability. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
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.
Source code in autogen/agentchat/conversable_agent.py
process_all_messages_before_reply #
Calls any registered capability hooks to process all messages, potentially modifying the messages.
Source code in autogen/agentchat/conversable_agent.py
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.
Source code in autogen/agentchat/conversable_agent.py
print_usage_summary #
Print the usage summary.
Source code in autogen/agentchat/conversable_agent.py
get_actual_usage #
get_total_usage #
register_handoff #
Register a single handoff condition (OnContextCondition or OnCondition).
| PARAMETER | DESCRIPTION |
|---|---|
condition | The condition to add (OnContextCondition, OnCondition) TYPE: |
Source code in autogen/agentchat/conversable_agent.py
register_handoffs #
Register multiple handoff conditions (OnContextCondition or OnCondition).
| PARAMETER | DESCRIPTION |
|---|---|
conditions | List of conditions to add TYPE: |
Source code in autogen/agentchat/conversable_agent.py
register_input_guardrail #
Register a guardrail to be used for input validation.
| PARAMETER | DESCRIPTION |
|---|---|
guardrail | The guardrail to register. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
register_input_guardrails #
Register multiple guardrails to be used for input validation.
| PARAMETER | DESCRIPTION |
|---|---|
guardrails | List of guardrails to register. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
register_output_guardrail #
Register a guardrail to be used for output validation.
| PARAMETER | DESCRIPTION |
|---|---|
guardrail | The guardrail to register. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
register_output_guardrails #
Register multiple guardrails to be used for output validation.
| PARAMETER | DESCRIPTION |
|---|---|
guardrails | List of guardrails to register. TYPE: |
Source code in autogen/agentchat/conversable_agent.py
run_input_guardrails #
Run input guardrails for an agent before the reply is generated.
| PARAMETER | DESCRIPTION |
|---|---|
messages | The messages to check against the guardrails. |
Source code in autogen/agentchat/conversable_agent.py
run_output_guardrails #
Run output guardrails for an agent after the reply is generated.
| PARAMETER | DESCRIPTION |
|---|---|
reply | The reply generated by the agent. |