Gemini gốc
Tạo nội dung dạng Stream
Truyền phát nội dung được tạo bằng định dạng Google Gemini API
Phiên bản SSE của Gemini generateContent. Sự kiện có thể chỉ chứa metadata hoặc nội dung trung gian chưa có finishReason. Luồng kết thúc tại EOF và không thêm dấu [DONE].
Tham số đường dẫn
Tên model (ví dụ: gemini-2.5-pro, gemini-3.5-flash).
Tham số truy vấn
API key (phương thức thay thế cho xác thực qua header).
Nội dung yêu cầu
Tương tự như Tạo nội dung.
Khả năng hỗ trợ generationConfig.candidateCount, công cụ, loại media và các trường tùy chọn khác phụ thuộc vào model đã chọn.
Phản hồi (Response)
Trả về một luồng (stream) các đối tượng JSON, mỗi đối tượng chứa một phần của phản hồi.
Yêu cầu
curl -N -X POST "https://api.tokenlab.sh/v1beta/models/gemini-2.5-pro:streamGenerateContent?key=sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"contents": [
{
"parts": [{"text": "Tell me a story"}]
}
]
}'import google.generativeai as genai
genai.configure(
api_key="sk-your-api-key",
transport="rest",
client_options={"api_endpoint": "api.tokenlab.sh"}
)
model = genai.GenerativeModel("gemini-2.5-pro")
response = model.generate_content("Tell me a story", stream=True)
for chunk in response:
for candidate in chunk.candidates:
for part in candidate.content.parts:
if part.text:
print(part.text, end="")const response = await fetch(
'https://api.tokenlab.sh/v1beta/models/gemini-2.5-pro:streamGenerateContent?key=sk-your-api-key',
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
contents: [{ parts: [{ text: 'Tell me a story' }] }]
})
}
);
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
console.log(decoder.decode(value, { stream: true }));
}package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func main() {
payload := map[string]interface{}{
"contents": []map[string]interface{}{
{"parts": []map[string]string{{"text": "Tell me a story"}}},
},
}
jsonData, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST",
"https://api.tokenlab.sh/v1beta/models/gemini-2.5-pro:streamGenerateContent?key=sk-your-api-key",
bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}<?php
$payload = [
'contents' => [
['parts' => [['text' => 'Tell me a story']]]
]
];
$ch = curl_init('https://api.tokenlab.sh/v1beta/models/gemini-2.5-pro:streamGenerateContent?key=sk-your-api-key');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_WRITEFUNCTION => function($ch, $data) {
echo $data;
return strlen($data);
}
]);
curl_exec($ch);
curl_close($ch);Ví dụ đầu vào thị giác
Các yêu cầu vision streaming sử dụng cùng cấu trúc contents[].parts[] như endpoint không streaming.
{
"contents": [
{
"role": "user",
"parts": [
{ "text": "Please describe this image." },
{
"inline_data": {
"mime_type": "image/jpeg",
"data": "/9j/4AAQSkZJRgABAQ..."
}
}
]
}
]
}Phản hồi
{
"candidates": [
{
"content": {
"role": "model",
"parts": [
{"text": "Once upon a time"}
]
}
}
]
}Xác thực
BearerAuth Xác thực bằng Khóa API. Tạo hoặc quản lý khóa API trong Dashboard > API > API Keys.
Vị trí: header
Tham số đường dẫn
Tên model (ví dụ: gemini-2.5-pro)
Tham số truy vấn
API key (thay thế cho xác thực bằng header)
Header
Chính sách phân phối theo yêu cầu. Ghi đè các mặc định của API key và Workspace. Tự động thử TokenLab Verified trước và có thể chuyển đổi một lần sang Official chỉ trước khi xuất, chấp nhận yêu cầu hoặc tạo tài nguyên cố định.
Giá trị hợp lệ
- "auto"
- "verified"
- "official"
Nội dung yêu cầu
application/json
Yêu cầu GenerateContent Gemini gốc. Cả tên ProtoJSON lowerCamelCase và tên proto snake_case gốc đều được chấp nhận và bảo toàn, bao gồm cả các yêu cầu hỗn hợp. Nếu cả hai cách viết của một trường đều có mặt, TokenLab không hợp nhất chúng hoặc chọn ưu tiên cục bộ. Các trường không xác định được chuyển tiếp trên cơ sở nỗ lực tốt nhất.
Phản hồi
text/event-stream
application/json
application/json
application/json
application/json
application/json