CanalAPI
示例

curl

通过命令行发起 Chat Completion 请求。

基本对话补全

curl "$CANALAPI_BASE_URL/chat/completions" \
  -H "Authorization: Bearer $CANALAPI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [
      {"role": "system", "content": "请尽量简洁。"},
      {"role": "user", "content": "用一句话总结:CanalAPI 聚合多种 LLM。"}
    ]
  }'

流式

curl --no-buffer "$CANALAPI_BASE_URL/chat/completions" \
  -H "Authorization: Bearer $CANALAPI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-mini",
    "stream": true,
    "messages": [{"role": "user", "content": "用流式生成一首关于河流的俳句。"}]
  }'

--no-buffer 必不可少 —— 否则 curl 会自行缓冲响应,你将看不到增量输出。

Function Calling(工具调用)

curl "$CANALAPI_BASE_URL/chat/completions" \
  -H "Authorization: Bearer $CANALAPI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-mini",
    "tools": [{
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "获取指定城市的当前天气。",
        "parameters": {
          "type": "object",
          "properties": {"city": {"type": "string"}},
          "required": ["city"]
        }
      }
    }],
    "messages": [{"role": "user", "content": "东京现在天气如何?"}]
  }'

本页目录