3 Star 5 Fork 3

Gitee 极速下载/codegpt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/appleboy/CodeGPT
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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

使用安装脚本

# 下载并运行安装脚本
bash < <(curl -sSL https://raw.githubusercontent.com/appleboy/CodeGPT/main/install.sh)

或手动下载并运行:

chmod +x install.sh
./install.sh

可配置的环境变量

变量名 默认值 说明
VERSION latest 要安装的 CodeGPT 版本(默认为最新发布版)
INSTALL_DIR $HOME/.codegpt/bin 安装目录
CURL_INSECURE false 是否跳过 SSL 验证 (true/false)

使用示例:

# 安装指定版本到自定义目录
VERSION=1.1.0 INSTALL_DIR=/opt/codegpt ./install.sh

该脚本会:

  1. 检测您的操作系统和架构
  2. 下载最新版二进制文件
  3. 将程序加入您的 PATH

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

$ codegpt version
Version: 1.1.0
Git Commit: 899396a
Build Time: 2025-05-16T15:52:38Z
Go Version: 1.24.3
OS/Arch: darwin/arm64

从源码安装

从源码安装:

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

使用 VSCode Devcontainer

在 devcontainer.json 中添加 feature

"features": {
  "ghcr.io/kvokka/features/codegpt:1": {}
}

配置

请先创建你的 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 或 VertexAI Gemini 服务。参见 Gemini API 文档VertexAI 文档
请在配置文件中更新以下参数。

配置选项

选项 描述 示例值 是否必填 默认值
openai.provider 设置为 gemini 以使用 Gemini 提供商 gemini
gemini.api_key Gemini 或 VertexAI 的 API key xxxxxxx
gemini.model 模型名称(参见 Gemini 模型 gemini-2.0-flash
gemini.backend Gemini 后端:BackendGeminiAPI(默认,适用于 Gemini API)或 BackendVertexAI(VertexAI) BackendGeminiAPI BackendGeminiAPI
gemini.project_id VertexAI 项目 ID(如使用 BackendVertexAI 必填) my-gcp-project 条件必填
gemini.location VertexAI 区域(如使用 BackendVertexAI 必填) us-central1 条件必填

示例:Gemini API(默认后端)

codegpt config set openai.provider gemini
codegpt config set gemini.api_key xxxxxxx
codegpt config set openai.model gemini-2.0-flash
# gemini.backend 默认为 BackendGeminiAPI,可省略

示例:VertexAI Gemini

codegpt config set openai.provider gemini
codegpt config set openai.model gemini-1.5-pro-preview-0409
codegpt config set gemini.backend BackendVertexAI
codegpt config set gemini.project_id my-gcp-project
codegpt config set gemini.location us-central1
flowchart TD User([用户]) subgraph CodeGPT GeminiClient([Gemini 提供商]) end subgraph Google GeminiAPI([Gemini API]) VertexAI([VertexAI Gemini]) end User -->|Completion / GetSummaryPrefix| GeminiClient GeminiClient -- BackendGeminiAPI --> GeminiAPI GeminiAPI -- Response (text, usage) --> GeminiClient GeminiClient -- BackendVertexAI --> VertexAI VertexAI -- Response (text, usage) --> GeminiClient GeminiClient --> User

支持 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 工具的用户体验和文档

- 添加预编译二进制文件的下载链接
- 包括
MIT License Copyright (c) 2023 Bo-Yi Wu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

CodeGPT 是一个用 Go 语言编写的 CLI,可以使用 ChatGPT AI(gpt-4,gpt-3.5-turbo 模型)为你写 git commit 信息或做代码审查简报 展开 收起
README
MIT
取消

发行版

暂无发行版

近期动态

接近2年前同步了仓库
2年前创建了仓库
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/codegpt.git
git@gitee.com:mirrors/codegpt.git
mirrors
codegpt
codegpt
main

搜索帮助