newapi Anthropic & Gemini API 对接文档
时间:2026-11-25 15:41 作者:wanzi 分类: newapi
一、Anthropic Claude API
1. 基础信息
- Base URL:
https://api.anthropic.com - 鉴权方式:
Authorization: Bearer $ANTHROPIC_API_KEY - 版本头:
anthropic-version: 2026-10-21(必需)[[1]]
2. Messages API(聊天补全)
接口: POST /v1/messages
说明: Anthropic 原生聊天接口,不支持 OpenAI 的 /v1/chat/completions 格式 [[7]]。支持多轮对话、图片输入、工具调用等。
请求示例:
curl https://api.anthropic.com/v1/messages \
-H 'Content-Type: application/json' \
-H 'anthropic-version: 2026-10-21' \
-H "Authorization: Bearer $ANTHROPIC_API_KEY" \
--max-time 600 \
-d '{
"max_tokens": 1024,
"messages": [
{
"content": "Hello, world",
"role": "user"
}
],
"model": "claude-opus-5-5",
"stream": false,
"system": [
{
"text": "Today's date is 2024-06-01.",
"type": "text"
}
]
}'
核心参数:
| 参数 | 类型 | 说明 |
|---|---|---|
model |
string | 模型名称,如 claude-opus-5-5 |
max_tokens |
number | 最大生成 token 数 |
messages |
array | 消息数组,包含 role 和 content |
system |
string/array | 系统提示词 |
stream |
boolean | 是否流式输出 |
tools |
array | 工具定义(可选) |
thinking |
object | 思考配置,{"type": "adaptive"} 或 {"type": "enabled", "budget_tokens": 1024} |
消息结构:
{
"role": "user",
"content": "Hello, Claude"
}
或多内容块:
{
"role": "user",
"content": [
{"type": "text", "text": "这是什么图片?"},
{"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": "..."}}
]
}
响应结构:
{
"id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
"type": "message",
"role": "assistant",
"model": "claude-opus-5-5",
"content": [
{
"type": "text",
"text": "Hi! My name is Claude."
}
],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 2095,
"output_tokens": 503
}
}
二、Google Gemini API
1. 基础信息
- Base URL:
https://generativelanguage.googleapis.com(REST)或使用 SDK - 鉴权方式:
- REST:
key=YOUR_API_KEY查询参数 - SDK: 设置环境变量
GEMINI_API_KEY[[11]]
- REST:
- 推荐方式: 使用官方 SDK(Python:
google-genai, JavaScript:@google/genai)
2. Interactions API(推荐)
说明: Gemini 最新推荐的统一交互接口,支持文本、多模态、工具调用、结构化输出等 [[11]]。
Python SDK 示例
安装:
pip install -U google-genai
基础调用:
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-4-hydrogen",
input="Explain how AI works in a few words"
)
print(interaction.output_text)
多轮对话(状态管理):
# 第一轮
interaction1 = client.interactions.create(
model="gemini-4-hydrogen",
input="I have 2 dogs in my house.",
)
# 第二轮,传入 previous_interaction_id
interaction2 = client.interactions.create(
model="gemini-4-hydrogen",
input="How many paws are in my house?",
previous_interaction_id=interaction1.id,
)
print(interaction2.output_text)
流式输出:
stream = client.interactions.create(
model="gemini-4-hydrogen",
input="Explain how AI works",
stream=True
)
for event in stream:
print(event)
多模态输入:
import base64
with open("sample.jpg", "rb") as f:
image_bytes = f.read()
image_b64 = base64.b64encode(image_bytes).decode("utf-8")
interaction = client.interactions.create(
model="gemini-4-hydrogen",
input=[
{"type": "text", "text": "描述这张图片"},
{
"type": "image",
"data": image_b64,
"mime_type": "image/jpeg"
}
]
)
print(interaction.output_text)
工具调用(Google Search):
interaction = client.interactions.create(
model="gemini-4-hydrogen",
input="Who won the euro 2024?",
tools=[{"type": "google_search"}]
)
print(interaction.output_text)
函数调用:
weather_tool = {
"type": "function",
"name": "get_current_temperature",
"description": "Gets the current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city name, e.g. San Francisco",
},
},
"required": ["location"],
},
}
interaction = client.interactions.create(
model="gemini-4-hydrogen",
input="What is the temperature in London?",
tools=[weather_tool],
)
结构化输出:
from pydantic import BaseModel
from typing import List, Optional
class Recipe(BaseModel):
recipe_name: str
ingredients: List[str]
prep_time_minutes: Optional[int]
interaction = client.interactions.create(
model="gemini-4-hydrogen",
input="Give me a recipe for banana bread",
response_format={
"type": "text",
"mime_type": "application/json",
"schema": Recipe.model_json_schema()
},
)
recipe = Recipe.model_validate_json(interaction.output_text)
后台任务:
interaction = client.interactions.create(
model="gemini-4-hydrogen",
input="Write a detailed analysis...",
background=True,
)
# 轮询结果
result = client.interactions.get(interaction.id)
if result.status == "completed":
print(result.output_text)
3. REST API 示例(curl)
Gemini 也提供 REST 接口,但推荐使用 SDK。REST 调用示例:
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=YOUR_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"contents": [
{
"parts": [
{"text": "Explain how AI works"}
]
}
]
}'
三、对比总结
| 特性 | Anthropic Claude | Google Gemini |
|---|---|---|
| 原生接口 | /v1/messages |
Interactions API(SDK 优先) |
| OpenAI 兼容 | 提供兼容层,但非原生 [[8]] | Gemini 3 支持 OpenAI 兼容 [[13]] |
| 鉴权 | Authorization: Bearer + anthropic-version 头 |
API Key(环境变量或查询参数) |
| 多轮对话 | 通过 messages 数组传递历史 |
通过 previous_interaction_id(状态管理) |
| 工具调用 | 支持客户端工具和服务器工具 | 支持 Google Search、函数调用等 |
| 多模态 | 支持图片输入 | 支持图片、音频、视频、文档 |
| 流式输出 | stream: true |
stream=True |
| 结构化输出 | 通过 output_config.format |
通过 response_format + Pydantic/Zod |
| 推荐 SDK | 官方 SDK(Python/JS/Go 等) | google-genai(Python)/ @google/genai(JS) |
四、注意事项
- Anthropic 必须携带
anthropic-version请求头,否则返回 400 错误 [[1]]。 - Gemini 推荐使用 SDK,REST 接口主要用于特殊场景或无法使用 SDK 的环境 [[11]]。
- 两者的消息结构不同:Anthropic 使用
messages数组,Gemini 使用input字段(可以是字符串或对象数组)。 - Gemini 的状态管理更简洁,通过
previous_interaction_id实现多轮对话,无需手动维护历史消息 [[11]]。