| 1 | # AutoCodeRover: Autonomous Program Improvement |
| 2 | |
| 3 |  |
| 4 | |
| 5 | <p align="center"> |
| 6 | <a href="https://arxiv.org/abs/2404.05427"><strong>ArXiv Paper</strong></a> |
| 7 | </p> |
| 8 | |
| 9 | |
| 10 | ## ๐ฃ Updates |
| 11 | |
| 12 | - [April 29, 2024] Added support for Claude and Llama models. Find the list of supported models [here](#using-a-different-model)! Support for more models coming soon. |
| 13 | - [April 19, 2024] AutoCodeRover now supports running on [GitHub issues](#github-issue-mode-set-up-and-run-on-new-github-issues) and [local issues](#local-issue-mode-set-up-and-run-on-local-repositories-and-local-issues)! Feel free to try it out and we welcome your feedback! |
| 14 | |
| 15 | ## ๐ Overview |
| 16 | |
| 17 | AutoCodeRover is a fully automated approach for resolving GitHub issues (bug fixing and feature addition) where LLMs are combined with analysis and debugging capabilities to prioritize patch locations ultimately leading to a patch. |
| 18 | |
| 19 | AutoCodeRover resolves ~**16%** of issues of [SWE-bench](https://www.swebench.com) (total 2294 GitHub issues) and ~**22%** issues of [SWE-bench lite](https://www.swebench.com/lite.html) (total 300 GitHub issues), improving over the current state-of-the-art efficacy of AI software engineers. |
| 20 | |
| 21 | <p align="center"> |
| 22 | <img src=https://github.com/nus-apr/auto-code-rover/assets/48704330/e10c3270-442c-4673-8656-735c892dfb66 width=500/> |
| 23 | </p> |
| 24 | |
| 25 | AutoCodeRover works in two stages: |
| 26 | |
| 27 | - ๐ Context retrieval: The LLM is provided with code search APIs to navigate the codebase and collect relevant context. |
| 28 | - ๐ Patch generation: The LLM tries to write a patch, based on retrieved context. |
| 29 | |
| 30 | ### โจ Highlights |
| 31 | |
| 32 | AutoCodeRover has two unique features: |
| 33 | |
| 34 | - Code search APIs are *Program Structure Aware*. Instead of searching over files by plain string matching, AutoCodeRover searches for relevant code context (methods/classes) in the abstract syntax tree. |
| 35 | - When a test suite is available, AutoCodeRover can take advantage of test cases to achieve an even higher repair rate, by performing *statistical fault localization*. |
| 36 | |
| 37 | ## ๐ arXiv Paper |
| 38 | ### AutoCodeRover: Autonomous Program Improvement [[arXiv 2404.05427]](https://arxiv.org/abs/2404.05427) |
| 39 | |
| 40 | <p align="center"> |
| 41 | <a href="https://arxiv.org/abs/2404.05427"> |
| 42 | <img src="https://github.com/nus-apr/auto-code-rover/assets/48704330/c6422951-a6e8-4494-9403-b5ada3d9ee7d" alt="First page of arXiv paper" width="570"> |
| 43 | </a> |
| 44 | </p> |
| 45 | |
| 46 | For referring to our work, please cite and mention: |
| 47 | ``` |
| 48 | @misc{zhang2024autocoderover, |
| 49 | title={AutoCodeRover: Autonomous Program Improvement}, |
| 50 | author={Yuntong Zhang and Haifeng Ruan and Zhiyu Fan and Abhik Roychoudhury}, |
| 51 | year={2024}, |
| 52 | eprint={2404.05427}, |
| 53 | archivePrefix={arXiv}, |
| 54 | primaryClass={cs.SE} |
| 55 | } |
| 56 | ``` |
| 57 | |
| 58 | ## โ๏ธ Example: Django Issue #32347 |
| 59 | |
| 60 | As an example, AutoCodeRover successfully fixed issue [#32347](https://code.djangoproject.com/ticket/32347) of Django. See the demo video for the full process: |
| 61 | |
| 62 | https://github.com/nus-apr/auto-code-rover/assets/48704330/719c7a56-40b8-4f3d-a90e-0069e37baad3 |
| 63 | |
| 64 | ### Enhancement: leveraging test cases |
| 65 | |
| 66 | AutoCodeRover can resolve even more issues, if test cases are available. See an example in the video: |
| 67 | |
| 68 | https://github.com/nus-apr/auto-code-rover/assets/48704330/26c9d5d4-04e0-4b98-be55-61c1d10a36e5 |
| 69 | |
| 70 | ## ๐ Setup & Running |
| 71 | |
| 72 | ### Setup API key and environment |
| 73 | |
| 74 | We recommend running AutoCodeRover in a Docker container. |
| 75 | |
| 76 | Set the `OPENAI_KEY` env var to your [OpenAI key](https://help.openai.com/en/articles/4936850-where-do-i-find-my-openai-api-key): |
| 77 | |
| 78 | ``` |
| 79 | export OPENAI_KEY=sk-YOUR-OPENAI-API-KEY-HERE |
| 80 | ``` |
| 81 | (Alternatively, if you want to use Anthropic models instead, set `ANTHROPIC_API_KEY`.) |
| 82 | |
| 83 | Build and start the docker image: |
| 84 | |
| 85 | ``` |
| 86 | docker build -f Dockerfile -t acr . |
| 87 | docker run -it -e OPENAI_KEY="${OPENAI_KEY:-OPENAI_API_KEY}" -p 3000:3000 -p 5000:5000 acr |
| 88 | ``` |
| 89 | |
| 90 | Alternatively, you can use `Dockerfile.scratch` which supports arm64 (Apple silicon) and ppc in addition to amd64. |
| 91 | `Dockerfile.scratch` will build both SWE-bench (from https://github.com/yuntongzhang/SWE-bench.git) and ACR. |
| 92 | |
| 93 | ``` |
| 94 | docker build -f Dockerfile.scratch -t acr . |
| 95 | ``` |
| 96 | |
| 97 | There are build args for customizing the build in `Dockerfile.scratch` like this: |
| 98 | |
| 99 | ``` |
| 100 | docker build --build-arg GIT_EMAIL=your@email.com --build-arg GIT_NAME=your_id \ |
| 101 | --build-arg SWE_BENCH_REPO=https://github.com/your_id/SWE-bench.git \ |
| 102 | -f Dockerfile.scratch -t acr . |
| 103 | ``` |
| 104 | |
| 105 | After setting up, we can run ACR in three modes: |
| 106 | |
| 107 | 1. GitHub issue mode: Run ACR on a live GitHub issue by providing a link to the issue page. |
| 108 | 2. Local issue mode: Run ACR on a local repository and a file containing the issue description. |
| 109 | 3. SWE-bench mode: Run ACR on SWE-bench task instances. |
| 110 | |
| 111 | ### [GitHub issue mode] Set up and run on new GitHub issues |
| 112 | |
| 113 | If you want to use AutoCodeRover for new GitHub issues in a project, prepare the following: |
| 114 | |
| 115 | - Link to clone the project (used for `git clone ...`). |
| 116 | - Commit hash of the project version for AutoCodeRover to work on (used for `git checkout ...`). |
| 117 | - Link to the GitHub issue page. |
| 118 | |
| 119 | Then, in the docker container (or your local copy of AutoCodeRover), run the following commands to set up the target project |
| 120 | and generate patch: |
| 121 | |
| 122 | ``` |
| 123 | cd /opt/auto-code-rover |
| 124 | conda activate auto-code-rover |
| 125 | PYTHONPATH=. python app/main.py github-issue --output-dir output --setup-dir setup --model gpt-4-0125-preview --model-temperature 0.2 --task-id <task id> --clone-link <link for cloning the project> --commit-hash <any version that has the issue> --issue-link <link to issue page> |
| 126 | ``` |
| 127 | Here is an example command for running ACR on an issue from the langchain GitHub issue tracker: |
| 128 | |
| 129 | ``` |
| 130 | PYTHONPATH=. python app/main.py github-issue --output-dir output --setup-dir setup --model gpt-4-0125-preview --model-temperature 0.2 --task-id langchain-20453 --clone-link https://github.com/langchain-ai/langchain.git --commit-hash cb6e5e5 --issue-link https://github.com/langchain-ai/langchain/issues/20453 |
| 131 | ``` |
| 132 | |
| 133 | The `<task id>` can be any string used to identify this issue. |
| 134 | |
| 135 | If patch generation is successful, the path to the generated patch will be printed in the end. |
| 136 | |
| 137 | Web UI is also provided for visualization of the issue fixing process. |
| 138 | In the docker shell, run the following command: |
| 139 | |
| 140 | ```bash |
| 141 | cd /opt/auto-code-rover/demo_vis/ |
| 142 | bash run.sh |
| 143 | ``` |
| 144 | |
| 145 | then open the url `localhost:3000` in the web explorer. |
| 146 | |
| 147 | |
| 148 | ### [Local issue mode] Set up and run on local repositories and local issues |
| 149 | |
| 150 | Instead of cloning a remote project and run ACR on an online issue, you can also prepare the local repository and issue beforehand, |
| 151 | if that suits the use case. |
| 152 | |
| 153 | For running ACR on a local issue and local codebase, prepare a local codebase and write an issue description into a file, |
| 154 | and run the following commands: |
| 155 | |
| 156 | ``` |
| 157 | cd /opt/auto-code-rover |
| 158 | conda activate auto-code-rover |
| 159 | PYTHONPATH=. python app/main.py local-issue --output-dir output --model gpt-4-0125-preview --model-temperature 0.2 --task-id <task id> --local-repo <path to the local project repository> --issue-file <path to the file containing issue description> |
| 160 | ``` |
| 161 | |
| 162 | If patch generation is successful, the path to the generated patch will be printed in the end. |
| 163 | |
| 164 | |
| 165 | ### [SWE-bench mode] Set up and run on SWE-bench tasks |
| 166 | |
| 167 | This mode is for running ACR on existing issue tasks contained in SWE-bench. |
| 168 | |
| 169 | #### Set up |
| 170 | |
| 171 | In the docker container, we need to first set up the tasks to run in SWE-bench (e.g., `django__django-11133`). The list of all tasks can be found in [`conf/swe_lite_tasks.txt`](conf/swe_lite_tasks.txt). |
| 172 | |
| 173 | The tasks need to be put in a file, one per line: |
| 174 | |
| 175 | ``` |
| 176 | cd /opt/SWE-bench |
| 177 | echo django__django-11133 > tasks.txt |
| 178 | ``` |
| 179 | |
| 180 | Or if running on arm64 (e.g. Apple silicon), try this one which doesn't depend on Python 3.6 (which isn't supported in this env): |
| 181 | |
| 182 | ``` |
| 183 | echo django__django-16041 > tasks.txt |
| 184 | ``` |
| 185 | |
| 186 | Then, set up these tasks by running: |
| 187 | |
| 188 | ``` |
| 189 | cd /opt/SWE-bench |
| 190 | conda activate swe-bench |
| 191 | python harness/run_setup.py --log_dir logs --testbed testbed --result_dir setup_result --subset_file tasks.txt |
| 192 | ``` |
| 193 | |
| 194 | Once the setup for this task is completed, the following two lines will be printed: |
| 195 | |
| 196 | ``` |
| 197 | setup_map is saved to setup_result/setup_map.json |
| 198 | tasks_map is saved to setup_result/tasks_map.json |
| 199 | ``` |
| 200 | |
| 201 | The `testbed` directory will now contain the cloned source code of the target project. |
| 202 | A conda environment will also be created for this task instance. |
| 203 | |
| 204 | _If you want to set up multiple tasks together, put their ids in `tasks.txt` and follow the same steps._ |
| 205 | |
| 206 | #### Run a single task in SWE-bench |
| 207 | |
| 208 | Before running the task (`django__django-11133` here), make sure it has been set up as mentioned [above](#set-up-one-or-more-tasks-in-swe-bench). |
| 209 | |
| 210 | ``` |
| 211 | cd /opt/auto-code-rover |
| 212 | conda activate auto-code-rover |
| 213 | PYTHONPATH=. python app/main.py swe-bench --model gpt-4-0125-preview --setup-map ../SWE-bench/setup_result/setup_map.json --tasks-map ../SWE-bench/setup_result/tasks_map.json --output-dir output --task django__django-11133 |
| 214 | ``` |
| 215 | |
| 216 | The output of the run can then be found in `output/`. For example, the patch generated for `django__django-11133` can be found at a location like this: `output/applicable_patch/django__django-11133_yyyy-MM-dd_HH-mm-ss/extracted_patch_1.diff` (the date-time field in the directory name will be different depending on when the experiment was run). |
| 217 | |
| 218 | #### Run multiple tasks in SWE-bench |
| 219 | |
| 220 | First, put the id's of all tasks to run in a file, one per line. Suppose this file is `tasks.txt`, the tasks can be run with |
| 221 | |
| 222 | ``` |
| 223 | cd /opt/auto-code-rover |
| 224 | conda activate auto-code-rover |
| 225 | PYTHONPATH=. python app/main.py swe-bench --model gpt-4-0125-preview --setup-map ../SWE-bench/setup_result/setup_map.json --tasks-map ../SWE-bench/setup_result/tasks_map.json --output-dir output --task-list-file /opt/SWE-bench/tasks.txt |
| 226 | ``` |
| 227 | |
| 228 | **NOTE**: make sure that the tasks in `tasks.txt` have all been set up in SWE-bench. See the steps [above](#set-up-one-or-more-tasks-in-swe-bench). |
| 229 | |
| 230 | #### Using a config file |
| 231 | |
| 232 | Alternatively, a config file can be used to specify all parameters and tasks to run. See `conf/vanilla-lite.conf` for an example. |
| 233 | Also see [EXPERIMENT.md](EXPERIMENT.md) for the details of the items in a conf file. |
| 234 | A config file can be used by: |
| 235 | |
| 236 | ``` |
| 237 | python scripts/run.py conf/vanilla-lite.conf |
| 238 | ``` |
| 239 | |
| 240 | ### Using a different model |
| 241 | |
| 242 | AutoCodeRover works with different foundation models. You can set the foundation model to be used with the `--model` command line argument. |
| 243 | |
| 244 | The current list of supported models: |
| 245 | |
| 246 | | | Model | AutoCodeRover cmd line argument | |
| 247 | |:--------------:|---------------|--------------| |
| 248 | | OpenAI | gpt-4-turbo-2024-04-09 | --model gpt-4-turbo-2024-04-09 | |
| 249 | | | gpt-4-0125-preview | --model gpt-4-0125-preview | |
| 250 | | | gpt-4-1106-preview | --model gpt-4-1106-preview | |
| 251 | | | gpt-3.5-turbo-0125 | --model gpt-3.5-turbo-0125 | |
| 252 | | | gpt-3.5-turbo-1106 | --model gpt-3.5-turbo-1106 | |
| 253 | | | gpt-3.5-turbo-16k-0613 | --model gpt-3.5-turbo-16k-0613 | |
| 254 | | | gpt-3.5-turbo-0613 | --model gpt-3.5-turbo-0613 | |
| 255 | | | gpt-4-0613 | --model gpt-4-0613 | |
| 256 | | Anthropic | Claude 3 Opus | --model claude-3-opus-20240229 | |
| 257 | | | Claude 3 Sonnet | --model claude-3-sonnet-20240229 | |
| 258 | | | Claude 3 Haiku | --model claude-3-haiku-20240307 | |
| 259 | | Meta | Llama 3 70B | --model llama3:70b | |
| 260 | | | Llama 3 8B | --model llama3 | |
| 261 | |
| 262 | Before using the llama models, please [install ollama](https://ollama.com/download/linux) and download the corresponding models with ollama (e.g. `ollama pull llama3`). |
| 263 | |
| 264 | ## Experiment Replication |
| 265 | |
| 266 | Please refer to [EXPERIMENT.md](EXPERIMENT.md) for information on experiment replication. |
| 267 | |
| 268 | ## โ๏ธ Contacts |
| 269 | |
| 270 | For any queries, you are welcome to open an issue. |
| 271 | |
| 272 | Alternatively, contact us at: {[yuntong](https://yuntongzhang.github.io/),[hruan](https://www.linkedin.com/in/haifeng-ruan-701a731a4/),[zhiyufan](https://zhiyufan.github.io/)}@comp.nus.edu.sg. |
| 273 | |
| 274 | ## Acknowledgements |
| 275 | |
| 276 | This work was partially supported by a Singapore Ministry of Education (MoE) Tier 3 grant "Automated Program Repair", MOE-MOET32021-0001. |