glm-5.3
最后更新:2026-11-14
POSThttps://www.sagelib.com/anthropic/v1/messages
glm-5.3 模型的聊天生成接口,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 |
可选值:"glm-5.3" 模型名称,须精确匹配 |
*messages array |
可选值:content 对话消息列表,首条须为 user 角色 |
*max_tokens integer |
可选值:1 ~ 131072 最大输出 token 数,上限 131072 |
stream boolean |
默认值:false 可选值:true、false 是否启用 SSE 流式输出 |
temperature float |
默认值:1 可选值:0.0 ~ 1.0 采样温度,控制输出随机性 |
top_p float |
默认值:0.95 可选值:0.01 ~ 1.0 核采样概率阈值 |
thinking object |
可选值:type: enabled only 思考配置,该模型强制开启思考,仅支持 enabled |
reasoning_effort string |
默认值:max 可选值:low、high、max 思考深度(模型原生参数) |
tools array |
可选值:Array of Anthropic-format tool definitions 工具定义列表,含 name/description/input_schema |
tool_choice string |
默认值:auto 可选值:auto、none 工具选择策略 |
request_id string |
可选值:6-64 characters 请求唯一标识符(模型原生参数) |
user_id string |
可选值:6-128 characters 终端用户唯一标识符(模型原生参数) |
do_sample boolean |
默认值:true 可选值:true、false 是否启用采样策略(模型原生参数) |
请求示例
curl -v 'https://www.sagelib.com/anthropic/messages' \
-H "x-api-key: ***" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "glm-5.3",
"max_tokens": 2048,
"messages": [
{
"role": "user",
"content": "Hello"
}
]
}'
import json
import requests
# Configure base variables
base_url = "https://www.sagelib.com" # 把 base_url 换成你的网关地址 https://www.sagelib.com
api_key = "***" # Replace with your 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 = {
"model": "glm-5.3",
"max_tokens": 2048,
"messages": [
{
"role": "user",
"content": "Hello"
}
]
}
# Send the POST request
response = requests.post(url, headers=headers, json=data)
# Print the status code and the returned 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 = """
{
"model": "glm-5.3",
"max_tokens": 2048,
"messages": [
{
"role": "user",
"content": "Hello"
}
]
}
""";
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 {
// Use ofInputStream to handle the streaming response
HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if ("[DONE]".equals(data)) {
break;
}
// Tip: you can bring in Gson or Jackson here to parse the JSON in data and extract delta.content
System.out.println("Received stream chunk: " + data);
}
}
}
} 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 := `
{
"model": "glm-5.3",
"max_tokens": 2048,
"messages": [
{
"role": "user",
"content": "Hello"
}
]
}
`
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)
// Pretty-print the returned JSON data
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/anthropic/messages' \
-H "x-api-key: ***" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "glm-5.3",
"max_tokens": 8192,
"thinking": {
"type": "enabled"
},
"reasoning_effort": "max",
"messages": [
{
"role": "user",
"content": "Analyze the pros and cons comparison of Paxos vs Raft consensus algorithms"
}
]
}'
import json
import requests
# Configure base variables
base_url = "https://www.sagelib.com" # 把 base_url 换成你的网关地址 https://www.sagelib.com
api_key = "***" # Replace with your 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 = {
"model": "glm-5.3",
"max_tokens": 8192,
"thinking": {
"type": "enabled"
},
"reasoning_effort": "max",
"messages": [
{
"role": "user",
"content": "Analyze the pros and cons comparison of Paxos vs Raft consensus algorithms"
}
]
}
# Send the POST request
response = requests.post(url, headers=headers, json=data)
# Print the status code and the returned 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 = """
{
"model": "glm-5.3",
"max_tokens": 8192,
"thinking": {
"type": "enabled"
},
"reasoning_effort": "max",
"messages": [
{
"role": "user",
"content": "Analyze the pros and cons comparison of Paxos vs Raft consensus algorithms"
}
]
}
""";
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 {
// Use ofInputStream to handle the streaming response
HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if ("[DONE]".equals(data)) {
break;
}
// Tip: you can bring in Gson or Jackson here to parse the JSON in data and extract delta.content
System.out.println("Received stream chunk: " + data);
}
}
}
} 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 := `
{
"model": "glm-5.3",
"max_tokens": 8192,
"thinking": {
"type": "enabled"
},
"reasoning_effort": "max",
"messages": [
{
"role": "user",
"content": "Analyze the pros and cons comparison of Paxos vs Raft consensus algorithms"
}
]
}
`
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)
// Pretty-print the returned JSON data
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/anthropic/messages' \
-H "x-api-key: ***" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "glm-5.3",
"max_tokens": 4096,
"tools": [
{
"name": "get_weather",
"description": "Query the weather of a specified city",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string"
}
},
"required": [
"city"
]
}
}
],
"messages": [
{
"role": "user",
"content": "How is the weather in Beijing today?"
}
]
}'
import json
import requests
# Configure base variables
base_url = "https://www.sagelib.com" # 把 base_url 换成你的网关地址 https://www.sagelib.com
api_key = "***" # Replace with your 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 = {
"model": "glm-5.3",
"max_tokens": 4096,
"tools": [
{
"name": "get_weather",
"description": "Query the weather of a specified city",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string"
}
},
"required": [
"city"
]
}
}
],
"messages": [
{
"role": "user",
"content": "How is the weather in Beijing today?"
}
]
}
# Send the POST request
response = requests.post(url, headers=headers, json=data)
# Print the status code and the returned 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 = """
{
"model": "glm-5.3",
"max_tokens": 4096,
"tools": [
{
"name": "get_weather",
"description": "Query the weather of a specified city",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string"
}
},
"required": [
"city"
]
}
}
],
"messages": [
{
"role": "user",
"content": "How is the weather in Beijing today?"
}
]
}
""";
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 {
// Use ofInputStream to handle the streaming response
HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if ("[DONE]".equals(data)) {
break;
}
// Tip: you can bring in Gson or Jackson here to parse the JSON in data and extract delta.content
System.out.println("Received stream chunk: " + data);
}
}
}
} 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 := `
{
"model": "glm-5.3",
"max_tokens": 4096,
"tools": [
{
"name": "get_weather",
"description": "Query the weather of a specified city",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string"
}
},
"required": [
"city"
]
}
}
],
"messages": [
{
"role": "user",
"content": "How is the weather in Beijing today?"
}
]
}
`
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)
// Pretty-print the returned JSON data
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 |
响应唯一标识,如 msg_${message_id} |
type string |
固定为 message |
role string |
固定为 assistant |
model string |
实际使用的模型名称 |
content array |
内容块数组 |
stop_reason string |
停止原因:end_turn / max_tokens / stop_sequence / tool_use |
stop_sequence string |
匹配的停止序列(stop_reason=stop_sequence 时) |
usage object |
|
input_tokens integer |
输入 token 数 |
output_tokens integer |
输出 token 数(含推理 token) |
POSThttps://www.sagelib.com/openai/chat/completions
glm-5.3 模型的聊天生成接口,OpenAI直连,支持多轮上下文、流式输出、工具调用、结构化输出等。
接口鉴权方式请参考:API接入与鉴权
请求参数
请求头
| 参数名称 | 描述 |
|---|---|
*Content-Type string |
application/json(JSON 端点)或 multipart/form-data; boundary=...(multipart 端点,保持原始 boundary) |
*Authorization string |
鉴权令牌,格式为 Bearer ${AIGATEWAY_TOKEN} |
Body 参数
| 参数名称 | 描述 |
|---|---|
*model string |
可选值:"glm-5.3" 模型 ID,固定为 glm-5.3 |
*messages array |
对话消息列表,支持 system/user/assistant/tool 四种角色 |
request_id string |
可选值:6-64 characters 请求唯一标识符,用于链路追踪 |
user_id string |
可选值:6-128 characters 终端用户唯一标识符 |
temperature float |
默认值:1 可选值:0.0 ~ 1.0 采样温度,值越大输出越随机,建议与 top_p 二选一 |
top_p float |
默认值:0.95 可选值:0.01 ~ 1.0 核采样参数 |
do_sample boolean |
默认值:true 可选值:true、false 是否启用采样策略,false 时用贪心策略 |
max_tokens integer |
默认值:65536 可选值:1 ~ 131072 模型输出最大 token 数 |
stream boolean |
默认值:false 可选值:true、false 是否以 SSE 流式返回 |
thinking object |
可选值:type: enabled only 思考配置,GLM-5.3 强制开启思考 |
reasoning_effort string |
默认值:max 可选值:low、high、max 推理程度,thinking 开启时生效 |
tool_stream boolean |
默认值:false 可选值:true、false 流式返回 Function Calls,仅 GLM-5.3 及以上系列支持 |
tools array |
工具定义数组,模型可据此发起工具调用,最多 128 个函数 |
tool_choice string |
默认值:auto 可选值:auto 工具选择策略,仅支持 auto |
stop array |
停止词列表,最多 4 项 |
response_format object |
可选值:text、json_object 结构化输出格式 |
请求示例
curl -v 'https://www.sagelib.com/openai/chat/completions' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"content": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries.",
"role": "user"
}
],
"model": "glm-5.3"
}'
import json
import requests
# Configure base variables
base_url = "https://www.sagelib.com" # 把 base_url 换成你的网关地址 https://www.sagelib.com
api_key = "***" # Replace with your Bearer token
url = f"https://www.sagelib.com/openai/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
data = {
"messages": [
{
"content": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries.",
"role": "user"
}
],
"model": "glm-5.3"
}
# Send the POST request
response = requests.post(url, headers=headers, json=data)
# Print the status code and the returned 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 = """
{
"messages": [
{
"content": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries.",
"role": "user"
}
],
"model": "glm-5.3"
}
""";
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 {
// Use ofInputStream to handle the streaming response
HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if ("[DONE]".equals(data)) {
break;
}
// Tip: you can bring in Gson or Jackson here to parse the JSON in data and extract delta.content
System.out.println("Received stream chunk: " + data);
}
}
}
} 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 := `
{
"messages": [
{
"content": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries.",
"role": "user"
}
],
"model": "glm-5.3"
}
`
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)
// Pretty-print the returned JSON data
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 '{
"messages": [
{
"content": "List the time complexity of 5 sorting algorithms; output as a JSON array with each item containing name, best, average, worst fields; ensure valid JSON output.",
"role": "user"
}
],
"model": "glm-5.3",
"response_format": {
"type": "json_object"
}
}'
import json
import requests
# Configure base variables
base_url = "https://www.sagelib.com" # 把 base_url 换成你的网关地址 https://www.sagelib.com
api_key = "***" # Replace with your Bearer token
url = f"https://www.sagelib.com/openai/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
data = {
"messages": [
{
"content": "List the time complexity of 5 sorting algorithms; output as a JSON array with each item containing name, best, average, worst fields; ensure valid JSON output.",
"role": "user"
}
],
"model": "glm-5.3",
"response_format": {
"type": "json_object"
}
}
# Send the POST request
response = requests.post(url, headers=headers, json=data)
# Print the status code and the returned 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 = """
{
"messages": [
{
"content": "List the time complexity of 5 sorting algorithms; output as a JSON array with each item containing name, best, average, worst fields; ensure valid JSON output.",
"role": "user"
}
],
"model": "glm-5.3",
"response_format": {
"type": "json_object"
}
}
""";
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 {
// Use ofInputStream to handle the streaming response
HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if ("[DONE]".equals(data)) {
break;
}
// Tip: you can bring in Gson or Jackson here to parse the JSON in data and extract delta.content
System.out.println("Received stream chunk: " + data);
}
}
}
} 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 := `
{
"messages": [
{
"content": "List the time complexity of 5 sorting algorithms; output as a JSON array with each item containing name, best, average, worst fields; ensure valid JSON output.",
"role": "user"
}
],
"model": "glm-5.3",
"response_format": {
"type": "json_object"
}
}
`
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)
// Pretty-print the returned JSON data
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 '{
"messages": [
{
"content": "Introduce the history of AI in detail, starting from the Turing test in the 1950s, covering expert systems, machine learning, deep learning, and large language models, including key figures, milestones, and technical breakthroughs; elaborate each stage as fully as possible.",
"role": "user"
}
],
"model": "glm-5.3",
"stream": true
}'
import json
import requests
# Configure base variables
base_url = "https://www.sagelib.com" # 把 base_url 换成你的网关地址 https://www.sagelib.com
api_key = "***" # Replace with your Bearer token
url = f"https://www.sagelib.com/openai/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
data = {
"messages": [
{
"content": "Introduce the history of AI in detail, starting from the Turing test in the 1950s, covering expert systems, machine learning, deep learning, and large language models, including key figures, milestones, and technical breakthroughs; elaborate each stage as fully as possible.",
"role": "user"
}
],
"model": "glm-5.3",
"stream": True
}
# Send the POST request
response = requests.post(url, headers=headers, json=data)
# Print the status code and the returned 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 = """
{
"messages": [
{
"content": "Introduce the history of AI in detail, starting from the Turing test in the 1950s, covering expert systems, machine learning, deep learning, and large language models, including key figures, milestones, and technical breakthroughs; elaborate each stage as fully as possible.",
"role": "user"
}
],
"model": "glm-5.3",
"stream": 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 {
// Use ofInputStream to handle the streaming response
HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if ("[DONE]".equals(data)) {
break;
}
// Tip: you can bring in Gson or Jackson here to parse the JSON in data and extract delta.content
System.out.println("Received stream chunk: " + data);
}
}
}
} 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 := `
{
"messages": [
{
"content": "Introduce the history of AI in detail, starting from the Turing test in the 1950s, covering expert systems, machine learning, deep learning, and large language models, including key figures, milestones, and technical breakthroughs; elaborate each stage as fully as possible.",
"role": "user"
}
],
"model": "glm-5.3",
"stream": 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)
// Pretty-print the returned JSON data
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 '{
"messages": [
{
"content": "How is the weather in Beijing today? Please use a tool to query.",
"role": "user"
}
],
"model": "glm-5.3",
"tool_choice": "auto",
"tools": [
{
"function": {
"description": "Get the current weather of a specified city",
"name": "get_weather",
"parameters": {
"properties": {
"city": {
"description": "City name",
"type": "string"
}
},
"required": [
"city"
],
"type": "object"
}
},
"type": "function"
}
]
}'
import json
import requests
# Configure base variables
base_url = "https://www.sagelib.com" # 把 base_url 换成你的网关地址 https://www.sagelib.com
api_key = "***" # Replace with your Bearer token
url = f"https://www.sagelib.com/openai/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
data = {
"messages": [
{
"content": "How is the weather in Beijing today? Please use a tool to query.",
"role": "user"
}
],
"model": "glm-5.3",
"tool_choice": "auto",
"tools": [
{
"function": {
"description": "Get the current weather of a specified city",
"name": "get_weather",
"parameters": {
"properties": {
"city": {
"description": "City name",
"type": "string"
}
},
"required": [
"city"
],
"type": "object"
}
},
"type": "function"
}
]
}
# Send the POST request
response = requests.post(url, headers=headers, json=data)
# Print the status code and the returned 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 = """
{
"messages": [
{
"content": "How is the weather in Beijing today? Please use a tool to query.",
"role": "user"
}
],
"model": "glm-5.3",
"tool_choice": "auto",
"tools": [
{
"function": {
"description": "Get the current weather of a specified city",
"name": "get_weather",
"parameters": {
"properties": {
"city": {
"description": "City name",
"type": "string"
}
},
"required": [
"city"
],
"type": "object"
}
},
"type": "function"
}
]
}
""";
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 {
// Use ofInputStream to handle the streaming response
HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if ("[DONE]".equals(data)) {
break;
}
// Tip: you can bring in Gson or Jackson here to parse the JSON in data and extract delta.content
System.out.println("Received stream chunk: " + data);
}
}
}
} 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 := `
{
"messages": [
{
"content": "How is the weather in Beijing today? Please use a tool to query.",
"role": "user"
}
],
"model": "glm-5.3",
"tool_choice": "auto",
"tools": [
{
"function": {
"description": "Get the current weather of a specified city",
"name": "get_weather",
"parameters": {
"properties": {
"city": {
"description": "City name",
"type": "string"
}
},
"required": [
"city"
],
"type": "object"
}
},
"type": "function"
}
]
}
`
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)
// Pretty-print the returned JSON data
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 |
响应唯一标识(as- 前缀) |
object string |
固定为 chat.completion |
model string |
模型名 glm-5.3 |
choices array |
|
usage object |
|
prompt_tokens integer |
输入 token 数 |
completion_tokens integer |
输出 token 数 |
completion_tokens_details object |
|
reasoning_tokens integer |
思考 token 数 |