BYOKSBYOKS Docs

API

Chat Completions

创建聊天补全是 BYOKS 最常用的 API。

端点

POST /v1/chat/completions

请求参数

必填参数

参数类型说明
modelstring模型 ID,如 gpt-4oclaude-3-5-sonnet-20241022
messagesarray消息数组

可选参数

参数类型默认值说明
streambooleanfalse是否启用流式响应
temperaturenumber1采样温度,0-2
max_tokensinteger-最大生成 Token 数
top_pnumber1核采样参数
frequency_penaltynumber0频率惩罚,-2 到 2
presence_penaltynumber0存在惩罚,-2 到 2
stopstring | array-停止序列
toolsarray-可用工具列表
tool_choicestring | objectauto工具选择策略
response_formatobject-响应格式
seedinteger-随机种子
userstring-用户标识

消息格式

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内容被过滤

模型特定参数

不同模型支持的参数略有不同:

参数OpenAIAnthropicGoogle
temperature
max_tokens是 (必填)
top_p
frequency_penalty
presence_penalty
seed

BYOKS 会自动处理不支持的参数(忽略或转换)。

下一步