Executable Code Actions Elicit Better LLM Agents

大型语言模型(LLM)代理具有广泛的能力,例如调用工具和控制机器人,显示出在应对现实世界挑战方面的巨大潜力。LLM代理通常被提示生成JSON或以预定义格式的文本来执行操作,这通常受到受限制的行动空间(例如,预定义工具的范围)和受限制的灵活性(例如,无法组合多个工具)的限制。本文提出使用可执行的Python代码将LLM代理的行动整合到一个统一的行动空间(CodeAct)中。CodeAct与Python解释器集成,可以执行代码操作,并通过多轮交互动态修改先前的操作或发出新的操作以响应新的观察结果。我们对17个LLM在API-Bank和一个新的策划基准上进行了广泛的分析,结果表明CodeAct的表现优于广泛使用的替代方案(成功率高达20%)。CodeAct的良好表现激励我们构建一个开源的LLM代理,它通过执行可解释的代码与环境进行交互,并使用自然语言与用户进行协作。为此,我们收集了一个指令调整数据集CodeActInstruct,其中包括使用CodeAct进行的7k多轮交互。我们展示它可以与现有数据一起用于在面向代理的任务中改进模型,而不会影响其通用能力。CodeActAgent从Llama2和Mistral进行了微调,集成了Python解释器,并具有独特的定制功能,可以使用现有库执行复杂任务(例如,模型训练)并自主进行调试。
Large Language Model (LLM) agents, capable of performing a broad range of actions, such as invoking tools and controlling robots, show great potential in tackling real-world challenges. LLM agents are typically prompted to produce actions by generating JSON or text in a pre-defined format, which is usually limited by constrained action space (e.g., the scope of pre-defined tools) and restricted flexibility (e.g., inability to compose multiple tools). This work proposes to use executable Python code to consolidate LLM agents' actions into a unified action space (CodeAct). Integrated with a Python interpreter, CodeAct can execute code actions and dynamically revise prior actions or emit new actions upon new observations through multi-turn interactions. Our extensive analysis of 17 LLMs on API-Bank and a newly curated benchmark shows that CodeAct outperforms widely used alternatives (up to 20% higher success rate). The encouraging performance of CodeAct motivates us to build an open-source LLM agent that interacts with environments by executing interpretable code and collaborates with users using natural language. To this end, we collect an instruction-tuning dataset CodeActInstruct that consists of 7k multi-turn interactions using CodeAct. We show that it can be used with existing data to improve models in agent-oriented tasks without compromising their general capability. CodeActAgent, finetuned from Llama2 and Mistral, is integrated with Python interpreter and uniquely tailored to perform sophisticated tasks (e.g., model training) using existing libraries and autonomously self-debug.
许愿