aicodeswitch 2.1.2 → 2.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -12
- package/dist/server/database.js +4 -0
- package/dist/server/main.js +39 -0
- package/dist/server/proxy-server.js +6 -4
- package/dist/types/index.js +9 -0
- package/dist/ui/assets/index-BVblbmz5.css +1 -0
- package/dist/ui/assets/index-BqBZrCTy.js +470 -0
- package/dist/ui/assets/logo-CRx_w1Ov.png +0 -0
- package/dist/ui/index.html +2 -2
- package/package.json +6 -2
- package/.github/workflows/publish-to-npm.yaml +0 -34
- package/CHANGELOG.md +0 -43
- package/CLAUDE.md +0 -253
- package/TECH.md +0 -315
- package/dist/ui/assets/index-B9EyKK90.js +0 -452
- package/dist/ui/assets/index-Bf2ZEtnh.css +0 -1
- package/dist/ui/vendors-recommand.md +0 -14
- package/public/donate-to-me.png +0 -0
- package/public/migration.md +0 -7
- package/public/vendors-recommand.md +0 -14
- package/schemes/claude.schema.md +0 -945
- package/schemes/openai.schema.md +0 -2162
package/schemes/claude.schema.md
DELETED
|
@@ -1,945 +0,0 @@
|
|
|
1
|
-
# Claude API Schema 标准参考
|
|
2
|
-
|
|
3
|
-
Anthropic 官方 Messages API 数据格式规范和完整使用指南。
|
|
4
|
-
|
|
5
|
-
## 目录
|
|
6
|
-
- [API 概览](#api-概览)
|
|
7
|
-
- [Messages API 标准格式](#messages-api-标准格式)
|
|
8
|
-
- [模型选择](#模型选择)
|
|
9
|
-
- [思考 (Thinking) 功能](#思考-thinking-功能)
|
|
10
|
-
- [流式事件](#流式事件)
|
|
11
|
-
- [错误处理](#错误处理)
|
|
12
|
-
- [快速参考](#快速参考)
|
|
13
|
-
- [最佳实践](#最佳实践)
|
|
14
|
-
|
|
15
|
-
---
|
|
16
|
-
|
|
17
|
-
## API 概览
|
|
18
|
-
|
|
19
|
-
### 官方 Messages API
|
|
20
|
-
|
|
21
|
-
这是 **唯一的标准 Claude API**:
|
|
22
|
-
|
|
23
|
-
```
|
|
24
|
-
POST /v1/messages
|
|
25
|
-
Host: api.anthropic.com
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
所有下列都使用此 API:
|
|
29
|
-
- 代码生成任务
|
|
30
|
-
- 自然对话
|
|
31
|
-
- 文本分析
|
|
32
|
-
- 图像识别
|
|
33
|
-
- 工具调用
|
|
34
|
-
- 深度推理(Thinking)
|
|
35
|
-
|
|
36
|
-
### 模型功能矩阵
|
|
37
|
-
|
|
38
|
-
| 特性 | Claude 3.5 Sonnet | Claude 3.5 Haiku | Claude 3 Opus | 支持情况 |
|
|
39
|
-
|-----|-----------|-----------|---------|---------|
|
|
40
|
-
| **最大 context** | 200k | 200k | 200k | 全部支持 |
|
|
41
|
-
| **最佳用途** | 平衡性能 | 快速应答 | 复杂推理 | 都用同 API |
|
|
42
|
-
| **流式响应** | ✅ | ✅ | ✅ | 全部支持 |
|
|
43
|
-
| **工具调用** | ✅ | ✅ | ✅ | 全部支持 |
|
|
44
|
-
| **图像支持** | ✅ | ✅ | ✅ | 全部支持 |
|
|
45
|
-
| **Thinking 功能** | ✅ | ✅ | ✅ | 全部支持 |
|
|
46
|
-
|
|
47
|
-
---
|
|
48
|
-
|
|
49
|
-
## Messages API 标准格式
|
|
50
|
-
|
|
51
|
-
### 请求格式 (完整规范)
|
|
52
|
-
|
|
53
|
-
```typescript
|
|
54
|
-
POST /v1/messages
|
|
55
|
-
Host: api.anthropic.com
|
|
56
|
-
Content-Type: application/json
|
|
57
|
-
x-api-key: sk-ant-...
|
|
58
|
-
anthropic-version: 2023-06-01
|
|
59
|
-
|
|
60
|
-
{
|
|
61
|
-
// ===== 必需字段 =====
|
|
62
|
-
"model": string, // 模型ID
|
|
63
|
-
"max_tokens": number, // 1-200000
|
|
64
|
-
"messages": Array<Message>, // 用户消息历史
|
|
65
|
-
|
|
66
|
-
// ===== 可选字段 =====
|
|
67
|
-
"system"?: string | SystemBlock[], // 系统提示
|
|
68
|
-
"temperature"?: number, // 0.0-1.0,采样温度
|
|
69
|
-
"top_p"?: number, // 0.0-1.0,核采样
|
|
70
|
-
"top_k"?: number, // 排名采样
|
|
71
|
-
"stream"?: boolean, // 是否流式响应
|
|
72
|
-
"tools"?: Tool[], // 工具定义
|
|
73
|
-
"tool_choice"?: ToolChoice, // 工具调用策略
|
|
74
|
-
"stop_sequences"?: string[], // 停止序列
|
|
75
|
-
"thinking"?: ThinkingConfig, // 思考配置
|
|
76
|
-
"budget_tokens"?: number // 思考预算(新名称)
|
|
77
|
-
}
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
### 消息格式
|
|
81
|
-
|
|
82
|
-
```typescript
|
|
83
|
-
interface Message {
|
|
84
|
-
role: "user" | "assistant";
|
|
85
|
-
content: string | ContentBlock[];
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
type ContentBlock =
|
|
89
|
-
| TextContent
|
|
90
|
-
| ImageContent
|
|
91
|
-
| ToolUseContent
|
|
92
|
-
| ToolResultContent
|
|
93
|
-
| ThinkingContent
|
|
94
|
-
| DocumentContent;
|
|
95
|
-
|
|
96
|
-
// 文本内容
|
|
97
|
-
interface TextContent {
|
|
98
|
-
type: "text";
|
|
99
|
-
text: string;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// 图像内容
|
|
103
|
-
interface ImageContent {
|
|
104
|
-
type: "image";
|
|
105
|
-
source: {
|
|
106
|
-
type: "base64" | "url" | "file";
|
|
107
|
-
media_type?: "image/jpeg" | "image/png" | "image/gif" | "image/webp";
|
|
108
|
-
data?: string; // base64 编码
|
|
109
|
-
url?: string;
|
|
110
|
-
file_id?: string;
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// 工具使用
|
|
115
|
-
interface ToolUseContent {
|
|
116
|
-
type: "tool_use";
|
|
117
|
-
id: string;
|
|
118
|
-
name: string;
|
|
119
|
-
input: object;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// 工具结果
|
|
123
|
-
interface ToolResultContent {
|
|
124
|
-
type: "tool_result";
|
|
125
|
-
tool_use_id: string;
|
|
126
|
-
content: string | object;
|
|
127
|
-
is_error?: boolean;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// 思考内容
|
|
131
|
-
interface ThinkingContent {
|
|
132
|
-
type: "thinking";
|
|
133
|
-
thinking: string;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// 文档内容
|
|
137
|
-
interface DocumentContent {
|
|
138
|
-
type: "document";
|
|
139
|
-
source: {
|
|
140
|
-
type: "file" | "base64";
|
|
141
|
-
file_id?: string;
|
|
142
|
-
data?: string;
|
|
143
|
-
media_type?: string;
|
|
144
|
-
};
|
|
145
|
-
title?: string;
|
|
146
|
-
}
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
### 响应格式 (标准)
|
|
150
|
-
|
|
151
|
-
```typescript
|
|
152
|
-
{
|
|
153
|
-
"id": string, // 消息 ID
|
|
154
|
-
"type": "message", // 固定值
|
|
155
|
-
"role": "assistant", // 角色
|
|
156
|
-
"content": ContentBlock[], // 响应内容
|
|
157
|
-
"model": string, // 使用的模型
|
|
158
|
-
"stop_reason":
|
|
159
|
-
"end_turn" |
|
|
160
|
-
"max_tokens" |
|
|
161
|
-
"tool_use" |
|
|
162
|
-
"stop_sequence" |
|
|
163
|
-
"max_thinking_length",
|
|
164
|
-
"stop_sequence"?: string | null, // 触发的停止序列
|
|
165
|
-
"usage": {
|
|
166
|
-
"input_tokens": number, // 输入 token 数
|
|
167
|
-
"output_tokens": number, // 输出 token 数
|
|
168
|
-
"cache_creation_input_tokens"?: number, // 缓存创建 token
|
|
169
|
-
"cache_read_input_tokens"?: number // 缓存读取 token
|
|
170
|
-
},
|
|
171
|
-
"thinking_tokens_used"?: number // 思考使用的 token
|
|
172
|
-
}
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
### 最小请求示例
|
|
176
|
-
|
|
177
|
-
```bash
|
|
178
|
-
curl -X POST https://api.anthropic.com/v1/messages \
|
|
179
|
-
-H "x-api-key: sk-ant-..." \
|
|
180
|
-
-H "anthropic-version: 2023-06-01" \
|
|
181
|
-
-H "content-type: application/json" \
|
|
182
|
-
-d '{
|
|
183
|
-
"model": "claude-3-5-sonnet-20241022",
|
|
184
|
-
"max_tokens": 1024,
|
|
185
|
-
"messages": [
|
|
186
|
-
{"role": "user", "content": "Hello"}
|
|
187
|
-
]
|
|
188
|
-
}'
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
### 响应示例
|
|
192
|
-
|
|
193
|
-
```json
|
|
194
|
-
{
|
|
195
|
-
"id": "msg_1234567890",
|
|
196
|
-
"type": "message",
|
|
197
|
-
"role": "assistant",
|
|
198
|
-
"content": [
|
|
199
|
-
{
|
|
200
|
-
"type": "text",
|
|
201
|
-
"text": "Hello! How can I help you today?"
|
|
202
|
-
}
|
|
203
|
-
],
|
|
204
|
-
"model": "claude-3-5-sonnet-20241022",
|
|
205
|
-
"stop_reason": "end_turn",
|
|
206
|
-
"usage": {
|
|
207
|
-
"input_tokens": 10,
|
|
208
|
-
"output_tokens": 15
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
---
|
|
214
|
-
|
|
215
|
-
## 模型选择
|
|
216
|
-
|
|
217
|
-
Anthropic 官方发布的模型列表(使用同一 Messages API):
|
|
218
|
-
|
|
219
|
-
### 最新模型(推荐)
|
|
220
|
-
|
|
221
|
-
```typescript
|
|
222
|
-
const models = {
|
|
223
|
-
"claude-3-5-sonnet-20241022": {
|
|
224
|
-
description: "最强性能/成本比(推荐首选)",
|
|
225
|
-
input_cost: "$3 / 1M tokens",
|
|
226
|
-
output_cost: "$15 / 1M tokens",
|
|
227
|
-
best_for: "大多数应用"
|
|
228
|
-
},
|
|
229
|
-
"claude-3-5-haiku-20241022": {
|
|
230
|
-
description: "最快,成本最低",
|
|
231
|
-
input_cost: "$0.80 / 1M tokens",
|
|
232
|
-
output_cost: "$4 / 1M tokens",
|
|
233
|
-
best_for: "快速应答,简单任务"
|
|
234
|
-
},
|
|
235
|
-
"claude-3-opus-20240229": {
|
|
236
|
-
description: "最强推理能力",
|
|
237
|
-
input_cost: "$15 / 1M tokens",
|
|
238
|
-
output_cost: "$75 / 1M tokens",
|
|
239
|
-
best_for: "复杂分析,深度推理"
|
|
240
|
-
}
|
|
241
|
-
};
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
### 选择决策树
|
|
245
|
-
|
|
246
|
-
```
|
|
247
|
-
问题类型?
|
|
248
|
-
├─ 简单查询 (是) ──> claude-3-5-haiku
|
|
249
|
-
├─ 代码生成 (中等复杂) ──> claude-3-5-sonnet
|
|
250
|
-
├─ 复杂推理、深度分析 ──> claude-3-opus
|
|
251
|
-
└─ 成本敏感 ──> haiku
|
|
252
|
-
|
|
253
|
-
需要深思熟虑吗?
|
|
254
|
-
├─ 是 ──> 启用 thinking 功能
|
|
255
|
-
└─ 否 ──> 直接生成
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
---
|
|
259
|
-
|
|
260
|
-
## 多轮对话示例
|
|
261
|
-
|
|
262
|
-
```bash
|
|
263
|
-
curl -X POST https://api.anthropic.com/v1/messages \
|
|
264
|
-
-H "x-api-key: sk-ant-..." \
|
|
265
|
-
-d '{
|
|
266
|
-
"model": "claude-3-5-sonnet-20241022",
|
|
267
|
-
"max_tokens": 1024,
|
|
268
|
-
"messages": [
|
|
269
|
-
{
|
|
270
|
-
"role": "user",
|
|
271
|
-
"content": "什么是 Python?"
|
|
272
|
-
},
|
|
273
|
-
{
|
|
274
|
-
"role": "assistant",
|
|
275
|
-
"content": "Python 是一门高级编程语言..."
|
|
276
|
-
},
|
|
277
|
-
{
|
|
278
|
-
"role": "user",
|
|
279
|
-
"content": "给个简单的例子"
|
|
280
|
-
}
|
|
281
|
-
]
|
|
282
|
-
}'
|
|
283
|
-
```
|
|
284
|
-
|
|
285
|
-
---
|
|
286
|
-
|
|
287
|
-
## 工具调用示例
|
|
288
|
-
|
|
289
|
-
```bash
|
|
290
|
-
curl -X POST https://api.anthropic.com/v1/messages \
|
|
291
|
-
-H "x-api-key: sk-ant-..." \
|
|
292
|
-
-d '{
|
|
293
|
-
"model": "claude-3-5-sonnet-20241022",
|
|
294
|
-
"max_tokens": 1024,
|
|
295
|
-
"tools": [
|
|
296
|
-
{
|
|
297
|
-
"name": "get_weather",
|
|
298
|
-
"description": "获取天气信息",
|
|
299
|
-
"input_schema": {
|
|
300
|
-
"type": "object",
|
|
301
|
-
"properties": {
|
|
302
|
-
"location": {
|
|
303
|
-
"type": "string",
|
|
304
|
-
"description": "城市名称"
|
|
305
|
-
}
|
|
306
|
-
},
|
|
307
|
-
"required": ["location"]
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
],
|
|
311
|
-
"messages": [
|
|
312
|
-
{
|
|
313
|
-
"role": "user",
|
|
314
|
-
"content": "旧金山的天气如何?"
|
|
315
|
-
}
|
|
316
|
-
]
|
|
317
|
-
}'
|
|
318
|
-
```
|
|
319
|
-
|
|
320
|
-
---
|
|
321
|
-
|
|
322
|
-
## 图像处理示例
|
|
323
|
-
|
|
324
|
-
```json
|
|
325
|
-
{
|
|
326
|
-
"model": "claude-3-5-sonnet-20241022",
|
|
327
|
-
"max_tokens": 1024,
|
|
328
|
-
"messages": [
|
|
329
|
-
{
|
|
330
|
-
"role": "user",
|
|
331
|
-
"content": [
|
|
332
|
-
{
|
|
333
|
-
"type": "image",
|
|
334
|
-
"source": {
|
|
335
|
-
"type": "base64",
|
|
336
|
-
"media_type": "image/jpeg",
|
|
337
|
-
"data": "iVBORw0KGgoAAAANSUhEUgAAAA..."
|
|
338
|
-
}
|
|
339
|
-
},
|
|
340
|
-
{
|
|
341
|
-
"type": "text",
|
|
342
|
-
"text": "这张图片里有什么?"
|
|
343
|
-
}
|
|
344
|
-
]
|
|
345
|
-
}
|
|
346
|
-
]
|
|
347
|
-
}
|
|
348
|
-
```
|
|
349
|
-
|
|
350
|
-
---
|
|
351
|
-
|
|
352
|
-
## 思考 (Thinking) 功能
|
|
353
|
-
|
|
354
|
-
### 官方定义
|
|
355
|
-
|
|
356
|
-
思考(Extended Thinking)是 Anthropic 提供的一项功能,允许模型在生成最终响应前进行内部推理。
|
|
357
|
-
|
|
358
|
-
**支持情况**:所有模型都支持,通过同一个 Messages API 启用
|
|
359
|
-
|
|
360
|
-
**关键特性**:
|
|
361
|
-
- 思考内容对用户可见
|
|
362
|
-
- 思考 token 单独计费,通常更便宜(约为输出成本的 30-50%)
|
|
363
|
-
- 预算范围:1,000 - 500,000 tokens
|
|
364
|
-
- 改进复杂问题的准确性
|
|
365
|
-
|
|
366
|
-
### 配置方式
|
|
367
|
-
|
|
368
|
-
```json
|
|
369
|
-
{
|
|
370
|
-
"thinking": {
|
|
371
|
-
"type": "enabled",
|
|
372
|
-
"budget_tokens": 10000
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
```
|
|
376
|
-
|
|
377
|
-
### 预算选择指南
|
|
378
|
-
|
|
379
|
-
```typescript
|
|
380
|
-
const budgetRecommendations = {
|
|
381
|
-
"simple_tasks": 2000, // 快速计算、简单推理
|
|
382
|
-
"moderate_tasks": 5000, // 代码审查、一般分析
|
|
383
|
-
"complex_tasks": 15000, // 数学证明、架构设计
|
|
384
|
-
"very_complex": 30000 // 深度研究、复杂算法
|
|
385
|
-
};
|
|
386
|
-
```
|
|
387
|
-
|
|
388
|
-
### 响应格式示例
|
|
389
|
-
|
|
390
|
-
```json
|
|
391
|
-
{
|
|
392
|
-
"content": [
|
|
393
|
-
{
|
|
394
|
-
"type": "thinking",
|
|
395
|
-
"thinking": "让我分析这个问题...\n第一步:识别问题类型\n第二步:考虑边界情况\n第三步:推导解决方案"
|
|
396
|
-
},
|
|
397
|
-
{
|
|
398
|
-
"type": "text",
|
|
399
|
-
"text": "基于以上分析,答案是..."
|
|
400
|
-
}
|
|
401
|
-
],
|
|
402
|
-
"thinking_tokens_used": 4521,
|
|
403
|
-
"usage": {
|
|
404
|
-
"input_tokens": 50,
|
|
405
|
-
"output_tokens": 150,
|
|
406
|
-
"thinking_tokens_used": 4521
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
```
|
|
410
|
-
|
|
411
|
-
### 完整示例
|
|
412
|
-
|
|
413
|
-
```bash
|
|
414
|
-
curl -X POST https://api.anthropic.com/v1/messages \
|
|
415
|
-
-H "x-api-key: sk-ant-..." \
|
|
416
|
-
-H "anthropic-version: 2023-06-01" \
|
|
417
|
-
-d '{
|
|
418
|
-
"model": "claude-3-opus-20240229",
|
|
419
|
-
"max_tokens": 2048,
|
|
420
|
-
"thinking": {
|
|
421
|
-
"type": "enabled",
|
|
422
|
-
"budget_tokens": 10000
|
|
423
|
-
},
|
|
424
|
-
"messages": [
|
|
425
|
-
{
|
|
426
|
-
"role": "user",
|
|
427
|
-
"content": "证明 sqrt(2) 是无理数"
|
|
428
|
-
}
|
|
429
|
-
]
|
|
430
|
-
}'
|
|
431
|
-
```
|
|
432
|
-
|
|
433
|
-
### 处理思考超时
|
|
434
|
-
|
|
435
|
-
当响应中 `stop_reason === "max_thinking_length"` 时,表示思考预算不足:
|
|
436
|
-
|
|
437
|
-
```typescript
|
|
438
|
-
if (response.stop_reason === "max_thinking_length") {
|
|
439
|
-
// 增加预算后重新请求
|
|
440
|
-
const newBudget = (response.thinking_tokens_used || 5000) * 1.5;
|
|
441
|
-
console.log(`思考预算不足,将${newBudget}重试`);
|
|
442
|
-
|
|
443
|
-
// 重新发送请求,使用更大的 budget_tokens
|
|
444
|
-
}
|
|
445
|
-
```
|
|
446
|
-
|
|
447
|
-
---
|
|
448
|
-
|
|
449
|
-
## 流式事件
|
|
450
|
-
|
|
451
|
-
### 启用流式响应
|
|
452
|
-
|
|
453
|
-
在请求中添加:
|
|
454
|
-
```json
|
|
455
|
-
{"stream": true}
|
|
456
|
-
```
|
|
457
|
-
|
|
458
|
-
### 事件类型
|
|
459
|
-
|
|
460
|
-
#### 消息开始
|
|
461
|
-
```
|
|
462
|
-
event: message_start
|
|
463
|
-
data: {"type":"message_start","message":{...}}
|
|
464
|
-
```
|
|
465
|
-
|
|
466
|
-
#### 内容块开始
|
|
467
|
-
```
|
|
468
|
-
event: content_block_start
|
|
469
|
-
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
|
|
470
|
-
```
|
|
471
|
-
|
|
472
|
-
#### 文本增量
|
|
473
|
-
```
|
|
474
|
-
event: content_block_delta
|
|
475
|
-
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}
|
|
476
|
-
```
|
|
477
|
-
|
|
478
|
-
#### 思考增量
|
|
479
|
-
```
|
|
480
|
-
event: content_block_delta
|
|
481
|
-
data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"让我分析..."}}
|
|
482
|
-
```
|
|
483
|
-
|
|
484
|
-
#### 工具输入增量
|
|
485
|
-
```
|
|
486
|
-
event: content_block_delta
|
|
487
|
-
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"arg\":"}}
|
|
488
|
-
```
|
|
489
|
-
|
|
490
|
-
#### 内容块结束
|
|
491
|
-
```
|
|
492
|
-
event: content_block_stop
|
|
493
|
-
data: {"type":"content_block_stop","index":0}
|
|
494
|
-
```
|
|
495
|
-
|
|
496
|
-
#### Token 更新
|
|
497
|
-
```
|
|
498
|
-
event: message_delta
|
|
499
|
-
data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":42,"thinking_tokens":5234}}
|
|
500
|
-
```
|
|
501
|
-
|
|
502
|
-
#### 消息停止
|
|
503
|
-
```
|
|
504
|
-
event: message_stop
|
|
505
|
-
data: {"type":"message_stop"}
|
|
506
|
-
```
|
|
507
|
-
|
|
508
|
-
### 流式请求示例
|
|
509
|
-
|
|
510
|
-
```bash
|
|
511
|
-
curl -X POST http://api.anthropic.com/v1/messages \
|
|
512
|
-
-H "Content-Type: application/json" \
|
|
513
|
-
-H "Authorization: Bearer sk-ant-..." \
|
|
514
|
-
-d '{
|
|
515
|
-
"model": "claude-3-sonnet-20240229",
|
|
516
|
-
"max_tokens": 512,
|
|
517
|
-
"stream": true,
|
|
518
|
-
"messages": [
|
|
519
|
-
{"role": "user", "content": "Write a poem about spring"}
|
|
520
|
-
]
|
|
521
|
-
}'
|
|
522
|
-
```
|
|
523
|
-
|
|
524
|
-
---
|
|
525
|
-
|
|
526
|
-
## 错误处理
|
|
527
|
-
|
|
528
|
-
### 错误响应格式
|
|
529
|
-
|
|
530
|
-
```json
|
|
531
|
-
{
|
|
532
|
-
"type": "error",
|
|
533
|
-
"error": {
|
|
534
|
-
"type": "error_type",
|
|
535
|
-
"message": "描述性错误信息"
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
```
|
|
539
|
-
|
|
540
|
-
### 常见错误
|
|
541
|
-
|
|
542
|
-
| HTTP 状态码 | 错误类型 | 原因 | 处理方案 |
|
|
543
|
-
|-----------|---------|------|--------|
|
|
544
|
-
| 400 | invalid_request_error | 参数错误 | 检查请求格式 |
|
|
545
|
-
| 401 | authentication_error | API Key 无效 | 更新 API Key |
|
|
546
|
-
| 429 | rate_limit_error | 超过速率限制 | 指数退避重试 |
|
|
547
|
-
| 500 | api_error | 服务器错误 | 重试或等待 |
|
|
548
|
-
| 529 | model_overloaded_error | 模型过载 | 稍后重试 |
|
|
549
|
-
|
|
550
|
-
### 重试策略
|
|
551
|
-
|
|
552
|
-
```typescript
|
|
553
|
-
async function requestWithRetry(
|
|
554
|
-
request: any,
|
|
555
|
-
maxRetries: number = 3
|
|
556
|
-
) {
|
|
557
|
-
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
558
|
-
try {
|
|
559
|
-
const response = await fetch(endpoint, {
|
|
560
|
-
method: 'POST',
|
|
561
|
-
headers: {...},
|
|
562
|
-
body: JSON.stringify(request)
|
|
563
|
-
});
|
|
564
|
-
|
|
565
|
-
if (response.status === 429) {
|
|
566
|
-
// 速率限制:指数退避
|
|
567
|
-
const delay = Math.pow(2, attempt - 1) * 1000;
|
|
568
|
-
await new Promise(r => setTimeout(r, delay));
|
|
569
|
-
continue;
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
if (!response.ok) {
|
|
573
|
-
throw new Error(`HTTP ${response.status}`);
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
return await response.json();
|
|
577
|
-
} catch (error) {
|
|
578
|
-
if (attempt === maxRetries) throw error;
|
|
579
|
-
await new Promise(r => setTimeout(r, 100 * attempt));
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
```
|
|
584
|
-
|
|
585
|
-
### 错误响应示例
|
|
586
|
-
|
|
587
|
-
```json
|
|
588
|
-
{
|
|
589
|
-
"type": "error",
|
|
590
|
-
"error": {
|
|
591
|
-
"type": "invalid_request_error",
|
|
592
|
-
"message": "max_tokens must be between 1 and 4096"
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
```
|
|
596
|
-
|
|
597
|
-
---
|
|
598
|
-
|
|
599
|
-
## 快速参考
|
|
600
|
-
|
|
601
|
-
### 常用参数速查
|
|
602
|
-
|
|
603
|
-
| 参数 | 范围 | 默认值 | 说明 |
|
|
604
|
-
|-----|------|--------|------|
|
|
605
|
-
| temperature | 0.0-1.0 | 1.0 | 采样随机度,越低越稳定 |
|
|
606
|
-
| top_p | 0.0-1.0 | - | 核采样,控制多样性 |
|
|
607
|
-
| max_tokens | 1-4096* | - | 最大输出长度 *Chat 可到 200k |
|
|
608
|
-
| stream | true/false | false | 是否启用流式响应 |
|
|
609
|
-
| stop_sequences | 字符串数组 | - | 停止生成的关键词 |
|
|
610
|
-
|
|
611
|
-
### API 端点(官方)
|
|
612
|
-
|
|
613
|
-
```bash
|
|
614
|
-
# 官方 API(所有请求都使用此端点)
|
|
615
|
-
POST /v1/messages
|
|
616
|
-
|
|
617
|
-
# 官方 API 基础 URL
|
|
618
|
-
https://api.anthropic.com
|
|
619
|
-
|
|
620
|
-
# 认证头
|
|
621
|
-
x-api-key: sk-ant-YOUR_API_KEY
|
|
622
|
-
|
|
623
|
-
# 版本头(必需)
|
|
624
|
-
anthropic-version: 2023-06-01
|
|
625
|
-
|
|
626
|
-
# Content-Type
|
|
627
|
-
Content-Type: application/json
|
|
628
|
-
```
|
|
629
|
-
|
|
630
|
-
**注意**:官方只定义了 `/v1/messages` 端点,不存在 `/claude-code/v1/messages` 或 `/claude-chat/v1/messages`。这些是代理层的实现细节。
|
|
631
|
-
|
|
632
|
-
### 常用工具定义模板
|
|
633
|
-
|
|
634
|
-
```typescript
|
|
635
|
-
{
|
|
636
|
-
"tools": [
|
|
637
|
-
{
|
|
638
|
-
"name": "function_name",
|
|
639
|
-
"description": "函数功能描述",
|
|
640
|
-
"input_schema": {
|
|
641
|
-
"type": "object",
|
|
642
|
-
"properties": {
|
|
643
|
-
"param1": {
|
|
644
|
-
"type": "string",
|
|
645
|
-
"description": "参数说明"
|
|
646
|
-
},
|
|
647
|
-
"param2": {
|
|
648
|
-
"type": "number",
|
|
649
|
-
"description": "参数说明"
|
|
650
|
-
}
|
|
651
|
-
},
|
|
652
|
-
"required": ["param1"]
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
]
|
|
656
|
-
}
|
|
657
|
-
```
|
|
658
|
-
|
|
659
|
-
---
|
|
660
|
-
|
|
661
|
-
## 最佳实践
|
|
662
|
-
|
|
663
|
-
### 1. 选择合适的模型(官方推荐)
|
|
664
|
-
|
|
665
|
-
```typescript
|
|
666
|
-
// 快速简单查询 - 最省成本
|
|
667
|
-
"model": "claude-3-5-haiku-20241022"
|
|
668
|
-
|
|
669
|
-
// 一般任务 - 最佳成本/性能比(推荐首选)
|
|
670
|
-
"model": "claude-3-5-sonnet-20241022"
|
|
671
|
-
|
|
672
|
-
// 复杂推理 - 最强能力
|
|
673
|
-
"model": "claude-3-opus-20240229"
|
|
674
|
-
|
|
675
|
-
// 注意:所有模型都使用同一 API (/v1/messages)
|
|
676
|
-
```
|
|
677
|
-
|
|
678
|
-
### 2. 流式 vs 非流式
|
|
679
|
-
|
|
680
|
-
```typescript
|
|
681
|
-
// 使用流式:长响应、实时反馈
|
|
682
|
-
"stream": true
|
|
683
|
-
|
|
684
|
-
// 使用非流式:简短回复、需要完整响应
|
|
685
|
-
"stream": false
|
|
686
|
-
```
|
|
687
|
-
|
|
688
|
-
### 3. Token 成本控制
|
|
689
|
-
|
|
690
|
-
```typescript
|
|
691
|
-
// 估算请求成本
|
|
692
|
-
const estimatedCost = {
|
|
693
|
-
inputTokens: request.messages.length * avgTokensPerMessage,
|
|
694
|
-
outputTokens: max_tokens * 0.5, // 粗略估计
|
|
695
|
-
thinkingTokens: thinking ? budget_tokens : 0
|
|
696
|
-
};
|
|
697
|
-
|
|
698
|
-
// 思考成本通常比输出便宜 40-80%
|
|
699
|
-
const thinkingCostMultiplier = 0.4; // 40% of output cost
|
|
700
|
-
```
|
|
701
|
-
|
|
702
|
-
### 4. 错误处理核心清单
|
|
703
|
-
|
|
704
|
-
```typescript
|
|
705
|
-
✅ 检查 HTTP 状态码
|
|
706
|
-
✅ 检查 response.type === "error"
|
|
707
|
-
✅ 实现指数退避重试(429 状态码)
|
|
708
|
-
✅ 设置合理的请求超时(30-120 秒)
|
|
709
|
-
✅ 记录错误详情用于调试
|
|
710
|
-
✅ 对于 streaming,监听每个事件的错误
|
|
711
|
-
```
|
|
712
|
-
|
|
713
|
-
### 5. 思考功能使用规则
|
|
714
|
-
|
|
715
|
-
```typescript
|
|
716
|
-
// ✅ 启用思考的场景
|
|
717
|
-
if (task.complexity === 'high' ||
|
|
718
|
-
task.requires('reasoning', 'proof', 'analysis')) {
|
|
719
|
-
thinking = { type: 'enabled', budget_tokens: 10000 };
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
// ❌ 不启用思考的场景
|
|
723
|
-
if (task.type === 'simple_lookup' ||
|
|
724
|
-
task.needsSpeed === true) {
|
|
725
|
-
thinking = { type: 'disabled' };
|
|
726
|
-
}
|
|
727
|
-
```
|
|
728
|
-
|
|
729
|
-
### 6. 对话管理
|
|
730
|
-
|
|
731
|
-
```typescript
|
|
732
|
-
// 保持合理的历史记录
|
|
733
|
-
const maxMessages = 20;
|
|
734
|
-
if (messages.length > maxMessages) {
|
|
735
|
-
// 移除旧消息,保留系统消息和最近消息
|
|
736
|
-
messages = messages.slice(-maxMessages);
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
// 或使用 system 提示词总结长历史
|
|
740
|
-
system = `前面的对话中用户提到了...`;
|
|
741
|
-
```
|
|
742
|
-
|
|
743
|
-
### 7. 超时设置建议
|
|
744
|
-
|
|
745
|
-
```typescript
|
|
746
|
-
// 基于 max_tokens 计算超时
|
|
747
|
-
const timeout = {
|
|
748
|
-
shortResponse: 10000, // max_tokens < 1k
|
|
749
|
-
mediumResponse: 30000, // max_tokens 1k-10k
|
|
750
|
-
longResponse: 120000, // max_tokens > 10k
|
|
751
|
-
withThinking: 180000 // 启用思考时加倍
|
|
752
|
-
};
|
|
753
|
-
```
|
|
754
|
-
|
|
755
|
-
### 8. 提示词缓存优化
|
|
756
|
-
|
|
757
|
-
```typescript
|
|
758
|
-
// 对重复的长系统提示使用缓存
|
|
759
|
-
{
|
|
760
|
-
"system": [
|
|
761
|
-
{
|
|
762
|
-
"type": "text",
|
|
763
|
-
"text": "[大型系统提示]",
|
|
764
|
-
"cache_control": { "type": "ephemeral" }
|
|
765
|
-
}
|
|
766
|
-
]
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
// 缓存可以减少成本和延迟
|
|
770
|
-
```
|
|
771
|
-
|
|
772
|
-
### 9. 图像处理最佳实践
|
|
773
|
-
|
|
774
|
-
```typescript
|
|
775
|
-
// Base64 最适合小图像(<100KB)
|
|
776
|
-
{
|
|
777
|
-
"type": "image",
|
|
778
|
-
"source": {
|
|
779
|
-
"type": "base64",
|
|
780
|
-
"media_type": "image/jpeg",
|
|
781
|
-
"data": "iVBORw0KGgoAAAANS..."
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
// URL 最适合网络图像
|
|
786
|
-
{
|
|
787
|
-
"type": "image",
|
|
788
|
-
"source": {
|
|
789
|
-
"type": "url",
|
|
790
|
-
"url": "https://example.com/image.jpg"
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
```
|
|
794
|
-
|
|
795
|
-
### 10. 监控和日志
|
|
796
|
-
|
|
797
|
-
```typescript
|
|
798
|
-
interface RequestMetrics {
|
|
799
|
-
requestId: string;
|
|
800
|
-
model: string;
|
|
801
|
-
inputTokens: number;
|
|
802
|
-
outputTokens: number;
|
|
803
|
-
thinkingTokens?: number;
|
|
804
|
-
responseTime: number;
|
|
805
|
-
status: 'success' | 'error' | 'retry';
|
|
806
|
-
timestamp: Date;
|
|
807
|
-
}
|
|
808
|
-
|
|
809
|
-
// 定期分析指标优化成本
|
|
810
|
-
const avgCostPerRequest = totalTokens / requestCount;
|
|
811
|
-
```
|
|
812
|
-
|
|
813
|
-
---
|
|
814
|
-
|
|
815
|
-
## 代码示例库
|
|
816
|
-
|
|
817
|
-
### Python 基础示例
|
|
818
|
-
|
|
819
|
-
```python
|
|
820
|
-
import requests
|
|
821
|
-
import json
|
|
822
|
-
|
|
823
|
-
def call_claude(messages, model="claude-3-sonnet-20240229", max_tokens=1024):
|
|
824
|
-
response = requests.post(
|
|
825
|
-
"http://api.anthropic.com/v1/messages",
|
|
826
|
-
headers={
|
|
827
|
-
"Content-Type": "application/json",
|
|
828
|
-
"Authorization": "Bearer your-api-key"
|
|
829
|
-
},
|
|
830
|
-
json={
|
|
831
|
-
"model": model,
|
|
832
|
-
"max_tokens": max_tokens,
|
|
833
|
-
"messages": messages
|
|
834
|
-
}
|
|
835
|
-
)
|
|
836
|
-
return response.json()
|
|
837
|
-
|
|
838
|
-
# 使用
|
|
839
|
-
result = call_claude([
|
|
840
|
-
{"role": "user", "content": "Hello!"}
|
|
841
|
-
])
|
|
842
|
-
print(result["content"][0]["text"])
|
|
843
|
-
```
|
|
844
|
-
|
|
845
|
-
### JavaScript 流式示例
|
|
846
|
-
|
|
847
|
-
```javascript
|
|
848
|
-
async function callClaudeStream(messages, onChunk) {
|
|
849
|
-
const response = await fetch(
|
|
850
|
-
'http://api.anthropic.com/v1/messages',
|
|
851
|
-
{
|
|
852
|
-
method: 'POST',
|
|
853
|
-
headers: {
|
|
854
|
-
'Content-Type': 'application/json',
|
|
855
|
-
'Authorization': 'Bearer your-api-key'
|
|
856
|
-
},
|
|
857
|
-
body: JSON.stringify({
|
|
858
|
-
model: 'claude-3-sonnet-20240229',
|
|
859
|
-
max_tokens: 1024,
|
|
860
|
-
stream: true,
|
|
861
|
-
messages
|
|
862
|
-
})
|
|
863
|
-
}
|
|
864
|
-
);
|
|
865
|
-
|
|
866
|
-
const reader = response.body.getReader();
|
|
867
|
-
const decoder = new TextDecoder();
|
|
868
|
-
let buffer = '';
|
|
869
|
-
|
|
870
|
-
while (true) {
|
|
871
|
-
const { done, value } = await reader.read();
|
|
872
|
-
if (done) break;
|
|
873
|
-
|
|
874
|
-
buffer += decoder.decode(value, { stream: true });
|
|
875
|
-
const events = buffer.split('\n\n');
|
|
876
|
-
buffer = events.pop() || '';
|
|
877
|
-
|
|
878
|
-
for (const event of events) {
|
|
879
|
-
if (!event.trim()) continue;
|
|
880
|
-
const data = JSON.parse(event.split('data: ')[1]);
|
|
881
|
-
|
|
882
|
-
if (data.delta?.type === 'text_delta') {
|
|
883
|
-
onChunk(data.delta.text);
|
|
884
|
-
}
|
|
885
|
-
}
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
```
|
|
889
|
-
|
|
890
|
-
### TypeScript 工具调用示例
|
|
891
|
-
|
|
892
|
-
```typescript
|
|
893
|
-
interface ToolCall {
|
|
894
|
-
name: string;
|
|
895
|
-
arguments: Record<string, any>;
|
|
896
|
-
}
|
|
897
|
-
|
|
898
|
-
async function executeToolCall(toolCall: ToolCall): Promise<string> {
|
|
899
|
-
const tools: Record<string, Function> = {
|
|
900
|
-
'get_weather': async (location: string) => {
|
|
901
|
-
return `Weather in ${location}: Sunny, 25°C`;
|
|
902
|
-
},
|
|
903
|
-
'calculate': async (expression: string) => {
|
|
904
|
-
return eval(expression).toString();
|
|
905
|
-
}
|
|
906
|
-
};
|
|
907
|
-
|
|
908
|
-
const tool = tools[toolCall.name];
|
|
909
|
-
if (!tool) throw new Error(`Tool ${toolCall.name} not found`);
|
|
910
|
-
|
|
911
|
-
return await tool(...Object.values(toolCall.arguments));
|
|
912
|
-
}
|
|
913
|
-
```
|
|
914
|
-
|
|
915
|
-
---
|
|
916
|
-
|
|
917
|
-
## 版本信息
|
|
918
|
-
|
|
919
|
-
- **文档版本**:2.0(标准官方格式)
|
|
920
|
-
- **基于文档**:Anthropic 官方 API 文档
|
|
921
|
-
- **支持的模型**:Claude 3.5 Sonnet、Claude 3.5 Haiku、Claude 3 Opus
|
|
922
|
-
- **最后更新**:2026 年 2 月 3 日
|
|
923
|
-
- **API 基础 URL**:https://api.anthropic.com
|
|
924
|
-
- **API 版本**:anthropic-version: 2023-06-01
|
|
925
|
-
|
|
926
|
-
---
|
|
927
|
-
|
|
928
|
-
## 常见问题更正
|
|
929
|
-
|
|
930
|
-
| 问题 | ❌ 常见误解 | ✅ 官方事实 |
|
|
931
|
-
|-----|-----------|----------|
|
|
932
|
-
| Claude Code 和 Chat API 有区别吗? | "它们是不同的 API" | ❌ 假的。都使用同一个 `/v1/messages` 端点 |
|
|
933
|
-
| 如何选择使用哪个 API? | "基于应用名称选择" | ❌ 不存在这样的选择。通过 `model` 参数选择能力 |
|
|
934
|
-
| 是否有 `/claude-code` 端点? | "官方提供此端点" | ❌ 官方只提供 `/v1/messages` 端点 |
|
|
935
|
-
| 模型决定了 API 格式吗? | "不同模型格式不同" | ❌ 所有模型使用完全相同的请求/响应格式 |
|
|
936
|
-
| 我应该使用哪个模型? | "取决于是否用于代码" | ✅ 正确。任何模型都可以做代码,选择取决于性能和成本需求 |
|
|
937
|
-
| 思考功能是否只在某些 API 可用? | "仅在某些端点可用" | ❌ 所有模型都支持,通过同一 API |
|
|
938
|
-
|
|
939
|
-
---
|
|
940
|
-
|
|
941
|
-
## 相关资源
|
|
942
|
-
|
|
943
|
-
- [Anthropic 官方文档](https://docs.anthropic.com)
|
|
944
|
-
- [Claude API 参考](https://docs.anthropic.com/en/api/messages)
|
|
945
|
-
- [模型概览](https://docs.anthropic.com/en/docs/about/models/models-overview)
|