专业的代码修复能力评估工具,基于HumanEvalPack数据集,支持API模型和本地模型集成。
cd /mnt/afs/zhangyingying/HumanEvalFix && chmod +x run_api_evaluation.sh && ./run_api_evaluation.sh --samples 164 && echo "任务完成,结果保存在 /mnt/afs/zhangyingying/HumanEvalFix/results/"
使用自定义API进行代码修复评估:
# 基本用法 - 使用默认API配置
./run_api_evaluation.sh
# 快速测试(5个样本)
./run_api_evaluation.sh --samples 5
# 完整评估(164个样本)
./run_api_evaluation.sh --samples 164
# 测试其他编程语言
./run_api_evaluation.sh --language javascript --samples 10
./run_api_evaluation.sh --language java --samples 20如果有本地模型:
# 基本用法 - 评估Python代码修复能力
./run_evaluation.sh /path/to/your/model
# 只测试前10个样本(快速验证)
./run_evaluation.sh /path/to/your/model -s 10
# 测试其他编程语言
./run_evaluation.sh /path/to/your/model -l js # JavaScript
./run_evaluation.sh /path/to/your/model -l java # Java
./run_evaluation.sh /path/to/your/model -l cpp # C++./run_api_evaluation.sh --help # API模型帮助
./run_evaluation.sh --help # 本地模型帮助# 安装依赖
pip install datasets huggingface_hub pandas numpy requests
# 创建结果目录
mkdir -p results项目已预配置使用自定义API,无需额外设置:
- API地址:
https://api.apiyi.com/v1/responses - 模型:
gpt-4o-mini - API密钥: 已内置配置
如需修改API配置,编辑 api_model.py 中的 CustomAPIModel 类。
编辑 model_integration.py 中的 CustomModel 类,实现我们们的模型接口:
class CustomModel(BaseCodeFixModel):
def load_model(self) -> bool:
"""加载我们的模型"""
# TODO: 替换为你们的模型加载代码
from your_model_package import YourModel, YourTokenizer
self.tokenizer = YourTokenizer.from_pretrained(self.model_path)
self.model = YourModel.from_pretrained(self.model_path)
return True
def fix_code(self, buggy_code: str, instruction: str, bug_type: str) -> str:
"""使用我们的模型修复代码"""
# TODO: 替换为你们的代码修复逻辑
prompt = self._build_prompt(buggy_code, instruction, bug_type)
inputs = self.tokenizer.encode(prompt, return_tensors="pt")
outputs = self.model.generate(inputs, max_length=512, temperature=0.1)
fixed_code = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
return self._extract_code(fixed_code)# 完整评估(164个样本)
./run_evaluation.sh /path/to/your/model
# 快速测试(10个样本)
./run_evaluation.sh /path/to/your/model -s 10
# 指定输出目录
./run_evaluation.sh /path/to/your/model -o my_results评估完成后会生成两个文件:
{language}_evaluation_results_{timestamp}.json- 完整结果{language}_summary_{timestamp}.json- 统计摘要
使用自定义API的实际测试结果:
$ ./run_api_evaluation.sh --samples 5
🎯 总体成功率: 100.0%
📊 各类型Bug修复情况:
missing logic: 1/1 (100.0%)
operator misuse: 2/2 (100.0%)
excess logic: 1/1 (100.0%)
variable misuse: 1/1 (100.0%){
"task_id": "Python/0",
"bug_type": "missing logic",
"failure_symptoms": "incorrect output",
"buggy_code": " for idx, elem in enumerate(numbers):\n for idx2, elem2 in enumerate(numbers):\n if idx != idx2:\n distance = elem - elem2\n if distance < threshold:\n return True\n\n return False",
"instruction": "Check if in given list of numbers, are any two numbers closer to each other than given threshold."
}{
"task_id": "Python/0",
"bug_type": "missing logic",
"fix_successful": true,
"test_passed": true,
"fixed_code": " for idx, elem in enumerate(numbers):\n for idx2, elem2 in enumerate(numbers):\n if idx != idx2:\n distance = abs(elem - elem2) # 修复:添加abs()函数\n if distance < threshold:\n return True\n\n return False"
}{
"model": "API-gpt-4o-mini",
"language": "python",
"summary": {
"total_samples": 5,
"successful_fixes": 5,
"failed_fixes": 0,
"success_rate_percent": 100.0
},
"bug_type_performance": {
"missing logic": "1/1 (100.0%)",
"operator misuse": "2/2 (100.0%)",
"excess logic": "1/1 (100.0%)",
"variable misuse": "1/1 (100.0%)"
}
}| 语言 | 标识符 | 样本数量 |
|---|---|---|
| Python | python |
164 |
| JavaScript | js |
164 |
| Java | java |
164 |
| Go | go |
164 |
| C++ | cpp |
164 |
| Rust | rust |
164 |
| Bug类型 | 描述 | 示例 |
|---|---|---|
missing logic |
缺失逻辑 | 缺少abs()函数、缺少边界检查 |
excess logic |
多余逻辑 | 多余的条件判断、不必要的操作 |
operator misuse |
操作符误用 | >= vs >, == vs = |
variable misuse |
变量误用 | 错误的变量名、作用域问题 |
value misuse |
值误用 | 错误的常量、索引越界 |
function misuse |
函数误用 | 错误的函数调用、参数错误 |
humanevalfix/
├── README.md # 本文档
├── run_api_evaluation.sh # API模型评估脚本(推荐)
├── run_evaluation.sh # 本地模型评估脚本
├── api_model.py # API模型接口
├── api_config.py # API配置管理
├── model_integration.py # 模型集成接口
├── analyze_results.py # 结果分析脚本
├── requirements.txt # 依赖列表
└── results/ # 评估结果
├── python_evaluation_results_*.json
└── python_summary_*.json
python3 model_integration.py \
--model-path /path/to/your/model \
--language python \
--samples 50 \
--output-dir custom_resultspython3 analyze_results.py results/python_evaluation_results_*.jsonfor lang in python js java; do
./run_evaluation.sh /path/to/your/model -l $lang -s 20 -o results_$lang
done- 总体成功率: 成功修复的样本数 / 需要修复的样本数
- 按类型成功率: 每种bug类型的修复成功率
- 测试通过率: 修复后代码通过单元测试的比例
fix_successful: 是否成功修复(buggy代码测试失败 + 修复后代码测试通过)test_passed: 修复后的代码是否通过测试buggy_pass: 原始buggy代码是否意外通过测试canonical_pass: 标准答案是否通过测试(用于验证测试用例正确性)