⚠️ Alpha内测版本警告:此为早期内部构建版本,尚不完整且可能存在错误,欢迎大家提Issue反馈问题或建议。
Skip to content

API 配置指南

版本: v1.0
最后更新: 2026-06-03

本指南说明如何配置 LLM API,以运行第 5 章的 llm_api_demo.py

前置条件: 已完成 快速开始
需要的命令: pip install -e ".[llm]"


API 提供商对比

提供商模型特点地址国内访问
DeepSeekdeepseek-chat国内首选,极低价格platform.deepseek.com✅ 直接可用
阿里云百炼qwen-plusQwen系列,中文强bailian.aliyun.com✅ 直接可用
智谱 AIglm-4-flashGLM系列,有免费额度open.bigmodel.cn✅ 直接可用
Anthropicclaude-sonnet-4-6综合能力强console.anthropic.com❌ 需VPN
OpenAIgpt-4o-mini生态最广platform.openai.com❌ 需VPN

推荐: 国内用户选择 DeepSeek阿里云百炼


获取 API 密钥

DeepSeek(国内首选)

  1. 访问 platform.deepseek.com
  2. 注册账户
  3. 创建 API Key
  4. 复制密钥

阿里云百炼

  1. 访问 bailian.aliyun.com
  2. 注册或登录
  3. 创建 API Key
  4. 复制密钥

智谱 AI

  1. 访问 open.bigmodel.cn
  2. 注册或登录
  3. 创建 API Key
  4. 复制密钥

OpenAI / Anthropic

需要境外网络和信用卡。


设置环境变量

方式 1:临时设置(当前终端)

bash
# DeepSeek
export DEEPSEEK_API_KEY="your-key-here"

# 阿里云百炼
export DASHSCOPE_API_KEY="your-key-here"

# 智谱 AI
export ZHIPUAI_API_KEY="your-key-here"

# OpenAI
export OPENAI_API_KEY="your-key-here"

# Anthropic
export ANTHROPIC_API_KEY="your-key-here"

然后运行实验:

bash
python code/ch05_llm_basics/llm_api_demo.py

方式 2:永久设置

Linux / macOS:

bash
# 编辑 ~/.bashrc 或 ~/.zshrc
echo 'export DEEPSEEK_API_KEY="your-key-here"' >> ~/.bashrc
source ~/.bashrc

Windows (PowerShell):

powershell
$env:DEEPSEEK_API_KEY="your-key-here"
[Environment]::SetEnvironmentVariable("DEEPSEEK_API_KEY", "your-key-here", "User")

方式 3:.env 文件(推荐)

在项目根目录创建 .env

bash
DEEPSEEK_API_KEY=your-key-here
DASHSCOPE_API_KEY=your-key-here
ZHIPUAI_API_KEY=your-key-here
OPENAI_API_KEY=your-key-here
ANTHROPIC_API_KEY=your-key-here

然后在 Python 中加载:

python
from dotenv import load_dotenv
load_dotenv()

运行第 5 章 API 实验

使用 DeepSeek

bash
export DEEPSEEK_API_KEY="your-key-here"
python code/ch05_llm_basics/llm_api_demo.py

使用阿里云百炼

bash
export DASHSCOPE_API_KEY="your-key-here"
python code/ch05_llm_basics/llm_api_demo.py

使用智谱 AI

bash
export ZHIPUAI_API_KEY="your-key-here"
python code/ch05_llm_basics/llm_api_demo.py

使用 OpenAI

bash
export OPENAI_API_KEY="your-key-here"
python code/ch05_llm_basics/llm_api_demo.py

代码示例

调用 DeepSeek API

python
from openai import OpenAI

client = OpenAI(
    api_key="your-deepseek-key",
    base_url="https://api.deepseek.com"
)

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "user", "content": "你好,请介绍一下 DSP"}
    ]
)

print(response.choices[0].message.content)

调用阿里云百炼 API

python
from openai import OpenAI

client = OpenAI(
    api_key="your-dashscope-key",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)

response = client.chat.completions.create(
    model="qwen-plus",
    messages=[
        {"role": "user", "content": "你好,请介绍一下 DSP"}
    ]
)

print(response.choices[0].message.content)

常见问题

API 调用超时?

检查网络连接。国内用户可以尝试:

  • 使用国内 API(DeepSeek / 阿里云百炼 / 智谱)
  • 检查是否需要代理
  • 增加超时时间

API Key 无效?

  • 确认 Key 已复制完整(无多余空格)
  • 确认 Key 未过期
  • 在提供商网站验证 Key 是否有效

没有 API 预算?

尝试免费方案:

  • 阿里云百炼: 有新用户免费额度
  • 智谱 AI: 有免费额度
  • 本地 Ollama: 完全免费(见 本地推理指南

想用本地模型而不是 API?

本地推理指南 的 Ollama 部分。


获取帮助

本教程采用 CC BY-NC-SA 4.0 许可协议