共计 2941 个字符,预计需要花费 8 分钟才能阅读完成。
ChatGPT 与 Claude 新手入门指南
作为当前最受欢迎的两大 AI 对话模型,ChatGPT(OpenAI)和 Claude(Anthropic)各有特点。本文将带您从零开始了解这两个模型,并通过实际代码示例展示如何快速上手。

1. 模型概览与适用场景
ChatGPT 和 Claude 都是基于 Transformer 架构的大型语言模型,但在设计理念和应用场景上存在一些差异:
- ChatGPT:
- 由 OpenAI 开发,以对话流畅性和创造性见长
- 适合内容生成、创意写作、代码辅助等场景
-
提供多个版本(如 gpt-3.5-turbo、gpt-4)
-
Claude:
- 由 Anthropic 开发,强调安全性和可控性
- 适合需要精确控制输出的场景,如客服、合规内容生成
- 提供 Claude Instant 和 Claude 2 等版本
2. API 设计对比
请求格式差异
-
ChatGPT API:
{ "model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Hello!"}] } -
Claude API:
{ "model": "claude-2", "prompt": "\n\nHuman: Hello!\n\nAssistant:", "max_tokens_to_sample": 300 }
响应结构
- ChatGPT 返回完整的对话历史
- Claude 返回单次补全结果
速率限制
- ChatGPT:免费用户 3 次 / 分钟,付费用户更高
- Claude:通常 5 -15 次 / 分钟,具体取决于账户类型
3. Python 实战代码
基础 API 调用(requests 方式)
import requests
import json
# ChatGPT 调用
def chatgpt_query(api_key, message):
headers = {"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": message}]
}
response = requests.post(
"https://api.openai.com/v1/chat/completions",
headers=headers,
json=data
)
return response.json()["choices"][0]["message"]["content"]
# Claude 调用
def claude_query(api_key, prompt):
headers = {
"x-api-key": api_key,
"Content-Type": "application/json"
}
data = {
"model": "claude-2",
"prompt": f"\n\nHuman: {prompt}\n\nAssistant:",
"max_tokens_to_sample": 300
}
response = requests.post(
"https://api.anthropic.com/v1/complete",
headers=headers,
json=data
)
return response.json()["completion"]
使用官方 SDK(更推荐)
# OpenAI SDK
from openai import OpenAI
client = OpenAI(api_key="your-api-key")
def chatgpt_sdk(message):
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": message}]
)
return response.choices[0].message.content
# Claude SDK(需安装 anthropic)import anthropic
client = anthropic.Anthropic(api_key="your-api-key")
def claude_sdk(prompt):
response = client.completions.create(
model="claude-2",
prompt=f"\n\nHuman: {prompt}\n\nAssistant:",
max_tokens_to_sample=300
)
return response.completion
流式响应实现
# ChatGPT 流式响应
def chatgpt_stream(message):
stream = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": message}],
stream=True
)
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
# Claude 流式响应
def claude_stream(prompt):
with client.completions.stream(
model="claude-2",
prompt=f"\n\nHuman: {prompt}\n\nAssistant:",
max_tokens_to_sample=300
) as stream:
for text in stream:
print(text.completion, end="", flush=True)
4. 生产环境避坑指南
- 超时处理:
- 问题:API 调用可能因网络问题超时
-
解决:设置合理的超时参数并实现重试机制
-
Token 计算:
- 问题:超出模型 token 限制会导致请求失败
-
解决:使用
tiktoken(OpenAI) 或anthropic.count_tokens预计算 -
速率限制:
- 问题:频繁请求会触发速率限制
-
解决:实现请求队列和速率控制
-
成本控制:
- 问题:意外的大量请求可能导致高额费用
-
解决:设置使用量告警,监控 token 消耗
-
内容过滤:
- 问题:可能生成不合适内容
- 解决:实现后处理过滤,或使用模型的 content moderation 功能
5. 实践任务:智能路由系统
尝试构建一个能自动选择合适模型的系统,基于以下规则:
- 当用户请求需要创造性响应时(如写诗、故事),优先使用 ChatGPT
- 当请求需要精确、安全的响应时(如客服咨询),优先使用 Claude
- 根据当前 API 的响应时间动态调整路由策略
- 实现 fallback 机制,当首选模型不可用时自动切换
def smart_router(query):
# 实现你的路由逻辑
if needs_creativity(query):
return chatgpt_sdk(query)
else:
return claude_sdk(query)
结语
通过本文,您应该已经掌握了 ChatGPT 和 Claude 的基础使用方法。两个模型各有优势,在实际项目中可以根据需求灵活选择。建议从简单对话功能开始,逐步尝试更复杂的应用场景。
正文完
发表至: 未分类
近三天内
