task
TaskConfig
¶
Bases: BaseModel
Configuration for a Task. This is a container for any params that
we didn't include in the task __init__
method.
We may eventually move all the task init params to this class, analogous to how
we have config classes for Agent
, ChatAgent
, LanguageModel
, etc.
Attributes:
Name | Type | Description |
---|---|---|
inf_loop_cycle_len |
int
|
max exact-loop cycle length: 0 => no inf loop test |
inf_loop_dominance_factor |
float
|
dominance factor for exact-loop detection |
inf_loop_wait_factor |
int
|
wait this * cycle_len msgs before loop-check |
restart_as_subtask |
bool
|
whether to restart every run of this task when run as a subtask. |
addressing_prefix |
str
|
"@"-like prefix an agent can use to address other
agents, or entities of the agent. E.g., if this is "@", the addressing
string would be "@Alice", or "@user", "@llm", "@agent", etc.
If this is an empty string, then addressing is disabled.
Default is empty string "".
CAUTION: this is a deprecated practice, since normal prompts
can accidentally contain such addressing prefixes, and will break
your runs. This could happen especially when your prompt/context
contains code, but of course could occur in normal text as well.
Instead, use the |
allow_subtask_multi_oai_tools |
bool
|
whether to allow multiple OpenAI tool-calls to be sent to a sub-task. |
recognize_string_signals |
bool
|
whether to recognize string-based signaling like DONE, SEND_TO, PASS, etc. Default is True, but note that we don't need to use string-based signaling, and it is recommended to use the new Orchestration tools instead (see agent/tools/orchestration.py), e.g. DoneTool, SendTool, etc. |
Task(agent=None, name='', llm_delegate=False, single_round=False, system_message='', user_message='', restart=True, default_human_response=None, interactive=True, only_user_quits_root=True, erase_substeps=False, allow_null_result=False, max_stalled_steps=5, default_return_type=None, done_if_no_response=[], done_if_response=[], config=TaskConfig(), **kwargs)
¶
A Task
wraps an Agent
object, and sets up the Agent
's goals and instructions.
A Task
maintains two key variables:
self.pending_message
, which is the message awaiting a response, andself.pending_sender
, which is the entity that sent the pending message.
The possible responders to self.pending_message
are the Agent
's own "native"
responders (agent_response
, llm_response
, and user_response
), and
the run()
methods of any sub-tasks. All responders have the same type-signature
(somewhat simplified):
The main top-level method in the Task
class is run()
, which repeatedly calls
step()
until done()
returns true. The step()
represents a "turn" in the
conversation: this method sequentially (in round-robin fashion) calls the responders
until it finds one that generates a valid response to the pending_message
(as determined by the valid()
method). Once a valid response is found,
step()
updates the pending_message
and pending_sender
variables,
and on the next iteration, step()
re-starts its search for a valid response
from the beginning of the list of responders (the exception being that the
human user always gets a chance to respond after each non-human valid response).
This process repeats until done()
returns true, at which point run()
returns
the value of result()
, which is the final result of the task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent |
Agent
|
agent associated with the task |
None
|
name |
str
|
name of the task |
''
|
llm_delegate |
bool
|
Whether to delegate "control" to LLM; conceptually,
the "controlling entity" is the one "seeking" responses to its queries,
and has a goal it is aiming to achieve, and decides when a task is done.
The "controlling entity" is either the LLM or the USER.
(Note within a Task there is just one
LLM, and all other entities are proxies of the "User" entity).
See also: |
False
|
single_round |
bool
|
If true, task runs until one message by "controller"
(i.e. LLM if |
False
|
system_message |
str
|
if not empty, overrides agent's system_message |
''
|
user_message |
str
|
if not empty, overrides agent's user_message |
''
|
restart |
bool
|
if true, resets the agent's message history at every run. |
True
|
default_human_response |
str | None
|
default response from user; useful for
testing, to avoid interactive input from user.
[Instead of this, setting |
None
|
default_return_type |
Optional[type]
|
if not None, extracts a value of this type from the result of self.run() |
None
|
interactive |
bool
|
if true, wait for human input after each non-human
response (prevents infinite loop of non-human responses).
Default is true. If false, then |
True
|
only_user_quits_root |
bool
|
if true, when interactive=True, only user can quit the root task (Ignored when interactive=False). |
True
|
erase_substeps |
bool
|
if true, when task completes, erase intermediate
conversation with subtasks from this agent's |
False
|
allow_null_result |
bool
|
If true, create dummy NO_ANSWER response when no valid response is found
in a step.
Optional, default is False.
Note: In non-interactive mode, when this is set to True,
you can have a situation where an LLM generates (non-tool) text,
and no other responders have valid responses, and a "Null result"
is inserted as a dummy response from the User entity, so the LLM
will now respond to this Null result, and this will continue
until the LLM emits a DONE signal (if instructed to do so),
otherwise langroid detects a potential infinite loop after
a certain number of such steps (= |
False
|
max_stalled_steps |
int
|
task considered done after this many consecutive steps with no progress. Default is 3. |
5
|
done_if_no_response |
List[Responder]
|
consider task done if NULL response from any of these responders. Default is empty list. |
[]
|
done_if_response |
List[Responder]
|
consider task done if NON-NULL response from any of these responders. Default is empty list. |
[]
|
Source code in langroid/agent/task.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
|
clone(i)
¶
Returns a copy of this task, with a new agent.
Source code in langroid/agent/task.py
kill_session(session_id='')
classmethod
¶
Kill the session with the given session_id.
kill()
¶
add_sub_task(task)
¶
Add a sub-task (or list of subtasks) that this task can delegate (or fail-over) to. Note that the sequence of sub-tasks is important, since these are tried in order, as the parent task searches for a valid response (unless a sub-task is explicitly addressed).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task |
Task | List[Task] | Tuple[Task, TaskConfig] | List[Tuple[Task, TaskConfig]]
|
A task, or list of tasks, or a tuple of task and task config, or a list of tuples of task and task config. These tasks are added as sub-tasks of the current task. The task configs (if any) dictate how the tasks are run when invoked as sub-tasks of other tasks. This allows users to specify behavior applicable only in the context of a particular task-subtask combination. |
required |
Source code in langroid/agent/task.py
init(msg=None)
¶
Initialize the task, with an optional message to start the conversation.
Initializes self.pending_message
and self.pending_sender
.
Args:
msg (str|ChatDocument): optional message to start the conversation.
Returns:
Type | Description |
---|---|
ChatDocument | None
|
the initialized |
ChatDocument | None
|
Currently not used in the code, but provided for convenience. |
Source code in langroid/agent/task.py
reset_all_sub_tasks()
¶
Recursively reset message history & state of own agent and those of all sub-tasks.
run(msg=None, turns=-1, caller=None, max_cost=0, max_tokens=0, session_id='', allow_restart=True, return_type=None)
¶
Synchronous version of run_async()
.
See run_async()
for details.
Source code in langroid/agent/task.py
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
|
run_async(msg=None, turns=-1, caller=None, max_cost=0, max_tokens=0, session_id='', allow_restart=True, return_type=None)
async
¶
Loop over step()
until task is considered done or turns
is reached.
Runs asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
Any
|
initial user-role message to process; if None,
the LLM will respond to its initial |
None
|
turns |
int
|
number of turns to run the task for; default is -1, which means run until task is done. |
-1
|
caller |
Task | None
|
the calling task, if any |
None
|
max_cost |
float
|
max cost allowed for the task (default 0 -> no limit) |
0
|
max_tokens |
int
|
max tokens allowed for the task (default 0 -> no limit) |
0
|
session_id |
str
|
session id for the task |
''
|
allow_restart |
bool
|
whether to allow restarting the task |
True
|
return_type |
Optional[Type[T]]
|
desired final result type |
None
|
Returns:
Type | Description |
---|---|
Optional[ChatDocument | T]
|
Optional[ChatDocument]: valid result of the task. |
Source code in langroid/agent/task.py
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 |
|
step(turns=-1)
¶
Synchronous version of step_async()
. See step_async()
for details.
TODO: Except for the self.response() calls, this fn should be identical to
step_async()
. Consider refactoring to avoid duplication.
Source code in langroid/agent/task.py
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 |
|
step_async(turns=-1)
async
¶
A single "turn" in the task conversation: The "allowed" responders in this
turn (which can be either the 3 "entities", or one of the sub-tasks) are
tried in sequence, until a valid response is obtained; a valid
response is one that contributes to the task, either by ending it,
or producing a response to be further acted on.
Update self.pending_message
to the latest valid response (or NO_ANSWER
if no valid response was obtained from any responder).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
turns |
int
|
number of turns to process. Typically used in testing where there is no human to "quit out" of current level, or in cases where we want to limit the number of turns of a delegated agent. |
-1
|
Returns (ChatDocument|None):
Updated self.pending_message
. Currently the return value is not used
by the task.run()
method, but we return this as a convenience for
other use-cases, e.g. where we want to run a task step by step in a
different context.
Source code in langroid/agent/task.py
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 |
|
response(e, turns=-1)
¶
Sync version of response_async()
. See response_async()
for details.
Source code in langroid/agent/task.py
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 |
|
response_async(e, turns=-1)
async
¶
Get response to self.pending_message
from a responder.
If response is valid (i.e. it ends the current turn of seeking
responses):
-then return the response as a ChatDocument object,
-otherwise return None.
Args:
e (Responder): responder to get response from.
turns (int): number of turns to run the task for.
Default is -1, which means run until task is done.
Returns:
Type | Description |
---|---|
Optional[ChatDocument]
|
Optional[ChatDocument]: response to |
Optional[ChatDocument]
|
valid, None otherwise |
Source code in langroid/agent/task.py
1490 1491 1492 1493 1494 1495 1496 1497 1498 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 |
|
result(status=None)
¶
Get result of task. This is the default behavior. Derived classes can override this.
Note the result of a task is returned as if it is from the User entity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
status |
StatusCode
|
status of the task when it ended |
None
|
Returns: ChatDocument: result of task
Source code in langroid/agent/task.py
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 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 |
|
done(result=None, r=None)
¶
Check if task is done. This is the default behavior. Derived classes can override this. Args: result (ChatDocument|None): result from a responder r (Responder|None): responder that produced the result Not used here, but could be used by derived classes. Returns: bool: True if task is done, False otherwise StatusCode: status code indicating why task is done
Source code in langroid/agent/task.py
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 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 |
|
valid(result, r)
¶
Is the result from a Responder (i.e. an entity or sub-task) such that we can stop searching for responses in this step?
Source code in langroid/agent/task.py
log_message(resp, msg=None, mark=False)
¶
Log current pending message, and related state, for lineage/debugging purposes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
resp |
Responder
|
Responder that generated the |
required |
msg |
ChatDocument
|
Message to log. Defaults to None. |
None
|
mark |
bool
|
Whether to mark the message as the final result of
a |
False
|
Source code in langroid/agent/task.py
set_color_log(enable=True)
¶
Flag to enable/disable color logging using rich.console.
In some contexts, such as Colab notebooks, we may want to disable color logging
using rich.console, since those logs show up in the cell output rather than
in the log file. Turning off this feature will still create logs, but without
the color formatting from rich.console
Args:
enable (bool): value of self.color_log
to set to,
which will enable/diable rich logging