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智能体,它通过执行可解释的代码与环境交互,并使用自然语言与用户协作。为此,我们收集了一个包含7k轮使用CodeAct进行多轮交互的指令微调数据集CodeActInstruct。我们证明,它可以与现有数据结合使用,在不损害模型通用能力的情况下,提升模型在面向智能体任务中的表现。基于Llama2和Mistral微调的CodeActAgent,集成了Python解释器,并经过独特定制,能够使用现有库执行复杂任务(例如模型训练)并自主进行自我调试。