流式传输
POST
https://generativelanguage.googleapis.com/v1/models/gemini-pro:streamGenerateContentGemini API 提供了另一种从生成式 AI 模型接收响应的方式:以数据流的形式接收。流式响应在模型生成增量数据时,会将这些数据发送回您的应用。借助此功能,您可以快速响应用户请求以显示进度,并打造更具互动性的体验。
请求参数
Query 参数
key
string
可选
示例值:
{{API_KEY}}
Header 参数
Content-Type
string
可选
示例值:
application/json
Body 参数application/json
contents
array [object {2}]
必需
role
string
可选
必须是“user”或“model”。
parts
array [object {1}]
可选
示例
{
"contents": [
{
"role": "user",
"parts": [
{
"text": "广东省省会是哪里?"
}
]
}
]
}
示例代码
返回响应
成功(200)
HTTP 状态码: 200
内容格式: JSONapplication/json
数据结构
array of:
示例
[
{
"candidates": [
{
"content": {
"parts": [
{
"text": "广州市"
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0,
"safetyRatings": [
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"probability": "NEGLIGIBLE"
}
]
}
],
"promptFeedback": {
"safetyRatings": [
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"probability": "NEGLIGIBLE"
}
]
}
}
]
最后修改时间: 8 个月前