【论文分享】【Agent】TDAG: A Multi-Agent Framework based on Dynamic Task Decomposition and Agent Generation

   1.论文信息

标题:TDAG: A Multi-Agent Framework based on Dynamic Task Decomposition and Agent Generation/TDAG:一种基于动态任务分解和Agent生成的多Agent框架

收录的会议/期刊:Elsevier Neural Networks

作者信息:

arxiv:🔗https://arxiv.org/pdf/2402.10178

github网站:🔗https://github.com/yxwang8775/TDAG

github代码:🔗https://github.com/yxwang8775/TDAG

2.泛读

a. 名词解释

  • Agent生成

        自动创建或实例化具有特定功能和职责的智能代理(Agent)的过程。

动态性与适应性

按需创建:根据任务需求和当前系统状态,动态生成专门负责特定子任务的代理。例如,当需要查找火车票时,框架自动生成一个 “票务查询代理”。

任务驱动:代理的类型、数量和能力由任务分解的结果决定。如示例中,复杂的旅行规划任务被分解为 “查找交通”“安排市内行程” 等子任务,每个子任务对应一个或多个代理。

与任务分解的协同

任务分解为生成依据:框架先将复杂任务拆分为子任务(如示例中的subtask),再为每个子任务生成对应的代理。

迭代优化:任务分解和代理生成可能是迭代过程。例如,当发现某个子任务超出当前代理能力时,框架会进一步分解或调整代理配置。

在旅行规划中的具体体现

在提供的示例中,系统根据旅行需求动态生成:

  • 票务代理:处理火车票查询(如 “查找武汉到广州的可用火车票”)。
  • 行程规划代理:安排市内游览顺序和时间(如 “制定广州详细旅行游览计划”)。

b.创新点

1. 提出动态任务分解与智能体生成(TDAG)多智能体框架

传统方法将复杂任务分解为固定子任务后不再调整,易导致错误传播(早期子任务失败影响整体),且依赖手动设计的子智能体,通用性和扩展性差。而 TDAG 框架通过动态任务分解,能根据前序子任务的结果实时更新后续子任务,避免错误累积;同时为每个子任务自动生成专用子智能体,无需人工干预,提升了对复杂、不可预测场景的适应性。

2. 引入增量技能库与工具文档优化
  • 工具文档生成:对原始工具文档进行重构和优化,去除冗余信息,确保子智能体能高效理解和使用工具(如数据库、代码解释器)
  • 增量技能库:子智能体完成任务后总结经验,形成包含 “名称、细节、解决方案” 的技能条目,通过 SentenceBERT 模型检索相似技能并持续更新,提升对重复或相似任务的处理效率,且技能可根据环境动态调整

3.精读

a.控制代码demo全流程解析

i.启动

pip install -r requirements.txt
python serve.py

### run

python ./run/run_incre.py

ii.流程总结

步骤调用Agent作用主要输出
1MainAgent任务理解与分解子任务列表
2SubAgent子任务推理与执行(多轮对话)子任务结果 <result>...</result>
3VerifyAgent结果自动验证<action>success()</action>
4UpdateAgent技能库扩展/合并新/优化技能条目
5-(over prompt)计划归纳输出<plan>...</plan>
6TravelSimulator仿真与评分errors, score, state

iii.主代码(调用run方法)

for type in [1, 2, 3]:
    begin = 0
    end = 999
    step = 1
    extend_end = extend_ends[type-1]
    task_ids = range(begin, end, step)
    run(type, task_ids, extend_end=extend_end)

for type in [1, 2, 3]:

  • 遍历三种类型的数据集(type=1, 2, 3),通常对应三种不同的数据文件(如 data_type1.json、data_type2.json、data_type3.json),每种类型代表不同的任务难度、约束或实验场景。

begin = 0

end = 999

step = 1

  • 设置任务编号的起始、结束和步长。
  • 这里默认从第0个任务开始,最多到第998个任务(实际会根据数据集长度自动截断),每次步长为1,即逐个任务处理。

extend_end = extend_ends[type-1]

  • extend_ends 是一个长度为3的列表,每个元素对应每种type的“技能库扩展截止编号”。
  • 只有在任务编号小于 extend_end 时,才会对技能库进行扩展(即前N个任务用于技能积累,后面只用已有技能)

task_ids = range(begin, end, step)

  • 生成当前type下要处理的所有任务编号列表。

run(type, task_ids, extend_end=extend_end)

  • 调用 run 函数,批量处理当前type下的所有任务。
  • run 函数会自动读取对应的数据集文件,依次对每个任务进行多智能体推理、执行、验证、仿真、评分等全流程。

iv.run方法(调用run_item)

def run(type, task_ids, extend_end=999):
    output_dir = f'./output/travel/{method}/{model_name}/type{type}'
    data_path = f'./data/travel/data_type{type}.json'

    with open(data_path, 'r', encoding='utf-8') as file:
        data = json.load(file)
    global extend
    for task_id, d in enumerate(data):
        if task_id < extend_end:
            extend = True
        else:
            extend = False
        if task_id not in task_ids:
            continue
        print(f'data_idx={task_id}:{time.localtime()}')
        prediction = []
        # try:
        global agent_id
        agent_id = 0
        output_path = f'{output_dir}/{task_id}'
        if not os.path.exists(output_path):
            os.makedirs(output_path)
        prediction_file = f'{output_path}/prediction.json'
        task = d['question']
        simulator = TravelSimulator(**d['demands']['TravelSimulator'])
        simulator.create_constraints(d['demands']['Constraints'])
        plan_strs, completed = run_item(task=task, output_path=output_path)
        for plan_str in plan_strs:
            print(f'plan_str={plan_str}')
            simulator.action(plan_str)
        simulator.over()
        errors = simulator.get_errors()
        score = simulator.get_score()
        print(f'state={simulator.state}')
        prediction.append(
            {"question": d['question'], "plan": plan_strs, "errors": errors, "score": score,
             "over": completed, "state": str(simulator.state)})
        print(f'prediction={prediction}')
        with open(prediction_file, 'w', encoding='utf-8') as f:
            json.dump(prediction, f, indent=4, ensure_ascii=False)
output_dir = f'./output/travel/{method}/{model_name}/type{type}'

data_path = f'./data/travel/data_type{type}.json'

with open(data_path, 'r', encoding='utf-8') as file:

    data = json.load(file)
  • 构造输出目录和数据文件路径。
  • 读取对应类型的数据集(如 data_type1.json),加载为 data 列表。
global extend

for task_id, d in enumerate(data):

    if task_id < extend_end:

        extend = True

    else:

        extend = False

    if task_id not in task_ids:

        continue
  • 遍历数据集中的每个任务(d)。
  • 如果当前任务编号小于 extend_end,则允许扩展技能库(extend=True),否则只用已有技能(extend=False)。
  • 只处理 task_ids 中指定的任务编号,其余跳过。
print(f'data_idx={task_id}:{time.localtime()}')
prediction = []
global agent_id
agent_id = 0
output_path = f'{output_dir}/{task_id}'
if not os.path.exists(output_path):
    os.makedirs(output_path)
prediction_file = f'{output_path}/prediction.json'
  • 打印当前任务编号和时间,便于追踪进度。
  • 初始化 prediction 列表,用于存储当前任务的预测结果。
  • 重置 agent_id,保证每个任务的agent编号独立。
  • 创建当前任务的输出目录和预测结果文件路径。
task = d['question']
simulator = TravelSimulator(**d['demands']['TravelSimulator'])
simulator.create_constraints(d['demands']['Constraints'])
  • 取出当前任务的自然语言描述。
  • 用任务需求初始化 TravelSimulator 仿真器,并设置约束条件(如预算、时间、停留天数等)。
plan_strs, completed = run_item(task=task, output_path=output_path)
  • 调用 run_item,自动完成多智能体推理、任务分解、子任务执行、结果验证、计划归纳等全流程。
  • 返回所有 <plan>...</plan> 片段(plan_strs)和是否完成标志(completed)。
for plan_str in plan_strs:
    print(f'plan_str={plan_str}')
    simulator.action(plan_str)
simulator.over()
errors = simulator.get_errors()
score = simulator.get_score()
print(f'state={simulator.state}')
  • 依次将每个计划片段输入仿真器,模拟实际执行。
  • 调用 simulator.over() 完成仿真。
  • 获取仿真过程中的错误、得分和最终状态。
prediction.append(
    {"question": d['question'], "plan": plan_strs, "errors": errors, "score": score,
     "over": completed, "state": str(simulator.state)})
print(f'prediction={prediction}')
with open(prediction_file, 'w', encoding='utf-8') as f:
    json.dump(prediction, f, indent=4, ensure_ascii=False)
  • 将当前任务的原始问题、计划、仿真错误、得分、完成标志、最终状态等信息,保存到 prediction 列表。
  • 打印预测结果,便于调试。
  • 将结果写入 prediction.json 文件,便于后续分析和评测。

v.run_item

def run_item(task, output_path="."):
    global skills, skills_save
    skills = copy.deepcopy(skills_save)

    max_iter = 10
    global agent_id
    if type == 3:
        from agents.main_agent.prompt import EXAMPLE_MESSAGES_3
        main_agent = MainAgent(model_name=model_name, task=task,
                               record_path=f'{output_path}/main_agent_{agent_id}_record.json',
                               example_message=EXAMPLE_MESSAGES_3)
    elif type == 2:
        from agents.main_agent.prompt import EXAMPLE_MESSAGES_2
        main_agent = MainAgent(model_name=model_name, task=task,
                               record_path=f'{output_path}/main_agent_{agent_id}_record.json',
                               example_message=EXAMPLE_MESSAGES_2)
    else:
        from agents.main_agent.prompt import EXAMPLE_MESSAGES
        main_agent = MainAgent(model_name=model_name, task=task,
                               record_path=f'{output_path}/main_agent_{agent_id}_record.json',
                               example_message=EXAMPLE_MESSAGES)

    agent_id += 1
    cur_iter = 0
    completed = False
    try:
        while cur_iter < max_iter:
            cur_iter += 1
            response = main_agent.get_response()
            text = response['content']
            functions = main_agent.parse_functions(text)
            if len(functions) == 0:
                print('no action')
                main_agent.messages.append(
                    {'role': 'user',
                     'content': f'Available actions:{main_agent.get_action_space()}\nGive me the action between <action> and </action>.'})
                response = main_agent.get_response()
                text = response['content']
                functions = main_agent.parse_functions(text)

            if len(functions) == 0:
                functions.append({'function_name': 'over', 'args': []})

            if functions[0]['function_name'] == 'subagent_handle':
                functions[0]['args'].append(main_agent)
                functions[0]['args'].append(output_path)
                observation = invoke_function(functions[0])
                main_agent.add_user_prompt(observation)

                subtask_name = functions[0]['args'][0]
                subtask_idx = main_agent.get_subtask_idx(subtask_name)
                if subtask_idx >= 0:
                    main_agent.subtasks[subtask_idx]['result'] = observation

            elif functions[0]['function_name'] == 'over':
                completed = True
                main_agent.add_over_prompt()
                response = main_agent.get_response()
                text = response['content']
                break
            else:
                main_agent.messages.append(
                    {'role': 'user',
                     'content': f'No valid action Found. Available actions:{main_agent.get_action_space()}\nGive me the action between <action> and </action>.'})

        if cur_iter >= max_iter:
            main_agent.add_over_prompt()
            response = main_agent.get_response()
            text = response['content']

    except openai.error.InvalidRequestError as e:
        for idx in range(len(main_agent.messages) - 1, 0, -1):
            if main_agent.messages[idx]["role"] == "user":
                break
        main_agent.messages = main_agent.messages[:idx - 2]  # 从后往前删掉idx:user, idx-1:assistant, idx-2:user
        main_agent.add_over_prompt()
        response = main_agent.get_response()
        text = response['content']

    check_result = check_plan_format(text)
    if check_result == "All formats are correct." and completed:
        skills_save = skills
        with open(library_path, 'w', encoding='utf-8') as f:
            json.dump(skills_save, f, indent=4)
        with open(f"{output_path}/skill.json", 'w', encoding='utf-8') as f:
            json.dump(skills_save, f, indent=4)

    if "No valid plan format found." in check_result:
        main_agent.messages.append({"role": "user", "content": f"{check_result}"})
        response = main_agent.get_response()
        text = response['content']
        check_result = check_plan_format(text)
    if check_result != "All formats are correct.":
        main_agent.messages.append({"role": "user", "content": f"{check_result}"})
        response = main_agent.get_response()
        text = response['content']
    plan_strs = get_content_list(text, begin_str='<plan>', end_str='</plan>')

    return plan_strs, completed
  • run_item 是单个任务的多智能体推理主流程,集成了主任务分解、子任务调度、结果归纳、格式检查、技能库保存等功能。
  • 支持多轮对话、错误自适应、自动修正,保证输出高质量、标准化的计划结果。
  • 是整个多智能体系统“智能推理-自动归纳-知识积累”闭环的关键环节。
global skills, skills_save
skills = copy.deepcopy(skills_save)
  • 每次运行任务前,重置技能库为初始状态,保证每个任务独立。
MainAgent 初始化
max_iter = 10
global agent_id
if type == 3:
    from agents.main_agent.prompt import EXAMPLE_MESSAGES_3
    main_agent = MainAgent(model_name=model_name, task=task,
                           record_path=f'{output_path}/main_agent_{agent_id}_record.json',
                           example_message=EXAMPLE_MESSAGES_3)
elif type == 2:
    from agents.main_agent.prompt import EXAMPLE_MESSAGES_2
    main_agent = MainAgent(model_name=model_name, task=task,
                           record_path=f'{output_path}/main_agent_{agent_id}_record.json',
                           example_message=EXAMPLE_MESSAGES_2)
else:
    from agents.main_agent.prompt import EXAMPLE_MESSAGES
    main_agent = MainAgent(model_name=model_name, task=task,
                           record_path=f'{output_path}/main_agent_{agent_id}_record.json',
                           example_message=EXAMPLE_MESSAGES)
agent_id += 1
 多轮主智能体对话
functions = main_agent.parse_functions(text)

 解析大模型输出,提取所有 <action>...</action> 结构,得到一个函数调用列表(如 [{'function_name': 'subagent_handle', 'args': [...]}, ...])。

如果本轮没有解析到任何 action(即大模型没有输出 <action>...</action>),说明大模型没有给出明确的下一步操作。

打印提示“no action”。

向消息队列追加一条 user 消息,内容为“Available actions:xxx\nGive me the action between <action> and </action>.”,明确要求大模型输出 action。

再次调用 main_agent.get_response(),重新获取大模型回复,并重新解析 action。

if len(functions) == 0:
   functions.append({'function_name': 'over', 'args': []})

如果补充提示后依然没有解析到 action,为防止死循环,强制将 functions 设为 [{'function_name': 'over', 'args': []}],即让主流程进入终止/归纳阶段。


if functions[0]['function_name'] == 'subagent_handle':
    functions[0]['args'].append(main_agent)
    functions[0]['args'].append(output_path)
    observation = invoke_function(functions[0])
    main_agent.add_user_prompt(observation)

    subtask_name = functions[0]['args'][0]
    subtask_idx = main_agent.get_subtask_idx(subtask_name)
    if subtask_idx >= 0:
        main_agent.subtasks[subtask_idx]['result'] = observation

如果大模型输出的第一个 action 是 subagent_handle,说明需要处理一个子任务。

给 subagent_handle 这个 action 的参数列表追加 main_agent 和 output_path,为后续递归调用子任务处理函数做准备。

调用 invoke_function,动态执行 subagent_handle,即递归进入子任务的多智能体处理流程,并返回子任务的执行结果(observation)。

将子任务的执行结果作为 observation,生成新的 user prompt,继续与大模型对话。

获取当前子任务的名称,并查找其在主智能体子任务列表中的索引。

如果找到了,记录该子任务的执行结果,便于后续归纳和输出。

subagent_handle2方法

b.先学习再考核/前面积累经验,后面用已有技能解决问题

i.控制代码

    for task_id, d in enumerate(data):
        if task_id < extend_end:
            extend = True
        else:
            extend = False
        if task_id not in task_ids:
            continue

通过extend_end来判断是否扩展知识库,如果当前任务编号小于 extend_end,则允许扩展技能库(extend=True),否则只用已有技能(extend=False)

extend_ends = [32, 32, 57]
extend_end = extend_ends[type-1]

extend_end 是一个阈值,比如对于 type=1,可能是32。

前 extend_end 个任务(如编号0~31)在处理时,extend=True,表示允许将新遇到的子任务和解决方案加入技能库,让系统“学习新技能”。

后面的任务(如编号32及以后),extend=False,表示不再扩展技能库,只能用前面学到的技能来解决新任务,考察系统的“泛化能力”。

if task_id not in task_ids:

    continue

task_ids 是一个编号列表,比如 range(0, 999, 1),即0,1,2,...,998。

只有 task_id 在 task_ids 里的任务才会被处理,其余的直接跳过。

这样可以灵活控制要处理哪些任务,比如只处理前10个、只处理偶数编号等。

ii.实现代码

c.实验

d.消融实验

e.Agent剖析

i.MainAgent

class MainAgent(BaseAgent):
    def __init__(self, task, record_path=None, model_name='gpt-3.5-turbo-0613', proxy='http://127.0.0.1:10809',example_message=EXAMPLE_MESSAGES, api_interval=20):
        super().__init__(record_path=record_path, model_name=model_name, proxy=proxy,api_interval=api_interval)
        self.task = task
        self.subtasks = []
        self.completed_tasks = []
        self.system_prompt = self.generate_system_prompt()
        self.messages.append({'role': 'system',
                              'content': self.system_prompt})
        self.action_apace = self.get_action_space()
        self.messages.extend(example_message)
        self.messages.append({'role': 'user',
                              'content': self.generate_first_user_prompt(task=self.task)})

    def generate_system_prompt(self):
        system_prompt = SYSTEM_PROMPT
        replace_dict = {
        }
        return generate_prompt(template=system_prompt, replace_dict=replace_dict)

    def generate_first_user_prompt(self, task):
        user_prompt = USER_FIRST
        replace_dict = {
            '{{task}}': task
        }
        return generate_prompt(template=user_prompt, replace_dict=replace_dict)

    def add_user_prompt(self, observation):
        user_prompt = USER_PROMPT
        replace_dict = {
            '{{observation}}': observation,
            '{{action_space}}': str(self.action_apace)
        }
        prompt = generate_prompt(template=user_prompt, replace_dict=replace_dict)
        self.messages.append({'role': 'user', 'content': prompt})

    def add_over_prompt(self):
        self.messages.append({'role': 'user', 'content': USER_OVER_PROMPT})

    def __init__(self, task, record_path=None, model_name='gpt-3.5-turbo-0613', proxy='http://127.0.0.1:10809',example_message=EXAMPLE_MESSAGES, api_interval=20):
        super().__init__(record_path=record_path, model_name=model_name, proxy=proxy,api_interval=api_interval)
        self.task = task #任务名称
        self.subtasks = [] #子任务列表
        self.completed_tasks = [] #已完成列表
        self.system_prompt = self.generate_system_prompt()  #生成系统提示词
        self.messages.append({'role': 'system',
                              'content': self.system_prompt})  #加入消息队列
        self.action_apace = self.get_action_space() 
        self.messages.extend(example_message) #加入示例消息,帮助大模型理解任务分解格式
        self.messages.append({'role': 'user',
                              'content': self.generate_first_user_prompt(task=self.task)})  生成首条用户提示词

# 提示词
SYSTEM_PROMPT
SYSTEM_PROMPT = f'''You are an autonomous intelligent agent tasked with making travel plans for Bob. To be successful, it is very important to follow the following rules:
1. You should only issue one action at a time.
2. You should reason step by step and then issue the next action.
3. Your response should be formatted as follows:
THOUGHT: the thought process to achieve the goal, including the subtask to be handled.
ACTION: the action you call to assign the subtask or submit the task.

EXTERNAL RESOURCES:
1. A database containing information about train tickets, attractions, and city transportation.
2. A python notebook to execute python code for numerical operations and planning. 

PLAN AND SUBTASK:
If the subtask cannot be easily solved directly or requires the use of external resources, please assign it to another agent to complete (such as "find the cheapest train from Beijing to Shanghai in 2023-7-1"), otherwise you can complete it yourself. You may need to wait for results from other agents before proceeding to the next step of the task. If you need help from other agents, please clearly describe the task objectives, background, and precautions of the subtask. 

A subtask-structure has the following json component and surrounded with <subtask></subtask> as follows:
{subtask_format}
Use "<action>subagent_handle(subtask_name)</action>" if you want to assign a subtask to other agents for completion.

* Important Notice *
- Always make feasible and efficient plans that can lead to successful task solving. Never create new subtasks that are similar or same as the existing subtasks.
- For subtasks with similar goals, try to do them together in one subtask with a list of subgoals, rather than split them into multiple subtasks.
- You only need to complete the tasks in the given requirement, don't do anything extra.
- You can plan multiple subtasks if you want.
- Not all sub-tasks need to be assigned to sub-agent to complete, you can complete some simple tasks yourself.

{TASK_DESCRIPTION}

--- Your Workflow ---
1. You will first be given a task.
2. Then you will start to solve it step by step. Multiple subtasks need to be completed during the solution process. If you cannot solve some subtasks independently, for example, it is difficult or requires the use of external resources, please use "<action>subagent_handle(subtask_name)</action>"  and wait for the response of other agents. After getting a response, continue with the task.
3. Finally, call over() to indicate task completion. After the content in task requirements is completed, call over() immediately without making unnecessary planning.

--- Available Actions ---
1. <action>subagent_handle(subtask_name)</action>
If you want to assign a subtask to other agents for completion, call <action>subagent_handle(subtask_name)</action>
Before you call it, give me the subtask surrounded with <subtask></subtask>.
2. <action>over()</action>
When you think the task is completed, call "<action>over()</action>". After the content in task requirements is completed, call over() immediately without making unnecessary planning.

Now you will be given a task. Start planning and give me an action.
'''

你是一个自主智能代理,负责为鲍勃制定旅行计划。为了成功完成任务,遵循以下规则非常重要:

1. 每次只能发出一个行动指令。

2. 应逐步推理,然后再发出下一个行动指令。

3. 你的回应格式如下:

思考:实现目标的思考过程,包括需要处理的子任务。

行动:你调用的用于分配子任务或提交任务的行动。

外部资源:

1. 一个包含火车票、景点和城市交通信息的数据库。

2. 一个用于执行数值运算和规划的python笔记本。

计划与子任务:

如果子任务难以直接解决或需要使用外部资源,请将其分配给其他代理完成(例如“查找2023年7月1日从北京到上海的最便宜火车”);否则,你可以自行完成。在进行任务的下一步之前,可能需要等待其他代理的结果。如果需要其他代理的帮助,请清晰描述子任务的目标、背景和注意事项。

子任务结构包含以下json组件,并需用<subtask></subtask>包裹,格式如下: {subtask_format}

如果你想将子任务分配给其他代理完成,请使用“<action>subagent_handle(subtask_name)</action>”。

* 重要注意事项 *

- 始终制定可行且高效的计划,以确保任务成功解决。切勿创建与现有子任务相似或相同的新子任务。

- 对于目标相似的子任务,尽量在一个子任务中以子目标列表的形式一并处理,而非拆分为多个子任务。

- 只需完成给定要求中的任务,无需额外操作。

- 可以根据需要规划多个子任务。

- 并非所有子任务都需要分配给子代理完成,你可以自行完成一些简单任务。

{任务描述}

--- 你的工作流程 ---

1. 首先会给你一个任务。

2. 然后你将逐步解决它。在解决过程中需要完成多个子任务。如果某些子任务无法独立解决(例如难度较大或需要使用外部资源),请使用“<action>subagent_handle(subtask_name)</action>”并等待其他代理的响应。收到响应后,继续处理任务。

3. 最后,调用over()表示任务完成。完成任务要求中的内容后,立即调用over(),无需进行不必要的规划。

--- 可用行动 ---

1. <action>subagent_handle(subtask_name)</action> 如果你想将子任务分配给其他代理完成,请调用<action>subagent_handle(subtask_name)</action>。调用前,请用<subtask></subtask>包裹子任务。

2. <action>over()</action>

当你认为任务已完成时,调用“<action>over()</action>”。完成任务要求中的内容后,立即调用over(),无需进行不必要的规划。

现在会给你一个任务。开始规划并给出一个行动指令。

EXAMPLE_MESSAGES
EXAMPLE_MESSAGES = [{'role': 'system', 'name': 'example_user',
                        'content': '''Task Requirements: Bob is in Shanghai and going to travel in several cities, please make a ticket purchase plan and travel sequence for him.The demands are as follows:
1. visit ['Beijing']. The order doesn't matter and he needs to return to Shanghai finally.
2. He is free to travel from 2023.7.1 to 2023.7.20. The budget for transportation is 1000.0 CNY.
3. Play at least 3 days in Beijing.
4. If you arrive in a city before 12:00 noon, that day can be counted as a day of play. If it's past 12 o'clock, it doesn't count as a day.
5. On the basis of completing the above conditions (especially the budget), spend as little time as possible.
'''},
                       {'role': 'system', 'name': 'example_assistant', 'content':
                           '''THOUGHT:\nBased on the requirements, we can know that Bob need to go to Beijing from Shanghai, stay in Beijing for 3 days and then go to Shanghai from Beijing.
Given the task, the first step is to find available train tickets that fit Bob's schedule and budget. This is a subtask that requires the use of external resources, so I will assign it to another agent.
<subtask>
{
"subtask_name": "find available train tickets",
"goal": "Find train tickets from Shanghai to Beijing and back to Shanghai that fit within the travel dates, budget, and allow for at least 3 full days of play in Beijing. If the arrival is before 12:00 noon, it counts as a day of play.",
"criticism": "Must ensure that the total cost of the round trip tickets does not exceed the budget of 1000.0 CNY and that the timings allow for at least 3 full days in Beijing for visit. So you need to allow time between train rides(arrival in a city and departure from the city). For each ticket, you must give me the ticket number, origin, destination, departure time, arrival time and the price.",
"milestones": ["Identify a suitable train from Shanghai to Beijing.", "Identify a return train from Beijing to Shanghai ensuring at least 3 days in Beijing before departing.", "Ensure the total cost of both tickets is within the budget of 1000.0 CNY."]
}
</subtask>
ACTION:
<action>subagent_handle("find available train tickets")</action>

Once I receive the information about the train tickets, I will proceed with planning the rest of Bob's travel schedule.
'''},

                       {'role': 'system', 'name': 'example_user',
                        'content': '''Action Result:
For Bob's travel from Beijing to Chengdu and back, the updated option is:
- Departure on train **G1371** from Beijing to Chengdu on **2023-07-02 08:57**, arriving at **2023-07-02 17:54**. This costs **747.0 CNY** and takes approximately 8 hours 57 minutes.
- Return on train **K1382** from Chengdu to Beijing on **2023-07-02 19:04**, arriving at **2023-07-03 11:52**. This costs **252.0 CNY** and takes about 16 hours 48 minutes.
The total round trip costs **999.0 CNY** and will take about 25 hours 45 minutes.
For future planning:
With this new itinerary, Bob will be able to spend a full 24 hours in Chengdu.
Bob can now proceed to research places of interest in Chengdu and plan his day accordingly. He can use the 'place' and 'in_city_transport' tables for this purpose.
Bob should also consider the transport options within Chengdu, ensuring he can visit his chosen destinations without spending too much time commuting.
'''},
                       {'role': 'system', 'name': 'example_assistant', 'content':
                           '''THOUGHT:\nNext we can get the final plan. This task is simple so I can do it without other agents. We just need to use go_to_city() and stay_in() here. Based on the information provided, here is the final travel plan for Bob:
<plan>go_to_city("Beijing", "Chengdu", "2023-07-02 08:57", "2023-07-02 17:54", "G1371")</plan>
<plan>stay_in("Chengdu", "2023-07-02 17:54", "2023-07-03 17:54")</plan>
<plan>go_to_city("Chengdu", "Beijing", "2023-07-03 19:04", "2023-07-04 11:52", "K1382")</plan>
ACTION:
The task is completed. <action>over()</over>
'''}]


- 用户(example_user):任务要求:Bob 在上海,即将去几个城市旅行,请为他制定购票计划和旅行顺序。需求如下:
  1. 访问 ['北京']。顺序不限,他最终需要返回上海。
  2. 他可以在2023年7月1日至2023年7月20日之间自由旅行。交通预算为1000.0元人民币。
  3. 在北京至少玩3天。
  4. 如果你在中午12:00之前到达一个城市,那一天可以算作游玩的一天。如果超过12点,则不算作一天。
  5. 在完成上述条件(特别是预算)的基础上,尽可能少花时间。
- 助手(example_assistant):根据要求,我们可以知道Bob需要从上海去北京,在北京待3天,然后从北京回上海。根据任务,第一步是找到适合Bob时间表和预算的可用火车票。这是一个需要使用外部资源的子任务,所以我将分配给另一个代理。
  - 子任务:查找可用火车票
    - 目标:找到从上海到北京再返回上海的火车票,符合旅行日期、预算,并允许在北京至少玩3整天。如果到达时间在中午12:00之前,算作一天的游玩。
    - 批评:必须确保往返行程的总费用不超过预算1000.0元人民币,并且时间安排允许在北京至少游玩3整天。因此,你需要在火车行程之间留出时间(到达一个城市和离开该城市)。对于每张票,你必须给我提供票号、起点、终点、出发时间、到达时间和价格。
    - 里程碑:["确定从上海到北京的合适火车。", "确定从北京返回上海的火车,确保在北京至少停留3天后再出发。", "确保两张票的总费用在1000.0元人民币的预算内。"]
  - 动作:调用子代理处理“查找可用火车票”

一旦我收到关于火车票的信息,我将继续规划Bob的其余旅行日程。

- 用户(example_user):动作结果:对于Bob从北京到成都再返回的旅行,更新的选项是:
  - 乘坐 **G1371** 次列车于 **2023年7月2日08:57** 从北京出发前往成都,**17:54** 到达。费用为 **747.0元人民币**,耗时约8小时57分钟。
  - 乘坐 **K1382** 次列车于 **2023年7月2日19:04** 从成都返回北京,**11:52** 到达。费用为 **252.0元人民币**,耗时约16小时48分钟。
  往返总费用为 **999.0元人民币**,耗时约25小时45分钟。
  未来规划:有了新的行程安排,Bob将能够在成都度过完整的24小时。
  Bob现在可以开始研究成都的名胜古迹,并相应地规划他的一天。他可以为此目的使用“地点”和“市内交通”表格。
  Bob还应考虑成都内的交通选项,确保他可以在不花费太多通勤时间的情况下访问他选择的目的地。

- 助手(example_assistant):接下来我们可以得到最终计划。这个任务很简单,所以我可以在没有其他代理的情况下完成。我们只需要在这里使用 go_to_city() 和 stay_in()。根据提供的信息,这是Bob的最终旅行计划:
  - 前往城市("北京", "成都", "2023年7月2日08:57", "2023年7月2日17:54", "G1371")
  - 停留("成都", "2023年7月2日17:54", "2023年7月3日17:54")
  - 前往城市("成都", "北京", "2023年7月3日19:04", "2023年7月4日11:52", "K1382")
  动作:任务已完成。结束()

EXAMPLE_MESSAGES_2 = [{'role': 'system', 'name': 'example_user',
                        'content': '''Task Requirements: Bob is going to play in Wuhan, please make a visit plan for him.The demands are as follows:\n1. He lives in Wuhan Railway Hotel. He needs to visit ['Han Show', 'Hubu Alley', 'Yellow Crane Tower', 'Tan Hua Lin', 'Baotong Temple'] and the order can be decided by you.\n2. 2 days(2023-07-06 00:00 ~ 2023-07-08 00:00) for this trip. He can only go out to play from 7:00 to 22:00 every day, and needs to go back to the hotel to rest at other times.\n3. Visit Han Show for 150 minutes, Hubu Alley for 180 minutes, Yellow Crane Tower for 240 minutes, Tan Hua Lin for 240 minutes, Baotong Temple for 300 minutes.\n4. Ignore the opening hours of attractions, assuming they are open all day\n5. On the basis of completing the above conditions, spend as little time as possible (when He visits all of the above places, the earlier, the better) so you should carefully consider the visiting sequence and timing.'''},
                       {'role': 'system', 'name': 'example_assistant', 'content':
                           '''THOUGHT:
To create an efficient travel plan for Bob, our first subtask is to determine the optimal sequence of visits to the five specified locations in Wuhan. This sequence should minimize the overall travel time between these attractions.
Subtask 1: Get Visit Sequence In Wuhan
<subtask>
{
"subtask_name": "Optimal Visit Sequence In Wuhan",
"goal": "Find the most efficient sequence for visiting 'Han Show', 'Hubu Alley', 'Yellow Crane Tower', 'Tan Hua Lin', and 'Baotong Temple' in Wuhan. This involves determining the order that minimizes travel time between these attractions.",
"criticism": "The challenge is in accurately estimating travel times between different locations without real-time data. The chosen sequence might not be the absolute best due to unforeseen circumstances like traffic conditions.",
"milestones": [
"Determine the locations of 'Han Show', 'Hubu Alley', 'Yellow Crane Tower', 'Tan Hua Lin', and 'Baotong Temple'.",
"Estimate the travel time between each pair of attractions.",
"Use an algorithm or heuristic to find the sequence of visits that minimizes the total travel time.",
"Confirm the final sequence for the visits."
],
"result_format": "a list of spots to visit"
}</subtask>

Subtask 2: creating a detailed itinerary for Bob's visit to Wuhan, incorporating the optimal sequence of attractions based on the result of subtask 1.
<subtask>
{
"subtask_name": "Create a detailed travel and visit plan in Wuhan",
"goal": "Create a detailed travel and visit plan for Bob in Wuhan, including travel to and from 'Tan Hua Lin', 'Baotong Temple', 'Han Show', 'Hubu Alley', and 'Yellow Crane Tower', as well as the duration of each visit.",
"criticism": "The plan may face challenges due to the lack of real-time traffic data and the need to estimate travel times.",
"milestones": [
"Retrieve travel times between places from the database.",
"Schedule each activity (travel and visit) within the allowed time frame (7:00 to 22:00 each day).",
"Ensure the total visit time at each attraction meets the specified duration.",
"Compile the final itinerary in a coherent and chronological order."
],
"result_format": "A schedule consisting with multiple <plan>go_to_place(origin,destination,departure_time,arrival_time)</plan> and <plan>visit(place,begin_time,end_time)</plan>."
}</subtask>
Then we can get the final plan and call over().
Now we first let another agent solve subtask1. 
ACTION:
<action>subagent_handle("Optimal Visit Sequence In Wuhan")</action>
We will need to wait for the results from the other agents before we can proceed to the next step of the task.
'''},

                       {'role': 'system', 'name': 'example_user',
                        'content': '''Action:
<action>subagent_handle("Optimal Visit Sequence")</action>
Result:
The optimal visit sequence is ['Tan Hua Lin', 'Baotong Temple', 'Han Show', 'Hubu Alley', 'Yellow Crane Tower']. ......
'''}]

EXAMPLE_MESSAGES_3 = [{'role': 'system', 'name': 'example_user',
                        'content': '''Task Requirements: Bob is going to visit some spots across cities, please make a visit plan for him, including inter-city transportation, intra-city transportation, and visit time for each spot.The demands are as follows:\n1. He now lives in Wuhan Railway Hotel in Wuhan. In Guangzhou, he needs to visit Canton Tower for 150 minutes, Baiyun Mountain for 210 minutes.In Hangzhou, he needs to visit China National Tea Museum for 120 minutes, Quaint Water Towns for 150 minutes, Lingyin Temple for 240 minutes, West Lake for 180 minutes. And the order can be decided by you.\n2. 5 days (2023-07-9 07:00 ~ 2023-07-14 00:00) for this trip.\n3. Ignore the opening hours of attractions, assuming they are open all day\n4. On the basis of completing the above conditions (especially the time limit), spend as little money as possible.
'''},
                       {'role': 'system', 'name': 'example_assistant', 'content':
                           '''
THOUGHT:\nBased on the requirements, Bob need to visit ["Guanzhou","Hangzhou"] and now he is in Wuhan. A simple way is to first find a random order of the cities. The order of visiting cities can be [Guangzhou, Hangzhou] according to the location. So he needs to: 1. go to Guangzhou from Wuhan. 2. visit apots in Wuhan. 3.go to Hangzhou from Wuhan. 4.visit Hangzhou. Then the task is completed.
Subtask 1: get train ticket information from Wuhan to Guangzhou
<subtask>
{
"subtask_name": "Find available train tickets from Wuhan to Guangzhou",
"goal": "Find available train tickets from Wuhan to Guangzhou",
"criticism": "Consider the departure time and the price.",
"milestones": ["Get the ticket number, departure time, arrival time and price"],
"result_format": "the information of the ticket: origin_city:str,destination_city:str,departure_time,arrival_time,ticket_number"
}
</subtask>
Subtask 2: Arranging Intra-city Transportation and Visits in Guangzhou
<subtask>
{
"subtask_name": "Create a detailed travel and visit plan in Guangzhou",
"goal": "Create a detailed travel and visit plan for Bob in Guangzhou, including travel to and from 'Canton Tower' and 'Baiyun Mountain', as well as the duration of each visit.",
"criticism": "Time management is crucial to ensure Bob spends the required time at each attraction. Also, consider the time spent in traffic from one place to another.",
"milestones": ["Decide visit order", "Calculate travel time between spots and the time for visiting", "Arrange transportation and visit schedule starting from Guangzhou Railway Hotel,including go_to_place() and visit(). The begin time is determined by the results of the previous subtasks."],
"result_format": "A schedule consisting with multiple <plan>go_to_place(origin,destination,departure_time,arrival_time)</plan> and <plan>visit(place,begin_time,end_time)</plan>."
}
</subtask>


- 用户(example_user):任务要求:Bob 将去武汉游玩,请为他制定一个访问计划。要求如下:
  1. 他住在武汉铁路酒店。他需要访问 ['汉秀', '户部巷', '黄鹤楼', '昙华林', '宝通寺'],顺序可以由你决定。
  2. 这次旅行有2天时间(2023年7月6日00:00 ~ 2023年7月8日00:00)。他每天只能在7:00到22:00之间出去玩,其他时间需要回酒店休息。
  3. 参观汉秀150分钟,户部巷180分钟,黄鹤楼240分钟,昙华林240分钟,宝通寺300分钟。
  4. 忽略景点的开放时间,假设它们全天开放。
  5. 在完成上述条件的基础上,尽可能少花时间(当他访问所有上述地方时,越早越好),所以你应该仔细考虑访问顺序和时间安排。

- 助手(example_assistant):为了给Bob创建一个高效的旅行计划,我们的第一个子任务是确定在武汉访问五个指定地点的最优顺序。这个顺序应该最小化这些景点之间的总旅行时间。
  - 子任务1:获取武汉访问顺序
    - 目标:找到访问“汉秀”、“户部巷”、“黄鹤楼”、“昙华林”和“宝通寺”的最有效顺序。这涉及到确定景点之间的旅行时间顺序。
    - 批评:挑战在于在没有实时数据的情况下准确估计不同地点之间的旅行时间。由于交通状况等不可预见的情况,选择的顺序可能不是绝对最好的。
    - 里程碑:["确定“汉秀”、“户部巷”、“黄鹤楼”、“昙华林”和“宝通寺”的位置。", "估计每对景点之间的旅行时间。", "使用算法或启发式找到最小化总旅行时间的访问顺序。", "确认访问的最终顺序。"]
    - 结果格式:访问地点的列表
  - 子任务 2:为Bob在武汉的访问创建一个详细的行程,结合子任务1的结果中的最优景点顺序。
    - 目标:为Bob在武汉创建一个详细的旅行和访问计划,包括前往“昙华林”、“宝通寺”、“汉秀”、“户部巷”和“黄鹤楼”的旅行,以及每次访问的持续时间。
    - 批评:由于缺乏实时交通数据和需要估计旅行时间,计划可能会面临挑战。
    - 里程碑:["从数据库中检索地点之间的旅行时间。", "在允许的时间框架内(每天7:00到22:00)安排每项活动(旅行和访问)。", "确保每个景点的总访问时间满足指定的持续时间。", "以连贯和按时间顺序编制最终行程。"]
    - 结果格式:由多个<plan>go_to_place(起点,目的地,出发时间,到达时间)</plan>和<plan>visit(地点,开始时间,结束时间)</plan>组成的时间表。
然后我们可以得到最终计划并调用结束()。
现在我们首先让另一个代理解决子任务1。
动作:<action>subagent_handle("Optimal Visit Sequence In Wuhan")</action>
在我们可以进行任务的下一步之前,我们需要等待其他代理的结果。

- 用户(example_user):动作:<action>subagent_handle("Optimal Visit Sequence")</action>
  结果:最优访问顺序是 ['昙华林', '宝通寺', '汉秀', '户部巷', '黄鹤楼']。......

Subtask 3: Finding Transportation from Guangzhou to Hangzhou (to be modified based on the execution results of the previous subtasks.)
<subtask>
{
"subtask_name": "Find Guangzhou-Hangzhou Transportation",
"goal": "Find the most cost-effective transportation option from Guangzhou to Hangzhou that fits within the travel time frame.",
"criticism": "Consider the time taken for travel and any layovers, ensuring Bob has enough time to visit all attractions in Hangzhou within the specified days.",
"milestones": ["Find available transportation options", "Select the most cost-effective option", "Ensure arrival in Hangzhou allows enough time for visiting all planned attractions"],
"result_format": "the information of the ticket: origin_city:str,destination_city:str,departure_time,arrival_time,ticket_number"
}
</subtask>

Subtask 4: Arranging Intra-city Transportation and Visits in Hangzhou (to be modified based on the execution results of the previous subtasks.)
<subtask>
{
"subtask_name": "Create a detailed travel and visit plan in Hangzhou",
"goal": "Create a detailed travel and visit plan for Bob in Hangzhou, including travel to and from 'China National Tea Museum', 'Quaint Water Towns', 'Lingyin Temple', and 'West Lake', as well as the duration of each visit.",
"criticism": "Time management is crucial to ensure Bob spends the required time at each attraction. Also, consider the time spent in traffic from one place to another.",
"milestones": ["Decide visit order", "Calculate visit and travel time between spots", "Arrange transportation and visit schedule starting from Hangzhou Railway Hotel. The begin time is determined by the results of the previous subtasks."],
"result_format": "A schedule consisting with multiple <plan>go_to_place(origin,destination,departure_time,arrival_time)</plan> and <plan>visit(place,begin_time,end_time)</plan>."
}
</subtask>
Then we can get the final plan and call over().
Now we first let another agent solve subtask 1 to get the departure_time, arrival_time and ticket_number for <plan>go_to_city()</plan>.
ACTION:\n<action>subagent_handle("Find available train tickets from Wuhan to Guangzhou")</action>
We need to wait for the results from the other agents before we can take the next action.
'''},
                       {'role': 'system', 'name': 'example_user',
                        'content': '''Action:
<action>subagent_handle("Find available train tickets from Wuhan to Guangzhou")</action>
Result: Bob can take the train "G2034". The information is as follows:
ticket number: "G2034", departure time: "2023-07-09 13:00", arrival time: "2023-07-09 20:00".
He can start his visit in Guangzhou next day(2023-07-10 07:00).'''},

                       {'role': 'system', 'name': 'example_assistant', 'content':
                           '''Thought: good! Then we should arrange his visits in Guangzhou, including <plan>go_to_place(...)</plan> and <plan>visit(...)</plan>. According to the result of subtask 1, we can know that the begin_time in subtask 2 is 2023-07-10 07:00 and we need to make some adjustments to subtask2, as follows:
Subtask 2: Arranging Intra-city Transportation and Visits in Guangzhou
<subtask>
{
"subtask_name": "Create a detailed travel and visit plan in Guangzhou",
"goal": "Create a detailed travel and visit plan for Bob in Guangzhou, including travel to and from 'Canton Tower' and 'Baiyun Mountain', as well as the duration of each visit.",
"criticism": "Time management is crucial to ensure Bob spends the required time at each attraction and has enough time to transfer to the next city. Also, consider traffic conditions and travel time between spots.",
"milestones": ["Determine the tour route", "Arrange transportation and visit schedule starting from Guangzhou Railway Hotel at 2023-07-10 07:00."],
"result_format": "A schedule consisting with multiple <plan>go_to_place(origin,destination,departure_time,arrival_time)</plan> and <plan>visit(place,begin_time,end_time)</plan>.    1.go_to_place(origin:str,destination:str,departure_time,arrival_time): go to destination from origin. depart at departure_time and arrive at arrival_time.    2.visit(place:str,begin_time,end_time): visit somewhere from begin_time to end_time. You have to go somewhere before you can visit it."}
</subtask>
ACTION:<action>subagent_handle("Create a detailed travel and visit plan in Guangzhou")</action>
'''}
                       ]

ii.agent_generator

  • 动态优化:根据具体子任务,从完整的动作库中筛选出最相关的动作。
  • 提升效率:避免agent看到无关的动作,减少干扰,提高执行效率。
  • 任务适配:让每个子任务都有最适合的"工具集",实现精准的任务处理。

设计亮点:

  • 示例驱动:通过具体案例教会大模型优化策略。
  • 上下文感知:结合总任务和子任务进行智能优化。
  • 格式标准化:统一的输入输出格式,便于系统处理。
# 提示词
ORIGINAL_DOCUMENT
ORIGINAL_DOCUMENT = """<action>execute_sql(statement:string)</action>
You can use the action ’execute_sql’ to get some information from the database.In order to prevent too many pieces of data, please add "LIMIT 5" after the sql statement.

Tables in the database are as follows.
Table railway:
CREATE TABLE railway(
            number TEXT, /*Ticket Number*/ 
            origin TEXT,
            destination TEXT,
            departure_time DATETIME,
            arrival_time DATETIME,
            duration INTEGER, /*Duration of the train journey in minutes*/
            price REAL
        )
From table railway, you can filter the items according to the origin, destination, and departure date, and then plan according to the obtained information. Don’t use duration as a filtering condition.
If you buy a ticket, please point out the number of it. 

Table place:
CREATE TABLE place(
    name TEXT,
    city TEXT,
    type TEXT,
    opening_hours_begin INTEGER,
    opening_hours_end INTEGER
)
From table place, you can filter the items according to the city to know some information of places in this city.

Table in_city_transport:
CREATE TABLE in_city_transport(
    origin TEXT,
    destination TEXT,
    duration INTEGER, /*indicates how many minutes are spent in traffic*/
    city TEXT
)

From table in_city_transport, you can know how long you may spend from origin to destination.
Before visiting a spot, you must spend time going to this spot from the hotel or another spot.

Be careful to set conditions, otherwise too many items will be retrieved from the database.
Usage examples:
<action>execute_sql('''
SELECT * FROM place
WHERE city = 'Beijing';''')</action>
<action>execute_sql('''
SELECT origin, destination, duration FROM in_city_transport
    WHERE city = 'Hangzhou' AND 
    (origin IN ('West Lake', 'Lingyin Temple', 'Hefang Street', 'Quaint Water Towns') AND 
    destination IN ('West Lake', 'Lingyin Temple', 'Hefang Street', 'Quaint Water Towns'));''')</action> 
<action>execute_sql('''
SELECT * FROM railway
WHERE origin = 'Beijing'
  AND destination = 'Shanghai'
  AND DATE(departure_time) >= '2023-07-25'
  LIMIT 5;''')</action>. 


<action>execute_python(code:string)</action>
You can use the action ‘execute_python()’ to assist your planning. You must use print() in the python code to get what you want to know. If you have some data, you can write a python program to judge which schemes are better, python can help you easily perform numerical calculations and comparisons.
You can use dynamic programming, greedy algorithm or direct traversal to get the better plan.
The code must be complete and directly executable. The executor will not store the results of previous code calculations, so the previous results need to be re-entered.
The python code should be surrounded by \"\"\" and \"\"\".
e.g.
<action>execute_python(\"\"\"
price=[540, 300, 210]
print(f 'The total cost is {{sum(price)}}')
\"\"\")</action>
"""

<action>execute_sql(statement:string)</action>
你可以使用动作 'execute_sql' 从数据库获取一些信息。为了防止数据过多,请在sql语句后添加"LIMIT 5"。

数据库中的表如下:
表 railway(铁路):
CREATE TABLE railway(
    number TEXT, /*车票号码*/ 
    origin TEXT, /*出发地*/
    destination TEXT, /*目的地*/
    departure_time DATETIME, /*出发时间*/
    arrival_time DATETIME, /*到达时间*/
    duration INTEGER, /*火车行程时长(分钟)*/
    price REAL /*价格*/
)
从表 railway 中,你可以根据出发地、目的地和出发日期筛选项目,然后根据获得的信息进行规划。不要使用duration作为筛选条件。
如果你购买车票,请指出车票号码。

表 place(地点):
CREATE TABLE place(
    name TEXT, /*名称*/
    city TEXT, /*城市*/
    type TEXT, /*类型*/
    opening_hours_begin INTEGER, /*开放时间开始*/
    opening_hours_end INTEGER /*开放时间结束*/
)
从表 place 中,你可以根据城市筛选项目,了解该城市的一些地点信息。

表 in_city_transport(市内交通):
CREATE TABLE in_city_transport(
    origin TEXT, /*起点*/
    destination TEXT, /*终点*/
    duration INTEGER, /*表示交通花费的分钟数*/
    city TEXT /*城市*/
)
从表 in_city_transport 中,你可以知道从起点到终点可能需要花费多长时间。
在访问一个景点之前,你必须花费时间从酒店或另一个景点前往这个景点。

请小心设置条件,否则会从数据库中检索到太多项目。
使用示例:
<action>execute_sql('''
SELECT * FROM place
WHERE city = 'Beijing';''')</action>
<action>execute_sql('''
SELECT origin, destination, duration FROM in_city_transport
    WHERE city = 'Hangzhou' AND 
    (origin IN ('West Lake', 'Lingyin Temple', 'Hefang Street', 'Quaint Water Towns') AND 
    destination IN ('West Lake', 'Lingyin Temple', 'Hefang Street', 'Quaint Water Towns'));''')</action> 
<action>execute_sql('''
SELECT * FROM railway
WHERE origin = 'Beijing'
  AND destination = 'Shanghai'
  AND DATE(departure_time) >= '2023-07-25'
  LIMIT 5;''')</action>

<action>execute_python(code:string)</action>
你可以使用动作 'execute_python()' 来协助你的规划。你必须在python代码中使用print()来获得你想要知道的信息。如果你有一些数据,你可以编写python程序来判断哪个方案更好,python可以帮助你轻松进行数值计算和比较。
你可以使用动态规划、贪心算法或直接遍历来获得更好的计划。
代码必须完整且可直接执行。执行器不会存储之前代码计算的结果,所以之前的结果需要重新输入。
python代码应该用\"\"\"和\"\"\"包围。
例如:
<action>execute_python(\"\"\"
price=[540, 300, 210]
print(f 'The total cost is {{sum(price)}}')
\"\"\")</action>

  • 定义可用动作:明确告诉agent可以使用 execute_sql 和 execute_python 两个动作。
  • 数据库结构说明:详细描述三个表的结构和用途,让agent知道如何查询数据。
  • 使用规范:提供具体的使用示例和注意事项,确保agent能正确使用这些动作。
SYSTEM_PROMPT(系统提示词)
SYSTEM_PROMPT = f"""
You are a prompt generator, who is capable of generating prompts for an autonomous agent.
You will be told a subtask that the agent needs to complete. The subtask is part of the total task.
You need to optimize the following documents based on the tasks (subtask) that the agent needs to complete.
This agent use actions completely according to the following document. Please provide the agent with a prompt for the action instructions. You don't need to complete the task, but optimize the document. The optimized document will be given to the agent so that it can use the action correctly.
The original document is as follows:
<original Action Document>{ORIGINAL_DOCUMENT}</original Action Document>

*** Important Notice ***
- Surround the new document with <action document></action document>.
- You don't need to complete the task, but optimize the document.  The optimized document will be given to the agent so that it can use the action correctly.
- Optimize the document by combining the total tasks and subtasks. For example, the agent may not need all the content of the document. Only give the actions required to complete the subtask.
- Try not to delete details in the document so that the agent cannot use the action correctly. For example, sometimes some tables in the database can be deleted.
"""

你是一个提示词生成器,能够为自主智能体生成提示词。
你将被告知智能体需要完成的子任务。该子任务是总任务的一部分。
你需要根据智能体需要完成的任务(子任务)来优化以下文档。
这个智能体完全按照以下文档使用动作。请为智能体提供动作指令的提示词。你不需要完成任务,但要优化文档。优化后的文档将提供给智能体,以便它能够正确使用动作。
原始文档如下:
<original Action Document>{ORIGINAL_DOCUMENT}</original Action Document>

*** 重要通知 ***
- 用 <action document></action document> 包围新文档。
- 你不需要完成任务,但要优化文档。优化后的文档将提供给智能体,以便它能够正确使用动作。
- 通过结合总任务和子任务来优化文档。例如,智能体可能不需要文档的所有内容。只给出完成子任务所需的动作。
- 尽量不要删除文档中的细节,以免智能体无法正确使用动作。例如,有时可以删除数据库中的某些表。

  • 角色定义:明确AgentGenerator的角色是"提示词生成器"。
  • 任务目标:根据具体子任务优化原始动作文档,而不是完成任务本身。
  • 优化原则:保留必要细节,删除无关内容,让agent能高效完成任务。
EXAMPLE_MESSAGES(示例消息)
EXAMPLE_MESSAGES = [{'role': 'system', 'name': 'example_user',
                     'content': '''<agent task>
The subtask the agent need to complete is:
{'subtask_name': 'find_available_train_tickets', 'goal': 'Find train tickets from Shanghai to Beijing and back to Shanghai that fit within the travel dates, budget, and allow for at least 3 full days of play in Beijing. If the arrival is before 12:00 noon, it counts as a day of play.', 'criticism': 'Must ensure that the total cost of the round trip tickets does not exceed the budget of 1000.0 CNY and that the timings allow for at least 3 full days in Beijing for visit. So you need to allow time between train rides(arrival in a city and departure from the city). For each ticket, you must give me the ticket number, origin, destination, departure time, arrival time and the price.', 'milestones': ['Identify a suitable train from Shanghai to Beijing.', 'Identify a return train from Beijing to Shanghai ensuring at least 3 days in Beijing before departing.', 'Ensure the total cost of both tickets is within the budget of 1000.0 CNY.']}
The total task is: 
Bob is in Shanghai and going to travel in several cities, please make a ticket purchase plan and travel sequence for him.The demands are as follows:
1. visit ['Beijing']. The order doesn't matter and he needs to return to Shanghai finally.
2. He is free to travel from 2023.7.1 to 2023.7.20. The budget for transportation is 1000.0 CNY.
3. Play at least 3 days in Beijing.
4. If you arrive in a city before 12:00 noon, that day can be counted as a day of play. If it's past 12 o'clock, it doesn't count as a day.
5. On the basis of completing the above conditions (especially the budget), spend as little time as possible.
The part that has been completed is:
None
</agent task>
Now you need to give me the optimized document based on the tasks (subtask) that the agent needs to complete. Surround the new document with <action document></action document>.
'''},
                    {'role': 'system', 'name': 'example_assistant', 'content':
                        """THOUGHT: Given that the subtask that the agent need to complete is:
{
"sub-task name": "Research Transportation Options",
"goal": "Find the most cost-effective and time-saving transportation options from Beijing to Chengdu and back to Beijing within the budget of 1800.0 CNY.",
"criticism": "If the transportation options exceed the budget, we will need to find alternatives or adjust the itinerary. Additionally, scheduling might pose a challenge if there are limited options.",
"milestones": [
"Determine available transport modes from Beijing to Chengdu (train, bus, flight).",
"Find the costs associated with each transportation mode.",
"Optimize the selection based on cost and travel time.",
"Ensure that the total round trip transportation cost is within 1800.0 CNY."
]} The agent needs to use:
1.<action>execute_python(code:string)</action> to calculate the costs for different transport modes and optimize the selection based on cost and travel time within the given budget.
2.<action>execute_sql(statement:string)</action> to get the information of train tickets. Only Table railway is useful for this subtask.
So the optimized document is as follows:
DOCUMENT:
<action document>
<action>execute_sql(statement:string)</action>
You can use the action ’execute_sql’ to get some information from the database.In order to prevent too many pieces of data, please add "LIMIT 5" after the sql statement.

Tables in the database are as follows.
Table railway:
CREATE TABLE railway(
            number TEXT, /*Ticket Number*/ 
            origin TEXT,
            destination TEXT,
            departure_time DATETIME,
            arrival_time DATETIME,
            duration INTEGER, /*Duration of the train journey in minutes*/
            price REAL
        )
From table railway, you can filter the items according to the origin, destination, and departure date, and then plan according to the obtained information. Don’t use duration as a filtering condition.
If you buy a ticket, please point out the number of it. 

Be careful to set conditions, otherwise too many items will be retrieved from the database.
Usage examples:
<action>execute_sql('''
SELECT * FROM railway
WHERE origin = 'Beijing'
  AND destination = 'Shanghai'
  AND DATE(departure_time) >= '2023-07-25'
  LIMIT 5;''')</action>. 

<action>execute_python(code:string)</action>
You can use the action ‘execute_python()’ to assist your planning. You must use print() in the python code to get what you want to know. If you have some data, you can write a python program to judge which schemes are better, python can help you easily perform numerical calculations and comparisons.
You can use dynamic programming, greedy algorithm or direct traversal to get the better plan.
The code must be complete and directly executable. The executor will not store the results of previous code calculations, so the previous results need to be re-entered.
If you want to use execute_sql in execute_python, add 'from utils.travel import execute_sql' in python code.
The python code should be surrounded by \"\"\" and \"\"\".
e.g.
<action>execute_python(\"\"\"
price=[540, 300, 210]
print(f 'The total cost is {{sum(price)}}')
\"\"\")</action>
</action document>.
"""},
                    {'role': 'system', 'name': 'example_user',
                     "content":"""<agent task>
The subtask the agent need to complete is:
{'subtask_name': 'Optimal Visit Sequence In Wuhan', 
'goal': "Find the most efficient sequence for visiting 'Han Show', 'Hubu Alley', 'Yellow Crane Tower', 'Tan Hua Lin', and 'Baotong Temple' in Wuhan. This involves determining the order that minimizes travel time between these attractions.", 
'criticism': 'The challenge is in accurately estimating travel times between different locations without real-time data. The chosen sequence might not be the absolute best due to unforeseen circumstances like traffic conditions.', 
'milestones': ["Determine the locations of 'Han Show', 'Hubu Alley', 'Yellow Crane Tower', 'Tan Hua Lin', and 'Baotong Temple'.", 'Estimate the travel time between each pair of attractions.', 'Use an algorithm or heuristic to find the sequence of visits that minimizes the total travel time.', 'Confirm the final sequence for the visits.'], 
'result_format': 'a list of spots to visit'}
The total task is: 
Bob is going to play in Wuhan, please make a visit plan for him.The demands are as follows:
1. He lives in Wuhan Railway Hotel. He needs to visit ['Han Show', 'Hubu Alley', 'Yellow Crane Tower', 'Tan Hua Lin', 'Baotong Temple'] and the order can be decided by you.
2. 2 days(2023-07-06 00:00 ~ 2023-07-08 00:00) for this trip. He can only go out to play from 7:00 to 22:00 every day, and needs to go back to the hotel to rest at other times.
3. Visit Han Show for 150 minutes, Hubu Alley for 180 minutes, Yellow Crane Tower for 240 minutes, Tan Hua Lin for 240 minutes, Baotong Temple for 300 minutes.
4. Ignore the opening hours of attractions, assuming they are open all day
5. On the basis of completing the above conditions, spend as little time as possible (when He visits all of the above places, the earlier, the better) so you should carefully consider the visiting sequence and timing.
The part that has been completed is:
None
</agent task>
Now you need to give me the optimized document based on the tasks (subtask) that the agent needs to complete. Surround the new document with <action document></action document>.
"""},
{'role': 'system', 'name': 'example_assistant', 'content':
                        """THOUGHT: Given that the subtask that the agent need to complete is:
{'subtask_name': 'Optimal Visit Sequence In Wuhan', 
'goal': "Find the most efficient sequence for visiting 'Han Show', 'Hubu Alley', 'Yellow Crane Tower', 'Tan Hua Lin', and 'Baotong Temple' in Wuhan. This involves determining the order that minimizes travel time between these attractions.", 
'criticism': 'The challenge is in accurately estimating travel times between different locations without real-time data. The chosen sequence might not be the absolute best due to unforeseen circumstances like traffic conditions.', 
'milestones': ["Determine the locations of 'Han Show', 'Hubu Alley', 'Yellow Crane Tower', 'Tan Hua Lin', and 'Baotong Temple'.", 'Estimate the travel time between each pair of attractions.', 'Use an algorithm or heuristic to find the sequence of visits that minimizes the total travel time.', 'Confirm the final sequence for the visits.'], 
'result_format': 'a list of spots to visit'} The agent needs to use:
1.<action>execute_python(code:string)</action> to calculate the time spent for different visit sequences and perform other timing calculations.
2.<action>execute_sql(statement:string)</action> to get the travel time between spots and get some information about spots. Table place and in_city_transport are useful for this subtask.
So the optimized document is as follows:
DOCUMENT:
<action document>
<action>execute_sql(statement:string)</action>
You can use the action ’execute_sql’ to get some information from the database.In order to prevent too many pieces of data, please add "LIMIT 5" after the sql statement.

Tables in the database are as follows.

Table place:
CREATE TABLE place(
    name TEXT,
    city TEXT,
    type TEXT,
    opening_hours_begin INTEGER,
    opening_hours_end INTEGER
)
From table place, you can filter the items according to the city to know some information of places in this city.

Table in_city_transport:
CREATE TABLE in_city_transport(
    origin TEXT,
    destination TEXT,
    duration INTEGER, /*indicates how many minutes are spent in traffic*/
    city TEXT
)

From table in_city_transport, you can know how long you may spend from origin to destination.
Before visiting a spot, you must spend time going to this spot from the hotel or another spot.

Be careful to set conditions, otherwise too many items will be retrieved from the database.
Usage examples:
<action>execute_sql('''
SELECT * FROM place
WHERE city = 'Beijing';''')</action>
<action>execute_sql('''
SELECT origin, destination, duration FROM in_city_transport
    WHERE city = 'Hangzhou' AND 
    (origin IN ('West Lake', 'Lingyin Temple', 'Hefang Street', 'Quaint Water Towns') AND 
    destination IN ('West Lake', 'Lingyin Temple', 'Hefang Street', 'Quaint Water Towns'));''')</action> 
    

<action>execute_python(code:string)</action>
You can use the action ‘execute_python()’ to assist your planning. You must use print() in the python code to get what you want to know. If you have some data, you can write a python program to judge which schemes are better, python can help you easily perform numerical calculations and comparisons.
You can use dynamic programming, greedy algorithm or direct traversal to get the better plan.
The code must be complete and directly executable. The executor will not store the results of previous code calculations, so the previous results need to be re-entered.
The python code should be surrounded by \"\"\" and \"\"\".
e.g.
<action>execute_python(\"\"\"
price=[540, 300, 210]
print(f 'The total cost is {{sum(price)}}')
\"\"\")</action>
</action document>.
"""}
]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值