主题
Anthropic SDK 使用
你可以使用 Anthropic 官方 SDK 接入 Zivv API,只需修改 base_url 即可。
Python
安装
bash
pip install anthropic基本用法
python
import anthropic
client = anthropic.Anthropic(
base_url="https://zivv.pro",
api_key="sk-你的Key",
)
message = client.messages.create(
model="YOUR_MODEL_ID",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello!"}
],
)
print(message.content[0].text)流式输出
python
import anthropic
client = anthropic.Anthropic(
base_url="https://zivv.pro",
api_key="sk-你的Key",
)
with client.messages.stream(
model="YOUR_MODEL_ID",
max_tokens=1024,
messages=[
{"role": "user", "content": "用 Python 写一个二叉树"}
],
) as stream:
for text in stream.text_stream:
print(text, end="")Node.js / TypeScript
安装
bash
npm install @anthropic-ai/sdk基本用法
typescript
import Anthropic from '@anthropic-ai/sdk'
const client = new Anthropic({
baseURL: 'https://zivv.pro',
apiKey: 'sk-你的Key',
})
const message = await client.messages.create({
model: 'YOUR_MODEL_ID',
max_tokens: 1024,
messages: [
{ role: 'user', content: 'Hello!' }
],
})
console.log(message.content[0].text)流式输出
typescript
import Anthropic from '@anthropic-ai/sdk'
const client = new Anthropic({
baseURL: 'https://zivv.pro',
apiKey: 'sk-你的Key',
})
const stream = client.messages.stream({
model: 'YOUR_MODEL_ID',
max_tokens: 1024,
messages: [
{ role: 'user', content: '用 TypeScript 写一个二叉树' }
],
})
for await (const event of stream) {
if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
process.stdout.write(event.delta.text)
}
}注意事项
- Anthropic 格式的 Base URL 不带
/v1后缀(SDK 会自动拼接) max_tokens是 Anthropic Messages 请求的必填参数;可用上限取决于模型、协议和当前网关限制- 支持
anthropic-versionheader,默认值2023-06-01 - 认证方式:请求头
x-api-key或Authorization: Bearer
