cURL 调用
通过 HTTP API 直接调用 CloudBase AI 大模型,适用于后端服务、脚本、任意编程语言。
准备工作
API 端点
https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/<PROVIDER>/chat/completions
| 参数 | 说明 | 示例 |
|---|---|---|
| ENV_ID | 环境 ID | your-env-id |
| PROVIDER | 模型提供商 | cloudbase |
认证
在请求头中添加 API Key:
Authorization: Bearer <YOUR_API_KEY>
文本生成
非流式调用
curl -X POST 'https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase/chat/completions' \
-H 'Authorization: Bearer <YOUR_API_KEY>' \
-H 'Content-Type: application/json' \
-d '{
"model": "hy3",
"messages": [
{"role": "user", "content": "介绍一下李白"}
],
"stream": false
}'
响应示例:
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1234567890,
"model": "hy3",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "李白(701年—762年),字太白,号青莲居士..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 150,
"total_tokens": 160
}
}
流式调用
curl -X POST 'https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase/chat/completions' \
-H 'Authorization: Bearer <YOUR_API_KEY>' \
-H 'Content-Type: application/json' \
-H 'Accept: text/event-stream' \
-d '{
"model": "hy3",
"messages": [
{"role": "user", "content": "介绍一下你自己"}
],
"stream": true
}'
响应格式(SSE):
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"我"},"index":0}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"是"},"index":0}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"DeepSeek"},"index":0}]}
data: [DONE]
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | 模型名称 |
| messages | array | 是 | 消息列表 |
| stream | boolean | 否 | 是否流式返回,默认 false |
| temperature | number | 否 | 采样温度,范围 0-2 |
| top_p | number | 否 | 核采样,范围 0-1 |
| max_tokens | number | 否 | 最大生成 token 数 |