Skip to content

Latest commit

 

History

History
417 lines (292 loc) · 16.2 KB

README.zh-cn.md

File metadata and controls

417 lines (292 loc) · 16.2 KB

CodeGPT

Lint and Testing codecov Go Report Card

cover

English | 繁體中文 | 简体中文

一个用 Go 编写的 CLI 工具,它使用 ChatGPT AI(gpt-4o,gpt-4 模型)为你编写 git 提交信息或提供代码审查摘要,并自动安装 git prepare-commit-msg hook

flow

功能

code review

安装

在 macOS 上通过 Homebrew 安装

brew tap appleboy/tap
brew install codegpt

在 Windows 上通过 Chocolatey 安装

choco install codegpt

可以从 发布页面 下载预编译的二进制文件。将二进制文件权限更改为 755 并将其复制到系统 bin 目录。使用如下所示的 codegpt 命令。

$ codegpt version
version: v0.4.3 commit: xxxxxxx

从源代码安装:

go install github.com/appleboy/CodeGPT/cmd/codegpt@latest

设置

请先创建你的 OpenAI API Key。可以在 OpenAI 平台 生成新的 API Key。

register

环境变量是设置在操作系统上的变量,而不是在应用程序内。它由名称和值组成。我们建议将变量名称设置为 OPENAI_API_KEY

参见 API Key 安全最佳实践

export OPENAI_API_KEY=sk-xxxxxxx

或者将你的 API key 存储在自定义配置文件中。

codegpt config set openai.api_key sk-xxxxxxx

这将在你的主目录中创建一个 .codegpt.yaml 文件($HOME/.config/codegpt/.codegpt.yaml)。以下选项可用。

选项 描述
openai.base_url 替换默认的基本 URL (https://api.openai.com/v1)。
openai.api_key openai 平台页面 生成 API key。
openai.org_id 在 API 请求中有时使用的组织标识符。参见 组织设置。仅适用于 openai 服务。
openai.model 默认模型是 gpt-4o,你可以更改为其他自定义模型(Groq 或 OpenRouter 提供)。
openai.proxy HTTP/HTTPS 客户端代理。
openai.socks SOCKS 客户端代理。
openai.timeout 默认 HTTP 超时时间是 10s(十秒)。
openai.skip_verify 默认skip_verify 设置为 false,可以将其更改为 true 以忽略 SSL 验证。
openai.max_tokens 默认最大 token 数是 300。参见参考 max_tokens
openai.temperature 默认温度是 1。参见参考 temperature
git.diff_unified 生成具有 <n> 行上下文的差异,默认是 3
git.exclude_list git diff 命令中排除文件。
openai.provider 默认服务提供商是 openai,你可以更改为 azure
output.lang 默认语言是 en,可用语言有 zh-twzh-cnja
openai.top_p 默认 top_p 是 1.0。参见参考 top_p
openai.frequency_penalty 默认 frequency_penalty 是 0.0。参见参考 frequency_penalty
openai.presence_penalty 默认 presence_penalty 是 0.0。参见参考 presence_penalty
prompt.folder 默认提示文件夹是 $HOME/.config/codegpt/prompt

如何自定义默认提示文件夹

默认提示文件夹位于 $HOME/.config/codegpt/prompt。你可以通过执行以下命令将其更改为其他目录:

codegpt config set prompt.folder /path/to/your/prompt

要从自定义文件夹加载提示文件,请运行:

codegpt prompt --load

执行后,你将看到类似以下的消息:

save code_review_file_diff.tmpl to /Users/xxxxx/.config/codegpt/prompt/code_review_file_diff.tmpl
save summarize_file_diff.tmpl to /Users/xxxxx/.config/codegpt/prompt/summarize_file_diff.tmpl
save summarize_title.tmpl to /Users/xxxxx/.config/codegpt/prompt/summarize_title.tmpl
save conventional_commit.tmpl to /Users/xxxxx/.config/codegpt/prompt/conventional_commit.tmpl

如何切换到 Azure OpenAI 服务

请从 Azure 资源管理门户左侧菜单获取 API keyEndpointModel deployments 列表。

azure01

azure02

更新你的配置文件。

codegpt config set openai.provider azure
codegpt config set openai.base_url https://xxxxxxxxx.openai.azure.com/
codegpt config set openai.api_key xxxxxxxxxxxxxxxx
codegpt config set openai.model xxxxx-gpt-4o

支持 Gemini API 服务

使用 Gemini API 构建,你可以参见 Gemini API 文档。在你的配置文件中更新 providerapi_key。请从 Gemini API 页面创建 API key。

codegpt config set openai.provider gemini
codegpt config set openai.api_key xxxxxxx
codegpt config set openai.model gemini-1.5-flash-latest

支持 Anthropic API 服务

使用 Anthropic API 构建,你可以参见 Anthropic API 文档。在你的配置文件中更新 providerapi_key。请从 Anthropic API 页面创建 API key。

codegpt config set openai.provider anthropic
codegpt config set openai.api_key xxxxxxx
codegpt config set openai.model claude-3-5-sonnet-20241022

参见 Anthropic API 文档 中的模型列表。

如何切换到 Groq API 服务

请从 Groq API 服务获取 API key,请访问 这里。在你的配置文件中更新 base_urlapi_key

codegpt config set openai.provider openai
codegpt config set openai.base_url https://api.groq.com/openai/v1
codegpt config set openai.api_key gsk_xxxxxxxxxxxxxx
codegpt config set openai.model llama3-8b-8192

GroqCloud 目前支持 以下模型

  1. 生产模型
  2. 预览模型

如何切换到 ollama API 服务

我们可以使用 ollama API 服务中的 Llama3 模型,请访问 这里。在你的配置文件中更新 base_url

# 拉取 llama3 8b 模型
ollama pull llama3
ollama cp llama3 gpt-4o

尝试使用 ollama API 服务。

curl http://localhost:11434/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {
        "role": "user",
        "content": "Hello!"
      }
    ]
  }'

在你的配置文件中更新 base_url。你不需要在配置文件中设置 api_key

codegpt config set openai.base_url http://localhost:11434/v1

如何切换到 OpenRouter API 服务

你可以参见 支持的模型列表,模型使用可以由用户、开发者或两者支付,并且可能会在 可用性 上有所变化。你还可以通过 API 获取模型、价格和限制。

以下示例使用免费模型名称:meta-llama/llama-3-8b-instruct:free

codegpt config set openai.provider openai
codegpt config set openai.base_url https://openrouter.ai/api/v1
codegpt config set openai.api_key sk-or-v1-xxxxxxxxxxxxxxxx
codegpt config set openai.model google/learnlm-1.5-pro-experimental:free

要将你的应用程序包含在 openrouter.ai 排名中并在 openrouter.ai 排名中显示,你可以在配置文件中设置 openai.headers

codegpt config set openai.headers "HTTP-Referer=https://github.com/appleboy/CodeGPT X-Title=CodeGPT"
  • HTTP-Refer:可选,用于将你的应用程序包含在 openrouter.ai 排名中。
  • X-Title:可选,用于在 openrouter.ai 排名中显示。

使用

有两种方法可以使用 codegpt 命令生成提交信息:CLI 模式和 Git Hook。

CLI 模式

你可以直接调用 codegpt 为你的暂存更改生成提交信息:

git add <files...>
codegpt commit --preview

提交信息如下所示。

使用 gpt-4o 模型总结提交信息
我们正在尝试总结 git diff
我们正在尝试总结拉取请求的标题
================提交摘要====================

feat: 在 commit 命令和模板文件中添加预览标志并移除 disableCommit 标志。

- 在 `commit` 命令中添加 `preview` 标志
- 从 `prepare-commit-msg` 模板文件中移除 `disbaleCommit` 标志

==================================================
将提交信息写入 .git/COMMIT_EDITMSG 文件

或将所有 git 提交信息翻译成其他语言(繁体中文简体中文日语

codegpt commit --lang zh-tw --preview

考虑以下结果:

使用 gpt-4o 模型总结提交信息
我们正在尝试总结 git diff
我们正在尝试总结拉取请求的标题
我们正在尝试将 git 提交信息翻译成繁体中文
================提交摘要====================

功能:重构 codegpt commit 命令标记

- 将「codegpt commit」命令新增「预览」标记
- 从「codegpt commit」命令中移除「--disableCommit」标记

==================================================
将提交信息写入 .git/COMMIT_EDITMSG 文件

你可以使用 --amend 标志创建一个新的提交来替换当前分支的尖端。

codegpt commit --amend

更改提交信息模板

默认提交信息模板如下:

{{ .summarize_prefix }}: {{ .summarize_title }}

{{ .summarize_message }}

使用 --template_string 参数更改模板格式:

codegpt commit --preview --template_string \
  "[{{ .summarize_prefix }}]: {{ .summarize_title }}"

使用 --template_file 参数更改模板文件格式:

codegpt commit --preview --template_file your_file_path

向 git 提交信息模板添加自定义变量:

{{ .summarize_prefix }}: {{ .summarize_title }}

{{ .summarize_message }}

{{ if .JIRA_URL }}{{ .JIRA_URL }}{{ end }}

使用 --template_vars 参数向 git 提交信息模板添加自定义变量:

codegpt commit --preview --template_file your_file_path --template_vars \
  JIRA_URL=https://jira.example.com/ABC-123

使用 --template_vars_file 参数从文件加载自定义变量:

codegpt commit --preview --template_file your_file_path --template_vars_file your_file_path

参见 template_vars_file 格式如下:

JIRA_URL=https://jira.example.com/ABC-123

Git hook

你还可以使用 prepare-commit-msg hook 将 codegpt 与 Git 集成。这允许你正常使用 Git 并在提交前编辑提交信息。

安装 hook

你想在 Git 仓库中安装 hook:

codegpt hook install

卸载 hook

你想从 Git 仓库中移除 hook:

codegpt hook uninstall

安装后暂存你的文件并提交:

git add <files...>
git commit

codegpt 将为你生成提交信息并将其传回 Git。Git 将使用配置的编辑器打开它供你审查/编辑。然后,要提交,请保存并关闭编辑器!

$ git commit
使用 gpt-4o 模型总结提交信息
我们正在尝试总结 git diff
我们正在尝试总结拉取请求的标题
================提交摘要====================

改进 OpenAI 工具的用户体验和文档

- 添加预编译二进制文件的下载链接
- 包括