-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Json response #12
Comments
I ran into the same problem recently. The link you provided uses the Gemini API v1beta version, which supports an But you can easily tell gemini to only answer with a JSON response. Unfortunately, Gemini embeds the JSON code in a markdown code block. You then have to extract the JSON from that. Example$result = Gemini::geminiPro()
->generateContent('Please generate a list of the worlds biggest car manufacturers in JSON format, with the name of the manufacturer as key and yearly sold vehicles in float as value. IMPORTANT: Please answer in JSON format only, without any explanation.');
preg_match('/```json(.*?)```/s', $result->text(), $matches);
if (empty($matches[1])) {
throw new \Exception('No valid JSON found in gemini response.');
}
// trim leading and trailng space
$json = trim($matches[1]);
echo $json; Result
|
Hi,
I am wondering how can I return a json response as per the document here:
https://ai.google.dev/gemini-api/docs/api-overview?_gl=1*1rnrc7o*_ga*ODAxNjQ3NDk0LjE3MTc1NTE4NjE.*_ga_P1DBVKWT6V*MTcxOTE4ODg3Ny4zMi4xLjE3MTkxODkwNTguNjAuMC44MTkzNTYwMjU.#json
The text was updated successfully, but these errors were encountered: