聊天生成

最后更新:2026-11-14

POSThttps://www.sagelib.com/chat/completions

专为对话设计,支持多轮上下文、角色区分,更贴近聊天的自然交互,适用于日常聊天/问答、多轮任务协作、个性化服务等场景。

接口鉴权方式请参考:API接入与鉴权

当前展示通用接口文档,各模型支持的参数存在差异。请点击下方模型标签查看专属文档。

请求参数

请求头

参数名称 描述
*Authorization string 鉴权令牌,格式为 Bearer ${AIGATEWAY_TOKEN}
*Content-Type string 默认值:application/json
可选值:application/json
内容类型,固定为 application/json

Body 参数

参数名称 描述
model string 默认值:Per AI gateway configuration
模型名称,根据AI网关配置,可不传
*messages array 对话的内容。元素为消息对象,含role和content。纯文本输入示例:"messages": [{"role": "system", "content": "你是一个产品咨询助手。"}, {"role": "user", "content": "请介绍下example的ECA边缘应用产品?"}];图片url输入示例:"messages": [{"role": "user", "content": [{"type": "text", "text": "请解答图片中的题目"}, {"type": "image_url", "image_url": {"url": "https://example.com/my-image.jpg"}}]}];图片base64输入示例:"messages": [{"role": "user", "content": [{"type": "text", "text": "请解答图片中的题目"}, {"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAABAAAAAQ...."}}]}]
    *role string 可选值:system、user、assistant
消息发送者角色
    *content object 消息内容。当为字符串时,直接携带消息内容;当为数组时,元素为对象,包含type(枚举text/image_url,必填)、text(字符串,type为text时必填)、image_url(对象,type为image_url时必填,含url字段:图片URL或Base64编码,Base64格式为 data:image/{IMAGE_FORMAT};base64,{IMAGE_BASE64},IMAGE_FORMAT为jpeg/png等图片格式)
stream boolean 默认值:Per AI gateway configuration
可选值:true、false
是否流式输出
max_tokens integer 默认值:Per AI gateway configuration
最大token数量
temperature number 默认值:1
可选值:[0, 2.0]
采样温度。较高数值输出更随机,较低数值输出更集中和确定
top_p number 默认值:1
可选值:[0, 1.0]
影响输出文本的多样性。取值越大则生成文本的多样性越强
tools array 包含一个或多个工具对象的数组,供模型在Function Calling中调用。设置tools且模型判断需要调用工具时,响应会通过tool_calls返回工具信息。工具调用示例:"tools": [{"type": "function", "function": {"name": "get_current_weather", "description": "Get the current weather in a given location", "parameters": {"type": "object", "properties": {"location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}}, "required": ["location"]}}}]
    type string 可选值:function
工具类型
    function object 函数定义
        name string 函数名称
        description string 函数描述
        parameters object 函数参数JSON Schema
            type string 默认值:object
            properties object
            required array
reasoning_effort string 默认值:Model default
可选值:low、medium、high、none
模型思考等级。支持的模型有OpenAI的o系列(不支持指定为none)、阿里云qwen3、百度ernie-4.5、Anthropic的claude系列(不支持none)及谷歌的gemini系列。与eca_thinking_config参数一起使用时eca_thinking_config优先生效
eca_thinking_config object 指定思考长度,claude系列与gemini 2.5系列支持,gemini 3系列建议使用reasoning_effort。JSON 示例:"eca_thinking_config":
    type string 可选值:adaptive、disabled、enabled
是否在回答中返回思考内容,仅claude系列支持adaptive
    budget_tokens integer 默认值:gemini series: -1; claude series: -
可选值:gemini series: [-1, 24576]; claude series: [1024, max_tokens]
指定thinking的tokens数量,type=enabled时生效
response_format object 可选值:{"type": "text"}: plain text reply; {"type": "json_schema","json_schema": {...} }: JSON string of the specified format, see https://json-schema.org/docs
返回内容的格式,仅OpenAI(从gpt-4o开始)与Google Gemini系列的模型支持。{"type":"text"}输出文字回复;{"type":"json_schema","json_schema":{...}}输出指定格式的JSON字符串,构造方式见 https://json-schema.org/docs
    type string 可选值:text、json_schema
text=输出文字;json_schema=输出指定格式JSON
    json_schema object 结构化输出schema
        name string
        schema object
        strict boolean
eca_enable_search boolean 默认值:false
可选值:true、false
是否开启联网搜索
eca_rag object RAG参数。RAG示例:"eca_rag":
    knowledge_id string 知识库的ID,必填
eca_context_management object 默认值:None
上下文管理配置,包含工具调用结果清理策略。上下文管理配置参数示例:"eca_context_management": {"edits": [{"type": "clear_tool_uses_20250919", "trigger": {"type": "input_tokens", "value": 100000}, "keep": {"type": "tool_uses", "value": 3}, "clear_tool_inputs": false}]}
    edits array 默认值:None
上下文编辑策略列表
        type string 默认值:None
可选值:clear_tool_uses_20250919
编辑策略类型(工具调用结果清理策略)
        trigger object 触发清理策略的条件
            type string 默认值:None
可选值:input_tokens、tool_uses
触发类型:input_tokens=输入token数;tool_uses=工具调用次数
            value number 默认值:100000
可选值:Positive integer
触发阈值,正整数
        keep object 清理后保留的最近工具调用/结果对的数量
            type string 默认值:None
可选值:tool_uses
            value number 默认值:3
可选值:Positive integer
保留数量,正整数
        clear_tool_inputs boolean 默认值:false
可选值:true、false
是否在清理工具结果时同时清理工具调用参数
eca_image_config object 图片生成参数,仅gemini-3.1-flash-image-preview、gemini-3-pro-image-preview、gemini-2.5-flash-image支持。示例:"eca_image_config":
    aspect_ratio string 默认值:1:1
可选值:1:1、2:3、3:2、3:4、4:3、4:5、5:4、9:16、16:9、21:9
生成图片宽高比
    image_size string 默认值:1K
可选值:1K、2K、4K
生成图片分辨率,仅gemini-3.1-flash-image-preview、gemini-3.0-pro-image-preview支持
modalities array 可选值:text: model returns text; image: model returns images (gemini series only); audio: model returns audio (openai series only)
模型生成的输出类型,一个模型可能支持多种类型组合,仅openai与gemini系列支持该参数。text=返回文本;image=返回图片(仅gemini系列支持);audio=返回音频(仅openai系列支持)。示例:"modalities": ["text", "image"]

请求示例

curl -v 'https://www.sagelib.com/chat/completions' \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-r1",
    "messages": [
      {
        "role": "user",
        "content": "What is 1+1?"
      }
    ]
  }'
import json
import requests

base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/chat/completions"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
    "model": "deepseek-r1",
    "messages": [
        {
            "role": "user",
            "content": "What is 1+1?"
        }
    ]
}

response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
    print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
    print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class LlmStreamClient {
    public static void main(String[] args) {
        String baseUrl = "https://www.sagelib.com";
        String apiKey = "***";
        String url = "https://www.sagelib.com/chat/completions";

        String jsonRequestBody = """
            {
                "model": "deepseek-r1",
                "messages": [
                    {
                        "role": "user",
                        "content": "What is 1+1?"
                    }
                ]
            }
            """;

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
                .build();

        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println("Status Code: " + response.statusCode());
            System.out.println(response.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
)

func main() {
    baseUrl := "https://www.sagelib.com"
    apiKey := "***"
    url := "https://www.sagelib.com/chat/completions"

    jsonRequestBody := `
            {
                "model": "deepseek-r1",
                "messages": [
                    {
                        "role": "user",
                        "content": "What is 1+1?"
                    }
                ]
            }
`

    req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
    if err != nil {
        fmt.Println("Failed to create request:", err)
        return
    }
    req.Header.Set("Authorization", "Bearer "+apiKey)
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Request failed:", err)
        return
    }
    defer resp.Body.Close()

    body, _ := io.ReadAll(resp.Body)
    var prettyBody map[string]interface{}
    if err := json.Unmarshal(body, &prettyBody); err == nil {
        pretty, _ := json.MarshalIndent(prettyBody, "", "  ")
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(pretty))
    } else {
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(body))
    }
}
curl -v 'https://www.sagelib.com/chat/completions' \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "edusolve-v1",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "Solve the problem in the image"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "https://xxxxxxx.png"
            }
          }
        ]
      }
    ],
    "stream": true,
    "max_tokens": 300
  }'
import json
import requests

base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/chat/completions"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
    "model": "edusolve-v1",
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "Solve the problem in the image"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://xxxxxxx.png"
                    }
                }
            ]
        }
    ],
    "stream": True,
    "max_tokens": 300
}

response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
    print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
    print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class LlmStreamClient {
    public static void main(String[] args) {
        String baseUrl = "https://www.sagelib.com";
        String apiKey = "***";
        String url = "https://www.sagelib.com/chat/completions";

        String jsonRequestBody = """
            {
                "model": "edusolve-v1",
                "messages": [
                    {
                        "role": "user",
                        "content": [
                            {
                                "type": "text",
                                "text": "Solve the problem in the image"
                            },
                            {
                                "type": "image_url",
                                "image_url": {
                                    "url": "https://xxxxxxx.png"
                                }
                            }
                        ]
                    }
                ],
                "stream": true,
                "max_tokens": 300
            }
            """;

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
                .build();

        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println("Status Code: " + response.statusCode());
            System.out.println(response.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
)

func main() {
    baseUrl := "https://www.sagelib.com"
    apiKey := "***"
    url := "https://www.sagelib.com/chat/completions"

    jsonRequestBody := `
            {
                "model": "edusolve-v1",
                "messages": [
                    {
                        "role": "user",
                        "content": [
                            {
                                "type": "text",
                                "text": "Solve the problem in the image"
                            },
                            {
                                "type": "image_url",
                                "image_url": {
                                    "url": "https://xxxxxxx.png"
                                }
                            }
                        ]
                    }
                ],
                "stream": true,
                "max_tokens": 300
            }
`

    req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
    if err != nil {
        fmt.Println("Failed to create request:", err)
        return
    }
    req.Header.Set("Authorization", "Bearer "+apiKey)
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Request failed:", err)
        return
    }
    defer resp.Body.Close()

    body, _ := io.ReadAll(resp.Body)
    var prettyBody map[string]interface{}
    if err := json.Unmarshal(body, &prettyBody); err == nil {
        pretty, _ := json.MarshalIndent(prettyBody, "", "  ")
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(pretty))
    } else {
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(body))
    }
}
curl -v 'https://www.sagelib.com/chat/completions' \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3-pro-image-preview",
    "stream": true,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "text": "Generate a new image as per my requirements: A Chinese model wearing this dress, everything else unchanged",
            "type": "text"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "https://xxxxx.jpg"
            }
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "https://xxxxx.jpg"
            }
          }
        ]
      }
    ]
  }'
import json
import requests

base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/chat/completions"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
    "model": "gemini-3-pro-image-preview",
    "stream": True,
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "text": "Generate a new image as per my requirements: A Chinese model wearing this dress, everything else unchanged",
                    "type": "text"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://xxxxx.jpg"
                    }
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://xxxxx.jpg"
                    }
                }
            ]
        }
    ]
}

response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
    print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
    print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class LlmStreamClient {
    public static void main(String[] args) {
        String baseUrl = "https://www.sagelib.com";
        String apiKey = "***";
        String url = "https://www.sagelib.com/chat/completions";

        String jsonRequestBody = """
            {
                "model": "gemini-3-pro-image-preview",
                "stream": true,
                "messages": [
                    {
                        "role": "user",
                        "content": [
                            {
                                "text": "Generate a new image as per my requirements: A Chinese model wearing this dress, everything else unchanged",
                                "type": "text"
                            },
                            {
                                "type": "image_url",
                                "image_url": {
                                    "url": "https://xxxxx.jpg"
                                }
                            },
                            {
                                "type": "image_url",
                                "image_url": {
                                    "url": "https://xxxxx.jpg"
                                }
                            }
                        ]
                    }
                ]
            }
            """;

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
                .build();

        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println("Status Code: " + response.statusCode());
            System.out.println(response.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
)

func main() {
    baseUrl := "https://www.sagelib.com"
    apiKey := "***"
    url := "https://www.sagelib.com/chat/completions"

    jsonRequestBody := `
            {
                "model": "gemini-3-pro-image-preview",
                "stream": true,
                "messages": [
                    {
                        "role": "user",
                        "content": [
                            {
                                "text": "Generate a new image as per my requirements: A Chinese model wearing this dress, everything else unchanged",
                                "type": "text"
                            },
                            {
                                "type": "image_url",
                                "image_url": {
                                    "url": "https://xxxxx.jpg"
                                }
                            },
                            {
                                "type": "image_url",
                                "image_url": {
                                    "url": "https://xxxxx.jpg"
                                }
                            }
                        ]
                    }
                ]
            }
`

    req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
    if err != nil {
        fmt.Println("Failed to create request:", err)
        return
    }
    req.Header.Set("Authorization", "Bearer "+apiKey)
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Request failed:", err)
        return
    }
    defer resp.Body.Close()

    body, _ := io.ReadAll(resp.Body)
    var prettyBody map[string]interface{}
    if err := json.Unmarshal(body, &prettyBody); err == nil {
        pretty, _ := json.MarshalIndent(prettyBody, "", "  ")
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(pretty))
    } else {
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(body))
    }
}
curl -v 'https://www.sagelib.com/chat/completions' \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful math tutor. Guide the user through the solution step by step."
      },
      {
        "role": "user",
        "content": "how can I solve 8x + 7 = -23"
      }
    ],
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "math_reasoning",
        "schema": {
          "type": "object",
          "properties": {
            "steps": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "explanation": {
                    "type": "string"
                  },
                  "output": {
                    "type": "string"
                  }
                },
                "required": [
                  "explanation",
                  "output"
                ],
                "additionalProperties": false
              }
            },
            "final_answer": {
              "type": "string"
            }
          },
          "required": [
            "steps",
            "final_answer"
          ],
          "additionalProperties": false
        },
        "strict": true
      }
    }
  }'
import json
import requests

base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/chat/completions"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
    "model": "gpt-4o",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful math tutor. Guide the user through the solution step by step."
        },
        {
            "role": "user",
            "content": "how can I solve 8x + 7 = -23"
        }
    ],
    "response_format": {
        "type": "json_schema",
        "json_schema": {
            "name": "math_reasoning",
            "schema": {
                "type": "object",
                "properties": {
                    "steps": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "explanation": {
                                    "type": "string"
                                },
                                "output": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "explanation",
                                "output"
                            ],
                            "additionalProperties": False
                        }
                    },
                    "final_answer": {
                        "type": "string"
                    }
                },
                "required": [
                    "steps",
                    "final_answer"
                ],
                "additionalProperties": False
            },
            "strict": True
        }
    }
}

response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
    print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
    print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class LlmStreamClient {
    public static void main(String[] args) {
        String baseUrl = "https://www.sagelib.com";
        String apiKey = "***";
        String url = "https://www.sagelib.com/chat/completions";

        String jsonRequestBody = """
            {
                "model": "gpt-4o",
                "messages": [
                    {
                        "role": "system",
                        "content": "You are a helpful math tutor. Guide the user through the solution step by step."
                    },
                    {
                        "role": "user",
                        "content": "how can I solve 8x + 7 = -23"
                    }
                ],
                "response_format": {
                    "type": "json_schema",
                    "json_schema": {
                        "name": "math_reasoning",
                        "schema": {
                            "type": "object",
                            "properties": {
                                "steps": {
                                    "type": "array",
                                    "items": {
                                        "type": "object",
                                        "properties": {
                                            "explanation": {
                                                "type": "string"
                                            },
                                            "output": {
                                                "type": "string"
                                            }
                                        },
                                        "required": [
                                            "explanation",
                                            "output"
                                        ],
                                        "additionalProperties": false
                                    }
                                },
                                "final_answer": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "steps",
                                "final_answer"
                            ],
                            "additionalProperties": false
                        },
                        "strict": true
                    }
                }
            }
            """;

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
                .build();

        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println("Status Code: " + response.statusCode());
            System.out.println(response.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
)

func main() {
    baseUrl := "https://www.sagelib.com"
    apiKey := "***"
    url := "https://www.sagelib.com/chat/completions"

    jsonRequestBody := `
            {
                "model": "gpt-4o",
                "messages": [
                    {
                        "role": "system",
                        "content": "You are a helpful math tutor. Guide the user through the solution step by step."
                    },
                    {
                        "role": "user",
                        "content": "how can I solve 8x + 7 = -23"
                    }
                ],
                "response_format": {
                    "type": "json_schema",
                    "json_schema": {
                        "name": "math_reasoning",
                        "schema": {
                            "type": "object",
                            "properties": {
                                "steps": {
                                    "type": "array",
                                    "items": {
                                        "type": "object",
                                        "properties": {
                                            "explanation": {
                                                "type": "string"
                                            },
                                            "output": {
                                                "type": "string"
                                            }
                                        },
                                        "required": [
                                            "explanation",
                                            "output"
                                        ],
                                        "additionalProperties": false
                                    }
                                },
                                "final_answer": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "steps",
                                "final_answer"
                            ],
                            "additionalProperties": false
                        },
                        "strict": true
                    }
                }
            }
`

    req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
    if err != nil {
        fmt.Println("Failed to create request:", err)
        return
    }
    req.Header.Set("Authorization", "Bearer "+apiKey)
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Request failed:", err)
        return
    }
    defer resp.Body.Close()

    body, _ := io.ReadAll(resp.Body)
    var prettyBody map[string]interface{}
    if err := json.Unmarshal(body, &prettyBody); err == nil {
        pretty, _ := json.MarshalIndent(prettyBody, "", "  ")
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(pretty))
    } else {
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(body))
    }
}

响应参数

Body 参数

参数名称 描述
id string 此次响应的唯一标识符,用于追踪和识别特定的响应
model string 实际处理本次请求的模型名称
object string 可选值:chat.completion
响应的对象类型,chat.completion表明是聊天完成后的响应
created integer 响应生成的时间戳,从Unix纪元(1970-01-01 00:00:00 UTC)到响应生成时刻所经过的秒数
choices array 候选回复数组,含 index/message/finish_reason
    index integer 当前选择在choices数组中的索引位置,一般从0开始
    message object 包含模型回复消息的相关信息
        role string 可选值:assistant
消息发送者角色,assistant表示是模型(助手)的回复
        content string 模型生成的具体回复内容,生图请求返回图片url或图片的base64编码,Base64格式为 data:image/${IMAGE_FORMAT};base64,${IMAGE_BASE64}
        reasoning_content string 思考内容
        tool_calls array 工具调用列表
            id string
            type string 可选值:function
            function object
                name string 工具函数名
                arguments string 工具调用参数JSON字符串
    finish_reason string 可选值:stop、length、content_filter、insufficient_system_resource、tool_calls
停止原因。stop=模型自然停止或遇到stop序列;length=输出达到上下文长度或max_tokens限制;content_filter=内容触发过滤策略被过滤;insufficient_system_resource=系统推理资源不足被打断;tool_calls=模型判断需调用工具
usage object 此次请求和响应所使用的token数量统计信息
    prompt_tokens integer 请求中使用token数,即用户输入内容占用token数
    completion_tokens integer 模型响应使用token数,即模型回复内容占用token数
    total_tokens integer 请求和响应总共使用token数,等于prompt_tokens与completion_tokens之和
    prompt_tokens_details object 请求token详细分类
    completion_tokens_details object 响应token详细分类

流式响应(SSE),content-type为text/event-stream。data为存储消息的集合,最后一个data值固定为[DONE]表示响应结束。data含id(对话唯一标识符)、object(对象名称)、created(创建时间戳Unix秒)、model(实际处理请求的模型名)、choices。delta为模型生成的completion消息,含role/content/reasoning_content/tool_calls等增量字段,生图请求返回图片url或base64编码

响应示例

{
  "id": "chatcmpl-8g199l9xsC4fPyEYpLywUtI2pQOl7",
  "model": "gpt-4o",
  "object": "chat.completion",
  "created": 1705024875,
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "China example Company is ..."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 14,
    "completion_tokens": 100,
    "total_tokens": 114
  }
}
{
  "id": "chatcmpl-CmzA77lMCLRue8u8BegChh49VgXks",
  "model": "gpt-4o",
  "object": "chat.completion",
  "created": 1765790887,
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "tool_calls": [
          {
            "id": "call_wG6E8K4r63TAQEpiCoHUFFLu",
            "type": "function",
            "function": {
              "name": "get_current_weather",
              "arguments": "{\"location\":\"Boston, MA\",\"unit\":\"fahrenheit\"}"
            }
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ],
  "usage": {
    "prompt_tokens": 161,
    "completion_tokens": 31,
    "total_tokens": 192
  }
}
data: {"id":"2cdfd198-31f6-9c69-ade8-82acb8c65ba1","object":"chat.completion.chunk","created":1739843952,"model":"gpt-4o","choices":[{"index":0,"delta":{"role":"assistant","content":"","reasoning_content":""}}]}
...
data: {"id":"2cdfd198-31f6-9c69-ade8-82acb8c65ba1","object":"chat.completion.chunk","created":1739843964,"model":"gpt-4o","choices":[{"index":0,"delta":{"role":"assistant","content":"","reasoning_content":""},"finish_reason":"stop"}]}
data: [DONE]

状态码

HTTP状态码 描述
200 请求成功
401 访问被拒绝,拒绝原因见响应主体。 可能的原因有:缺少Authorization请求头,token错误等。
403 访问被拒绝,拒绝原因见响应主体。 可能的原因有:请求主体格式错误、请求主体缺少用户问题等。
500 服务器内部错误,原因见响应主体
POSThttps://www.sagelib.com/anthropic/v1/messages

Anthropic直连模式支持通过边缘AI网关,以Anthropic标准 /v1/messages 接口格式访问Anthropic模型,除AI网关专属鉴权、路径参数、模型名称外,其余请求参数均使用Anthropic原生规范,仅需完成网关专属配置即可接入。

接口鉴权方式请参考:API接入与鉴权

当前展示通用接口文档,各模型支持的参数存在差异。请点击下方模型标签查看专属文档。

请求参数

请求头

参数名称 描述
*Content-Type string 可选值:application/json
application/json
*anthropic-version string 可选值:2023-06-01
Anthropic API 版本号,固定为 2023-06-01
*X-Api-Key string 鉴权令牌,格式为 ${AIGATEWAY_TOKEN}

Body 参数

参数名称 描述
*model string 可选值:See model list
用于完成请求的模型名称。
*messages array 输入消息列表。包含 role (user/assistant) 和 content 。
*max_tokens number 生成停止前的最大 Token 数。
system string 系统提示词,用于设定上下文或角色。
stream boolean 默认值:false
可选值:true、false
是否使用流式输出(SSE)。
temperature number 默认值:1
可选值:0.0 - 1.0
采样的随机性。靠近 0 更分析性,靠近 1 更创造性。
thinking object 可选值:{ "type": "enabled", "budget_tokens": 1024 }
启用 Claude 的扩展思考功能(Extended Thinking)。需指定 budget_tokens (≥1024)。
tools array 定义模型可使用的工具列表。
tool_choice object 默认值:auto
强制模型如何使用工具(自动、任何、特定工具或禁用)。
metadata object 请求的元数据,如 user_id 。
stop_sequences array 自定义停止生成的文本序列列表。
top_p number 可选值:0.0 - 1.0
核采样概率阈值。建议与 temperature 二选一使用。
top_k number 仅从概率最高的 K 个选项中采样。推荐仅用于高级用例。

请求示例

curl -v 'https://www.sagelib.com/anthropic/messages' \
  -H "x-api-key: ***" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "max_tokens": 1024,
    "messages": [
      {
        "content": "Hello, world",
        "role": "user"
      }
    ],
    "model": "anthropic.claude-sonnet-4-5"
  }'
import json
import requests

base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/anthropic/messages"
headers = {"x-api-key": api_key, "anthropic-version": "2023-06-01", "Content-Type": "application/json"}
data = {
    "max_tokens": 1024,
    "messages": [
        {
            "content": "Hello, world",
            "role": "user"
        }
    ],
    "model": "anthropic.claude-sonnet-4-5"
}

response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
    print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
    print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class LlmStreamClient {
    public static void main(String[] args) {
        String baseUrl = "https://www.sagelib.com";
        String apiKey = "***";
        String url = "https://www.sagelib.com/anthropic/messages";

        String jsonRequestBody = """
            {
                "max_tokens": 1024,
                "messages": [
                    {
                        "content": "Hello, world",
                        "role": "user"
                    }
                ],
                "model": "anthropic.claude-sonnet-4-5"
            }
            """;

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("x-api-key", apiKey).header("anthropic-version", "2023-06-01")
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
                .build();

        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println("Status Code: " + response.statusCode());
            System.out.println(response.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
)

func main() {
    baseUrl := "https://www.sagelib.com"
    apiKey := "***"
    url := "https://www.sagelib.com/anthropic/messages"

    jsonRequestBody := `
            {
                "max_tokens": 1024,
                "messages": [
                    {
                        "content": "Hello, world",
                        "role": "user"
                    }
                ],
                "model": "anthropic.claude-sonnet-4-5"
            }
`

    req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
    if err != nil {
        fmt.Println("Failed to create request:", err)
        return
    }
    req.Header.Set("x-api-key", apiKey)
    req.Header.Set("anthropic-version", "2023-06-01")
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Request failed:", err)
        return
    }
    defer resp.Body.Close()

    body, _ := io.ReadAll(resp.Body)
    var prettyBody map[string]interface{}
    if err := json.Unmarshal(body, &prettyBody); err == nil {
        pretty, _ := json.MarshalIndent(prettyBody, "", "  ")
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(pretty))
    } else {
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(body))
    }
}

响应参数

Body 参数

参数名称 描述
id string 消息的唯一标识符。
type string 可选值:message
响应对象类型,固定为 message。
role string 可选值:assistant
生成消息的角色,固定为 assistant。
model string 处理请求的模型名称。
content array 生成的内容块数组。通常包含 type: "text" 或 type: "tool_use" 的块。如果启用了思考功能,还会包含 type: "thinking" 块。
    type string 可选值:text、tool_use、thinking
内容块类型。
    text string 文本内容(type 为 text 时)。
stop_reason string 可选值:end_turn、max_tokens、stop_sequence、tool_use
停止生成的原因。end_turn=自然结束;max_tokens=达到最大 Token 限制;stop_sequence=遇到自定义停止序列;tool_use=模型调用了工具。
stop_sequence 命中的自定义停止序列,可为 null。
usage object Token 使用统计,包含输入、输出及缓存相关的 Token 计数。
    input_tokens integer 输入 Token 数。
    output_tokens integer 输出 Token 数。
    cache_creation_input_tokens integer 缓存创建输入 Token 数。
    cache_read_input_tokens integer 缓存读取输入 Token 数。

响应示例

{
  "id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
  "type": "message",
  "role": "assistant",
  "model": "claude-sonnet-4-5",
  "content": [
    {
      "type": "text",
      "text": "Hi! my name is Claude."
    }
  ],
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 2095,
    "output_tokens": 503,
    "cache_creation_input_tokens": 2051,
    "cache_read_input_tokens": 2051
  }
}

状态码

HTTP状态码 描述
200 请求成功
401 访问被拒绝,拒绝原因见响应主体,可能原因为鉴权信息错误(如 X-Api-Key 无效)等
403 访问被拒绝,原因见响应主体(如格式错误、参数缺失等)
500 服务器内部错误,原因见响应主体
POSThttps://www.sagelib.com/openai/chat/completions

OpenAI 直连模式支持通过边缘 AI 网关,以 OpenAI 标准 /v1/chat/completions、/v1/responses、/v1/images/、/v1/audio/、/v1/videos 等接口格式访问模型。除 AI 网关专属鉴权、URL 路径前缀、模型名称外,其余请求参数均使用 OpenAI 原生规范,仅需完成网关专属配置即可接入。客户端只需将原厂 SDK 的 base URL 替换为网关地址 + openai 前缀,请求体无需修改。

接口鉴权方式请参考:API接入与鉴权

当前展示通用接口文档,各模型支持的参数存在差异。请点击下方模型标签查看专属文档。

请求参数

请求头

参数名称 描述
*Content-Type string application/json(JSON 端点)或 multipart/form-data; boundary=...(multipart 端点,保持原始 boundary)
*Authorization string 鉴权令牌,格式为 Bearer ${AIGATEWAY_TOKEN}

Body 参数

参数名称 描述
*model string [chat/completions] 模型名称,可带前缀(openai.gpt-4o)或不带前缀(gpt-4o),网关自动识别并路由。响应中的 model 字段会被改写为客户端请求的模型名。
messages array [chat/completions] 当前对话的消息列表,每项含 role(system/user/assistant/tool)与 content。content 可为文本或数组(文本为 {"type":"text","text":"string"},图片为 {"type":"image_url","image_url":{"url":"data:image/png;base64,...","detail":"high"}})。
stream boolean 默认值:false
是否流式返回。
stream_options object [chat/completions] 流式选项。直连模式下网关会自动注入 stream_options.include_usage=true,使上游在流末尾以单独一帧返回真实 usage。
temperature float 默认值:1
采样温度。
top_p float 默认值:1
核采样概率。
max_tokens integer [chat/completions] 最大生成 Token 数(仅 gpt-4o 等老模型支持;推理模型请改用 max_completion_tokens)。
max_completion_tokens integer [chat/completions] 最大生成 Token 数(o 系列 / gpt-5 等推理模型使用,替代 max_tokens)。
tools array 模型可调用的工具列表(function 等)。典型形式:{"type":"function","function":{"name":"...","description":"...","parameters":{...}}}。Responses 端点还支持 web_search、file_search 等内置工具。
tool_choice string 默认值:auto
工具调用策略。
response_format object [chat/completions] 指定输出格式,如 {"type": "json_object"}。
reasoning_effort string 可选值:none、minimal、low、medium、high、xhigh
[chat/completions] 推理强度(none/minimal/low/medium/high/xhigh,适用 reasoning 模型)。
service_tier string 向上游触发额外计费层级,且不同上游支持不一,传给不兼容上游可能报错。
store boolean 允许上游永久存储请求/响应内容,涉及用户数据隐私,默认屏蔽。
input string [responses] 输入内容:纯文本字符串,或消息项数组(每项含 role/content,role 可为 user/assistant/system/developer)。content 可为文本 {"type":"input_text","text":"string"} 或图片 {"type":"input_image","image_url":"data:image/png;base64,..."}。
instructions string [responses] 系统级指令,贯穿整个会话,优先级高于 input 中的 developer/system 消息。
max_output_tokens integer [responses] 最大输出 Token 数(Responses API 不使用 max_tokens)。
parallel_tool_calls boolean 默认值:true
[responses] 是否允许并行工具调用。
reasoning object [responses] 推理配置,如 {"effort": "medium", "summary": "auto"}(仅 reasoning 模型)。
text object [responses] 文本输出格式,如 {"format": {"type": "json_object"}}。
truncation string 可选值:auto、disabled
[responses] 上下文截断策略(auto 自动截断 / disabled 不截断)。
previous_response_id string [responses] 不支持:上一次响应 ID,用于服务端串联会话。
include array [responses] 额外返回字段,如 ["file_search_call.results"]。
metadata object [responses] 附带键值对(最多 16 对)。
user string [responses] 终端用户标识。
background boolean [responses] 不支持:触发上游异步响应模式。

请求示例

curl -v 'https://www.sagelib.com/openai/chat/completions' \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "${MODEL}",
    "messages": [
      {
        "role": "user",
        "content": "Hello, how does AI work?"
      }
    ]
  }'
import json
import requests

base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/openai/chat/completions"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
    "model": "${MODEL}",
    "messages": [
        {
            "role": "user",
            "content": "Hello, how does AI work?"
        }
    ]
}

response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
    print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
    print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class LlmStreamClient {
    public static void main(String[] args) {
        String baseUrl = "https://www.sagelib.com";
        String apiKey = "***";
        String url = "https://www.sagelib.com/openai/chat/completions";

        String jsonRequestBody = """
            {
                "model": "${MODEL}",
                "messages": [
                    {
                        "role": "user",
                        "content": "Hello, how does AI work?"
                    }
                ]
            }
            """;

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
                .build();

        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println("Status Code: " + response.statusCode());
            System.out.println(response.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
)

func main() {
    baseUrl := "https://www.sagelib.com"
    apiKey := "***"
    url := "https://www.sagelib.com/openai/chat/completions"

    jsonRequestBody := `
            {
                "model": "${MODEL}",
                "messages": [
                    {
                        "role": "user",
                        "content": "Hello, how does AI work?"
                    }
                ]
            }
`

    req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
    if err != nil {
        fmt.Println("Failed to create request:", err)
        return
    }
    req.Header.Set("Authorization", "Bearer "+apiKey)
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Request failed:", err)
        return
    }
    defer resp.Body.Close()

    body, _ := io.ReadAll(resp.Body)
    var prettyBody map[string]interface{}
    if err := json.Unmarshal(body, &prettyBody); err == nil {
        pretty, _ := json.MarshalIndent(prettyBody, "", "  ")
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(pretty))
    } else {
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(body))
    }
}
curl -v 'https://www.sagelib.com/openai/chat/completions' \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "${MODEL}",
    "stream": true,
    "messages": [
      {
        "role": "user",
        "content": "Hello, how does AI work?"
      }
    ]
  }'
import json
import requests

base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/openai/chat/completions"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
    "model": "${MODEL}",
    "stream": True,
    "messages": [
        {
            "role": "user",
            "content": "Hello, how does AI work?"
        }
    ]
}

response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
    print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
    print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class LlmStreamClient {
    public static void main(String[] args) {
        String baseUrl = "https://www.sagelib.com";
        String apiKey = "***";
        String url = "https://www.sagelib.com/openai/chat/completions";

        String jsonRequestBody = """
            {
                "model": "${MODEL}",
                "stream": true,
                "messages": [
                    {
                        "role": "user",
                        "content": "Hello, how does AI work?"
                    }
                ]
            }
            """;

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
                .build();

        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println("Status Code: " + response.statusCode());
            System.out.println(response.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
)

func main() {
    baseUrl := "https://www.sagelib.com"
    apiKey := "***"
    url := "https://www.sagelib.com/openai/chat/completions"

    jsonRequestBody := `
            {
                "model": "${MODEL}",
                "stream": true,
                "messages": [
                    {
                        "role": "user",
                        "content": "Hello, how does AI work?"
                    }
                ]
            }
`

    req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
    if err != nil {
        fmt.Println("Failed to create request:", err)
        return
    }
    req.Header.Set("Authorization", "Bearer "+apiKey)
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Request failed:", err)
        return
    }
    defer resp.Body.Close()

    body, _ := io.ReadAll(resp.Body)
    var prettyBody map[string]interface{}
    if err := json.Unmarshal(body, &prettyBody); err == nil {
        pretty, _ := json.MarshalIndent(prettyBody, "", "  ")
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(pretty))
    } else {
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(body))
    }
}
curl -v 'https://www.sagelib.com/openai/chat/completions' \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "${MODEL}",
    "input": "Hello, how does AI work?"
  }'
import json
import requests

base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/openai/chat/completions"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
    "model": "${MODEL}",
    "input": "Hello, how does AI work?"
}

response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
    print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
    print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class LlmStreamClient {
    public static void main(String[] args) {
        String baseUrl = "https://www.sagelib.com";
        String apiKey = "***";
        String url = "https://www.sagelib.com/openai/chat/completions";

        String jsonRequestBody = """
            {
                "model": "${MODEL}",
                "input": "Hello, how does AI work?"
            }
            """;

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
                .build();

        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println("Status Code: " + response.statusCode());
            System.out.println(response.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
)

func main() {
    baseUrl := "https://www.sagelib.com"
    apiKey := "***"
    url := "https://www.sagelib.com/openai/chat/completions"

    jsonRequestBody := `
            {
                "model": "${MODEL}",
                "input": "Hello, how does AI work?"
            }
`

    req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
    if err != nil {
        fmt.Println("Failed to create request:", err)
        return
    }
    req.Header.Set("Authorization", "Bearer "+apiKey)
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Request failed:", err)
        return
    }
    defer resp.Body.Close()

    body, _ := io.ReadAll(resp.Body)
    var prettyBody map[string]interface{}
    if err := json.Unmarshal(body, &prettyBody); err == nil {
        pretty, _ := json.MarshalIndent(prettyBody, "", "  ")
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(pretty))
    } else {
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(body))
    }
}
curl -v 'https://www.sagelib.com/openai/chat/completions' \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "${MODEL}",
    "stream": true,
    "input": "Hello, how does AI work?"
  }'
import json
import requests

base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/openai/chat/completions"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {
    "model": "${MODEL}",
    "stream": True,
    "input": "Hello, how does AI work?"
}

response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
    print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
    print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class LlmStreamClient {
    public static void main(String[] args) {
        String baseUrl = "https://www.sagelib.com";
        String apiKey = "***";
        String url = "https://www.sagelib.com/openai/chat/completions";

        String jsonRequestBody = """
            {
                "model": "${MODEL}",
                "stream": true,
                "input": "Hello, how does AI work?"
            }
            """;

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
                .build();

        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println("Status Code: " + response.statusCode());
            System.out.println(response.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
)

func main() {
    baseUrl := "https://www.sagelib.com"
    apiKey := "***"
    url := "https://www.sagelib.com/openai/chat/completions"

    jsonRequestBody := `
            {
                "model": "${MODEL}",
                "stream": true,
                "input": "Hello, how does AI work?"
            }
`

    req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
    if err != nil {
        fmt.Println("Failed to create request:", err)
        return
    }
    req.Header.Set("Authorization", "Bearer "+apiKey)
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Request failed:", err)
        return
    }
    defer resp.Body.Close()

    body, _ := io.ReadAll(resp.Body)
    var prettyBody map[string]interface{}
    if err := json.Unmarshal(body, &prettyBody); err == nil {
        pretty, _ := json.MarshalIndent(prettyBody, "", "  ")
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(pretty))
    } else {
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(body))
    }
}

响应参数

Body 参数

参数名称 描述
id string 响应 ID(chat/completions 为 chatcmpl- 前缀;responses 为 resp_ 前缀)。
object string 对象类型(chat.completion 或 response)。
created integer 创建时间戳(秒)。
model string 处理请求的模型名(被网关改写为客户端请求的模型名)。
choices array [chat/completions] 生成的候选回复列表。每项含 message/delta(实际生成内容,流式为 delta)、index、finish_reason(停止原因:stop 正常结束,length 达到长度限制,tool_calls 工具调用,content_filter 内容过滤)。
output array [responses] 输出项数组,每项为一个 item,type 决定其结构:message(助手消息,含 role 与 content 数组,content 项 type 为 output_text/reasoning_text/output_image 等)、reasoning(推理过程项)、function_call(工具调用项,含 call_id/name/arguments)、web_search_call/file_search_call/computer_call 等(内置工具调用项)。
status string [responses] 响应状态(queued 排队 / in_progress 进行中 / completed 正常完成 / failed 失败 / incomplete 因 token 或工具限制未完成 / cancelled 已取消)。
usage object Token 使用统计。chat/completions 含 prompt_tokens/completion_tokens/total_tokens 及 prompt_tokens_details.cached_tokens、completion_tokens_details.reasoning_tokens;responses 含 input_tokens/output_tokens/total_tokens 及 input_tokens_details.cached_tokens、output_tokens_details.reasoning_tokens。流式时 usage 由 stream_options.include_usage 触发,在流末尾以 choices 为空数组的单独一帧返回。

流式响应(SSE)。chat/completions 每帧为 data: {JSON chunk},末尾 data: [DONE];responses 每帧带 event 与 data 两个字段,网关逐行原样转发。

响应示例

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1737456000,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello! I am happy to help. How can I assist you?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 20,
    "total_tokens": 30,
    "prompt_tokens_details": {
      "cached_tokens": 0
    },
    "completion_tokens_details": {
      "reasoning_tokens": 0
    }
  }
}
{
  "id": "resp_abc123",
  "object": "response",
  "created_at": 1737456000,
  "status": "completed",
  "model": "gpt-4o",
  "output": [
    {
      "type": "message",
      "id": "msg_abc123",
      "status": "completed",
      "role": "assistant",
      "content": [
        {
          "type": "output_text",
          "text": "Hello! I am happy to help.",
          "annotations": []
        }
      ]
    }
  ],
  "usage": {
    "input_tokens": 10,
    "output_tokens": 20,
    "total_tokens": 30,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "output_tokens_details": {
      "reasoning_tokens": 0
    }
  }
}
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1737456000,"model":"gpt-4o","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1737456000,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1737456000,"model":"gpt-4o","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1737456000,"model":"gpt-4o","choices":[],"usage":{"prompt_tokens":10,"completion_tokens":20,"total_tokens":30}}

data: [DONE]
event: response.created
data: {"type":"response.created","response":{"id":"resp_abc123","object":"response","status":"in_progress","model":"gpt-4o",...}}

event: response.output_item.added
data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","role":"assistant","content":[],"status":"in_progress"}}

event: response.output_text.delta
data: {"type":"response.output_text.delta","output_index":0,"content_index":0,"delta":"Hello"}

event: response.output_text.done
data: {"type":"response.output_text.done","output_index":0,"content_index":0,"text":"Hello! I am happy to help."}

event: response.completed
data: {"type":"response.completed","response":{"id":"resp_abc123","object":"response","status":"completed","model":"gpt-4o","usage":{"input_tokens":10,"output_tokens":20,"total_tokens":30}}}

状态码

HTTP状态码 描述
200 请求成功
400 请求参数错误(如 JSON 格式错误、无效的参数组合)
401 认证失败(API Key 无效)
403 权限不足或访问被拒绝;服务暂时不可用,请稍后重试
429 请求过多(速率限制)
500 服务器内部错误
POSThttps://www.sagelib.com/gemini/v1beta/models/{model}:streamGenerateContent

Google Gemini直连模式的流式聊天生成端点,以 Gemini 原生 :streamGenerateContent 接口格式流式访问 Gemini 模型,响应以 SSE 分块逐步返回。非流式调用请使用同路径的 :generateContent 接口(去掉 URL 中的 stream 前缀,参数格式完全相同)。除 AI 网关专属鉴权、路径参数、模型名称外,其余请求参数均使用 Gemini 原生规范,仅需完成网关专属配置即可接入。

接口鉴权方式请参考:API接入与鉴权

当前展示通用接口文档,各模型支持的参数存在差异。请点击下方模型标签查看专属文档。

请求参数

请求头

参数名称 描述
*Content-Type string 可选值:application/json
application/json
*x-goog-api-key string 鉴权令牌,格式为 ${AIGATEWAY_TOKEN}

Body 参数

参数名称 描述
*contents array 当前对话的内容列表。包含 role 和 parts 。每个对象含 role(通常为 "user" 或 "model")和 parts(内容部分数组,文本为 {"text":"string"},图片/音频/视频为 {"inline_data":{"mime_type":"string","data":"base64_string"}} 或 {"file_data":{"mime_type":"string","file_uri":"uri"}})。
system_instruction object 系统指令(System Prompt),用于设定模型的角色或行为。
generationConfig object 生成配置选项(如温度、Token限制等)。含 candidateCount(返回候选回复数量,通常为1)、stopSequences(自定义停止序列)、maxOutputTokens(最大生成Token数)、temperature(采样温度 0.0-2.0)、topP(核采样概率)、topK(Top-K采样)、responseMimeType(指定输出格式,如 "application/json")。
safetySettings array 安全设置列表,用于过滤仇恨言论、骚扰等内容。
tools array 模型可使用的工具列表(如函数调用 function_declarations 或代码执行 code_execution )。
toolConfig object 工具配置,例如强制使用特定函数。
cachedContent string 引用已缓存的上下文内容的资源名称。

请求示例

curl -v 'https://www.sagelib.com/gemini/v1beta/models/{model}:streamGenerateContent' \
  -H "x-goog-api-key: ***" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "parts": [
          {
            "text": "Hello, how does AI work?"
          }
        ]
      }
    ]
  }'
import json
import requests

base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/gemini/v1beta/models/{model}:streamGenerateContent"
headers = {"x-goog-api-key": api_key, "Content-Type": "application/json"}
data = {
    "contents": [
        {
            "parts": [
                {
                    "text": "Hello, how does AI work?"
                }
            ]
        }
    ]
}

response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
    print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
    print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class LlmStreamClient {
    public static void main(String[] args) {
        String baseUrl = "https://www.sagelib.com";
        String apiKey = "***";
        String url = "https://www.sagelib.com/gemini/v1beta/models/{model}:streamGenerateContent";

        String jsonRequestBody = """
            {
                "contents": [
                    {
                        "parts": [
                            {
                                "text": "Hello, how does AI work?"
                            }
                        ]
                    }
                ]
            }
            """;

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("x-goog-api-key", apiKey)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
                .build();

        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println("Status Code: " + response.statusCode());
            System.out.println(response.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
)

func main() {
    baseUrl := "https://www.sagelib.com"
    apiKey := "***"
    url := "https://www.sagelib.com/gemini/v1beta/models/{model}:streamGenerateContent"

    jsonRequestBody := `
            {
                "contents": [
                    {
                        "parts": [
                            {
                                "text": "Hello, how does AI work?"
                            }
                        ]
                    }
                ]
            }
`

    req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
    if err != nil {
        fmt.Println("Failed to create request:", err)
        return
    }
    req.Header.Set("x-goog-api-key", apiKey)
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Request failed:", err)
        return
    }
    defer resp.Body.Close()

    body, _ := io.ReadAll(resp.Body)
    var prettyBody map[string]interface{}
    if err := json.Unmarshal(body, &prettyBody); err == nil {
        pretty, _ := json.MarshalIndent(prettyBody, "", "  ")
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(pretty))
    } else {
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(body))
    }
}
curl -v 'https://www.sagelib.com/gemini/v1beta/models/{model}:streamGenerateContent' \
  -H "x-goog-api-key: ***" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "parts": [
          {
            "text": "Hello, how does AI work?"
          }
        ]
      }
    ]
  }'
import json
import requests

base_url = "https://www.sagelib.com"
api_key = "***"
url = f"https://www.sagelib.com/gemini/v1beta/models/{model}:streamGenerateContent"
headers = {"x-goog-api-key": api_key, "Content-Type": "application/json"}
data = {
    "contents": [
        {
            "parts": [
                {
                    "text": "Hello, how does AI work?"
                }
            ]
        }
    ]
}

response = requests.post(url, headers=headers, json=data)
print(f"Status Code: {response.status_code}")
try:
    print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
    print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class LlmStreamClient {
    public static void main(String[] args) {
        String baseUrl = "https://www.sagelib.com";
        String apiKey = "***";
        String url = "https://www.sagelib.com/gemini/v1beta/models/{model}:streamGenerateContent";

        String jsonRequestBody = """
            {
                "contents": [
                    {
                        "parts": [
                            {
                                "text": "Hello, how does AI work?"
                            }
                        ]
                    }
                ]
            }
            """;

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("x-goog-api-key", apiKey)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
                .build();

        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println("Status Code: " + response.statusCode());
            System.out.println(response.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
)

func main() {
    baseUrl := "https://www.sagelib.com"
    apiKey := "***"
    url := "https://www.sagelib.com/gemini/v1beta/models/{model}:streamGenerateContent"

    jsonRequestBody := `
            {
                "contents": [
                    {
                        "parts": [
                            {
                                "text": "Hello, how does AI work?"
                            }
                        ]
                    }
                ]
            }
`

    req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
    if err != nil {
        fmt.Println("Failed to create request:", err)
        return
    }
    req.Header.Set("x-goog-api-key", apiKey)
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Request failed:", err)
        return
    }
    defer resp.Body.Close()

    body, _ := io.ReadAll(resp.Body)
    var prettyBody map[string]interface{}
    if err := json.Unmarshal(body, &prettyBody); err == nil {
        pretty, _ := json.MarshalIndent(prettyBody, "", "  ")
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(pretty))
    } else {
        fmt.Println("Status Code:", resp.StatusCode)
        fmt.Println(string(body))
    }
}

响应参数

Body 参数

参数名称 描述
candidates array 生成的候选回复列表。每项含 content(实际生成的文本或多模态内容)、finishReason(停止原因,STOP 为正常结束,MAX_TOKENS 为达到长度限制,SAFETY 为安全拦截)、index、safetyRatings(内容安全评级结果)。
    content object 包含实际生成的文本或多模态内容,含 parts 数组与 role。
        parts array 内容部分数组。
        role string 生成角色,通常为 model。
    finishReason string 可选值:STOP、MAX_TOKENS、SAFETY
停止原因。STOP=正常结束;MAX_TOKENS=达到长度限制;SAFETY=安全拦截。
    index integer 候选索引位置。
    safetyRatings array 内容安全评级结果。
usageMetadata object Token 使用统计。
    promptTokenCount integer 输入 Token 数。
    candidatesTokenCount integer 输出 Token 数。
    totalTokenCount integer 总 Token 数。

响应示例

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "Hello! I am happy to help. How can I assist you?"
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0,
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 10,
    "candidatesTokenCount": 20,
    "totalTokenCount": 30
  }
}

状态码

HTTP状态码 描述
200 请求成功
400 请求参数错误(如 JSON 格式错误、无效的参数组合)
401 认证失败(API Key 无效)
403 权限不足或访问被拒绝
429 请求过多(速率限制)
500 服务器内部错误