task
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=False, erase_substeps=False, allow_null_result=True, max_stalled_steps=5, done_if_no_response=[], done_if_response=[])
¶
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
|
[Deprecated, not used; use |
False
|
single_round |
bool
|
[Deprecated: Use |
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 |
True
|
default_human_response |
str
|
default response from user; useful for
testing, to avoid interactive input from user.
[Instead of this, setting |
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, only user can quit the root task.
[This param is ignored & deprecated; Keeping for backward compatibility.
Instead of this, setting |
False
|
erase_substeps |
bool
|
if true, when task completes, erase intermediate
conversation with subtasks from this agent's |
False
|
allow_null_result |
bool
|
[Deprecated, may be removed in future.] If true, allow null (empty or NO_ANSWER) as the result of a step or overall task result. Optional, default is True. |
True
|
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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 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 |
|
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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task |
Task | List[Task]
|
sub-task(s) to add |
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
run(msg=None, turns=-1, caller=None, max_cost=0, max_tokens=0, session_id='')
¶
Synchronous version of run_async()
.
See run_async()
for details.
Source code in langroid/agent/task.py
run_async(msg=None, turns=-1, caller=None, max_cost=0, max_tokens=0, session_id='')
async
¶
Loop over step()
until task is considered done or turns
is reached.
Runs asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
str | ChatDocument
|
initial 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 |
''
|
Returns:
Type | Description |
---|---|
Optional[ChatDocument]
|
Optional[ChatDocument]: valid result of the task. |
Source code in langroid/agent/task.py
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
629 630 631 632 633 634 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 |
|
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
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 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 |
|
response(e, turns=-1)
¶
Sync version of response_async()
. See response_async()
for details.
Source code in langroid/agent/task.py
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
result()
¶
Get result of task. This is the default behavior. Derived classes can override this. Returns: ChatDocument: result of task
Source code in langroid/agent/task.py
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
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 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 |
|
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