流式生成内容
curl --request POST \
--url https://geekai.co/api/v1beta/models/{model}:streamGenerateContent \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"contents": [
{
"role": "user",
"parts": [
{
"text": "你好,请介绍一下自己。"
}
]
}
]
}
'import requests
url = "https://geekai.co/api/v1beta/models/{model}:streamGenerateContent"
payload = { "contents": [
{
"role": "user",
"parts": [{ "text": "你好,请介绍一下自己。" }]
}
] }
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({contents: [{role: 'user', parts: [{text: '你好,请介绍一下自己。'}]}]})
};
fetch('https://geekai.co/api/v1beta/models/{model}:streamGenerateContent', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://geekai.co/api/v1beta/models/{model}:streamGenerateContent",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'contents' => [
[
'role' => 'user',
'parts' => [
[
'text' => '你好,请介绍一下自己。'
]
]
]
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://geekai.co/api/v1beta/models/{model}:streamGenerateContent"
payload := strings.NewReader("{\n \"contents\": [\n {\n \"role\": \"user\",\n \"parts\": [\n {\n \"text\": \"你好,请介绍一下自己。\"\n }\n ]\n }\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://geekai.co/api/v1beta/models/{model}:streamGenerateContent")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"contents\": [\n {\n \"role\": \"user\",\n \"parts\": [\n {\n \"text\": \"你好,请介绍一下自己。\"\n }\n ]\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://geekai.co/api/v1beta/models/{model}:streamGenerateContent")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"contents\": [\n {\n \"role\": \"user\",\n \"parts\": [\n {\n \"text\": \"你好,请介绍一下自己。\"\n }\n ]\n }\n ]\n}"
response = http.request(request)
puts response.read_bodydata: {"candidates":[{"content":{"role":"model","parts":[{"text":"你好!"}]},"index":0}]}
data: {"candidates":[{"content":{"role":"model","parts":[{"text":"我是 Gemini。"}]},"finishReason":"STOP","index":0}]}
{
"error": {
"code": 123,
"message": "<string>",
"status": "<string>",
"details": [
{}
]
}
}Text Models
Stream generated content
POST
/
models
/
{model}
:streamGenerateContent
流式生成内容
curl --request POST \
--url https://geekai.co/api/v1beta/models/{model}:streamGenerateContent \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"contents": [
{
"role": "user",
"parts": [
{
"text": "你好,请介绍一下自己。"
}
]
}
]
}
'import requests
url = "https://geekai.co/api/v1beta/models/{model}:streamGenerateContent"
payload = { "contents": [
{
"role": "user",
"parts": [{ "text": "你好,请介绍一下自己。" }]
}
] }
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({contents: [{role: 'user', parts: [{text: '你好,请介绍一下自己。'}]}]})
};
fetch('https://geekai.co/api/v1beta/models/{model}:streamGenerateContent', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://geekai.co/api/v1beta/models/{model}:streamGenerateContent",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'contents' => [
[
'role' => 'user',
'parts' => [
[
'text' => '你好,请介绍一下自己。'
]
]
]
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://geekai.co/api/v1beta/models/{model}:streamGenerateContent"
payload := strings.NewReader("{\n \"contents\": [\n {\n \"role\": \"user\",\n \"parts\": [\n {\n \"text\": \"你好,请介绍一下自己。\"\n }\n ]\n }\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://geekai.co/api/v1beta/models/{model}:streamGenerateContent")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"contents\": [\n {\n \"role\": \"user\",\n \"parts\": [\n {\n \"text\": \"你好,请介绍一下自己。\"\n }\n ]\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://geekai.co/api/v1beta/models/{model}:streamGenerateContent")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"contents\": [\n {\n \"role\": \"user\",\n \"parts\": [\n {\n \"text\": \"你好,请介绍一下自己。\"\n }\n ]\n }\n ]\n}"
response = http.request(request)
puts response.read_bodydata: {"candidates":[{"content":{"role":"model","parts":[{"text":"你好!"}]},"index":0}]}
data: {"candidates":[{"content":{"role":"model","parts":[{"text":"我是 Gemini。"}]},"finishReason":"STOP","index":0}]}
{
"error": {
"code": 123,
"message": "<string>",
"status": "<string>",
"details": [
{}
]
}
}This API uses the same request format as Generate content and returns the model’s generated results incrementally. Set
alt=sse to receive the response via Server-Sent Events; each data event contains a JSON response chunk.
Provide your GeekAI API KEY in the x-goog-api-key header and replace {model} with the Gemini model ID. Available features depend on the model and proxy route; for the detailed format, see the official Gemini streaming API documentation.
curl --no-buffer 'https://geekai.co/api/v1beta/models/gemini-3-flash-preview:streamGenerateContent?alt=sse' \
-H "x-goog-api-key: $GEEKAI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{"contents":[{"parts":[{"text":"Hello, please introduce yourself."}]}]}'
Body
application/json
Response
流式生成结果;使用 alt=sse 时,data 事件包含 JSON 格式的响应片段。
SSE 事件流,事件之间以空行分隔。
Was this page helpful?
