AI 服务/大语言模型
Gemini 流式对话
接口说明:Gemini :streamGenerateContent API 的兼容端点(流式输出,SSE)。请求与响应格式遵循 Gemini 规范;鉴权使用 x-goog-api-key 头(Gemini SDK 默认携带),已有 Gemini SDK 只需替换 baseURL 与 API Key 即可接入,按 AiToEarn 积分计费。
调用本接口前,请先请求「对话模型列表」接口(GET /api/ai/models/chat),获取当前可用的模型列表。
POST
/
api
/
ai
/
v1beta
/
models
/
{model}
:streamGenerateContent
Gemini 流式对话
curl --request POST \
--url https://aitoearn.cn/api/ai/v1beta/models/{model}:streamGenerateContent \
--header 'Content-Type: application/json' \
--header 'x-goog-api-key: <api-key>' \
--data '
{
"model": "<string>",
"contents": [
{
"parts": [
{
"text": "<string>"
}
]
}
]
}
'import requests
url = "https://aitoearn.cn/api/ai/v1beta/models/{model}:streamGenerateContent"
payload = {
"model": "<string>",
"contents": [{ "parts": [{ "text": "<string>" }] }]
}
headers = {
"x-goog-api-key": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'x-goog-api-key': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({model: '<string>', contents: [{parts: [{text: '<string>'}]}]})
};
fetch('https://aitoearn.cn/api/ai/v1beta/models/{model}:streamGenerateContent', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://aitoearn.cn/api/ai/v1beta/models/{model}:streamGenerateContent"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"contents\": [\n {\n \"parts\": [\n {\n \"text\": \"<string>\"\n }\n ]\n }\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-goog-api-key", "<api-key>")
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))
}{}授权
Gemini SDK 兼容接口使用的 API Key header。
请求体
application/json
生成配置
Hide child attributes
Hide child attributes
控制输出随机性,值越高越发散,值越低越稳定
核采样参数,限制抽样时考虑的 token 最大累积概率。
Top-K 采样参数,限制抽样时考虑的最高概率 token 数量
需要返回的候选回答数量
候选回答的最大输出 token 数
是否返回 token 对数概率信息
每个解码步骤返回的高概率候选 token 数,仅在 responseLogprobs=true 时生效
存在惩罚,用于降低或提高已出现 token 的再次出现倾向
频次惩罚,根据 token 已出现次数抑制或鼓励重复
解码随机种子,用于提高输出可复现性
期望的响应类型
JSON 输出结构约束,通常配合 responseMimeType=application/json 使用
响应
第三方协议原生响应或 SSE 数据。不使用 AiToEarn 通用响应包裹。
第三方协议原生响应,不使用 AiToEarn 通用响应包裹。
⌘I
Gemini 流式对话
curl --request POST \
--url https://aitoearn.cn/api/ai/v1beta/models/{model}:streamGenerateContent \
--header 'Content-Type: application/json' \
--header 'x-goog-api-key: <api-key>' \
--data '
{
"model": "<string>",
"contents": [
{
"parts": [
{
"text": "<string>"
}
]
}
]
}
'import requests
url = "https://aitoearn.cn/api/ai/v1beta/models/{model}:streamGenerateContent"
payload = {
"model": "<string>",
"contents": [{ "parts": [{ "text": "<string>" }] }]
}
headers = {
"x-goog-api-key": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'x-goog-api-key': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({model: '<string>', contents: [{parts: [{text: '<string>'}]}]})
};
fetch('https://aitoearn.cn/api/ai/v1beta/models/{model}:streamGenerateContent', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://aitoearn.cn/api/ai/v1beta/models/{model}:streamGenerateContent"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"contents\": [\n {\n \"parts\": [\n {\n \"text\": \"<string>\"\n }\n ]\n }\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-goog-api-key", "<api-key>")
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))
}{}
