gbaptista / gemini-ai

A Ruby Gem for interacting with Gemini through Vertex AI, Generative Language API, or AI Studio, Google's generative AI services.
https://rubygems.org/gems/gemini-ai
MIT License
103 stars 21 forks source link

support/docs for json mode #18

Open jtoy opened 5 months ago

jtoy commented 5 months ago

does the gem support json mode as outlined here: https://ai.google.dev/gemini-api/docs/api-overview#curl_4

weilandia commented 5 months ago

The docs here work, but the confusing part is that json mode only works when using Vertex.

https://ai.google.dev/gemini-api/docs/api-overview#json

i.e.

client.generate_content(
  contents: messages,
  system_instructions: options[:system_message],
  generation_config: { response_mime_type: "application/json" }
)
gbaptista commented 5 months ago

@weilandia Thanks for testing and sharing! Yeah, I couldn't find examples/documentation to make it work outside of Vertex.

@jtoy Thanks for opening the issue! I added examples and documentation in the README:

weilandia commented 5 months ago

Heads up: Using JSON schema in the generation config only works with Pro.

"While Gemini 1.5 Flash models only accept a text description of the JSON schema you want returned, the Gemini 1.5 Pro models let you pass a schema object (or a Python type equivalent), and the model output will strictly follow that schema. This is also known as controlled generation or constrained decoding."

gbaptista commented 5 months ago

@weilandia Here's my test results:

Model Vertex AI Generative Language
gemini-pro-vision 🔒
gemini-pro 🟡
gemini-1.5-pro-preview-0514 🔒
gemini-1.5-pro-preview-0409 🔒
gemini-1.5-pro
gemini-1.5-flash-preview-0514 🟡 🔒
gemini-1.5-flash 🟡
gemini-1.0-pro-vision-latest 🔒 🔒
gemini-1.0-pro-vision-001 🔒
gemini-1.0-pro-vision 🔒
gemini-1.0-pro-latest 🔒
gemini-1.0-pro-002 🟡 🔒
gemini-1.0-pro-001
gemini-1.0-pro 🟡
gemini-ultra 🔒 🔒
gemini-1.0-ultra 🔒 🔒
gemini-1.0-ultra-001 🔒 🔒
asavageiv commented 4 months ago

It works with generative language on gemini-1.5-pro on the v1beta endpoint right now with JSON schema.

heidar commented 2 months ago

It works with generative language on gemini-1.5-pro on the v1beta endpoint right now with JSON schema.

I get a HTTP 400 response when doing this through the gem. The same request via curl seems to work fine though, and returns only JSON.

Either way, seems like Vertex is not needed for JSON responses anymore, but seems there is an issue in this gem stopping it from working?

heidar commented 2 months ago

It works for me when I set version: "v1beta" under credentials. Seems the default v1 does not support JSON responses.