API
Chat Completions
创建聊天补全是 BYOKS 最常用的 API。
端点
POST /v1/chat/completions请求参数
必填参数
| 参数 | 类型 | 说明 |
|---|---|---|
model | string | 模型 ID,如 gpt-4o、claude-3-5-sonnet-20241022 |
messages | array | 消息数组 |
可选参数
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
stream | boolean | false | 是否启用流式响应 |
temperature | number | 1 | 采样温度,0-2 |
max_tokens | integer | - | 最大生成 Token 数 |
top_p | number | 1 | 核采样参数 |
frequency_penalty | number | 0 | 频率惩罚,-2 到 2 |
presence_penalty | number | 0 | 存在惩罚,-2 到 2 |
stop | string | array | - | 停止序列 |
tools | array | - | 可用工具列表 |
tool_choice | string | object | auto | 工具选择策略 |
response_format | object | - | 响应格式 |
seed | integer | - | 随机种子 |
user | string | - | 用户标识 |
消息格式
interface Message {
role: 'system' | 'user' | 'assistant' | 'tool';
content: string | ContentPart[];
name?: string; // 可选的发送者名称
tool_calls?: ToolCall[]; // assistant 消息的工具调用
tool_call_id?: string; // tool 消息的调用 ID
}
interface ContentPart {
type: 'text' | 'image_url';
text?: string;
image_url?: {
url: string;
detail?: 'auto' | 'low' | 'high';
};
}请求示例
基础请求
curl https://api.byoks.com/v1/chat/completions \
-H "Authorization: Bearer your-byoks-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "你是一个有帮助的助手。"},
{"role": "user", "content": "你好!"}
]
}'流式请求
curl https://api.byoks.com/v1/chat/completions \
-H "Authorization: Bearer your-byoks-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "写一首诗"}],
"stream": true
}'带工具的请求
{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "北京天气怎么样?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名"}
},
"required": ["city"]
}
}
}
]
}响应格式
非流式响应
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1234567890,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!有什么我可以帮助你的吗?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 15,
"total_tokens": 35
}
}流式响应
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"role":"assistant"},"index":0}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"你"},"index":0}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"好"},"index":0}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{},"index":0,"finish_reason":"stop"}]}
data: [DONE]工具调用响应
{
"id": "chatcmpl-xxx",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_xxx",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\":\"北京\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
]
}finish_reason 说明
| 值 | 说明 |
|---|---|
stop | 正常结束或遇到停止序列 |
length | 达到 max_tokens 限制 |
tool_calls | 模型请求调用工具 |
content_filter | 内容被过滤 |
模型特定参数
不同模型支持的参数略有不同:
| 参数 | OpenAI | Anthropic | |
|---|---|---|---|
temperature | 是 | 是 | 是 |
max_tokens | 是 | 是 (必填) | 是 |
top_p | 是 | 是 | 是 |
frequency_penalty | 是 | 否 | 否 |
presence_penalty | 是 | 否 | 否 |
seed | 是 | 否 | 否 |
BYOKS 会自动处理不支持的参数(忽略或转换)。