[Looplia] AI Code Review + Gemini Proxy
用 Gemini 2.5 Flash + Anthropic API 相容層,利用 Looplia 的 workflow cli 做 Code Review
💬 覺得有幫助嗎?
🌟 分享給需要的朋友
💌 [訂閱電子報](https://leapdesign.ai/zh/subscribe/),每週收到最新技術文章
✨ 更多快速技術解決方案 → [https://leapdesign.ai/)
⚡ 快速版
Looplia + Gemini Proxy,無縫切換
步驟:
# 1. 設定 Gemini API key
export GEMINI_API_KEY=”AIzaSy...”
# 2. 啟動 proxy(Anthropic API 相容層)
npm install -g @vitorcen/gemini-cli-anthropic
gemini-cli-anthropic # 監聽 port 41242
# 3. 使用 Claude API 格式呼叫 Gemini
curl -X POST http://127.0.0.1:41242/v1/messages \
-H “x-api-key: dummy” \
-H “anthropic-version: 2023-06-01” \
-d ‘{”model”:”gemini-2.5-flash”,”messages”:[...]}’📖 完整版
利用 Looplia 的 workflow 利用 AI code review,Gemini 提供更大的 context 與多模態理解
本文用 gemini-cli-anthropic 提供 Anthropic API 相容層,無縫使用 Looplia。
💡 適合誰?
想使用 Gemini 2.5 Flash 進行 code review
原有 Gemini 生態系的想使用 Looplia 的 workflow cli
🔑 Part 1: 設定 Gemini API
步驟 1:獲取 Gemini API Key
這不用多說, 到 https://ai.dev/apikey
步驟 2:設定環境變數
export GEMINI_API_KEY=”AIxx_gxxx”
# 永久保存
echo ‘export GEMINI_API_KEY=”AIzaSy...”’ >> ~/.zshrc
source ~/.zshrc✅ 驗證 API Key
# 簡單測試
curl -X POST “https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${GEMINI_API_KEY}” \
-H ‘Content-Type: application/json’ \
-d ‘{”contents”:[{”parts”:[{”text”:”Hello”}]}]}’🚀 Part 2: 部署 gemini-cli-anthropic Proxy
什麼是 gemini-cli-anthropic?
[gemini-cli-anthropic] 是一個 proxy server,提供:
Anthropic API 相容層- 使用 Claude API 格式呼叫 Gemini
無縫切換 - 現有 Claude 工具無需修改即可使用 Gemini
完整支援 - streaming、tool use、multimodal 等功能
架構圖
Client (Claude API format)
↓
gemini-cli-anthropic proxy (port 41242)
↓ (轉換格式)
Google Gemini API
↓
Response (Claude API format)步驟 1:安裝 (方式 A - NPM 全域安裝)
npm install -g @vitorcen/gemini-cli-anthropic步驟 2:啟動 Proxy Server
gemini-cli-anthropic
# 輸出:
# Server listening on port 41242
# Using Gemini API Key: AIzaSy...xxx步驟 1-2:從原始碼安裝 (方式 B)
# Clone repository with submodules
git clone --recurse-submodules https://github.com/vitorcen/gemini-cli-anthropic.git
cd gemini-cli-anthropic
# 安裝相依套件
npm install
# 安裝 gemini-cli submodule
cd gemini-cli
npm install
cd ..
# 啟動 server
npm start預期輸出:
🚀 Gemini CLI Anthropic Proxy Server
📡 Listening on: http://127.0.0.1:41242
🔑 Using Gemini API Key: AIzaSy...xxx
✅ Server ready!步驟 3:驗證 Proxy
測試 proxy 是否正常運作:
curl -X POST http://127.0.0.1:41242/v1/messages \
-H “Content-Type: application/json” \
-H “x-api-key: dummy” \
-H “anthropic-version: 2023-06-01” \
-d ‘{
“model”: “gemini-2.5-flash”,
“max_tokens”: 1024,
“messages”: [{
“role”: “user”,
“content”: “你好,請用繁體中文回答:1+1=?”
}]
}’預期回應:
{
“id”: “msg_...”,
“type”: “message”,
“role”: “assistant”,
“content”: [{
“type”: “text”,
“text”: “1 + 1 等於 二。”
}],
“model”: “gemini-2.5-flash”,
“usage”: {
“input_tokens”: 15,
“output_tokens”: 8
}
}✅ 成功!Proxy 正常運作且支援繁體中文。
📝 Part 3: 實現 Code Review
Python 實作(完整範例)
創建 `gemini-review.py`:
#!/usr/bin/env python3
import json
import urllib.request
# 讀取要審查的程式碼
with open(’smart-code-review.sh’, ‘r’) as f:
code = f.read()[:1000] # 前 1000 字元
# 構建請求
data = {
“model”: “gemini-2.5-flash”,
“max_tokens”: 4096,
“messages”: [{
“role”: “user”,
“content”: f“”“請審查以下程式碼的品質、安全性和可讀性,用繁體中文詳細回答:
{code}
請提供:
1. 程式碼品質評估(1-10分)
2. 安全性問題(如有)
3. 可讀性改進建議
4. 最佳實踐建議
5. 優先級標註(P0/P1/P2)
“”“
}]
}
# 發送請求到 proxy
req = urllib.request.Request(
‘http://127.0.0.1:41242/v1/messages’,
data=json.dumps(data).encode(’utf-8’),
headers={
‘Content-Type’: ‘application/json’,
‘x-api-key’: ‘dummy’, # proxy 不需要真實 API key
‘anthropic-version’: ‘2023-06-01’
}
)
print(”🔍 開始 Code Review...”)
print(”📡 使用 Gemini 2.5 Flash via Proxy\n”)
with urllib.request.urlopen(req) as response:
result = json.loads(response.read())
# 顯示審查結果
print(”=” * 60)
print(”📋 CODE REVIEW REPORT”)
print(”=” * 60)
print(result[’content’][0][’text’])
print(”\n” + “=” * 60)
# 顯示使用統計
usage = result[’usage’]
print(f“\n📊 Token 使用情況:”)
print(f“ - Input tokens: {usage[’input_tokens’]}”)
print(f“ - Output tokens: {usage[’output_tokens’]}”)
print(f“ - Total: {usage[’input_tokens’] + usage[’output_tokens’]}”)Bash 實作
創建 `review-test.sh`:
#!/bin/bash
CODE=$(cat ~/dot-agents-test/smart-code-review.sh | head -30)
curl -X POST http://127.0.0.1:41242/v1/messages \
-H “Content-Type: application/json” \
-H “x-api-key: dummy” \
-H “anthropic-version: 2023-06-01” \
-d @- << JSON | jq -r ‘.content[0].text’
{
“model”: “gemini-2.5-flash”,
“max_tokens”: 4096,
“messages”: [{
“role”: “user”,
“content”: “請審查以下程式碼的品質、安全性和可讀性,用繁體中文詳細回答:\n\n${CODE}”
}]
}
🧪 Part 4: 實際測試結果
執行測試
cd ~/dot-agents-test
python3 gemini-proxy-review.py article-1-claude-subscription-code-review.md
測試輸出(實測結果)
🔍 開始 Code Review...
📡 使用 Gemini 2.5 Flash via Proxy
============================================================
📋 CODE REVIEW REPORT
============================================================
### 程式碼品質評估 (分數:7/10)
這份教學文章的結構清晰,分為快速版和完整版,便於不同程度的讀者
快速理解。提供的指令多數是正確且直接的,整體邏輯流暢。
### 安全性問題
**P1 - OAuth Token 硬編碼**
- 問題:直接將 OAuth token 以明文形式展示
- 風險:可能誤導使用者將其硬編碼到腳本或公開的地方
- 建議:使用佔位符 (YOUR_CLAUDE_OAUTH_TOKEN) 並警告切勿提交到版本控制
**P1 - curl | bash 自動安裝腳本**
- 問題:Bun 的安裝指令 `curl -fsSL https://bun.sh/install | bash`
- 風險:直接執行從網路獲取的腳本
- 建議:增加安全提示,建議使用者先審查腳本內容
**P0 - 指令不完整**
- 問題:Step 1 指令被截斷為 `claude setup-`
- 影響:導致指令無法執行
- 修正:應為 `claude setup-token`
### 可讀性改進建議
**P2 - 環境變數寫入**
- 問題:只提及 `~/.zshrc`,未考慮其他 shell 使用者
- 建議:提及 `~/.bashrc` 或更通用的 `~/.profile`
**P2 - 章節編排一致性**
- 問題:「實作步驟」不夠完整,與快速版有重複
- 建議:統一步驟說明或提供更深入的解釋
### 最佳實踐建議
1. **腳本魯棒性** - 使用 `set -euo pipefail`
2. **依賴鎖定** - 確保 `bun.lockb` 檔案提交到 Git
3. **環境變數冪等性** - 檢查變數是否存在再添加
============================================================
📊 Token 使用情況:
- Input tokens: 923
- Output tokens: 1702
- Total: 2625 tokens
⏱️ 執行時間:約 5-7 秒🚨 注意事項與限制
1. Proxy 可用性
⚠️ gemini-cli-anthropic proxy 必須持續運行
解決方案 A - systemd service (Linux)
or
解決方案 B - pm2 (Node.js)
💬 覺得有幫助嗎?
🌟 分享給需要的朋友
💌 [訂閱電子報](https://leapdesign.ai/zh/subscribe/),每週收到最新技術文章
✨ 更多快速技術解決方案 → [https://leapdesign.ai/)


