For more detailed usage information, please refer to our cookbook: Task Generation Cookbook

1. Concept

In the CAMEL framework, a task is a specific assignment that can be delegated to an agent and resolved by that agent. Tasks represent a higher-level concept than prompts and should be managed by other modules such as the Planner and Workforce. There are two key characteristics of a task:

  1. A task can be collaborative, requiring multiple agents to work together.
  2. A task can be decomposed and evolved.

1.1 Task Attributes

AttributeTypeDescription
contentstringA clear and concise description of the task at hand.
idstringAn unique string identifier for the task.
stateEnumThe task states: “OPEN”, “RUNNING”, “DONE”, “FAILED”, “DELETED”.
typestringThe type of a task. (TODO)
parentTaskThe parent task.
subtasksA list of TaskA list of sub tasks related the original Task.
resultstringThe Task result.

1.2 Task Methods

MethodTypeDescription
from_messageclassmethodLoad Task from Message.
to_messageclassmethodConvert Task to Message.
resetinstanceReset Task to initial state.
update_resultinstanceSet task result and mark the task as DONE.
set_idinstanceSet task id.
set_stateinstanceRecursively set the state of the task and its subtasks.
add_subtaskinstanceAdd child task.
remove_subtaskinstanceDelete a subtask by a giving id.
get_running_taskinstanceGet a RUNNING task.
to_stringinstanceConvert task to a string.
get_resultinstanceGet task result to a string.
decomposeinstanceDecompose a task to a list of sub-tasks.
composeinstanceCompose task result by the sub-tasks.
get_depthinstanceGet task depth while the root depth is 1.

2. Get Started

Creating a task involves defining its goal (content) and id:

2.1 Example of Task definition

from camel.tasks import Task

task = Task(
    content="Weng earns $12 an hour for babysitting. Yesterday, she just did 51 minutes of babysitting. How much did she earn?",
    id="0",
)

2.2 Example of multiple Tasks with a hierarchical structure.

# Creating the root task
root_task = Task(content="Prepare a meal", id="0")

# Creating subtasks for the root task
sub_task_1 = Task(content="Shop for ingredients", id="1")
sub_task_2 = Task(content="Cook the meal", id="2")
sub_task_3 = Task(content="Set the table", id="3")

# Creating subtasks under "Cook the meal"
sub_task_2_1 = Task(content="Chop vegetables", id="2.1")
sub_task_2_2 = Task(content="Cook rice", id="2.2")

# Adding subtasks to their respective parent tasks
root_task.add_subtask(sub_task_1)
root_task.add_subtask(sub_task_2)
root_task.add_subtask(sub_task_3)

sub_task_2.add_subtask(sub_task_2_1)
sub_task_2.add_subtask(sub_task_2_2)

# Printing the hierarchical task structure
print(root_task.to_string())
>>>
    Task 0: Prepare a meal
    Task 1: Shop for ingredients
    Task 2: Cook the meal
        Task 2.1: Chop vegetables
        Task 2.2: Cook rice
    Task 3: Set the table

3. Decomposing and composing a Task

Decomposing or composing a task involves defining its responsible agent, prompt template and agent response parser. Here is an example:

from camel.agents import ChatAgent
from camel.tasks import Task
from camel.tasks.task_prompt import (
    TASK_COMPOSE_PROMPT,
    TASK_DECOMPOSE_PROMPT,
)
from camel.messages import BaseMessage

sys_msg = BaseMessage.make_assistant_message(
    role_name="Assistant", content="You're a helpful assistant"
)
# Set up an agent
agent = ChatAgent(system_message=sys_msg)

task = Task(
    content="Weng earns $12 an hour for babysitting. Yesterday, she just did 51 minutes of babysitting. How much did she earn?",
    id="0",
)


new_tasks = task.decompose(agent=agent)
for t in new_tasks:
    print(t.to_string())
>>>
Task 0.0: Convert 51 minutes into hours.

Task 0.1: Calculate Weng's earnings for the converted hours at the rate of $12 per hour.

Task 0.2: Provide the final earnings amount based on the calculation.
# compose task result by the sub-tasks.
task.compose(agent=agent, template=TASK_COMPOSE_PROMPT)
print(task.result)

4. TaskManager

TaskManager is used to manage tasks.

MethodTypeDescription
topological_sortinstanceSort a list of tasks topologically.
set_tasks_dependenceinstanceSet relationship between root task and other tasks. Two relationships are currently supported: serial and parallel.
evolveinstanceEvolve a task to a new task and here it is only used for data generation.

Example

from camel.tasks import (
    Task,
    TaskManager,
)

sys_msg = "You're a helpful assistant"

# Set up an agent
agent = ChatAgent(system_message=sys_msg)


task = Task(
    content="Weng earns $12 an hour for babysitting. Yesterday, she just did 51 minutes of babysitting. How much did she earn?",
    id="0",
)
print(task.to_string())
>>>Task 0: Weng earns $12 an hour for babysitting. Yesterday, she just did 51 minutes of babysitting. How much did she earn?
task_manager = TaskManager(task)
evolved_task = task_manager.evolve(task, agent=agent)
print(evolved_task.to_string())
>>>Task 0.0: Weng earns $12 an hour for babysitting. Yesterday, she babysat for 1 hour and 45 minutes. If she also received a $5 bonus for exceptional service, how much did she earn in total for that day?

5. Conclusion

We offers a structured approach to task management, enabling efficient delegation and resolution of tasks by agents. With features such as task decomposition, composition, and hierarchical task structures, CAMEL provides the tools necessary to manage complex workflows. Whether handling simple tasks or intricate, multi-level assignments, CAMEL’s task management capabilities ensure that tasks are executed effectively and collaboratively, enhancing overall productivity.