🤖
Gemini REST APIでのJSON Modeの利用(1.5 Flash編)
-
generationConfig
でresponse_mime_type
にapplication/json
を指定する - Gemini 1.5 Proでは
generationConfig
でさらにresponseSchema
を指定して厳密にスキーマを定義することが可能でしたが、Gemini 1.5 Flashではプロンプト内でスキーマを指定する必要があります
プロンプトの例
Translate to Japanese.
A string in `<paragraph>` tag to `</paragraph>` tag is one paragraph.
If a paragraph of input is translated and a paragraph consists of multiple sentences, output an array consisting of multiple String.
Using this JSON schema:
Paragraph = {\"line\": number, \"text\": list[string]}
Return a `list[Paragraph]`
request
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"systemInstruction": {
"parts": [
{
"text": "Translate to Japanese.\n\nA string in `<paragraph>` tag to `</paragraph>` tag is one paragraph.\n\nIf a paragraph of input is translated and a paragraph consists of multiple sentences, output an array consisting of multiple String.\n\nUsing this JSON schema:\n\n Paragraph = {\"line\": number, \"text\": list[string]}\n\nReturn a `list[Paragraph]`\n"
}
]
},
"contents": [
{
"parts": [
{
"text": "<paragraph>Cat</paragraph>"
},
{
"text": "<paragraph>Dog</paragraph>"
}
]
}
],
"generationConfig": {
"response_mime_type": "application/json",
}
}'
レスポンスの candidates
-> content
-> parts
-> text
に JSONで返ってくるので更にパースして上げる必要があります
response
{
"candidates": [
{
"content": {
"parts": [
{
"text": "[{\"line\": 1, \"text\": [\"猫\"]}, {\"line\": 2, \"text\": [\"犬\"]}]\n"
}
],
"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"
}
]
}
],
"usageMetadata": {
"promptTokenCount": 128,
"candidatesTokenCount": 60,
"totalTokenCount": 188
}
}
Discussion