[教程推荐] Claude Code 用上国产顶尖模型:DeepSeek V3.1、Kimi-K2、Qwen3、GLM-4.5

大模型向量数据库容器

Claude Code 代表的 CLI 模式,俨然已经取代了 Cursor/Windsurf 等 AI IDE 模式,成为 AI 编程的最佳选择,就连一时风头无二的 Cursor(最快1亿美金 ARR 创造者、9亿美金融资、99亿美金估值)都被比了下去。Claude Code 核心开发和产品经理,被挖去 Cursor 两周后返回,也成为了一个耐人寻味的故事 😄

插播一下,我也整理了 Claude Code 的合集,给朋友们参考:

[真心建议收藏资源集合]全面释放 Claude Code 潜能:深度整合官方文档、最佳实践、开源项目资源与AI驱动开发的进阶技术

还有 Claude 合集,更实时更完整:Claude Claude Code

言归正传,Claude Code 就是那种“除了贵,没毛病”的产品,好是真好,贵也是真贵,Anthropic API 本身就是全球模型 API 里断档最贵的存在,而如果想深度使用 Claude Code,对应的订阅计划也很贵(200美金/月),更不用提 Anthropic 全球领先的账号检测,简称 “封号狂魔” 😂

那有没有办法,既要用上 Claude Code 的 Coding Agent 架构,又能保证编程效果没有明显下降,还能让模型 API 的价格降下来呢?

这种既要、又要、还要... 当然是可以的!而且切换为国产模型后,🪜也可以省了。

今天咱们讲讲怎么把 Claude 模型替换为国产模型: DeepSeek V3.1、Kimi-K2、Qwen3、GLM-4.5,还能正常使用 Claude Code。

开始之前先说一下替换的原理是什么,大家应该都了解 OpenAI API 格式,自 OpenAI API 推出后,各家模型 API 都适配了这种格式。让开发者切换模型变得很方便,只要替换 API URL 和 Token/Key 就可以,接入模型的代码完全不需要动。

同理,切换 Anthropic API 也是这样,需要模型 API 适配了 Anthropic API 格式,是不是很简单。

先从 DeepSeek V3.1 开始

设置环境变量

  
export ANTHROPIC_BASE_URL=https://api.deepseek.com/anthropic  
export ANTHROPIC_AUTH_TOKEN=${YOUR_API_KEY}  
export ANTHROPIC_MODEL=deepseek-chat  
export ANTHROPIC_SMALL_FAST_MODEL=deepseek-chat

重新加载存放环境变量的文件,比如

  
source $HOME/.zshrc

再启动 Claude Code 就可以了。

DeepSeek API Key 在这申请:

https://platform.deepseek.com

关于接入 Anthropic,DeepSeek 官方文档说明:

https://api-docs.deepseek.com/guides/anthropic\_api

然后是 Kimi-K2

设置环境变量

  
export ANTHROPIC_BASE_URL=https://api.moonshot.cn/anthropic/  
export ANTHROPIC_API_KEY=your_moonshot_api_key_here

备注:除环境变量设置外,其他步骤同 DeepSeek,可参考。

Moonshot Kimi API Key 在这申请:

https://platform.moonshot.ai/

再是 Qwen3-Coder

设置环境变量

  
export ANTHROPIC_BASE_URL=https://dashscope-intl.aliyuncs.com/api/v2/apps/claude-code-proxy  
export ANTHROPIC_AUTH_TOKEN=your-dashscope-apikey

备注:除环境变量设置外,其他步骤同 DeepSeek,可参考。

Qwen3 API Key 在这申请:

https://www.alibabacloud.com/help/en/model-studio/use-qwen-by-calling-api

https://openrouter.ai/qwen/qwen3-coder:free

关于接入 Anthropic,千问官方文档说明:

https://qwenlm.github.io/blog/qwen3-coder/

最后是 GLM-4.5

设置环境变量

  
export ANTHROPIC_BASE_URL=https://open.bigmodel.cn/api/anthropic  
export ANTHROPIC_AUTH_TOKEN=YOUR_API_KEY

备注:除环境变量设置外,其他步骤同 DeepSeek,可参考。

GLM-4.5 API Key 在这申请:

https://bigmodel.cn/console/overview

关于接入 Anthropic,智谱 AI 官方文档说明:

https://docs.bigmodel.cn/cn/guide/develop/claude#claude-code

脚本方式

除了手动设置变量的方法,还有开源项目提供了 Shell 脚本的方式:kimi cc(https://github.com/LLM-Red-Team/kimi-cc)

为了方便🪜受限的朋友访问,我把 Shell 脚本内容贴在这里,切换其他模型,只需要替换脚本中对应的 API URL 和 API Key 值即可。

  
#!/bin/bash  
set -e  
install_nodejs() {  
    local platform=$(uname -s)  
  
    case "$platform" in  
        Linux|Darwin)  
            echo "🚀 Installing Node.js on Unix/Linux/macOS..."  
  
            echo "📥 Downloading and installing nvm..."  
            curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash  
  
            echo "🔄 Loading nvm environment..."  
            \. "$HOME/.nvm/nvm.sh"  
  
            echo "📦 Downloading and installing Node.js v22..."  
            nvm install 22  
  
            echo -n "✅ Node.js installation completed! Version: "  
            node -v # Should print "v22.17.0".  
            echo -n "✅ Current nvm version: "  
            nvm current # Should print "v22.17.0".  
            echo -n "✅ npm version: "  
            npm -v # Should print "10.9.2".  
            ;;  
        *)  
            echo "Unsupported platform: $platform"  
            exit 1  
            ;;  
    esac  
}  
# Check if Node.js is already installed and version is >= 18  
if command -v node >/dev/null 2>&1; then  
    current_version=$(node -v | sed 's/v//')  
    major_version=$(echo $current_version | cut -d. -f1)  
  
    if [ "$major_version" -ge 18 ]; then  
        echo "Node.js is already installed: v$current_version"  
    else  
        echo "Node.js v$current_version is installed but version < 18. Upgrading..."  
        install_nodejs  
    fi  
else  
    echo "Node.js not found. Installing..."  
    install_nodejs  
fi  
# Check if Claude Code is already installed  
if command -v claude >/dev/null 2>&1; then  
    echo "Claude Code is already installed: $(claude --version)"  
else  
    echo "Claude Code not found. Installing..."  
    npm install -g @anthropic-ai/claude-code  
fi  
# Configure Claude Code to skip onboarding  
echo "Configuring Claude Code to skip onboarding..."  
node --eval '  
    const homeDir = os.homedir();   
    const filePath = path.join(homeDir, ".claude.json");  
    if (fs.existsSync(filePath)) {  
        const content = JSON.parse(fs.readFileSync(filePath, "utf-8"));  
        fs.writeFileSync(filePath,JSON.stringify({ ...content, hasCompletedOnboarding: true }, 2), "utf-8");  
    } else {  
        fs.writeFileSync(filePath,JSON.stringify({ hasCompletedOnboarding: true }), "utf-8");  
    }'  
# Prompt user for API key  
echo "🔑 Please enter your Moonshot API key:"  
echo "   You can get your API key from: https://platform.moonshot.cn/console/api-keys"  
echo "   Note: The input is hidden for security. Please paste your API key directly."  
echo ""  
read -s api_key  
echo ""  
if [ -z "$api_key" ]; then  
    echo "⚠️  API key cannot be empty. Please run the script again."  
    exit 1  
fi  
# Detect current shell and determine rc file  
current_shell=$(basename "$SHELL")  
case "$current_shell" in  
    bash)  
        rc_file="$HOME/.bashrc"  
        ;;  
    zsh)  
        rc_file="$HOME/.zshrc"  
        ;;  
    fish)  
        rc_file="$HOME/.config/fish/config.fish"  
        ;;  
    *)  
        rc_file="$HOME/.profile"  
        ;;  
esac  
# Add environment variables to rc file  
echo ""  
echo "📝 Adding environment variables to $rc_file..."  
# Check if variables already exist to avoid duplicates  
if [ -f "$rc_file" ] && grep -q "ANTHROPIC_BASE_URL\|ANTHROPIC_API_KEY" "$rc_file"; then  
    echo "⚠️ Environment variables already exist in $rc_file. Skipping..."  
else  
    # Append new entries  
    echo "" >> "$rc_file"  
    echo "# Claude Code environment variables" >> "$rc_file"  
    echo "export ANTHROPIC_BASE_URL=https://api.moonshot.cn/anthropic/" >> "$rc_file"  
    echo "export ANTHROPIC_API_KEY=$api_key" >> "$rc_file"  
    echo "✅ Environment variables added to $rc_file"  
fi  
echo ""  
echo "🎉 Installation completed successfully!"  
echo ""  
echo "🔄 Please restart your terminal or run:"  
echo "   source $rc_file"  
echo ""  
echo "🚀 Then you can start using Claude Code with:"  
echo "   claude"

设置成功,进入 Claude Code 后,看到显示的 API Base URL 是你设置的 API 供应商,就没问题了。

picture.image

最后:API Key 非常重要,如果大家要上传开源项目,或分享、截图等,务必保证 API Key 不要泄露,不然账单就可能会有“惊喜”了。

在平时使用或做开源项目时,把 API Key 等敏感信息留在环境变量里,不要 hardcode 在代码里。

截图或分享时,记得给 API Key 打码。

最后的最后,祝大家 Claude Code + 国产模型用的开心,真的用上几天之后,你就会彻底爱上 Claude Code,个人认为它真的是 AI 开发的优秀形态,我也会在 ClaudeClaude Code 这两个系列中持续更新,欢迎大家关注,朋友们有哪些使用 Claude Code 的实践或问题,也欢迎发给我。

0
0
0
0
关于作者
关于作者

文章

0

获赞

0

收藏

0

相关资源
大规模高性能计算集群优化实践
随着机器学习的发展,数据量和训练模型都有越来越大的趋势,这对基础设施有了更高的要求,包括硬件、网络架构等。本次分享主要介绍火山引擎支撑大规模高性能计算集群的架构和优化实践。
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论