🌐 LLM API 接口汇总

国内外主流大模型API调用指南

为什么需要了解LLM API?
┌─────────────────────────────────────────────────────────────────────────┐ │ LLM API 的价值 │ ├─────────────────────────────────────────────────────────────────────────┤ │ │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ 即开即用 │ → │ 按需付费 │ → │ 快速集成 │ │ │ │ │ │ │ │ │ │ │ │ 无需训练 │ │ 按token计费 │ │ SDK封装 │ │ │ │ 开箱即用 │ │ 成本可控 │ │ 几行代码 │ │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ │ │ │ 主流场景:原型开发、聊天机器人、内容生成、应用集成 │ │ │ └─────────────────────────────────────────────────────────────────────────┘

🇺🇸 国外主流API

厂商 模型 特点 价格
OpenAI GPT-4o, GPT-4, GPT-3.5 生态完善、功能全面 $5-30/M tokens
Anthropic Claude 3.5, Claude 3 长上下文、安全对齐 $3-15/M tokens
Google Gemini 1.5, Gemini Pro 多模态原生、多语言 $0.5-5/M tokens
Meta Llama 3 (开源) 开源免费、可私有部署 免费

🇨🇳 国内主流API

厂商 模型 特点 价格
百度 ERNIE 4.0, ERNIE 3.5 中文理解强、推理能力强 ¥0.012-0.12/千tokens
阿里 Qwen 2.5, Qwen-Max 开源生态好、性能优秀 ¥0.008-0.02/千tokens
腾讯 混元, 混元-Pro 多模态能力强 ¥0.01-0.1/千tokens
字节跳动 豆包, Doubao-pro 性价比高、响应快 ¥0.001-0.005/千tokens
智谱AI GLM-4, GLM-3-Turbo 中英双语、工具调用强 ¥0.005-0.1/千tokens
深度求索 DeepSeek-V2, DeepSeek-Chat 推理能力强、价格低 ¥0.001-0.004/千tokens
月之暗面 Kimi (Moonshot) 超长上下文(200万字) ¥0.012-0.12/千tokens
百川智能 Baichuan 4 中文优化 ¥0.012/千tokens
API调用最佳实践

🔐 安全建议

  • 使用环境变量存储API Key
  • 不要在代码中硬编码密钥
  • 设置API Key使用限制
  • 定期轮换密钥

💰 成本优化

  • 选择合适的模型大小
  • 减少不必要的上下文
  • 使用缓存避免重复调用
  • 设置max_tokens限制

🛡️ 错误处理

  • 实现重试机制
  • 处理Rate Limit错误
  • 设置超时时间
  • 记录调用日志

💬 messages 参数详解

什么是 messages 参数?
┌─────────────────────────────────────────────────────────────────────────┐ │ messages 参数的作用 │ ├─────────────────────────────────────────────────────────────────────────┤ │ │ │ messages = 对话历史记录 │ │ │ │ ┌─────────────────────────────────────────────────────────────────┐ │ │ │ │ │ │ │ 你 → "帮我写一首诗" ← 发送给模型的信息 │ │ │ │ │ │ │ │ 模型 → "好的,这是诗..." ← 模型的回复 │ │ │ │ │ │ │ │ messages 保存了对话的上下文,让模型理解对话的来龙去脉 │ │ │ │ │ │ │ └─────────────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────────┘
role 角色类型
┌─────────────────────────────────────────────────────────────────────────┐ │ role 的四种角色 │ ├─────────────────────────────────────────────────────────────────────────┤ │ │ │ ┌─────────────┬──────────────────────────────────────────────────┐ │ │ │ role │ 说明 │ │ │ ├─────────────┼──────────────────────────────────────────────────┤ │ │ │ system │ 系统指令:设定AI的身份和行为规则 │ │ │ │ │ "你是一个专业编程助手" │ │ │ ├─────────────┼──────────────────────────────────────────────────┤ │ │ │ user │ 用户输入:人类说的话 │ │ │ │ │ "帮我写一个排序算法" │ │ │ ├─────────────┼──────────────────────────────────────────────────┤ │ │ │ assistant │ AI回复:模型生成的内容 │ │ │ │ │ "这是Python的快速排序实现..." │ │ │ ├─────────────┼──────────────────────────────────────────────────┤ │ │ │ tool │ 工具调用:AI调用外部工具的结果(高级功能) │ │ │ └─────────────┴──────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────────┘

📝 通俗类比

📖 想象一个剧本

┌─────────────────────────────────────────┐ │ │ │ system: 设定角色 │ │ "你是一个老师,教初中生数学" │ │ │ │ user: 学生提问 │ │ "老师,什么是勾股定理?" │ │ │ │ assistant: 老师回答 │ │ "勾股定理是..." │ │ │ └─────────────────────────────────────────┘

system 决定角色,user 和 assistant 轮流对话

💬 微信聊天记录

┌─────────────────────────────────────────┐ │ │ │ system: 群公告 │ │ "本群是技术交流群,禁止灌水" │ │ │ │ user: 张三发消息 │ │ "有人知道Python怎么安装吗?" │ │ │ │ assistant: 李四回复 │ │ "去官网下载安装包..." │ │ │ └─────────────────────────────────────────┘

messages 就是聊天记录,AI根据上下文回复

💻 代码示例对比

示例:最简单的对话
# 最简单的 messages 格式
messages = [
    {"role": "user", "content": "你好!"}
]

# 返回的 reply
# assistant: "你好!有什么我可以帮你的吗?"
示例:带 system 设定
# 定义AI的角色和行为
messages = [
    {"role": "system", "content": "你是一个温柔的老师,说话要鼓励学生"},
    {"role": "user", "content": "我学不会编程怎么办?"}
]

# 返回的 reply
# assistant: "别担心,编程很有趣,我们从简单开始..."
示例:多轮对话
# 对话历史会累积
messages = [
    {"role": "system", "content": "你是一个助手"},
    # 第一轮
    {"role": "user", "content": "什么是AI?"},
    {"role": "assistant", "content": "AI是人工智能..."},
    # 第二轮
    {"role": "user", "content": "它能做什么?"},
    {"role": "assistant", "content": "AI可以..."},
    # 第三轮
    {"role": "user", "content": "python怎么学?"},  # AI还记得之前的上下文
]

# ⚠️ 注意:每次请求都要把之前的对话历史都传进去!
# 模型没有记忆,你需要把历史都发给它

⚠️ 重要注意事项

🔄 历史必须传递

  • 模型每次都是"全新"的,不记得上次对话
  • 必须把历史 messages 全部传入
  • 新消息追加到列表末尾

❌ 错误做法:每次只传最新问题

✅ 正确做法:每次都传完整对话历史

📏 长度限制

  • messages 有最大长度限制(通常是几万token)
  • 超长对话需要"裁剪"历史
  • 可以只保留最近的对话,或摘要关键信息

💡 提示:不同模型的上下文长度不同

🎯 最佳实践

场景 建议
设定角色 第一条 system 消息设定AI身份,如"你是一个专业客服"
Few-shot示例 给几个例子让模型学习格式,如:用户问"北京",回复"北京是中国的首都"
多轮对话 每次追加 user 和 assistant 的对话,保持上下文
长对话 只保留最近N轮,或使用 RAG 检索关键信息
结构化输出 在 system 中明确要求输出格式,如"用JSON格式回答"

🇺🇸 国外主流API

厂商 模型 特点 价格
OpenAI GPT-4o, GPT-4, GPT-3.5 生态完善、功能全面 $5-30/M tokens
Anthropic Claude 3.5, Claude 3 长上下文、安全对齐 $3-15/M tokens
Google Gemini 1.5, Gemini Pro 多模态原生、多语言 $0.5-5/M tokens
Meta Llama 3 (开源) 开源免费、可私有部署 免费

🇨🇳 国内主流API

厂商 模型 特点 价格
百度 ERNIE 4.0, ERNIE 3.5 中文理解强、推理能力强 ¥0.012-0.12/千tokens
阿里 Qwen 2.5, Qwen-Max 开源生态好、性能优秀 ¥0.008-0.02/千tokens
腾讯 混元, 混元-Pro 多模态能力强 ¥0.01-0.1/千tokens
字节跳动 豆包, Doubao-pro 性价比高、响应快 ¥0.001-0.005/千tokens
智谱AI GLM-4, GLM-3-Turbo 中英双语、工具调用强 ¥0.005-0.1/千tokens
深度求索 DeepSeek-V2, DeepSeek-Chat 推理能力强、价格低 ¥0.001-0.004/千tokens
月之暗面 Kimi (Moonshot) 超长上下文(200万字) ¥0.012-0.12/千tokens
百川智能 Baichuan 4 中文优化 ¥0.012/千tokens

💻 Python API 调用示例

示例1:OpenAI GPT-4
# 安装: pip install openai
from openai import OpenAI

# 初始化客户端
client = OpenAI(
    api_key="your-api-key",  # 替换为你的API Key
    base_url="https://api.openai.com/v1"
)

# 调用模型
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "你是一个专业助手"},
        {"role": "user", "content": "请解释什么是机器学习?"}
    ],
    temperature=0.7,
    max_tokens=1000
)

# 获取回复
print(response.choices[0].message.content)
示例2:Claude (Anthropic)
# 安装: pip install anthropic
from anthropic import Anthropic

# 初始化客户端
client = Anthropic(
    api_key="your-api-key"  # 替换为你的API Key
)

# 调用模型
response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1000,
    messages=[
        {"role": "user", "content": "请解释什么是深度学习?"}
    ]
)

# 获取回复
print(response.content[0].text)
示例3:百度文心一言
# 安装: pip install qianfan
import qianfan

# 初始化客户端
client = qianfan.ChatCompletion(
    ak="your-access-key",      # 替换为你的AK
    sk="your-secret-key"       # 替换为你的SK
)

# 调用模型
response = client.do(
    model="ernie-4.5-8k",
    messages=[
        {"role": "user", "content": "请解释什么是自然语言处理?"}
    ],
    temperature=0.7,
    penalty_score=1.0
)

# 获取回复
print(response["body"]["result"])
示例4:阿里通义千问
# 安装: pip install dashscope
from dashscope import ChatCompletion

# 调用模型
response = ChatCompletion.call(
    model="qwen-max",
    messages=[
        {"role": "user", "content": "请解释什么是卷积神经网络?"}
    ],
    temperature=0.7,
    max_tokens=1000
)

# 获取回复
print(response.output.choices[0].message.content)
示例5:深度求索 DeepSeek
# 安装: pip install openai
from openai import OpenAI

# 初始化客户端(兼容OpenAI接口)
client = OpenAI(
    api_key="your-api-key",  # 替换为你的API Key
    base_url="https://api.deepseek.com/v1"
)

# 调用模型
response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "user", "content": "请解释什么是Transformer架构?"}
    ],
    temperature=0.7,
    max_tokens=1000
)

# 获取回复
print(response.choices[0].message.content)
示例6:统一调用封装
"""
统一的大语言模型调用封装
支持切换不同提供商
"""
from abc import ABC, abstractmethod
from typing import List, Dict

class LLMProvider(ABC):
    """LLM提供商抽象基类"""
    
    @abstractmethod
    def chat(self, messages: List[Dict], **kwargs) -> str:
        pass

class OpenAIProvider(LLMProvider):
    def __init__(self, api_key: str, model: str = "gpt-4o"):
        from openai import OpenAI
        self.client = OpenAI(api_key=api_key)
        self.model = model
    
    def chat(self, messages: List[Dict], temperature: float = 0.7, **kwargs) -> str:
        response = self.client.chat.completions.create(
            model=self.model,
            messages=messages,
            temperature=temperature,
            **kwargs
        )
        return response.choices[0].message.content

class DeepSeekProvider(LLMProvider):
    def __init__(self, api_key: str, model: str = "deepseek-chat"):
        from openai import OpenAI
        self.client = OpenAI(
            api_key=api_key,
            base_url="https://api.deepseek.com/v1"
        )
        self.model = model
    
    def chat(self, messages: List[Dict], temperature: float = 0.7, **kwargs) -> str:
        response = self.client.chat.completions.create(
            model=self.model,
            messages=messages,
            temperature=temperature,
            **kwargs
        )
        return response.choices[0].message.content

class QwenProvider(LLMProvider):
    def __init__(self, api_key: str, model: str = "qwen-max"):
        self.api_key = api_key
        self.model = model
    
    def chat(self, messages: List[Dict], temperature: float = 0.7, **kwargs) -> str:
        from dashscope import ChatCompletion
        response = ChatCompletion.call(
            model=self.model,
            messages=messages,
            temperature=temperature,
            **kwargs
        )
        return response.output.choices[0].message.content

# 工厂函数
def create_llm_provider(provider: str, api_key: str, model: str = None) -> LLMProvider:
    providers = {
        "openai": lambda: OpenAIProvider(api_key, model or "gpt-4o"),
        "deepseek": lambda: DeepSeekProvider(api_key, model or "deepseek-chat"),
        "qwen": lambda: QwenProvider(api_key, model or "qwen-max"),
    }
    return providers[provider.lower()]()

# 使用示例
if __name__ == "__main__":
    messages = [
        {"role": "system", "content": "你是一个专业助手"},
        {"role": "user", "content": "什么是机器学习?"}
    ]
    
    # 切换不同提供商
    provider = create_llm_provider(
        provider="deepseek",
        api_key="your-api-key",
        model="deepseek-chat"
    )
    
    response = provider.chat(messages)
    print(response)
API调用最佳实践

🔐 安全建议

  • 使用环境变量存储API Key
  • 不要在代码中硬编码密钥
  • 设置API Key使用限制
  • 定期轮换密钥

💰 成本优化

  • 选择合适的模型大小
  • 减少不必要的上下文
  • 使用缓存避免重复调用
  • 设置max_tokens限制

🛡️ 错误处理

  • 实现重试机制
  • 处理Rate Limit错误
  • 设置超时时间
  • 记录调用日志