gemini-3.5-flash-lite

最后更新:2026-11-14

POSThttps://www.sagelib.com/gemini/v1beta/models/{model}:streamGenerateContent

gemini-3.5-flash-lite 模型的聊天生成接口,Gemini直连,支持多轮上下文、流式输出、工具调用、结构化输出等。

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

请求参数

请求头

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

Body 参数

参数名称 描述
*contents array 对话内容,含 role 和 parts 的 Content 列表。端点为单轮,多轮需拼接历史
system_instruction string 系统指令,当前仅支持文本
safety_settings array 安全设置,每 SafetyCategory 可覆盖默认阈值。子字段: category/threshold
tools array 工具/函数声明列表,使模型可与外部系统交互执行操作
tool_config object 工具配置。子字段: function_calling_config(mode/allowlist), retrieval_config
generationConfig.maxOutputTokens integer 可选值:>= 1
最大输出 token 数,上限 65536
generationConfig.stopSequences array 停止序列列表
generationConfig.seed integer 随机种子,确定性输出
generationConfig.responseMimeType string 输出 MIME 类型,如 application/json
generationConfig.responseSchema object 结构化输出的 JSON Schema,配合 response_mime_type=application/json
generationConfig.responseJsonSchema object 响应 JSON Schema(别名),与 response_schema 二选一
generationConfig.responseLogprobs boolean 是否返回输出 token 对数概率
generationConfig.logprobs integer 可选值:>= 0
返回最可能 N 个 token,需 response_logprobs=true
generationConfig.presencePenalty float 可选值:-2 ~ 2
存在惩罚 -2~2
generationConfig.frequencyPenalty float 可选值:-2 ~ 2
频率惩罚 -2~2
generationConfig.thinkingConfig object 思考配置。子字段: thinking_level(minimal/low/medium/high), include_thoughts(bool)
generationConfig.responseModalities array 可选值:TEXT
响应模态列表,仅支持 TEXT
generationConfig.cachedContent string 缓存内容引用,格式 cachedContents/${id}
generationConfig.labels object 请求标签(key-value),计费/监控/权限
generationConfig.automaticFunctionCalling object 自动函数调用。子字段: disable(bool), maximum_remote_call_count(int)
generationConfig.enableEnhancedCivicAnswers boolean 增强公民问题回答
generationConfig.serviceTier string 服务等级
generationConfig.modelArmorConfig object 模型安全防护配置
generationConfig.mediaResolution object 媒体分辨率控制
generationConfig.routingConfig object 路由配置。子字段: model_routing_preference
generationConfig.modelSelectionConfig object 模型变体自动选择策略

请求示例

curl -v 'https://www.sagelib.com/gemini/v1beta/models/gemini-3.5-flash-lite:streamGenerateContent' \
  -H "x-goog-api-key: ***" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "parts": [
          {
            "text": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries."
          }
        ],
        "role": "user"
      }
    ],
    "labels": {
      "env": "test",
      "team": "ai"
    }
  }'
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/gemini/v1beta/models/gemini-3.5-flash-lite:streamGenerateContent"

headers = {
    "x-goog-api-key": api_key,
    "Content-Type": "application/json",
}

data = {
    "contents": [
        {
            "parts": [
                {
                    "text": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries."
                }
            ],
            "role": "user"
        }
    ],
    "labels": {
        "env": "test",
        "team": "ai"
    }
}

# 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/gemini/v1beta/models/gemini-3.5-flash-lite:streamGenerateContent";

        String jsonRequestBody = """
            {
                "contents": [
                    {
                        "parts": [
                            {
                                "text": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries."
                            }
                        ],
                        "role": "user"
                    }
                ],
                "labels": {
                    "env": "test",
                    "team": "ai"
                }
            }
            """;

        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 {
            // 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/gemini/v1beta/models/gemini-3.5-flash-lite:streamGenerateContent"

    jsonRequestBody := `
            {
                "contents": [
                    {
                        "parts": [
                            {
                                "text": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries."
                            }
                        ],
                        "role": "user"
                    }
                ],
                "labels": {
                    "env": "test",
                    "team": "ai"
                }
            }
`

    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)
    // 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/gemini/v1beta/models/gemini-3.5-flash-lite:streamGenerateContent' \
  -H "x-goog-api-key: ***" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "parts": [
          {
            "text": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries."
          }
        ],
        "role": "user"
      }
    ],
    "generationConfig": {
      "maxOutputTokens": 1000
    }
  }'
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/gemini/v1beta/models/gemini-3.5-flash-lite:streamGenerateContent"

headers = {
    "x-goog-api-key": api_key,
    "Content-Type": "application/json",
}

data = {
    "contents": [
        {
            "parts": [
                {
                    "text": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries."
                }
            ],
            "role": "user"
        }
    ],
    "generationConfig": {
        "maxOutputTokens": 1000
    }
}

# 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/gemini/v1beta/models/gemini-3.5-flash-lite:streamGenerateContent";

        String jsonRequestBody = """
            {
                "contents": [
                    {
                        "parts": [
                            {
                                "text": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries."
                            }
                        ],
                        "role": "user"
                    }
                ],
                "generationConfig": {
                    "maxOutputTokens": 1000
                }
            }
            """;

        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 {
            // 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/gemini/v1beta/models/gemini-3.5-flash-lite:streamGenerateContent"

    jsonRequestBody := `
            {
                "contents": [
                    {
                        "parts": [
                            {
                                "text": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries."
                            }
                        ],
                        "role": "user"
                    }
                ],
                "generationConfig": {
                    "maxOutputTokens": 1000
                }
            }
`

    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)
    // 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/gemini/v1beta/models/gemini-3.5-flash-lite:streamGenerateContent' \
  -H "x-goog-api-key: ***" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "parts": [
          {
            "text": "Return a JSON object"
          }
        ]
      }
    ],
    "generationConfig": {
      "responseMimeType": "application/json"
    }
  }'
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/gemini/v1beta/models/gemini-3.5-flash-lite:streamGenerateContent"

headers = {
    "x-goog-api-key": api_key,
    "Content-Type": "application/json",
}

data = {
    "contents": [
        {
            "parts": [
                {
                    "text": "Return a JSON object"
                }
            ]
        }
    ],
    "generationConfig": {
        "responseMimeType": "application/json"
    }
}

# 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/gemini/v1beta/models/gemini-3.5-flash-lite:streamGenerateContent";

        String jsonRequestBody = """
            {
                "contents": [
                    {
                        "parts": [
                            {
                                "text": "Return a JSON object"
                            }
                        ]
                    }
                ],
                "generationConfig": {
                    "responseMimeType": "application/json"
                }
            }
            """;

        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 {
            // 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/gemini/v1beta/models/gemini-3.5-flash-lite:streamGenerateContent"

    jsonRequestBody := `
            {
                "contents": [
                    {
                        "parts": [
                            {
                                "text": "Return a JSON object"
                            }
                        ]
                    }
                ],
                "generationConfig": {
                    "responseMimeType": "application/json"
                }
            }
`

    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)
    // 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 参数

参数名称 描述
candidates array 候选回复列表
createTime string 创建时间(ISO 8601)
modelVersion string 模型版本标识
responseId string 请求唯一标识
usageMetadata object
    promptTokenCount integer 输入 token 数
    candidatesTokenCount integer 输出 token 数
    totalTokenCount integer 总 token 数
    trafficType string 流量类型(如 ON_DEMAND)