跳到主要内容

云开发 LLM API 支持协议介绍

云开发 CloudBase AI 支持以下协议:Chat Completions、Responses API、Anthropic Messages API,凡是支持自定义接口的 AI 工具都可以直接接入。

准备工作

Base URLhttps://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase

最通用的大模型对话协议,绝大多数 AI 工具默认使用此协议(OpenAI Compatible)。

以工具调用为例,完整流程需要两次 API 请求:

第一次请求:发送用户消息和可用工具定义

curl https://{{YOUR-ENV-ID}}.api.tcloudbasegateway.com/v1/ai/cloudbase/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer {{YOUR-API-KEY}}" \
-d '{
"model": "hy3-preview",
"messages": [
{ "role": "user", "content": "帮我查一下北京今天的天气。" }
],
"tools": [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "获取指定城市的天气预报",
"parameters": {
"type": "object",
"properties": {
"location": { "type": "string", "description": "城市名称" }
},
"required": ["location"]
}
}
}
]
}'

模型返回 finish_reason: "tool_calls",表示需要客户端执行工具。客户端调用工具获取结果后,将工具返回值追加到 messages 中发起第二次请求:

第二次请求:携带工具调用结果

curl https://{{YOUR-ENV-ID}}.api.tcloudbasegateway.com/v1/ai/cloudbase/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer {{YOUR-API-KEY}}" \
-d '{
"model": "hy3-preview",
"messages": [
{ "role": "user", "content": "帮我查一下北京今天的天气。" },
{
"role": "assistant",
"tool_calls": [{
"id": "call_123",
"type": "function",
"function": { "name": "get_current_weather", "arguments": "{\"location\": \"北京\"}" }
}]
},
{
"role": "tool",
"tool_call_id": "call_123",
"content": "{\"temperature\": \"25℃\", \"weather\": \"晴朗\"}"
}
]
}'

模型会根据工具返回的结果生成最终回复。