Skip to content

Commit

Permalink
Use the same example in both halves.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkDaoust committed May 28, 2024
1 parent 97909e0 commit 84323cf
Showing 1 changed file with 84 additions and 97 deletions.
181 changes: 84 additions & 97 deletions quickstarts/JSON_mode.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "Tce3stUlHN0L"
},
"source": [
"##### Copyright 2024 Google LLC."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"cellView": "form",
"id": "tuOe1ymfHZPu"
},
"outputs": [],
"source": [
"# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# https://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
]
},
{
"cell_type": "markdown",
"metadata": {
Expand All @@ -23,12 +54,12 @@
"source": [
"The Gemini API can be used to generate a JSON output if you set the schema that you would like to use.\n",
"\n",
"**Note**: Use Gemini 1.5 Pro when generating JSON. JSON schemas are only supported by Gemini 1.5 Pro right now."
"**Note**: JSON schemas are only supported by Gemini 1.5 Pro right now."
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {
"id": "qLuL9m7KhvxR"
},
Expand All @@ -46,7 +77,6 @@
"outputs": [],
"source": [
"import google.generativeai as genai\n",
"from google.generativeai.types import content_types\n",
"\n",
"import json\n",
"import dataclasses\n",
Expand All @@ -66,7 +96,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {
"id": "d6lYXRcjthKV"
},
Expand All @@ -86,103 +116,96 @@
"source": [
"## Activate JSON Mode\n",
"\n",
" Activate JSON mode by specifying `respose_mime_type` in the `generation_config` parameter. You'll use Gemini 1.5 Pro for this example, but note that you can use Gemini 1.5 Flash while describing your schema in the prompt."
" Activate JSON mode by specifying `respose_mime_type` in the `generation_config` parameter:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 3,
"metadata": {
"id": "i5Rod-lXRIhf"
},
"outputs": [],
"source": [
"model = genai.GenerativeModel(\"gemini-1.5-pro-latest\",\n",
"model = genai.GenerativeModel(\"gemini-1.5-flash-latest\",\n",
" generation_config={\"response_mime_type\": \"application/json\"})"
]
},
{
"cell_type": "code",
"execution_count": 12,
"cell_type": "markdown",
"metadata": {
"id": "JiIxKaLl4R0f"
"id": "4071a6143d31"
},
"outputs": [],
"source": [
"class Recipe(typing.TypedDict):\n",
" recipe_name: str"
"For this first example just describe the schema you want back:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 4,
"metadata": {
"id": "VV_bRJb15uqv"
"id": "K8ezjNb0RJ6Y"
},
"outputs": [
{
"data": {
"text/plain": [
"{'items': {'properties': {'recipe_name': {'type': 'string'}},\n",
" 'type': 'object'},\n",
" 'type': 'array'}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"response_schema = content_types._schema_for_class(list[Recipe])\n",
"response_schema"
"prompt = \"\"\"List a few popular cookie recipes using this JSON schema:\n",
"\n",
"Recipe = {'recipe_name': str}\n",
"Return: list[Recipe]\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 5,
"metadata": {
"id": "K8ezjNb0RJ6Y"
"id": "ggudoxK8RMlb"
},
"outputs": [],
"source": [
"prompt = \"\"\"List a few popular cookie recipes using this JSON schema, be sure to return an array:\n",
"{'type': 'object', 'properties': { 'recipe_name': {'type': 'string'}}}\"\"\""
"raw_response = model.generate_content(prompt)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9TqoNg3VSMYB"
},
"source": [
"Parse the string to JSON:"
]
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 6,
"metadata": {
"id": "ggudoxK8RMlb"
"id": "b99ee66972f5"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{\"recipe_name\": \"Chocolate Chip Cookies\"}, {\"recipe_name\": \"Peanut Butter Cookies\"}, {\"recipe_name\": \"Oatmeal Raisin Cookies\"}, {\"recipe_name\": \"Sugar Cookies\"}, {\"recipe_name\": \"Snickerdoodles\"}]\n",
"\n"
"[{'recipe_name': 'Chocolate Chip Cookies'}, {'recipe_name': 'Oatmeal Raisin Cookies'}, {'recipe_name': 'Snickerdoodles'}, {'recipe_name': 'Sugar Cookies'}, {'recipe_name': 'Peanut Butter Cookies'}]\n"
]
}
],
"source": [
"response = model.generate_content(prompt)\n",
"print(response.text)"
"response = json.loads(raw_response.text)\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9TqoNg3VSMYB"
"id": "1092c669169a"
},
"source": [
"Just for fun, parse the string to JSON, and then serialize it."
"For readability searialize and print it:"
]
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 7,
"metadata": {
"id": "WLDPREpmSMu5"
},
Expand All @@ -196,23 +219,23 @@
" \"recipe_name\": \"Chocolate Chip Cookies\"\n",
" },\n",
" {\n",
" \"recipe_name\": \"Peanut Butter Cookies\"\n",
" \"recipe_name\": \"Oatmeal Raisin Cookies\"\n",
" },\n",
" {\n",
" \"recipe_name\": \"Oatmeal Raisin Cookies\"\n",
" \"recipe_name\": \"Snickerdoodles\"\n",
" },\n",
" {\n",
" \"recipe_name\": \"Sugar Cookies\"\n",
" },\n",
" {\n",
" \"recipe_name\": \"Snickerdoodles\"\n",
" \"recipe_name\": \"Peanut Butter Cookies\"\n",
" }\n",
"]\n"
]
}
],
"source": [
"print(json.dumps(json.loads(response.text), indent=4))"
"print(json.dumps(response, indent=4))"
]
},
{
Expand All @@ -223,57 +246,21 @@
"source": [
"## Generate JSON from schema\n",
"\n",
"You can take a Python class, for instance, and use it as our schema for generating JSON. When passing in the `response_schema` parameter, use the Gemini 1.5 Pro model. Gemini 1.5 Flash does not support this."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "vP6teXff_H_L"
},
"outputs": [],
"source": [
"class Person(typing.TypedDict):\n",
" family_name: str\n",
" favorite_food: str"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "p0gxz0NNr8si"
},
"outputs": [],
"source": [
"prompt = \"Hello, describe some people, all fields are required.\""
"While `gemini-1.5-flash` models only accept a text description of the JSON you want back, `gemini-1.5-pro` models support \"controlled generation\" (aka \"constrained decoding\"). This allows you to pass a schema object (or a python type equivalent) and the output will strictly follow that schema.\n",
"\n",
"Following the same example as the previous section, here's that recipe type:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "SuZQTQZ8438K"
"id": "JiIxKaLl4R0f"
},
"outputs": [
{
"data": {
"text/plain": [
"{'items': {'properties': {'family_name': {'type': 'string'},\n",
" 'favorite_food': {'type': 'string'}},\n",
" 'type': 'object'},\n",
" 'type': 'array'}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"response_schema = content_types._schema_for_class(list[Person])\n",
"response_schema"
"class Recipe(typing.TypedDict):\n",
" recipe_name: str"
]
},
{
Expand All @@ -282,7 +269,7 @@
"id": "vBlWzt6M-2oM"
},
"source": [
"Using `generate_content`, we pass in the Python class `Person` defined above into the `generation_config`'s `response_schema` field."
"For this exaple you want a list of `Recipe` objects, so pass `list[Recipe]` to the `response_schema` field of the `generation_config`."
]
},
{
Expand All @@ -296,10 +283,10 @@
"model = genai.GenerativeModel(model_name=\"models/gemini-1.5-pro-latest\")\n",
"\n",
"result = model.generate_content(\n",
" prompt,\n",
" generation_config={\"response_mime_type\": \"application/json\",\n",
" \"response_schema\": response_schema,\n",
" },\n",
" \"List a few popular cookie recipes\",\n",
" generation_config=genai.GenerationConfig(\n",
" response_mime_type=\"application/json\",\n",
" response_schema = list[Recipe]),\n",
" request_options={\"timeout\": 600},\n",
")"
]
Expand All @@ -315,7 +302,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[{\"favorite_food\": \"pizza\"}] \n"
"[{\"recipe_name\": \"Chocolate Chip Cookies\"}, {\"recipe_name\": \"Peanut Butter Cookies\"}, {\"recipe_name\": \"Oatmeal Raisin Cookies\"}, {\"recipe_name\": \"Sugar Cookies\"}, {\"recipe_name\": \"Snickerdoodles\"}] \n"
]
}
],
Expand Down

0 comments on commit 84323cf

Please sign in to comment.