什麼是 Goose?
Goose 是 Block 公司開源的 AI 程式設計助手,旨在為開發者提供一個免費、可自訂的程式碼助手替代方案。與 Claude Code、Cursor 等商業工具不同,Goose 完全開源,支援多種 LLM 後端,包括本地模型。
核心特性
- 多模型支援:相容 Anthropic、OpenAI、Google、Ollama 等
- 本地優先:支援本地執行的開源模型,保護程式碼隱私
- 完全免費:MIT 許可證,無訂閱費用
- 終端原生:命令列介面,無縫整合開發工作流
- 可擴充套件:支援自訂外掛和工具
為什麼選擇 Goose?
與商業工具對比
| 特性 | Goose | Claude Code | Cursor |
|---|---|---|---|
| 價格 | 免費 | $20/月 | $20/月 |
| 開源 | ✅ | ❌ | ❌ |
| 本地模型 | ✅ | ❌ | 有限 |
| 隱私保護 | ✅ | ❌ | ❌ |
| 自訂擴充套件 | ✅ | 有限 | 有限 |
適用場景
- 預算有限的開發者:無需訂閱費用
- 隱私敏感專案:使用本地模型,程式碼不出本地
- 企業自部署:可定製和內部部署
- 學習和研究:開源程式碼可供學習
快速開始
安裝要求
- Python 3.10+
- pip 或 uv 套件管理器
- 至少 8GB RAM(使用本地模型建議 16GB+)
安裝步驟
# 方法 1:使用 pip
pip install goose-ai
# 方法 2:使用 uv(推薦,更快)
uv pip install goose-ai
# 方法 3:從原始碼安裝
git clone https://github.com/block/goose.git
cd goose
pip install -e .
配置模型
Goose 支援多種模型後端。以下是常見配置:
# 使用 Anthropic Claude(需要 API Key)
goose configure --provider anthropic --model claude-sonnet-4-20250514
# 使用 OpenAI
goose configure --provider openai --model gpt-4.1
# 使用本地 Ollama 模型(免費)
goose configure --provider ollama --model llama3.1:8b
# 使用 Google Gemini
goose configure --provider google --model gemini-2.5-pro
環境變數設定
# ~/.bashrc 或 ~/.zshrc
export ANTHROPIC_API_KEY="your-api-key"
export OPENAI_API_KEY="your-api-key"
export GOOGLE_API_KEY="your-api-key"
# Ollama 無需 API Key,確保服務執行
ollama serve
核心功能詳解
1. 程式碼生成與補全
Goose 可以生成完整的功能模組:
# 建立新檔案
goose create file src/utils.py --prompt "建立一個 Python 工具函數模組,包含字串處理、日期格式化和 JSON 解析功能"
# 編輯現有檔案
goose edit src/main.py --prompt "新增錯誤處理和日誌記錄功能"
2. 程式碼審查
# 審查程式碼變更
goose review --diff git diff HEAD~1
# 查詢潛在問題
goose analyze --security src/
3. 測試生成
# 為現有程式碼生成測試
goose test src/calculator.py --framework pytest
# 生成完整測試套件
goose test --all src/
4. 文件生成
# 生成函數文件
goose doc src/api.py --format google
# 生成專案 README
goose doc --readme
實戰示例
示例 1:建立 REST API
# 讓 Goose 建立完整的 FastAPI 專案
goose create project my-api --template fastapi
# 新增使用者認證功能
goose add auth --provider oauth2
# 生成資料庫模型
goose generate model User --fields "name:str,email:str,created_at:datetime"
示例 2:重構現有程式碼
# 重構函數以提高效能
goose refactor src/slow_function.py --goal "最佳化時間複雜度從 O(n²) 到 O(n)"
# 轉換為非同步程式碼
goose convert src/sync_code.py --to async
示例 3:除錯幫助
# 分析錯誤日誌
goose debug --error "Traceback: IndexError: list index out of range" --context src/parser.py
# 互動式除錯會話
goose debug --interactive src/main.py
高階配置
自訂外掛
Goose 支援建立自訂工具外掛:
# plugins/custom_tool.py
from goose.plugins import Plugin
class CustomTool(Plugin):
name = "custom_tool"
def execute(self, query: str) -> str:
# 自訂邏輯
return f"處理結果:{query}"
工作流自動化
# .goose/workflow.yaml
name: Daily Code Review
trigger: daily 9:00 AM
steps:
- analyze: src/
- review: --pending-changes
- report: --output slack
模型切換策略
# 配置模型路由
goose route --simple-tasks ollama/llama3.1:8b
goose route --complex-tasks anthropic/claude-sonnet-4
goose route --code-review openai/gpt-4.1
效能最佳化
本地模型最佳化
# 使用量化模型減少記憶體佔用
ollama pull llama3.1:8b-q4_K_M
# 配置 GPU 加速
export OLLAMA_NUM_GPU=1
export OLLAMA_MAX_VRAM=8GB
快取策略
# 啟用響應快取
goose config set cache.enabled true
goose config set cache.ttl 3600
# 預載入常用模型
goose prefetch llama3.1:8b claude-sonnet-4
常見問題
Q: Goose 和 Claude Code 有什麼區別?
A: Goose 是開源免費的,支援本地模型;Claude Code 是商業產品,僅支援 Anthropic 模型。Goose 更適合預算有限或需要隱私保護的場景。
Q: 本地模型效果如何?
A: 對於簡單任務,Llama 3.1 8B 表現良好;複雜任務建議使用雲端模型或更大的本地模型(70B+)。
Q: 如何保護程式碼隱私?
A: 使用本地 Ollama 模型,程式碼完全在本地處理。配置 goose config set privacy.mode local 停用所有外部呼叫。
Q: 支援哪些 IDE 整合?
A: 目前主要支援終端使用。VS Code 和 JetBrains 外掛正在開發中,可關注官方 GitHub。
總結
Goose 為開發者提供了一個強大的開源 AI 程式設計助手選擇。它的多模型支援、本地執行能力和完全免費的特性,使其成為 Claude Code 和 Cursor 的有力替代品。
推薦開始步驟:
- 使用
pip install goose-ai快速安裝 - 配置 Ollama 和本地模型體驗免費方案
- 根據需要新增雲端模型 API Key
- 在日常開發中逐步整合 Goose 工作流
對於追求隱私、預算有限或喜歡開源生態的開發者,Goose 值得嘗試。
相關連結: