269 lines
7.0 KiB
Markdown
269 lines
7.0 KiB
Markdown
<div align="center">
|
||
<div>
|
||
<img src=".github/screenshot.png" alt="AI Commits"/>
|
||
<h1 align="center">AI Commits</h1>
|
||
</div>
|
||
<p>一个使用 AI 为您编写 git 提交消息的 CLI 工具。再也不用手写提交消息了。</p>
|
||
<a href="https://www.npmjs.com/package/aicommits"><img src="https://img.shields.io/npm/v/aicommits" alt="当前版本"></a>
|
||
</div>
|
||
|
||
---
|
||
|
||
## 设置
|
||
|
||
> 支持的最低 Node.js 版本是最新的 v14。使用 `node --version` 检查您的 Node.js 版本。
|
||
|
||
1. 安装 _aicommits_:
|
||
|
||
```sh
|
||
npm install -g aicommits
|
||
```
|
||
|
||
2. 从 [OpenAI](https://platform.openai.com/account/api-keys) 获取您的 API 密钥
|
||
|
||
> 注意:如果您还没有账户,您需要创建一个账户并设置计费。
|
||
|
||
3. 设置密钥以便 aicommits 可以使用它:
|
||
|
||
```sh
|
||
aicommits config set OPENAI_KEY=<your token>
|
||
```
|
||
|
||
这将在您的主目录中创建一个 `.aicommits` 文件。
|
||
|
||
### 升级
|
||
|
||
使用以下命令检查已安装的版本:
|
||
|
||
```
|
||
aicommits --version
|
||
```
|
||
|
||
如果它不是[最新版本](https://github.com/Nutlope/aicommits/releases/latest),请运行:
|
||
|
||
```sh
|
||
npm update -g aicommits
|
||
```
|
||
|
||
## 使用方法
|
||
|
||
### CLI 模式
|
||
|
||
您可以直接调用 `aicommits` 来为您的暂存更改生成提交消息:
|
||
|
||
```sh
|
||
git add <files...>
|
||
aicommits
|
||
```
|
||
|
||
`aicommits` 会将未知标志传递给 `git commit`,因此您可以传入 [`commit` 标志](https://git-scm.com/docs/git-commit)。
|
||
|
||
例如,您可以在提交时暂存所有跟踪文件中的更改:
|
||
|
||
```sh
|
||
aicommits --all # 或 -a
|
||
```
|
||
|
||
> 👉 **提示:** 如果 `aicommits` 对您来说太长,可以使用 `aic` 别名。
|
||
|
||
#### 生成多个建议
|
||
|
||
有时推荐的提交消息不是最好的,所以您希望它生成几个以供选择。您可以通过传入 `--generate <i>` 标志来一次生成多个提交消息,其中 'i' 是生成的消息数量:
|
||
|
||
```sh
|
||
aicommits --generate <i> # 或 -g <i>
|
||
```
|
||
|
||
> 警告:这会使用更多的令牌,意味着成本更高。
|
||
|
||
#### 生成约定式提交
|
||
|
||
如果您想生成[约定式提交](https://conventionalcommits.org/),可以使用 `--type` 标志,后跟 `conventional`。这将提示 `aicommits` 根据约定式提交规范格式化提交消息:
|
||
|
||
```sh
|
||
aicommits --type conventional # 或 -t conventional
|
||
```
|
||
|
||
如果您的项目遵循约定式提交标准,或者您使用依赖此提交格式的工具,此功能会很有用。
|
||
|
||
### Git 钩子
|
||
|
||
您还可以通过 [`prepare-commit-msg`](https://git-scm.com/docs/githooks#_prepare_commit_msg) 钩子将 _aicommits_ 与 Git 集成。这让您可以像平常一样使用 Git,并在提交之前编辑提交消息。
|
||
|
||
#### 安装
|
||
|
||
在您要安装钩子的 Git 仓库中:
|
||
|
||
```sh
|
||
aicommits hook install
|
||
```
|
||
|
||
#### 卸载
|
||
|
||
在您要卸载钩子的 Git 仓库中:
|
||
|
||
```sh
|
||
aicommits hook uninstall
|
||
```
|
||
|
||
#### 使用方法
|
||
|
||
1. 暂存您的文件并提交:
|
||
|
||
```sh
|
||
git add <files...>
|
||
git commit # 仅在未传入消息时生成消息
|
||
```
|
||
|
||
> 如果您想自己编写消息而不是生成消息,可以简单地传入一个:`git commit -m "My message"`
|
||
|
||
2. Aicommits 将为您生成提交消息并将其传递回 Git。Git 将使用[配置的编辑器](https://docs.github.com/en/get-started/getting-started-with-git/associating-text-editors-with-git)打开它供您查看/编辑。
|
||
|
||
3. 保存并关闭编辑器以提交!
|
||
|
||
## 配置
|
||
|
||
### 读取配置值
|
||
|
||
要检索配置选项,请使用命令:
|
||
|
||
```sh
|
||
aicommits config get <key>
|
||
```
|
||
|
||
例如,要检索 API 密钥,您可以使用:
|
||
|
||
```sh
|
||
aicommits config get OPENAI_KEY
|
||
```
|
||
|
||
您还可以通过用空格分隔它们来一次检索多个配置选项:
|
||
|
||
```sh
|
||
aicommits config get OPENAI_KEY generate
|
||
```
|
||
|
||
### 设置配置值
|
||
|
||
要设置配置选项,请使用命令:
|
||
|
||
```sh
|
||
aicommits config set <key>=<value>
|
||
```
|
||
|
||
例如,要设置 API 密钥,您可以使用:
|
||
|
||
```sh
|
||
aicommits config set OPENAI_KEY=<your-api-key>
|
||
```
|
||
|
||
您还可以通过用空格分隔它们来一次设置多个配置选项,例如
|
||
|
||
```sh
|
||
aicommits config set OPENAI_KEY=<your-api-key> generate=3 locale=en
|
||
```
|
||
|
||
### 选项
|
||
|
||
#### OPENAI_KEY
|
||
|
||
必需
|
||
|
||
OpenAI API 密钥。您可以从 [OpenAI API 密钥页面](https://platform.openai.com/account/api-keys) 检索它。
|
||
|
||
#### locale
|
||
|
||
默认值:`en`
|
||
|
||
用于生成提交消息的语言环境。请参阅以下列表中的代码:https://wikipedia.org/wiki/List_of_ISO_639-1_codes。
|
||
|
||
#### generate
|
||
|
||
默认值:`1`
|
||
|
||
要生成的提交消息数量以供选择。
|
||
|
||
注意,这会使用更多的令牌,因为它会生成更多的结果。
|
||
|
||
#### proxy
|
||
|
||
设置用于请求的 HTTP/HTTPS 代理。
|
||
|
||
要清除代理选项,您可以使用命令(注意等号后的空值):
|
||
|
||
```sh
|
||
aicommits config set proxy=
|
||
```
|
||
|
||
#### model
|
||
|
||
默认值:`gpt-3.5-turbo`
|
||
|
||
要使用的聊天完成(`/v1/chat/completions`)模型。请参阅 [OpenAI 文档](https://platform.openai.com/docs/models/model-endpoint-compatibility)中可用模型的列表。
|
||
|
||
> 提示:如果您有权限,尝试升级到 [`gpt-4`](https://platform.openai.com/docs/models/gpt-4) 以获得更高级的代码分析。它可以处理双倍的输入大小,但成本更高。请访问 OpenAI 的网站了解更多信息。
|
||
|
||
#### timeout
|
||
|
||
向 OpenAI API 发出网络请求的超时时间(以毫秒为单位)。
|
||
|
||
默认值:`10000`(10 秒)
|
||
|
||
```sh
|
||
aicommits config set timeout=20000 # 20s
|
||
```
|
||
|
||
#### max-length
|
||
|
||
生成的提交消息的最大字符长度。
|
||
|
||
默认值:`50`
|
||
|
||
```sh
|
||
aicommits config set max-length=100
|
||
```
|
||
|
||
#### type
|
||
|
||
默认值:`""`(空字符串)
|
||
|
||
要生成的提交消息类型。将其设置为 "conventional" 以生成遵循约定式提交规范的提交消息:
|
||
|
||
```sh
|
||
aicommits config set type=conventional
|
||
```
|
||
|
||
您可以通过将其设置为空字符串来清除此选项:
|
||
|
||
```sh
|
||
aicommits config set type=
|
||
```
|
||
|
||
#### baseURL
|
||
|
||
默认值:`https://api.openai.com`
|
||
|
||
用于设置 OpenAI API 的基础 URL。如果您使用的是 OpenAI 的代理服务或其他兼容的 API 端点,可以通过此选项进行配置:
|
||
|
||
```sh
|
||
aicommits config set baseURL=https://your-proxy-url.com
|
||
```
|
||
|
||
这对于使用自定义 API 端点或代理服务的用户非常有用。
|
||
|
||
## 工作原理
|
||
|
||
这个 CLI 工具运行 `git diff` 来获取您所有的最新代码更改,将它们发送到 OpenAI 的 GPT-3,然后返回 AI 生成的提交消息。
|
||
|
||
视频即将推出,我将从头开始重建它,向您展示如何轻松构建由 AI 驱动的自己的 CLI 工具。
|
||
|
||
## 维护者
|
||
|
||
- **Hassan El Mghari**: [@Nutlope](https://github.com/Nutlope) [<img src="https://img.shields.io/twitter/follow/nutlope?style=flat&label=nutlope&logo=twitter&color=0bf&logoColor=fff" align="center">](https://twitter.com/nutlope)
|
||
|
||
- **Hiroki Osame**: [@privatenumber](https://github.com/privatenumber) [<img src="https://img.shields.io/twitter/follow/privatenumbr?style=flat&label=privatenumbr&logo=twitter&color=0bf&logoColor=fff" align="center">](https://twitter.com/privatenumbr)
|
||
|
||
## 贡献
|
||
|
||
如果您想在 [Issues](https://github.com/Nutlope/aicommits/issues) 中帮助修复错误或实现功能,请查看 [贡献指南](CONTRIBUTING.md) 以了解如何设置和测试项目。
|