# AutoPR

Fix issues with AI-generated pull requests, powered by ChatGPT

<table align="center">
  <tr>
    <td>
      <video src="https://user-images.githubusercontent.com/24586651/235325137-b4283565-f759-48f8-9e8b-39df144e0eb7.mov">
    </td>
  </tr>
</table>


## Usage

1、把 AutoPR 设置到项目中

创建 .github/workflows/autopr.yml, 模板:https://github.com/irgolic/AutoPR-template/blob/main/.github/workflows/autopr.yml

里面设置一下openai key

2、创建一个label,名称 AutoPR

3、new 一个 issue,包含清晰的问题和步骤描述。

4、增加 AutoPR 标签到这个 issue

自动触发 action,创建 autopr/issue-#  分支,并发起 pull request

5、人工 Review 代码,合并代码

## 源码分析

采用 github action 方式自动化pull request。 触发条件:

```
on:
  issues:
    types: [labeled]
   
```

标签为 AutoPR,则继续执行,否则exit:

```
    curl -s -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github+json" \
          "https://api.github.com/repos/${{ github.repository }}/collaborators/${{ github.event.sender.login }}" | jq -r '.message'
```

继续执行,采用的是 docker 容器方式:

```
docker://ghcr.io/irgolic/autopr:latest
```

执行 :

```
python -m autopr.gh_actions_entrypoint
```

执行 main方法,先切换到 autopr/issue-# 分支, commit,然后 push,发起pull  request

```
    main(
        repo_path=repo_path,
        event=event,
        commit_service=commit_service,
        publish_service=publish_service,
        settings=settings,
    )
```

获取提取词:

```
    prompt = rail.get_prompt_message()


    def get_string_params(self) -> dict[str, str]:
        return {
            'issue': str(self.issue),
            'filepaths_with_token_lengths': '\n'.join([
                file_descriptor.filepaths_with_token_lengths_to_str()
                for file_descriptor in self.file_descriptors
            ]),
            'token_limit': str(self.token_limit),
        }
```
可以看到提取词是 issue ,其中调用 openai 接口参数如下:

```
        messages = [
            {"role": "system", "content": system_prompt},
        ]
        for example in examples:
            messages.append({"role": "user", "content": example[0]})
            messages.append({"role": "assistant", "content": example[1]})
        messages.append({"role": "user", "content": prompt})

        openai_response = openai.ChatCompletion.create(
            model=self.model,
            messages=messages,
            temperature=temperature,
            max_tokens=max_tokens,
        )

```

先循环遍历 examples ,然后再提问, 下面是 system_prompt 系统 example 提取词:

```
        raw_system_prompt: str = 'You are a software developer and git nerd, a helpful planning and coding assistant.',
        rail_system_prompt: str = "You are a helpful assistant, "
                                  "able to express yourself purely through JSON, "
                                  "strictly and precisely adhering to the provided XML schemas.",
    ):

```


## 💎 Examples

Well-written issues often lead to better results.

- [Create a dice rolling bot](https://github.com/irgolic/AutoPR-template/pull/21)
- [Create a 'Tech Jargon Generator'](https://github.com/irgolic/AutoPR-template/pull/13)
- [Create a user-friendly weather app](https://github.com/irgolic/AutoPR-template/pull/15)
- [Write three programming interview challenges](https://github.com/irgolic/AutoPR-template/pull/11)
- [Replace `GPT2FastTokenizer` with `tiktoken`](https://github.com/irgolic/AutoPR/pull/44)