Anthropic Engineering Blog 中文翻译

create: 2025-01-06
update: 2026-08-10
author: thinkycx
title: 【译】在 SWE-bench Verified 上刷新纪录:Claude 3.5 Sonnet
description: Anthropic 介绍了升级版 Claude 3.5 Sonnet 在 SWE-bench Verified 基准测试上达到 49% 的 SOTA 成绩,并详细分享了其极简 Agent 脚手架设计——仅由一个 prompt、一个 Bash 工具和一个编辑工具组成,核心理念是将尽可能多的控制权交给模型本身。
category: translation
tags: anthropic, engineering, translation, benchmark

在 SWE-bench Verified 上刷新纪录:Claude 3.5 Sonnet

原文发布于 2025 年 1 月 6 日,作者 Erik Schluntz

SWE-bench Hero

引言

升级版 Claude 3.5 Sonnet 在 SWE-bench Verified 上达到 49%,超越此前 45% 的最佳成绩。 SWE-bench 是一个评估 AI 完成真实软件工程任务能力的基准测试。它考察模型能否解决来自热门开源 Python 仓库的 GitHub issue。

本文将介绍我们围绕模型构建的 Agent 脚手架设计。


SWE-bench 是什么?

SWE-bench 评估的是整个 Agent 系统——模型与其周围软件脚手架的组合。 每个任务中,AI 模型会拿到一个已配置好的 Python 环境和问题被解决前的仓库本地副本。模型必须理解代码、修改代码、测试代码,然后提交解决方案。最终评分依据的是解决原始 issue 的 PR 中的真实单元测试。

脚手架负责生成 prompt、解析输出以执行操作,并管理整个交互循环。

SWE-bench 为何受到广泛关注

原因 说明
真实工程任务 来自实际项目,而非竞赛题目
尚未被刷满 截至发文时,没有模型在 SWE-bench Verified 上突破 50%
评估整个 Agent 衡量的是模型+脚手架的综合能力,而非模型单独表现

SWE-bench Verified 是经过人工审核、确认可解的 500 道题目子集。


实现 SOTA 的方法

工具调用型 Agent

核心设计理念:将尽可能多的控制权交给语言模型本身,保持脚手架极简。 Agent 的组成非常简单:

组件 作用
一个 Prompt 描述任务和解题步骤
Bash Tool 执行 bash 命令
Edit Tool 查看和编辑文件与目录

采样持续进行,直到模型决定任务完成或超过 200k 上下文长度限制。

System Prompt

<uploaded_files>
{location}
</uploaded_files>
I've uploaded a python code repository in the directory {location} (not in /tmp/inputs). Consider the following PR description:

<pr_description>
{pr_description}
</pr_description>

Can you help me implement the necessary changes to the repository so that the requirements specified in the <pr_description> are met?
I've already taken care of all changes to any of the test files described in the <pr_description>. This means you DON'T have to modify the testing logic or any of the tests in any way!

Your task is to make the minimal changes to non-tests files in the {location} directory to ensure the <pr_description> is satisfied.

Follow these steps to resolve the issue:
1. As a first step, it might be a good idea to explore the repo to familiarize yourself with its structure.
2. Create a script to reproduce the error and execute it with `python <filename.py>` using the BashTool, to confirm the error
3. Edit the sourcecode of the repo to resolve the issue
4. Rerun your reproduce script and confirm that the error is fixed!
5. Think about edgecases and make sure your fix handles them as well

Your thinking should be thorough and so it's fine if it's very long.

Bash Tool 规格

{
   "name": "bash",
   "description": "Run commands in a bash shell\n* When invoking this tool, the contents of the \"command\" parameter does NOT need to be XML-escaped.\n* You don't have access to the internet via this tool.\n* You do have access to a mirror of common linux and python packages via apt and pip.\n* State is persistent across command calls and discussions with the user.\n* To inspect a particular line range of a file, e.g. lines 10-25, try 'sed -n 10,25p /path/to/the/file'.\n* Please avoid commands that may produce a very large amount of output.\n* Please run long lived commands in the background, e.g. 'sleep 10 &' or start a server in the background.",
   "input_schema": {
       "type": "object",
       "properties": {
           "command": {
               "type": "string",
               "description": "The bash command to run."
           }
       },
       "required": ["command"]
   }
}

Edit Tool(str_replace_editor)

{
   "name": "str_replace_editor",
   "description": "Custom editing tool for viewing, creating and editing files\n* State is persistent across command calls and discussions with the user\n* If `path` is a file, `view` displays the result of applying `cat -n`. If `path` is a directory, `view` lists non-hidden files and directories up to 2 levels deep\n* The `create` command cannot be used if the specified `path` already exists as a file\n* If a `command` generates a long output, it will be truncated and marked with `<response clipped>` \n* The `undo_edit` command will revert the last edit made to the file at `path`\n\nNotes for using the `str_replace` command:\n* The `old_str` parameter should match EXACTLY one or more consecutive lines from the original file. Be mindful of whitespaces!\n* If the `old_str` parameter is not unique in the file, the replacement will not be performed. Make sure to include enough context in `old_str` to make it unique\n* The `new_str` parameter should contain the edited lines that should replace the `old_str`"
}

为什么选择字符串替换? 经过实验验证,字符串替换方式的可靠性最高——模型指定要替换的 old_str 和新内容 new_str,只有在文件中恰好有一处匹配时才会执行替换。

工具始终要求使用绝对路径,以避免 Agent 切换出根目录后出现问题。


结果

模型 SWE-bench Verified 得分
Claude 3.5 Sonnet(升级版) 49%
此前 SOTA 45%
Claude 3.5 Sonnet(旧版) 33%
Claude 3 Opus 22%

Agent 行为示例

模型的典型工作流程:理解仓库结构、复现问题、修复代码、验证修复。 我们使用 SWE-Agent 框架作为基础。Agent 输出以 THOUGHT(思考)、ACTION(行动)和 OBSERVATION(观察)的格式呈现。

案例:RidgeClassifierCV 的 store_cv_values 问题

步骤 1: 模型使用 Edit Tool 查看仓库结构

步骤 2: 模型创建复现脚本:

import numpy as np
from sklearn import linear_model as lm

#test database
n = 100
x = np.random.randn(n, 30)
y = np.random.normal(size = n)

rr = lm.RidgeClassifierCV(alphas=np.arange(0.1, 1000, 0.1), normalize=True, 
                         store_cv_values=True).fit(x, y)

步骤 3: 运行脚本,确认复现了 TypeError

步骤 4: 使用 str_replace 修复代码——在 __init__ 中添加 store_cv_values=False 参数,并将其传递给父类构造函数

该模型在 12 步内完成了这个任务并提交。有些任务需要超过 100 轮;另一些情况下,模型会耗尽上下文窗口。

升级版 3.5 Sonnet 的一个显著改进是"更频繁地自我纠正",并展现出"尝试多种不同解决方案的能力,而非反复犯同样的错误"。


面临的挑战

高成本、评分复杂性和多模态缺失是主要挑战。 以下是我们遇到的关键问题:

挑战 详情
耗时与高 token 成本 许多成功的运行需要数百轮对话,消耗超过 100k token。升级版 Claude 3.5 Sonnet 非常"执着"。
评分问题 环境配置问题和 install patch 被重复应用导致了假失败。
隐藏测试 模型看不到评分所用的测试。有些失败源于在错误的抽象层级解决了问题;有些则是尽管解决了问题,但不符合单元测试的具体期望。
多模态缺失 没有实现让模型查看保存到文件系统或 URL 引用的图片的能力,使得调试某些任务(尤其是 Matplotlib 相关)非常困难,"容易产生模型幻觉"。

致谢

Erik Schluntz 优化了 SWE-bench Agent 并撰写了本文。Simon Biggs、Dawn Drain 和 Eric Christiansen 协助实现了基准测试。Shauna Kravec、Dawn Drain、Felipe Rosso、Nova DasSarma、Ven Chandrasekaran 等人为训练 Claude 3.5 Sonnet 的 Agent 编程能力做出了贡献。