Gemini原生格式 API 文档
Json格式化输出
基础信息
- Base URL:
https://www.dmxapi.cn
- API 版本:
v1beta
- 认证方式: API Key
接口说明
通过 POST 请求调用 Gemini 模型生成内容
请求地址
POST /v1beta/models/{model}:generateContent
请求参数
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
key | string | 是 | API密钥 (替换为******) |
model | string | 是 | 模型名称,如 gemini-2.5-flash |
请求头
Content-Type: application/json
请求体示例
python
import requests
import json
# 请求配置
model = "gemini-2.5-flash"
url = f"https://www.dmxapi.cn/v1beta/models/{model}:generateContent?key=******"
payload = {
"contents": [
{
"role": "user",
"parts": [{"text": "请列出几种流行的中国早餐,并包含配料的用量。"}]
}
],
"generationConfig": {
"responseMimeType": "application/json",
"responseSchema": {
"type": "ARRAY",
"items": {
"type": "OBJECT",
"properties": {
"recipeName": {"type": "STRING"},
"ingredients": {
"type": "ARRAY",
"items": {"type": "STRING"}
}
},
"propertyOrdering": ["recipeName", "ingredients"]
}
}
}
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, headers=headers, data=json.dumps(payload))
print(response.text)
响应示例
成功响应将返回符合指定schema的JSON格式数据
错误代码
状态码 | 说明 |
---|---|
400 | 请求参数错误 |
401 | 认证失败 |
500 | 服务器内部错误 |
注意事项
- 请妥善保管API密钥
- 请求体中的
responseSchema
用于指定输出格式 - 建议在请求头中始终包含
Content-Type: application/json