for answer, errors in zip(one["answer"], one["error"])://遍历answer和error,每一个正确代码可能有多个错误代码对应 for error in errors:
error_code = error["code"]//错误代码具体 row, col = error["pos"]//错误位置 error_desc = error["desc"]//错误类型
# 注意有给 leetcode 加的保险 //这里的conversation相当于我一次对话,input为输入,output为gpt的结果 conversation = { "instruction": f"Given a problem description, incorrect code, the location of the error within the code, and a description of what's wrong, I need you to identify the mistake and provide the corrected code. Problem Description:\n{description}", "input": f"Erroneous Code:\n{error_code}\n{fmain}\n\nError Location (row, column):\n({row}, {col})\n\nError Description:\n{error_desc}", "output": f"{answer}\n{fmain}", }
"instruction": f"Given a problem description, incorrect code, the location of the error within the code, and a description of what's wrong, I need you to identify the mistake and provide the corrected code. Problem Description:\n{description}", "input": f"Erroneous Code:\n{error_code}\n{fmain}\n\nError Location (row, column):\n({row}, {col})\n\nError Description:\n{error_desc}", "output": f"{answer}\n{fmain}", }
{ "id": "conv_id-1-1", "conversations": [ { "from": "human", "value": "Could you provide a C++ program template with a class named 'Solution' and a 'main' function? The problem it should address is described as follows:\n问题描述.\nPlease ensure to include the directive '#include <bits/stdc++.h>' and 'using namespace std;'." }, { "from": "gpt", "value": "Sure, here is a cpp program for this problem:\n错误代码\nint main() {}" }, { "from": "human", "value": "There is a bug in 5:10, bug description is '某某错误', please fix it." }, { "from": "gpt", "value": "Sorry, here is the fixed code:\n修复后的正确代码\nint main() {}" } ] }
id
由三个部分组成:f"{conv_id}-{idx + 1}-{sub_idx + 1}"。
conv_id: 来自 one["id"],通常表示问题或任务的编号。
idx + 1: 当前循环中的正确答案的编号。
sub_idx + 1: 当前循环中的错误代码的编号。
conversations
包含四次发言,人类和gpt各两次
第一段对话 (from: "human"):
发言内容请求 GPT 提供一个 C++ 程序模板,包含一个名为 Solution 的类和 main 函数。
模板的作用是解决问题 description 中描述的任务。
还要求包含 #include <bits/stdc++.h> 和 using namespace std; 这些标准的 C++ 导入语句。
{ "from": "human", "value": f"Could you provide a C++ program template with a class named 'Solution' and a 'main' function? The problem it should address is described as follows:\n{description}.\nPlease ensure to include the directive '#include <bits/stdc++.h>' and 'using namespace std;'." }
第二段对话 (from: "gpt"):
GPT 提供了一段包含错误的 C++ 代码,代码内容是 error_code,并包含 fmain,即 int main() {} 这段代码作为函数定义。
也就是说这里GPT一开始提供的是错误代码
{ "from": "gpt", "value": f"Sure, here is a cpp program for this problem:\n{error_code}\n{fmain}" }
def all(json_file_name: str, json_dir_name: str, transform): logger.info("Link start for {}".format(json_file_name))
intermediate_file_name = os.path.join(json_dir_name, "mid.json")# 存储中间结果 result_file_name = os.path.join(json_dir_name, "res.json")# 存储最终结果 with open(json_file_name, "r") as f: # list type data = json.load(f)# 加载json文件 one_to_remove = []
# mutate each element for idx, one in enumerate(data): mutate(one)
# 说明一个变异体都没成功生成 if len(one["answer"]) == 0: one_to_remove.append(idx)
# 在 data 中删除无效的 one for idx in sorted(one_to_remove, reverse=True): logger.info("Removing invalid problem {}".format(data[idx]["id"])) data.pop(idx)
# conversations convs = [] # transform into conversation,具体可以为one或者multi for one in data: convs.extend(transform(one))
# write intermediate result into mid.json,变异的中间结果作为mid.json with open(intermediate_file_name, "w") as r: json.dump(data, r, indent=4)
# write final result into res.json ,最后的对话作为res.json with open(result_file_name, "w") as r: json.dump(convs, r, indent=4)
main函数
main 函数控制整个脚本的运行逻辑,设置了日志配置,并根据命令行参数执行主流程
if __name__ == "__main__": if len(sys.argv) != 3: exit(1) json_file_name = sys.argv[1]# 文件名,如origin.json json_dir_name = os.path.dirname(json_file_name)# 文件夹路径