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.
翻译:大型语言模型(LLM)智能体能够执行广泛的操作,如调用工具和控制机器人,在应对现实挑战中展现出巨大潜力。现有LLM智能体通常通过生成预定义格式的JSON或文本来触发动作,但这种机制受限于固定的动作空间(例如预定义工具的范围)和有限的灵活性(例如无法组合多个工具)。本文提出利用可执行Python代码将LLM智能体的动作整合至统一动作空间(CodeAct)。通过与Python解释器集成,CodeAct可执行代码动作,并通过多轮交互动态修正先前动作或基于新观察结果生成新动作。我们在API-Bank和新构建的基准测试中对17个LLM进行的广泛分析表明,CodeAct性能优于广泛使用的替代方案(成功率最高提升20%)。CodeAct的优异表现促使我们构建一个开源LLM智能体,该智能体通过执行可解释代码与环境交互,并使用自然语言与用户协作。为此,我们基于CodeAct收集了包含7000个多轮交互的指令微调数据集CodeActInstruct,结果表明该数据集可与现有数据结合,在不损害通用能力的前提下提升模型在智能体任务上的表现。基于Llama2和Mistral微调的CodeActAgent已集成Python解释器,可独特地利用现有库执行复杂任务(如模型训练)并实现自主调试。