任务#

如需更详细的使用信息,请参考我们的操作手册:Task Generation Cookbook

1. 概念#

在CAMEL框架中,任务是指可以委托给代理并由该代理解决的具体工作。任务代表比提示更高层次的概念,应由规划器和工作组等其他模块进行管理。任务有两个关键特征:

  1. 任务可以是协作式的,需要多个代理共同工作。

  2. 任务可以被分解和演进。

1.1 任务属性#

属性

类型

描述

内容

字符串

对手头任务的清晰简洁描述。

id

string

任务的唯一字符串标识符。

状态

枚举

任务状态:"OPEN"、"RUNNING"、"DONE"、"FAILED"、"DELETED"。

type

string

任务的类型。(待办)

parent

Task

父任务。

subtasks

任务列表

与原始任务相关的子任务列表。

result

string

任务结果。

1.2 任务方法#

方法

类型

描述

from_message

classmethod

从消息加载任务。

to_message

classmethod

将任务转换为消息。

reset

instance

将任务重置为初始状态。

update_result

instance

设置任务结果并将任务标记为已完成。

set_id

instance

设置任务ID。

set_state

instance

递归设置任务及其子任务的状态。

add_subtask

instance

添加子任务。

remove_subtask

instance

通过给定ID删除子任务。

get_running_task

instance

获取一个正在运行的任务。

to_string

instance

将任务转换为字符串。

get_result

instance

获取任务结果到字符串。

decompose

instance

将任务分解为一系列子任务。

compose

instance

通过子任务组合任务结果。

get_depth

instance

获取任务深度,根深度为1。

2. 开始使用#

创建一个任务需要定义其目标(内容)和ID:

2.1 任务定义示例#

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 具有层级结构的多任务示例.#

# 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. 分解与组合任务#

分解或组合任务涉及定义其负责的代理、提示模板和代理响应解析器。以下是一个示例:

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, template=TASK_DECOMPOSE_PROMPT)
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 用于管理任务。

方法

类型

描述

topological_sort

instance

对任务列表进行拓扑排序。

set_tasks_dependence

instance

设置根任务与其他任务之间的关系。目前支持两种关系:串行和并行。

evolve

instance

将一个任务演变为新任务,此处仅用于数据生成。

示例#

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. 结论#

我们提供了一种结构化的任务管理方法,使代理能够高效地委派和解决任务。通过任务分解、组合和分层任务结构等功能,CAMEL提供了管理复杂工作流所需的工具。无论是处理简单任务还是复杂的多级任务分配,CAMEL的任务管理能力都能确保任务被有效且协作地执行,从而提高整体生产力。