Views
No views yet
transformers.transformers
torchvision
torchaudio
tensorboardSystem Prompt:
You are a code editor. You will be provided the original code snippet and an instruction that specifies the changes you need to make. You will produce the changed code, based on the original code and the instruction given. Only produce the code, do not include any additional prose.
User Prompt:
## Code Before:
{pre_edit_code}
## Instruction:
{instruction}
## Code After:1import re
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4def extract_first_python_block(text: str) -> str:
5 pattern = r"```python\s*(.*?)```"
6 match = re.search(pattern, text, re.DOTALL)
7 if match:
8 return match.group(1).strip()
9 return ""
10
11model_name ="zkzhang88/OpenCodeEdit-DSC-6.7B" #"zkzhang88/OpenCodeEdit-Qwen3-8B" "zkzhang88/OpenCodeEdit-Qwen2.5-7B" "zkzhang88/OpenCodeEdit-DSC-6.7B"
12
13tokenizer = AutoTokenizer.from_pretrained(model_name)
14model = AutoModelForCausalLM.from_pretrained(
15 model_name,
16 torch_dtype="auto",
17 device_map="auto"
18)
19
20pre_edit_code = """
21def fibonacci(n):
22 if n <= 1:
23 return n
24 return fibonacci(n-1) + fibonacci(n-2)
25"""
26SYSTEM_PROMPT = "You are a code editor. You will be provided the original code snippet and an instruction that specifies the changes you need to make. You will produce the changed code, based on the original code and the instruction given. Only produce the code, do not include any additional prose."
27instruction = "Optimize the calculation method for the Fibonacci sequence by reducing recursive calls and employing dynamic programming to enhance efficiency."
28
29formatted_input = f"""
30## Code Before:
31{pre_edit_code}
32## Instruction:
33{instruction}
34## Code After:
35"""
36
37messages = [
38 {"role": "system", "content": SYSTEM_PROMPT},
39 {"role": "user", "content": formatted_input}
40]
41
42text = tokenizer.apply_chat_template(
43 messages,
44 tokenize=False,
45 add_generation_prompt=True
46)
47
48model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
49
50generated_ids = model.generate(
51 **model_inputs,
52 max_new_tokens=512
53)
54
55
56output_ids = generated_ids[0][len(model_inputs.input_ids[0]):]
57content = tokenizer.decode(output_ids, skip_special_tokens=True).strip("\n")
58
59print(extract_first_python_block(content))@misc{zhang2025generatinghighqualitydatasetscode,
title={Generating High-Quality Datasets for Code Editing via Open-Source Language Models},
author={Zekai Zhang and Mingwei Liu and Zhenxi Chen and Linxi Liang and Yuxuan Chen and Guangsheng Ou and Yanlin Wang and Dan Li and Xin Peng and Zibin Zheng},
year={2025},
eprint={2509.25203},
archivePrefix={arXiv},
primaryClass={cs.SE},
url={https://arxiv.org/abs/2509.25203},
}