安装
- Python
- Node.js
- Go
复制
pip install anthropic
复制
npm install @anthropic-ai/sdk
复制
go get github.com/anthropics/anthropic-sdk-go
代码示例
- cURL
- Python
- Node.js
- Go
复制
curl https://api.pipellm.ai/v1/messages \
-H "Content-Type: application/json" \
-H "x-api-key: $PIPELLM_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d @- << 'EOF'
{
"model": "claude-sonnet-4-5-20250929",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Why is the sky blue?"}
]
}
EOF
复制
import os
import anthropic
client = anthropic.Anthropic(
api_key=os.getenv('PIPELLM_API_KEY'),
base_url='https://api.pipellm.ai'
)
message = client.messages.create(
model='claude-sonnet-4-5-20250929',
max_tokens=1024,
messages=[
{'role': 'user', 'content': 'Why is the sky blue?'}
]
)
print(message.content[0].text)
复制
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.PIPELLM_API_KEY,
baseURL: 'https://api.pipellm.ai'
});
const message = await client.messages.create({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages: [
{ role: 'user', content: 'Why is the sky blue?' }
]
});
console.log(message.content[0].text);
复制
package main
import (
"context"
"os"
"github.com/anthropics/anthropic-sdk-go"
"github.com/anthropics/anthropic-sdk-go/option"
)
func main() {
client := anthropic.NewClient(
option.WithAPIKey(os.Getenv("PIPELLM_API_KEY")),
option.WithBaseURL("https://api.pipellm.ai"),
)
message, _ := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
Model: anthropic.F("claude-sonnet-4-5-20250929"),
MaxTokens: anthropic.Int(1024),
Messages: anthropic.F([]anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock("Why is the sky blue?")),
}),
})
}
请求参数
| 参数 | 类型 | 必需 | 描述 |
|---|---|---|---|
model | string | 是 | 模型 ID(如 claude-sonnet-4-5-20250929) |
messages | array | 是 | 消息对象数组 |
max_tokens | integer | 是 | 最大生成 token 数 |
temperature | number | 否 | 采样温度(0-1) |
stream | boolean | 否 | 启用流式响应 |
响应格式
复制
{
"id": "msg_xxx",
"type": "message",
"role": "assistant",
"content": [
{"type": "text", "text": "The sky appears blue because..."}
],
"model": "claude-sonnet-4-5-20250929",
"stop_reason": "end_turn",
"usage": {
"input_tokens": 10,
"output_tokens": 50
}
}