vllm model 接入 claude code 指北

Step 1. vllm 启动 glm

vllm serve ./GLM-4.7 \
    --tensor-parallel-size 8 \
    --tool-call-parser glm47 \
    --reasoning-parser glm45 \
    --enable-auto-tool-choice \
    --served-model-name glm-47 \
    --api-key token-abc123 \
    --host 0.0.0.0 \
    --port 8001

注意这个 --tool-call-parser--reasoning-parser 一定要按照官方的来,我是参考的 zai-org/GLM-4.7 · Hugging Face 这个的写法

Step 2. 用 claude-code-router 把 vllm 的 openai-compatible 格式转换为 anthropic 格式

参考 GitHub - musistudio/claude-code-router: Use Claude Code as the foundation for coding infrastructure, allowing you to decide how to interact with the model while enjoying updates from Anthropic.

编写 ~/.claude-code-router/config.json 文件:

{
  "LOG": true,
  "LOG_LEVEL": "debug",

  "Providers": [
    {
      "name": "myvllm",
      "api_base_url": "https://xxx:port/v1/chat/completions",
      "api_key": "token-abc123",
      "models": ["glm-47"],

      "transformer": {
        "use": ["openai"]
      }
    }
  ],

  "Router": {
    "default": "myvllm,glm-47"
  }
}

Step 3. 启动 ccr code

运行 ccr code 用 ccr 来启动 claude code:

root@xxx-cpu-xxx:/path # ccr code
Service not running, starting service...
                                                                                                                                                                                                          
╭─── Claude Code v2.1.25 ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│                                                    │ Tips for getting started                                                                                                                                          │
│              Welcome back IcyFeather!              │ Run /init to create a CLAUDE.md file with instructions for Claude                                                                                                 │
│                                                    │ ─────────────────────────────────────────────────────────────────                                                                                                 │
│                                                    │ Recent activity                                                                                                                                                   │
│                       ▐▛███▜▌                      │ No recent activity                                                                                                                                                │
│                      ▝▜█████▛▘                     │                                                                                                                                                                   │
│                        ▘▘ ▝▝                       │                                                                                                                                                                   │
│       glm-47 · API Usage Billing ·                 │                                                                                                                                                                   │
│       xxx@gmail.com's Organization      │                                                                                                                                                                   │
│         /path        │                                                                                                                                                                   │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
                                                                                                                                                                                                                          
  /model to try Opus 4.5                                                                                                                                                                                                  
                                                                                                                                                                                                                          
❯ hi                                                                                                                                                                                                                      
                                                                                                                                                                                                                          
● Hello! How can I help you with your software engineering tasks today?                                                                                                                                                   
                                                                                                                                                                                                                          
❯ list cur dir files                                                                                                                                                                                                      
                                                                                                                                                                                                                          
● Read 1 file (ctrl+o to expand)                                                                                                                                                                                          
                                                                                                                                                                                                                          
● ┌──────┬──────────────────────────────────────┐                                                                                                                                                                         
  │ Type │                 Name                 │                                                                                                                                                                         
  ├──────┼──────────────────────────────────────┤                                                                                                                                                                         
  │ file │ CC-Switch-v3.10.2-Linux.deb (8.7 MB) │                                                                                                                                                                         
  ├──────┼──────────────────────────────────────┤                                                                                                                                                                         
  │ dir  │ LLaVA-Med                            │                                                                                                                                                                         
  ├──────┼──────────────────────────────────────┤                                                                                                                                                                         
  │ dir  │ agent                                │                                                                                                                                                                         
  ├──────┼──────────────────────────────────────┤                                                                                                                                                                         
  │ dir  │ aiscientist                          │                                                                                                                                                                         
  ├──────┼──────────────────────────────────────┤                                                                                                                                                                         
  │ dir  │ benchmark                            │                                                                                                                                                                         
  ├──────┼──────────────────────────────────────┤                                                                                                                                                                         
  │ dir  │ dataset                              │                                                                                                                                                                         
  ├──────┼──────────────────────────────────────┤                                                                                                                                                                         
  │ dir  │ download_model                       │                                                                                                                                                                         
  ├──────┼──────────────────────────────────────┤                                                                                                                                                                         
  │ dir  │ litellm                              │                                                                                                                                                                         
  ├──────┼──────────────────────────────────────┤                                                                                                                                                                         
  │ dir  │ llm_api_test                         │                                                                                                                                                                         
  ├──────┼──────────────────────────────────────┤                                                                                                                                                                         
  │ dir  │ med3dmodel                           │                                                                                                                                                                         
  ├──────┼──────────────────────────────────────┤                                                                                                                                                                         
  │ dir  │ medireason                           │                                                                                                                                                                         
  ├──────┼──────────────────────────────────────┤                                                                                                                                                                         
  │ dir  │ model_infer                          │                                                                                                                                                                         
  ├──────┼──────────────────────────────────────┤                                                                                                                                                                         
  │ dir  │ tmp                                  │                                                                                                                                                                         
  ├──────┼──────────────────────────────────────┤                                                                                                                                                                         
  │ dir  │ upload_model                         │                                                                                                                                                                         
  └──────┴──────────────────────────────────────┘                                                                                                                                                                         
                                          

如果要配置多个模型

比如我又配置了一个 Minimax-M2.1,编辑 ~/.claude-code-router/config.json

{
  "LOG": true,
  "LOG_LEVEL": "debug",

  "Providers": [
    {
      "name": "glm",
      "api_base_url": "https://xxx/v1/chat/completions",
      "api_key": "token-abc123",
      "models": ["glm-47"],
      "transformer": {
        "use": ["openai"]
      }
    },
    {
      "name": "mm",
      "api_base_url": "https://xxx/v1/chat/completions",
      "api_key": "token-abc123",
      "models": ["minimax-m21"],
      "transformer": {
        "use": ["openai"]
      }
    }
  ],
  "Router": {
    "default": "glm,glm-47",
    "think": "mm,minimax-m21"
  }
}

配置完了之后 ccr restart 重新 load 一下配置然后重新 ccr code 即可启动,用 /model minimax,minimax-m21 即可切换模型:

root@xxx:/path# ccr code
                                                                                                                                                                                                                                                    
 ▐▛███▜▌   Claude Code v2.1.25                                                                                                                                                                                                                           
▝▜█████▛▘  mm,minimax-m21 · API Usage Billing                                                                                                                                                                                                            
  ▘▘ ▝▝    /path                                                                                                                                                                     
                                                                                                                                                                                                                                                         
  /model to try Opus 4.5                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                         
❯ /model mm,minimax-m21                                                                                                                                                                                                                                  
  ⎿  Set model to mm,minimax-m21                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                         
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
❯                                                                                                                                                                                                                                                        
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  ? for shortcuts