mirror of
https://github.com/aiclientproxy/proxycast.git
synced 2026-09-24 23:10:56 +08:00
234 lines
4.1 KiB
Markdown
234 lines
4.1 KiB
Markdown
---
|
|
title: Claude API
|
|
description: Claude 兼容 API 端点
|
|
navigation:
|
|
icon: i-heroicons-chat-bubble-bottom-center-text
|
|
---
|
|
|
|
# Claude API
|
|
|
|
ProxyCast 提供 Anthropic Claude Messages API 兼容。
|
|
|
|
## /v1/messages
|
|
|
|
### 请求
|
|
|
|
```bash
|
|
POST /v1/messages
|
|
Content-Type: application/json
|
|
x-api-key: your-api-key
|
|
anthropic-version: 2023-06-01
|
|
```
|
|
|
|
### 请求体
|
|
|
|
```json
|
|
{
|
|
"model": "claude-sonnet-4-20250514",
|
|
"max_tokens": 1024,
|
|
"messages": [
|
|
{"role": "user", "content": "Hello!"}
|
|
]
|
|
}
|
|
```
|
|
|
|
### 参数说明
|
|
|
|
| 参数 | 类型 | 必填 | 说明 |
|
|
|------|------|------|------|
|
|
| model | string | ✅ | 模型名称 |
|
|
| max_tokens | integer | ✅ | 最大输出 Token |
|
|
| messages | array | ✅ | 消息列表 |
|
|
| system | string | ❌ | 系统提示词 |
|
|
| temperature | number | ❌ | 温度 (0-1) |
|
|
| top_p | number | ❌ | 采样参数 |
|
|
| top_k | integer | ❌ | Top-K 采样 |
|
|
| stream | boolean | ❌ | 是否流式响应 |
|
|
| stop_sequences | array | ❌ | 停止序列 |
|
|
| tools | array | ❌ | 工具定义 |
|
|
| tool_choice | object | ❌ | 工具选择策略 |
|
|
|
|
### 消息格式
|
|
|
|
```json
|
|
{
|
|
"role": "user",
|
|
"content": "Hello!"
|
|
}
|
|
```
|
|
|
|
或带图片:
|
|
|
|
```json
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "text", "text": "What's in this image?"},
|
|
{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "..."}}
|
|
]
|
|
}
|
|
```
|
|
|
|
### 响应
|
|
|
|
```json
|
|
{
|
|
"id": "msg_xxx",
|
|
"type": "message",
|
|
"role": "assistant",
|
|
"content": [
|
|
{"type": "text", "text": "Hello! How can I help you today?"}
|
|
],
|
|
"model": "claude-sonnet-4-20250514",
|
|
"stop_reason": "end_turn",
|
|
"usage": {
|
|
"input_tokens": 10,
|
|
"output_tokens": 20
|
|
}
|
|
}
|
|
```
|
|
|
|
### 流式响应
|
|
|
|
设置 `stream: true` 启用流式响应:
|
|
|
|
```bash
|
|
curl http://127.0.0.1:8999/v1/messages \
|
|
-H "x-api-key: your-api-key" \
|
|
-H "anthropic-version: 2023-06-01" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"model": "claude-sonnet-4-20250514",
|
|
"max_tokens": 1024,
|
|
"messages": [{"role": "user", "content": "Hello!"}],
|
|
"stream": true
|
|
}'
|
|
```
|
|
|
|
响应格式(SSE):
|
|
|
|
```
|
|
event: message_start
|
|
data: {"type":"message_start","message":{"id":"msg_xxx"}}
|
|
|
|
event: content_block_start
|
|
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
|
|
|
|
event: content_block_delta
|
|
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}
|
|
|
|
event: message_stop
|
|
data: {"type":"message_stop"}
|
|
```
|
|
|
|
## /v1/messages/count_tokens
|
|
|
|
### 请求
|
|
|
|
```bash
|
|
POST /v1/messages/count_tokens
|
|
Content-Type: application/json
|
|
x-api-key: your-api-key
|
|
anthropic-version: 2023-06-01
|
|
```
|
|
|
|
### 请求体
|
|
|
|
```json
|
|
{
|
|
"model": "claude-sonnet-4-20250514",
|
|
"messages": [
|
|
{"role": "user", "content": "Hello!"}
|
|
]
|
|
}
|
|
```
|
|
|
|
### 响应
|
|
|
|
```json
|
|
{
|
|
"input_tokens": 10
|
|
}
|
|
```
|
|
|
|
## 工具调用
|
|
|
|
### 定义工具
|
|
|
|
```json
|
|
{
|
|
"model": "claude-sonnet-4-20250514",
|
|
"max_tokens": 1024,
|
|
"messages": [{"role": "user", "content": "What's the weather in Tokyo?"}],
|
|
"tools": [
|
|
{
|
|
"name": "get_weather",
|
|
"description": "Get weather information",
|
|
"input_schema": {
|
|
"type": "object",
|
|
"properties": {
|
|
"location": {"type": "string"}
|
|
},
|
|
"required": ["location"]
|
|
}
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
### 工具调用响应
|
|
|
|
```json
|
|
{
|
|
"content": [
|
|
{
|
|
"type": "tool_use",
|
|
"id": "toolu_xxx",
|
|
"name": "get_weather",
|
|
"input": {"location": "Tokyo"}
|
|
}
|
|
],
|
|
"stop_reason": "tool_use"
|
|
}
|
|
```
|
|
|
|
## 示例代码
|
|
|
|
### Python
|
|
|
|
```python
|
|
import anthropic
|
|
|
|
client = anthropic.Anthropic(
|
|
base_url="http://127.0.0.1:8999",
|
|
api_key="your-api-key"
|
|
)
|
|
|
|
message = client.messages.create(
|
|
model="claude-sonnet-4-20250514",
|
|
max_tokens=1024,
|
|
messages=[{"role": "user", "content": "Hello!"}]
|
|
)
|
|
|
|
print(message.content[0].text)
|
|
```
|
|
|
|
### Node.js
|
|
|
|
```javascript
|
|
import Anthropic from '@anthropic-ai/sdk';
|
|
|
|
const client = new Anthropic({
|
|
baseURL: 'http://127.0.0.1:8999',
|
|
apiKey: 'your-api-key'
|
|
});
|
|
|
|
const message = await client.messages.create({
|
|
model: 'claude-sonnet-4-20250514',
|
|
max_tokens: 1024,
|
|
messages: [{ role: 'user', content: 'Hello!' }]
|
|
});
|
|
|
|
console.log(message.content[0].text);
|
|
```
|