跳转到主要内容
POST
/
api
/
v1
/
chat
/
completions
curl --request POST \
  --url https://api.crun.ai/api/v1/chat/completions \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "model": "gpt-4o-mini",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "Write a short tagline for a coffee brand."
    }
  ],
  "temperature": 0.7
}
'
{
  "id": "chatcmpl_abc123",
  "object": "chat.completion",
  "created": 1772294400,
  "model": "gpt-4o-mini",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Bright flavor, smooth finish, and roasted for everyday focus."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 18,
    "completion_tokens": 13,
    "total_tokens": 31
  }
}

API 地址

POST https://api.crun.ai/api/v1/chat/completions
该接口遵循 OpenAI Chat Completions 的请求和响应格式。 您可以直接搭配官方 OpenAI SDK,或其他 OpenAI 兼容客户端使用。

身份验证

您可以使用以下任一方式进行身份验证:
  • Authorization: Bearer YOUR_API_KEY
  • X-API-KEY: YOUR_API_KEY

请求示例

curl -X POST "https://api.crun.ai/api/v1/chat/completions" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "Summarize the benefits of serverless functions in 3 bullet points."
      }
    ],
    "temperature": 0.6
  }'

对话历史示例

CRUN 不会为您的应用管理多轮对话上下文。 如需继续一段对话,您必须自行维护历史消息,并在每次新请求中传入相关上下文。
curl -X POST "https://api.crun.ai/api/v1/chat/completions" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful support assistant."
      },
      {
        "role": "user",
        "content": "My package has not arrived yet. What should I check first?"
      },
      {
        "role": "assistant",
        "content": "First, check the tracking link, delivery address, and any carrier delay notices."
      },
      {
        "role": "user",
        "content": "The tracking page says delayed by weather. Draft a short reply to the customer."
      }
    ],
    "temperature": 0.6
  }'

结构化输出示例

当您需要模型返回 JSON 时,可以使用 response_format。如果需要严格遵循 JSON Schema,请选择支持 json_schema 的模型和上游服务。
curl -X POST "https://api.crun.ai/api/v1/chat/completions" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4",
    "messages": [
      {
        "role": "system",
        "content": "Extract tasks as JSON only."
      },
      {
        "role": "user",
        "content": "Maya should send the launch brief by Friday."
      }
    ],
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "task",
        "strict": true,
        "schema": {
          "type": "object",
          "properties": {
            "owner": { "type": "string" },
            "task": { "type": "string" },
            "due": { "type": "string" }
          },
          "required": ["owner", "task", "due"],
          "additionalProperties": false
        }
      }
    }
  }'

工具调用示例

Chat Completions 接受 OpenAI 兼容的 toolstool_choice 字段。您的应用需要自行执行工具调用,并在后续请求中把工具结果传回模型。
request body
{
  "model": "gpt-5.4",
  "messages": [
    {
      "role": "user",
      "content": "What is the weather like in Paris?"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "Get current weather for a city.",
        "parameters": {
          "type": "object",
          "properties": {
            "city": {
              "type": "string"
            }
          },
          "required": ["city"],
          "additionalProperties": false
        }
      }
    }
  ],
  "tool_choice": "auto"
}

视觉输入示例

当所选模型支持图像输入时,可以在用户消息中传入多模态 content parts。
request body
{
  "model": "gpt-5.4",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "Describe this chart in one paragraph."
        },
        {
          "type": "image_url",
          "image_url": {
            "url": "https://example.com/chart.png"
          }
        }
      ]
    }
  ],
  "max_completion_tokens": 300
}

流式请求示例

设置 stream=true 后,可以接收 Server-Sent Events。CRUN 会在可用时为流式请求启用 usage 信息。
curl -N "https://api.crun.ai/api/v1/chat/completions" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4",
    "stream": true,
    "messages": [
      {
        "role": "user",
        "content": "Write a three-step checklist for reviewing an API integration."
      }
    ],
    "stream_options": {
      "include_usage": true
    }
  }'

响应示例

{
  "id": "chatcmpl_abc123",
  "object": "chat.completion",
  "created": 1772294400,
  "model": "gpt-5.4",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "- No server management.\n- Automatic scaling for bursty traffic.\n- Pay only for actual execution time."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 22,
    "completion_tokens": 19,
    "total_tokens": 41
  }
}

注意事项

  • 设置 stream=true 可接收 text/event-stream 响应。
  • 如果省略 stream_options.include_usage,CRUN 会自动为流式请求启用该选项。
  • max_tokensmax_completion_tokens 都可以使用,并会受所选模型的输出 token 上限约束。
  • 未知模型 ID 会返回 OpenAI 风格的错误体,其中 code"model_not_found"
  • 对于新的有状态、工具密集或结构化输出工作流,也可以考虑 /responses 接口。Responses 使用 inputinstructionstext.format,而不是 messagesresponse_format

相关资源

Responses API

使用灵活输入、有状态追问、结构化输出和工具就绪的 response items 构建应用。

LLM 快速开始

了解 base URL、身份验证方式以及官方 SDK 的接入模式。

模型概览

浏览所有可用的图像、视频、音频和语言模型 API。

价格

前往价格页面,比较不同模型的计费规则。

授权

Authorization
string
header
必填

在 OpenAI 兼容 SDK 中,将您的 CRUN API Key 作为 Bearer token 使用。

请求体

application/json

OpenAI 兼容的 Chat Completions 请求。其他兼容字段会被接受,并在模型支持时透传。

model
string
必填

GET /api/v1/models 返回的公开模型 ID。

Required string length: 1 - 128
示例:

"gpt-4o-mini"

messages
object[]
必填

OpenAI 格式的对话消息。

Minimum array length: 1
temperature
number

采样温度。

必填范围: 0 <= x <= 2
示例:

0.7

top_p
number

核采样参数。

必填范围: 0 <= x <= 1
示例:

1

n
integer
默认值:1

要生成的补全结果数量。

必填范围: 1 <= x <= 8
示例:

1

stream
boolean
默认值:false

是否返回 Server-Sent Events 流式响应。

示例:

false

stop

停止序列或停止序列列表。

示例:

"###"

max_tokens
integer

最大输出 token 数,会受所选模型上限限制。

必填范围: x >= 1
示例:

512

max_completion_tokens
integer

最大补全 token 数,会受所选模型上限限制。

必填范围: x >= 1
示例:

512

presence_penalty
number

Presence penalty 参数。

必填范围: -2 <= x <= 2
示例:

0

frequency_penalty
number

Frequency penalty 参数。

必填范围: -2 <= x <= 2
示例:

0

user
string

用于可观测性的终端用户标识。

示例:

"user_123"

stream_options
object

流式输出选项。当 stream=true 且未传入该字段时,CRUN 默认启用 include_usage=true

示例:
{ "include_usage": true }
response_format
object

OpenAI 兼容结构化输出选项。Chat Completions 使用 response_format;Responses 使用 text.format

示例:
{ "type": "json_object" }
tools
object[]

OpenAI 兼容工具定义。应用需要执行工具调用,并在后续请求中返回工具结果。

tool_choice

工具选择策略。

示例:

"auto"

响应

成功的补全响应。stream=false 时返回 JSON,stream=true 时返回 SSE。

id
string
必填
示例:

"chatcmpl_abc123"

object
string
必填
示例:

"chat.completion"

created
integer
必填

Unix 时间戳,单位为秒。

示例:

1772294400

model
string
必填

客户端请求的公开模型 ID。

示例:

"gpt-4o-mini"

choices
object[]
必填
usage
object