MCPcopy Index your code
hub / github.com/appleboy/CodeGPT

github.com/appleboy/CodeGPT @v1.7.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.7.2 ↗ · + Follow
248 symbols 755 edges 58 files 154 documented · 62%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

CodeGPT

Lint and Testing Security Scanning Trivy Security Scan codecov Go Report Card

一個用 Go 編寫的 CLI 工具,使用 ChatGPT AI (gpt-4o, gpt-4 模型) 生成 git 提交訊息或提供代碼審查摘要。它還會自動安裝 git prepare-commit-msg hook

English | 繁體中文 | 简体中文

cover

目錄

flow

功能

  • 支援 Azure OpenAI ServiceOpenAI API、[Gemini][60]、[Anthropic][100]、[Ollama][41]、[Groq][30] 和 [OpenRouter][50]。
  • 遵循 conventional commits 規範
  • 整合 Git prepare-commit-msg Hook,詳情請參閱 Git Hooks 文件
  • 可透過指定上下文行數(預設:3)來自訂產生的差異內容。
  • 支援差異檔案排除模式(git diff)。
  • 將提交訊息翻譯為其他語言(支援 enzh-twzh-cn)。
  • 支援 SOCKS 代理或自訂 HTTP 網路代理。
  • 生成簡潔的程式碼審查摘要。
  • 支援串流輸出,即時顯示生成的 token。
  • 允許自訂提示模板及變數。 code review

安裝

macOS

透過 Homebrew 安裝:

brew tap appleboy/tap
brew install codegpt

Windows

透過 Chocolatey 安裝:

choco install codegpt

腳本與二進位安裝

大多數系統推薦使用安裝腳本自動安裝(推薦),亦可手動下載執行檔。

A. 自動安裝(推薦)

執行腳本,自動下載與安裝最新版:

bash < <(curl -sSL https://raw.githubusercontent.com/appleboy/CodeGPT/main/install.sh)

或手動下載腳本再執行:

curl -LO https://raw.githubusercontent.com/appleboy/CodeGPT/main/install.sh
chmod +x install.sh
./install.sh

B. 手動安裝(進階)

  1. release 頁面 下載最新二進位檔案。
  2. 更改權限:

sh chmod 755 codegpt

  1. 移動到 bin 目錄:

sh mv codegpt /usr/local/bin/

  1. 驗證安裝:

sh codegpt version

可設定的環境變數

變數名稱 預設值 說明
VERSION latest 要安裝的 CodeGPT 版本(預設為最新發布版)
INSTALL_DIR $HOME/.codegpt/bin 安裝目錄
INSECURE 未設定(預設停用) 只要設定該變數(值不限),就會啟用跳過 SSL 驗證模式

使用範例:

# 啟用 insecure 模式(忽略 SSL 驗證)
INSECURE=1 ./install.sh

# 安裝特定版本到自訂目錄
VERSION=1.1.0 INSTALL_DIR=/opt/codegpt ./install.sh

此腳本會:

  1. 偵測您的作業系統與架構
  2. 下載最新版二進位檔案
  3. 將程式加入您的 PATH

從源碼安裝

從源碼安裝:

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

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.api_key_helper 用於動態生成 API key 的 Shell 命令(例如從密碼管理器或密鑰服務獲取)。
openai.api_key_helper_refresh_interval api_key_helper 刷新憑證的間隔秒數(默認:900 秒 / 15 分鐘)。設置為 0 以禁用緩存。
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
openai.stream 啟用串流輸出以即時顯示生成的 token,預設為 false
prompt.folder 默認提示文件夾是 $HOME/.config/codegpt/prompt

使用 API Key Helper 動態獲取憑證

您可以使用 Shell 命令從密碼管理器或密鑰服務動態獲取 API key,而不是直接將其存儲在配置文件中。這特別適用於:

  • 從密碼管理器獲取密鑰(1Password、Bitwarden 等)
  • 使用雲端密鑰服務(AWS Secrets Manager、Google Secret Manager 等)
  • 實現密鑰輪換和短期憑證
  • 提高安全性,避免以明文存儲密鑰

設置 API Key Helper

配置 Shell 命令來獲取您的 API key:

# 使用 1Password CLI
codegpt config set openai.api_key_helper "op read op://vault/openai/api_key"

# 使用 AWS Secrets Manager
codegpt config set openai.api_key_helper "aws secretsmanager get-secret-value --secret-id openai-key --query SecretString --output text"

# 使用 Google Cloud Secret Manager
codegpt config set openai.api_key_helper "gcloud secrets versions access latest --secret=openai-api-key"

# 使用環境變數
codegpt config set openai.api_key_helper "echo \$MY_OPENAI_KEY"

# 自定義腳本
codegpt config set openai.api_key_helper "/path/to/get-api-key.sh"

配置刷新間隔

默認情況下,API key 會被緩存 15 分鐘(900 秒),以避免對密鑰服務的過度調用:

# 設置刷新間隔為 5 分鐘
codegpt config set openai.api_key_helper_refresh_interval 300

# 設置刷新間隔為 30 分鐘
codegpt config set openai.api_key_helper_refresh_interval 1800

# 禁用緩存(每次都重新獲取密鑰)
codegpt config set openai.api_key_helper_refresh_interval 0

Gemini 專用 API Key Helper

對於 Gemini 提供者,您可以設置單獨的 helper:

codegpt config set gemini.api_key_helper "gcloud secrets versions access latest --secret=gemini-key"
codegpt config set gemini.api_key_helper_refresh_interval 600

工作原理

  1. 首次執行:CodeGPT 運行您的 helper 命令並將 API key 緩存到 ~/.config/codegpt/.cache/,文件權限為 0600(僅所有者可讀寫)
  2. 後續執行:在刷新間隔內,CodeGPT 使用緩存的密鑰
  3. 過期後:CodeGPT 自動重新運行 helper 命令並更新緩存
  4. 安全性:緩存文件以僅所有者可讀寫的權限存儲

優先順序

當配置了多個 API key 來源時,CodeGPT 使用以下優先順序:

  1. openai.api_key_helper(如果已配置)
  2. openai.api_key(靜態配置)
  3. OPENAI_API_KEY 環境變數

如何自定義默認提示文件夾

默認提示文件夾位於 $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][60] API 服務

你可以使用 Gemini API 或 VertexAI Gemini 服務。請參閱 [Gemini API 文件][61] 與 [VertexAI 文件][63]。
請在設定檔中更新以下參數。

  • 請從 [Gemini API][62] 頁面(用於 BackendGeminiAPI)或 [VertexAI API Key][64](用於 BackendVertexAI)建立 API 金鑰。

設定選項

選項 說明 範例值 必填 預設值
openai.provider 設為 gemini 以使用 Gemini 提供者 gemini
gemini.api_key Gemini 或 VertexAI 的 API 金鑰 xxxxxxx
gemini.model 模型名稱(參見 [Gemini 模型][61])

Extension points exported contracts — how you extend this code

Generative (Interface)
Generative defines an interface for generative AI operations. It includes methods for creating completions and obtaining [3 …
core/openai.go
Option (Interface)
Option is an interface that specifies instrumentation configuration options.
git/options.go
Option (Interface)
Option is an interface that specifies instrumentation configuration options.
proxy/options.go
Option (Interface)
Option is an interface that specifies instrumentation configuration options.
provider/gemini/options.go
Option (Interface)
Option is an interface that specifies instrumentation configuration options.
provider/anthropic/options.go
Option (Interface)
Option is an interface that specifies instrumentation configuration options.
provider/openai/options.go

Core symbols most depended-on inside this repo

String
called by 31
core/openai.go
GetAPIKeyFromHelper
called by 17
util/api_key_helper_unix.go
GetAPIKeyFromHelperWithCache
called by 13
util/api_key_helper.go
GetLanguage
called by 8
prompt/language.go
New
called by 8
git/git.go
GetCredential
called by 8
util/credstore.go
GetTemplateByString
called by 7
util/template.go
NewHeaders
called by 5
core/transport/headers.go

Shape

Function 160
Method 52
Struct 22
Interface 6
FuncType 5
TypeAlias 3

Languages

Go100%

Modules by API surface

provider/openai/options.go23 symbols
provider/anthropic/options.go16 symbols
provider/gemini/options.go15 symbols
git/git.go15 symbols
util/api_key_helper_test.go13 symbols
util/api_key_helper_windows_test.go12 symbols
proxy/options.go11 symbols
provider/openai/openai.go11 symbols
util/template.go8 symbols
git/options.go8 symbols
util/credstore_test.go7 symbols
core/openai.go7 symbols

For agents

$ claude mcp add CodeGPT \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page