最新提交
历史
131 次提交131 次提交文件夹和文件
仓库文件导航
SWE-CARE:一个面向代码审查评估的全面性感知基准一个面向仓库级代码审查(CR)评估的全面性感知基准。
📝 概述
代码审查(CR)是指由团队中的其他开发者检查某位特定开发者所编写代码的过程。其旨在提升代码质量并发现代码缺陷,在软件质量维护中发挥着重要作用。已有一些研究提出了若干代码审查基准和自动化代码审查方法。然而,现有的代码审查基准和方法缺乏全面性,与真实场景并不接近。大语言模型(LLM)能力的快速提升使全面代码审查成为可能。为评估大语言模型在全面代码审查中的表现,我们构建了一个面向 Python 的全面性感知代码审查数据集,即 SWE-CARE。该数据集被划分为九种类型,每个实例的信息覆盖代码审查的全过程。此外,每个实例还包含仓库级特征。 基于该数据集,我们设计了一个用于评估大语言模型在代码审查中表现的框架。
🛠️ 环境搭建
按照以下步骤在本地搭建项目。
-
克隆仓库:
git clone https://github.com/your-username/SWE-CARE.git cd SWE-CARE
-
安装依赖: 本项目使用
uv进行包管理。请确保已安装 Python 3.10 或更高版本。pip install uv uv sync
或者,你可以使用
pip:pip install -e .
-
设置 pre-commit 钩子(用于开发): 本项目使用
ruff进行代码检查和格式化。pre-commit 钩子会在每次提交前自动运行这些检查。pre-commit install
🚀 快速开始:评估流水线
如需精简的评估工作流,请使用位于 scripts/run_eval_pipeline.py:
# Set up environment variables
export OPENAI_API_KEY="your-openai-api-key"
export LLM_EVALUATOR_OPENAI_API_KEY="your-evaluation-api-key"
# Run the complete pipeline (uses default Hugging Face dataset)
python scripts/run_eval_pipeline.py \
--output-dir results/pipeline_output \
--model gpt-4o \
--model-provider openai \
--file-source oracle
# Run with local dataset file
python scripts/run_eval_pipeline.py \
--dataset-name-or-path results/dataset/code_review_task_instances.jsonl \
--output-dir results/pipeline_output \
--model gpt-4o \
--model-provider openai \
--file-source oracle
# Use skeleton stubs for Python files (optional)
python scripts/run_eval_pipeline.py \
--dataset-name-or-path results/dataset/code_review_task_instances.jsonl \
--output-dir results/pipeline_output \
--model gpt-4o \
--model-provider openai \
--file-source bm25 \
--k 10 \
--retrieval-output-dir results/retrieval_output \
--use-skeleton
该脚本自动化了整个评估流程:文本生成 → 推理 → 评估。LLM 评估器默认使用 OpenAI o3 (可通过 --evaluator-model覆盖)。详细用法请参见 scripts/README.md 。
分析与报告
运行评估后,你可以生成全面的分析报告:
# Generate evaluation report from pipeline results
python scripts/eval_report.py \
--dataset-name-or-path results/dataset/code_review_task_instances.jsonl \
--eval-output-dir results/pipeline_output/evaluation/o3 \
--report-output-file results/evaluation_report.json
# Or use default Hugging Face dataset
python scripts/eval_report.py \
--eval-output-dir results/pipeline_output/evaluation/o3 \
--report-output-file results/evaluation_report.json
该命令会生成详细的统计数据,包括:
- 不同文件源设置(none、oracle、bm25 with k)下的模型表现
- 按评估器类型(RuleBasedEvaluator、LLMEvaluator)划分的表现细分
- 按元数据类别(问题领域、难度、预估审查工作量)划分的表现分析
- 按平均得分对所有模型-设置配置进行排名
- 识别缺失实例(为公平比较而赋予 0 分)
输出为一份全面的 JSON 报告,可用于进一步分析和可视化。
📊 数据收集
数据收集过程包含多个步骤,用于从 GitHub 收集和处理数据。该过程的主要脚本位于 src/swe_care/collect.
以下是每个步骤的命令行用法示例:
-
获取热门仓库:查找给定语言中星标最多的仓库。
python -m swe_care.collect get_top_repos \ --language "Python" \ --top-n 100 \ --output-dir "results/top_repos" \ --tokens "your_github_pat" -
获取拉取请求数据:使用 GitHub GraphQL API 从特定仓库获取 PR 数据。
python -m swe_care.collect get_graphql_prs_data \ --repo "<repo_owner>/<repo_name>" \ --output-dir "results/graphql_prs_data" \ --tokens "your_github_pat" \ --max-number 20 -
对 PR 数据进行分类通过评估提交并对审查评论进行标注,对 PR 数据进行分析和分类。
单文件处理:
python -m swe_care.collect classify_prs_data \ --graphql-prs-data-file "results/graphql_prs_data/<repo_owner>__<repo_name>_graphql_prs_data.jsonl" \ --output-dir "./results/classify_prs_data" \ --tokens "your_github_pat"批量处理(多个仓库):
python -m swe_care.collect classify_prs_data \ --graphql-prs-data-file "results/graphql_prs_data/" \ --output-dir "./results/classify_prs_data" \ --tokens "your_github_pat" \ --jobs 4该步骤结合了两项重要分析:
- 提交评估:使用启发式规则,根据质量指标(提交信息清晰度、大小、审查活动等)对提交进行评分
- 审查评论分类:根据所引用的行在合并提交中是否实际发生变更,或审查线程是否已解决、已过时或已折叠,来提取并标注审查评论。
-
构建代码审查数据集构建代码审查任务的最终数据集。此步骤需要由 LLM 对每个任务实例的元数据进行分类,例如问题领域、难度和审查工作量。
单文件处理:
# Example with OpenAI GPT-4o export OPENAI_API_KEY=<your_openai_api_key> python -m swe_care.collect build_code_review_dataset \ --graphql-prs-data-file "results/graphql_prs_data/<repo_owner>__<repo_name>_graphql_prs_data.jsonl" \ --pr-classification-file "results/classify_prs_data/<repo_owner>__<repo_name>_pr_classification.jsonl" \ --model "gpt-4o" \ --model-provider "openai" \ --model-args "temperature=0.7,top_p=0.9" \ --output-dir "./results/dataset" \ --tokens "your_github_pat" # Example with Anthropic Claude export ANTHROPIC_API_KEY=<your_anthropic_api_key> python -m swe_care.collect build_code_review_dataset \ --graphql-prs-data-file "results/graphql_prs_data/<repo_owner>__<repo_name>_graphql_prs_data.jsonl" \ --pr-classification-file "results/classify_prs_data/<repo_owner>__<repo_name>_pr_classification.jsonl" \ --model "claude-3-5-sonnet-20241022" \ --model-provider "anthropic" \ --model-args "temperature=0.5,max_tokens=4096" \ --output-dir "./results/dataset" \ --tokens "your_github_pat"批量处理(多个仓库):
export OPENAI_API_KEY=<your_openai_api_key> python -m swe_care.collect build_code_review_dataset \ --graphql-prs-data-file "results/graphql_prs_data/" \ --pr-classification-file "results/classify_prs_data/" \ --model "gpt-4o" \ --model-provider "openai" \ --model-args "temperature=0.7" \ --output-dir "./results/dataset" \ --tokens "your_github_pat" \ --jobs 4注意使用目录输入时,该工具将自动:
- 递归查找所有
*_graphql_prs_data.jsonl指定目录中的文件 - 将它们与对应的
*_pr_classification.jsonl文件 - 使用指定的作业数并发处理多个文件对
- 递归查找所有
你可以通过运行以下命令来查找每个脚本参数的更多详细信息: python -m swe_care.collect <subcommand> -h.
转换为奖励模型训练样本
这是一个额外的处理步骤,用于将 PR 分类数据转换为奖励模型训练样本,独立于主数据收集流水线。
单文件处理:
python -m swe_care.collect convert_to_rm_samples \
--graphql-prs-data-file "results/graphql_prs_data/<repo_owner>__<repo_name>_graphql_prs_data.jsonl" \
--pr-classification-file "results/classify_prs_data/<repo_owner>__<repo_name>_pr_classification.jsonl" \
--output-dir "./results/rm_samples" \
--file-source "none"
批量处理(多个仓库):
python -m swe_care.collect convert_to_rm_samples \
--graphql-prs-data-file "results/graphql_prs_data/" \
--pr-classification-file "results/classify_prs_data/" \
--output-dir "./results/rm_samples" \
--file-source "base_changed_files" \
--jobs 4
使用基于检索的文件源:
# Example with retrieved_all_files (requires --retrieval-output-dir)
python -m swe_care.collect convert_to_rm_samples \
--graphql-prs-data-file "results/graphql_prs_data/" \
--pr-classification-file "results/classify_prs_data/" \
--output-dir "./results/rm_samples" \
--file-source "retrieved_all_files" \
--retrieval-output-dir "./results/retrieval_output" \
--retrieval-max-files 10 \
--jobs 2
该步骤将分类后的 PR 数据转换为奖励模型的训练样本。每个样本包含:
- 问题陈述:使用以下内容从关闭的问题或 PR 描述中提取
extract_problem_statement工具 - 待审查补丁:来自该提交的实际代码变更(补丁)
- 正面评价:审查评论,其中所引用的行在合并提交中已被更改,且审查线程已解决
- 负面评价:所有其他审查评论
- 元数据:仓库信息、PR 编号、提交 SHA、PR URL 以及文件来源,用于可追溯性
文件来源选项
该 --file-source 参数控制文件内容如何包含在审查样本中:
none(默认):使用默认样本格式,不包含变更文件内容base_changed_files:在审查评论样本中包含基础提交与待审查提交之间补丁中变更文件的内容reviewed_file:包含变更文件内容,以匹配审查评论所适用的样本retrieved_base_changed_files:使用 BM25 根据 diff_hunk 内容从变更文件中检索相关文件retrieved_all_files:使用 BM25 根据 diff_hunk 内容从整个仓库中检索相关文件
当 --file-source 设置为除以下选项之外的任何选项时 none时,审查评论将包含一个 <code> 包含相关文件内容的部分,为训练提供更多上下文。基于检索的选项(retrieved_base_changed_files 和 retrieved_all_files)使用 BM25 相似度,根据审查评论的 diff_hunk 选择最相关的文件。
注意时,你还必须指定 来设置执行检索操作和存储临时文件的目录。 --file-source retrieved_all_files,你还必须指定 --retrieval-output-dir 用于设置执行检索操作和存储临时文件的目录。
重要:该 retrieved_all_files 文件源策略使用 Pyserini 进行 BM25 检索,这需要 Java 21。在使用此选项之前,请确保你的系统上已安装 Java 21。请参阅 Pyserini 安装指南 以了解详情。
输出文件遵循以下命名模式: <repo_owner>__<repo_name>_rm_samples.jsonl 并包含 RewardModelTrainingSample 每个训练实例都包含全面的元数据。
🔄 推理
推理模块提供两大主要功能:生成文本数据集,以及在代码审查任务上运行 LLM 推理。
1. 生成文本数据集
在运行评估之前,你可以使用不同的上下文策略从收集到的 SWE-CARE 数据中生成文本数据集。这会创建符合 LLM 评估所需格式的数据集。
# Example with no file context (using default Hugging Face dataset)
python -m swe_care.inference create_code_review_text \
--output-dir "results/code_review_text" \
--file-source "none"
# Example with local dataset file
python -m swe_care.inference create_code_review_text \
--dataset-name-or-path "results/dataset/code_review_task_instances.jsonl" \
--output-dir "results/code_review_text" \
--file-source "none"
# Example with oracle file source
python -m swe_care.inference create_code_review_text \
--dataset-name-or-path "results/dataset/code_review_task_instances.jsonl" \
--output-dir "results/code_review_text" \
--file-source "oracle" \
--tokens "your_github_pat"
# Example with BM25 retrieval
python -m swe_care.inference create_code_review_text \
--dataset-name-or-path "results/dataset/code_review_task_instances.jsonl" \
--output-dir "results/code_review_text" \
--file-source "bm25" \
--k 10 \
--retrieval-output-dir "results/retrieval_output" \
--tokens "your_github_pat" \
--jobs 4
# Example with all files
python -m swe_care.inference create_code_review_text \
--dataset-name-or-path "results/dataset/code_review_task_instances.jsonl" \
--output-dir "results/code_review_text" \
--file-source "all" \
--k 20 \
--retrieval-output-dir "results/retrieval_output" \
--tokens "your_github_pat" \
--jobs 4
文件来源策略
该 --file-source 参数支持不同的上下文文件选择策略:
- 无:无文件上下文,仅使用问题陈述和补丁
- oracle:使用真实基准文件(即在审查提交和合并提交中均实际发生变更的文件)
- :使用 BM25 检索,根据问题陈述选择相关文件(需要 和 ):使用 BM25 检索,根据问题陈述选择相关文件(需要
--k以及--retrieval-output-dir) - 全部:使用仓库中所有可用文件,直至达到指定上限(需要
--k以及--retrieval-output-dir)
文本生成的附加参数
--k:要包含的最大文件数(bm25 和 all 策略必需)--retrieval-output-dir:用于检索操作和 git 仓库的目录(bm25 及所有策略均必需)--tokens:用于 API 访问的 GitHub 个人访问令牌--jobs:用于处理的并行作业数(默认值:2)。使用 ProcessPoolExecutor 处理 bm25/all 策略,以获得更好的并行性--skip-existing:根据 instance_id 跳过输出文件中已存在的实例(默认值:False)。指定后,该工具将追加写入现有输出文件,而不是覆盖它
2. 运行 LLM 推理
生成文本数据集后,你可以使用各种 LLM API 运行推理,以生成代码审查预测。
# Example with OpenAI GPT-4o
export OPENAI_API_KEY=<your_openai_api_key>
python -m swe_care.inference run_api \
--dataset-file "results/code_review_text/dataset__oracle.jsonl" \
--model "gpt-4o" \
--model-provider "openai" \
--model-args "temperature=0.7,top_p=0.9" \
--output-dir "results/predictions" \
--jobs 4 \
--skip-existing
# Example with Anthropic Claude
export ANTHROPIC_API_KEY=<your_anthropic_api_key>
python -m swe_care.inference run_api \
--dataset-file "results/code_review_text/dataset__oracle.jsonl" \
--model "claude-3-5-sonnet-20241022" \
--model-provider "anthropic" \
--model-args "temperature=0.5,max_tokens=4096" \
--output-dir "results/predictions" \
--jobs 2
# Example with DeepSeek
export OPENAI_API_KEY=<your_deepseek_api_key>
python -m swe_care.inference run_api \
--dataset-file "results/code_review_text/dataset__oracle.jsonl" \
--model "deepseek-chat" \
--model-provider "deepseek" \
--output-dir "results/predictions" \
--jobs 1
支持的模型提供商
请参阅 python -m swe_care.inference run_api --help 支持的模型提供商和模型。
如果你使用的是除已提供的 API 提供商之外的其他 API 提供商,你可以使用 export OPENAI_BASE_URL=<your_openai_base_url> 或 export ANTHROPIC_BASE_URL=<your_anthropic_base_url> 以指定 API 的基础 URL。
LLM 推理参数
--dataset-file:文本数据集文件的路径(CodeReviewInferenceInstance 对象)--model:用于推理的模型名称--model-provider:模型提供商(openai、anthropic、deepseek、qwen、moonshot、gemini)--model-args:逗号分隔的模型参数(例如,temperature=0.7,top_p=0.9)--output-dir:保存生成预测的目录--jobs:用于推理的并行线程数(默认值:2)--skip-existing:跳过已有预测的实例(标志,默认值:False)
生成的预测结果将保存为 JSONL 文件,其中包含 CodeReviewPrediction 对象,随后可用于评估。
你可以通过运行以下命令来查看每个脚本各参数的更多详细信息: python -m swe_care.inference <subcommand> -h.
🚀 评估
评估框架用于评估模型在代码审查任务上的预测结果。主脚本为 src/swe_care/harness/code_review_eval.py.
支持的评估器
- LLM 评估器 (
llm_evaluator):基于多个维度(功能性、质量、风格、文档)评估代码审查质量。 - 基于规则的评估器 (
rule_based_evaluator):从审查文本中提取缺陷,并与参考缺陷进行比较。
注意论文中提到的奖励模型评估器可在 Huggingface.
评估支持的模型提供商
请参阅 python -m swe_care.harness code_review_eval --help 支持的评估器以及 LLM 模型(如果你想使用基于 LLM 的评估)。
示例
基于 LLM 的 OpenAI 评估
export OPENAI_API_KEY=<your_openai_api_key>
python -m swe_care.harness code_review_eval \
--dataset-name-or-path "results/code_review_task_instances.jsonl" \
--predictions-path "results/predictions/dataset__gpt-4o.jsonl" \
--output-dir "./results/evaluation" \
--evaluator "llm_evaluator" \
--model "gpt-4o" \
--model-provider "openai" \
--model-args "temperature=0.0" \
--jobs 4
# Or use default Hugging Face dataset
python -m swe_care.harness code_review_eval \
--predictions-path "results/predictions/dataset__gpt-4o.jsonl" \
--output-dir "./results/evaluation" \
--evaluator "llm_evaluator" \
--model "gpt-4o" \
--model-provider "openai" \
--model-args "temperature=0.0" \
--jobs 4
基于规则的评估
python -m swe_care.harness code_review_eval \
--dataset-name-or-path "results/code_review_task_instances.jsonl" \
--predictions-path "results/predictions/dataset__gpt-4o.jsonl" \
--output-dir "./results/evaluation" \
--evaluator "rule_based_evaluator" \
--jobs 4
多个评估器
export OPENAI_API_KEY=<your_openai_api_key>
python -m swe_care.harness code_review_eval \
--dataset-name-or-path "results/code_review_task_instances.jsonl" \
--predictions-path "results/predictions/dataset__gpt-4o.jsonl" \
--output-dir "./results/evaluation" \
--evaluator "llm_evaluator" "rule_based_evaluator" \
--model "gpt-4o" \
--model-provider "openai" \
--jobs 4
参数
--dataset-name-or-path:原始数据集文件的路径或 Hugging Face 数据集名称(默认:inclusionAI/SWE-CARE)--predictions-path:预测文件的路径(CodeReviewPrediction 对象)--output-dir:保存评估结果的目录--evaluator:要使用的一个或多个评估器类型(llm_evaluator,rule_based_evaluator)--model:用于 LLM 评估的模型名称(使用基于 LLM 的评估器时必填)--model-provider:用于 LLM 评估的模型提供商(使用基于 LLM 的评估器时必填)--model-args:用于 LLM 评估的逗号分隔模型参数--evaluator-args:评估器专用参数,格式为evaluator1:arg1=value1,arg2=value2;evaluator2:arg1=value1--jobs:用于评估的并行线程数(默认值:2)--skip-existing:跳过输出文件中已有评估结果的实例(标志,默认值:False)。指定后,该工具将追加到现有输出文件,而不是覆盖它
输出
评估结果保存为 JSONL 文件(<predictions_path.stem>_report.jsonl),其中包含每个实例的详细指标。你可以通过运行以下命令来查看每个脚本各参数的更多详细信息:。📜 引用如果你觉得我们的工作有帮助,请使用以下引用:关于未提供描述、网站或主题。资源ReadmeApache-2.0 许可证活动自定义属性星标18 个星标关注0 个关注分支13 个分支报告仓库发布包使用者贡献者语言 CodeReviewEvaluationResult 包含每个实例的详细指标。
你可以通过运行以下命令来查看每个脚本各参数的更多详细信息: python -m swe_care.harness <subcommand> -h.
📜 引用
如果您觉得我们的工作有帮助,请使用以下引用:
@misc{guo2025codefusecrbenchcomprehensivenessawarebenchmarkendtoend,
title={CodeFuse-CR-Bench: A Comprehensiveness-aware Benchmark for End-to-End Code Review Evaluation in Python Projects},
author={Hanyang Guo and Xunjin Zheng and Zihan Liao and Hang Yu and Peng DI and Ziyin Zhang and Hong-Ning Dai},
year={2025},
eprint={2509.14856},
archivePrefix={arXiv},
primaryClass={cs.SE},
url={https://arxiv.org/abs/2509.14856},
}
AI 热点