兼容 OpenAI 与火山引擎接口格式,无缝迁移
1. 注册账号并登录控制台
2. 在「API密钥管理」创建你的 API Key
3. 调用接口,开始使用 AI 能力
https://api.xtang.com/v1
在请求头中携带你的 API Key:
Authorization: Bearer sk-你的API密钥
curl https://api.xtang.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的API密钥" \
-d '{
"model": "doubao-seed-2-1-pro-260628",
"messages": [
{"role": "system", "content": "你是一个 helpful 的助手"},
{"role": "user", "content": "你好,请介绍一下你自己"}
],
"temperature": 0.7,
"max_tokens": 2000
}'
from openai import OpenAI
# 初始化客户端,替换为你的 API Key
client = OpenAI(
api_key="sk-你的API密钥",
base_url="https://api.xtang.com/v1"
)
# 对话补全
response = client.chat.completions.create(
model="doubao-seed-2-1-pro-260628",
messages=[
{"role": "system", "content": "你是一个 helpful 的助手"},
{"role": "user", "content": "写一个Python快速排序"}
],
temperature=0.7,
max_tokens=2000
)
print(response.choices[0].message.content)
print(f"本次消耗: 输入{response.usage.prompt_tokens} token, 输出{response.usage.completion_tokens} token")
import ark
# 初始化火山引擎 SDK 客户端
client = ark.Ark(
api_key="sk-你的API密钥",
base_url="https://api.xtang.com/api/v3"
)
# 调用对话模型
response = client.chat.completions.create(
model="doubao-seed-2-1-pro-260628",
messages=[{"role": "user", "content": "你好"}],
stream=False
)
print(response.choices[0].message.content)
# 图像生成示例
image_response = client.images.generations(
model="doubao-seedream-5-0-pro-260628",
prompt="一只可爱的橘猫坐在窗台上晒太阳",
size="1024x1024"
)
print(image_response.data[0].url)
| 错误码 | 说明 |
|---|---|
| 401 | API Key 无效或未登录 |
| 402 | 余额不足,请充值 |
| 403 | 无权限访问该模型 |
| 429 | 请求频率超限 |
| 500 | 服务器内部错误 |