client icon indicating copy to clipboard operation
client copied to clipboard

Add `response_mime_type` support to `generationConfig`

Open DrAliRagab opened this issue 1 year ago • 6 comments

According to google docs here https://ai.google.dev/docs/gemini_api_overview#json We can instruct Gemini to return json by adding "response_mime_type": "application/json" to generationConfig

I hope this will be added to generationConfig

DrAliRagab avatar Apr 09 '24 22:04 DrAliRagab

+1

Sinitra avatar May 10 '24 11:05 Sinitra

+1 ... and the corresponding responseSchema please example:

"responseMimeType" => "application/json", "responseSchema" => [ "type" => "ARRAY", "items" => [ "type" => "OBJECT", "properties" => [ "product" => [ "type" => "STRING" ] ] ] ]

Martin-Matchory avatar Jun 05 '24 12:06 Martin-Matchory

PR here, I will test this with a project shortly and update it as needed. Feel free to test for your purposes @Martin-Matchory @Sinitra @DrAliRagab and let me know if it works.

benbowler avatar Aug 05 '24 12:08 benbowler

I noticed this library has an open tested PR for mimetype support so I'll close this one.

benbowler avatar Aug 05 '24 14:08 benbowler

+1 ... and the corresponding responseSchema please example:

"responseMimeType" => "application/json", "responseSchema" => [ "type" => "ARRAY", "items" => [ "type" => "OBJECT", "properties" => [ "product" => [ "type" => "STRING" ] ] ] ]

Yes, both responseMimeType and responseSchema should be added.

This is a working example using laravel http:

$response = Http::baseUrl($this->baseUrl)
->withHeaders([
    'x-goog-api-key' => $this->apiKey,
])
->asJson()
->acceptJson()
->post("v1beta/models/{$this->model}:generateContent", [
    'contents'         => [
        'parts' => [
            [
                'text' => $instructions,
            ],
        ]
    ],
    'generationConfig' => [
        'responseMimeType' => 'application/json',
        'responseSchema'   => [
            'type'       => 'object',
            'properties' => [
                'score' => [
                    'type'    => 'number',
                    'minimum' => 0,
                    'maximum' => 1,
                    'format'  => 'float',
                ],
            ],
            'required'   => ['score'],
        ],
    ],
]);

DrAliRagab avatar Aug 06 '24 20:08 DrAliRagab

#65

evandromurilo avatar Feb 08 '25 18:02 evandromurilo