├── README.md ├── LICENSE ├── .gitignore └── Gemini_JSON.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # Gemini-JSON-Mode 2 | Gemini JSON Mode 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 AI Anytime 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/latest/usage/project/#working-with-version-control 110 | .pdm.toml 111 | .pdm-python 112 | .pdm-build/ 113 | 114 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 115 | __pypackages__/ 116 | 117 | # Celery stuff 118 | celerybeat-schedule 119 | celerybeat.pid 120 | 121 | # SageMath parsed files 122 | *.sage.py 123 | 124 | # Environments 125 | .env 126 | .venv 127 | env/ 128 | venv/ 129 | ENV/ 130 | env.bak/ 131 | venv.bak/ 132 | 133 | # Spyder project settings 134 | .spyderproject 135 | .spyproject 136 | 137 | # Rope project settings 138 | .ropeproject 139 | 140 | # mkdocs documentation 141 | /site 142 | 143 | # mypy 144 | .mypy_cache/ 145 | .dmypy.json 146 | dmypy.json 147 | 148 | # Pyre type checker 149 | .pyre/ 150 | 151 | # pytype static type analyzer 152 | .pytype/ 153 | 154 | # Cython debug symbols 155 | cython_debug/ 156 | 157 | # PyCharm 158 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 159 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 160 | # and can be added to the global gitignore or merged into this file. For a more nuclear 161 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 162 | #.idea/ 163 | -------------------------------------------------------------------------------- /Gemini_JSON.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "colab": { 22 | "base_uri": "https://localhost:8080/" 23 | }, 24 | "id": "jwjZGn76XBPm", 25 | "outputId": "ae06f589-3291-44e6-8c84-9b20fec30bd1" 26 | }, 27 | "outputs": [ 28 | { 29 | "output_type": "stream", 30 | "name": "stdout", 31 | "text": [ 32 | "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/158.8 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━\u001b[0m \u001b[32m153.6/158.8 kB\u001b[0m \u001b[31m5.4 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m158.8/158.8 kB\u001b[0m \u001b[31m3.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 33 | "\u001b[?25h" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "!pip install -q -U google-generativeai" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "source": [ 44 | "\"JSON mode\" is a mode to force LLM output to JSON format. Available in \"gemini-1.5-pro\" and \"gemini-1.5-flash\".\n", 45 | "\n", 46 | "Additionally, gemini-1.5-pro supports \"constrained decoding\", which allows you to pass a schema object (or equivalent Python type) at inference time and the output will strictly follow that schema." 47 | ], 48 | "metadata": { 49 | "id": "v-tb_VZBa83K" 50 | } 51 | }, 52 | { 53 | "cell_type": "code", 54 | "source": [ 55 | "from google.colab import userdata\n", 56 | "import google.generativeai as genai\n", 57 | "\n", 58 | "\n", 59 | "GOOGLE_API_KEY=userdata.get(\"GOOGLE_API_KEY\")\n", 60 | "genai.configure(api_key=GOOGLE_API_KEY)" 61 | ], 62 | "metadata": { 63 | "id": "fw-pS9_gYNHV" 64 | }, 65 | "execution_count": 2, 66 | "outputs": [] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "source": [ 71 | "model = genai.GenerativeModel(\n", 72 | " \"gemini-1.5-flash\",\n", 73 | " generation_config={\"response_mime_type\": \"application/json\"}\n", 74 | ")" 75 | ], 76 | "metadata": { 77 | "id": "Zn5PB80yY4se" 78 | }, 79 | "execution_count": 3, 80 | "outputs": [] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "source": [ 85 | "prompt = \"\"\"Using the following JSON schema,\n", 86 | "Please list 5 popular cookie recipes.\n", 87 | "Recipe = {'recipe_name': str}\n", 88 | "Return: list[Recipe]\"\"\"\n", 89 | "\n", 90 | "\n", 91 | "raw_response = model.generate_content(prompt)\n", 92 | "print(raw_response.text)" 93 | ], 94 | "metadata": { 95 | "colab": { 96 | "base_uri": "https://localhost:8080/", 97 | "height": 52 98 | }, 99 | "id": "sRICRiyaZAEe", 100 | "outputId": "e76d1d1c-469a-4310-dab0-8cd0f1777686" 101 | }, 102 | "execution_count": 4, 103 | "outputs": [ 104 | { 105 | "output_type": "stream", 106 | "name": "stdout", 107 | "text": [ 108 | "[{\"recipe_name\": \"Chocolate Chip Cookies\"}, {\"recipe_name\": \"Oatmeal Raisin Cookies\"}, {\"recipe_name\": \"Sugar Cookies\"}, {\"recipe_name\": \"Peanut Butter Cookies\"}, {\"recipe_name\": \"Snickerdoodles\"}]\n", 109 | "\n" 110 | ] 111 | } 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "source": [ 117 | "import json\n", 118 | "response = json.loads(raw_response.text)\n", 119 | "print(response)" 120 | ], 121 | "metadata": { 122 | "colab": { 123 | "base_uri": "https://localhost:8080/" 124 | }, 125 | "id": "FkDkL8d1ZOnD", 126 | "outputId": "565594f1-265f-4013-b60a-d423e58aa235" 127 | }, 128 | "execution_count": 5, 129 | "outputs": [ 130 | { 131 | "output_type": "stream", 132 | "name": "stdout", 133 | "text": [ 134 | "[{'recipe_name': 'Chocolate Chip Cookies'}, {'recipe_name': 'Oatmeal Raisin Cookies'}, {'recipe_name': 'Sugar Cookies'}, {'recipe_name': 'Peanut Butter Cookies'}, {'recipe_name': 'Snickerdoodles'}]\n" 135 | ] 136 | } 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "source": [ 142 | "## JSON output with constrained decoding" 143 | ], 144 | "metadata": { 145 | "id": "D6fzrb37Zi5t" 146 | } 147 | }, 148 | { 149 | "cell_type": "code", 150 | "source": [ 151 | "import typing_extensions as typing\n", 152 | "\n", 153 | "# Schema object definition\n", 154 | "class Recipe(typing.TypedDict):\n", 155 | " recipe_name: str" 156 | ], 157 | "metadata": { 158 | "id": "cATd0QYaZdpm" 159 | }, 160 | "execution_count": 6, 161 | "outputs": [] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "source": [ 166 | "Specify the JSON format in the schema object and execute inference.\n", 167 | "\n", 168 | "Specify \"application/json\" for response_mime_type of\n", 169 | "generation_config() and the schema object for response_schema ." 170 | ], 171 | "metadata": { 172 | "id": "mkqcKNSfavHm" 173 | } 174 | }, 175 | { 176 | "cell_type": "code", 177 | "source": [ 178 | "# Direct JSON output with constrained decoding\n", 179 | "result = model.generate_content(\n", 180 | " \"Please list 5 popular cookie recipes.\",\n", 181 | " generation_config=genai.GenerationConfig(\n", 182 | " response_mime_type=\"application/json\",\n", 183 | " response_schema = list[Recipe]\n", 184 | " ),\n", 185 | " request_options={\"timeout\": 600}, # timeout\n", 186 | ")\n", 187 | "print(result.text)" 188 | ], 189 | "metadata": { 190 | "colab": { 191 | "base_uri": "https://localhost:8080/", 192 | "height": 72 193 | }, 194 | "id": "HKClxn0TaMyo", 195 | "outputId": "7b36c59d-87f3-48e0-fed0-969d5a93931b" 196 | }, 197 | "execution_count": 7, 198 | "outputs": [ 199 | { 200 | "output_type": "stream", 201 | "name": "stdout", 202 | "text": [ 203 | "{\"recipes\": [{\"name\": \"Chocolate Chip Cookies\", \"ingredients\": [\"1 cup (2 sticks) unsalted butter, softened\", \"3/4 cup granulated sugar\", \"3/4 cup packed light brown sugar\", \"2 large eggs\", \"2 teaspoons pure vanilla extract\", \"2 1/4 cups all-purpose flour\", \"1 teaspoon baking soda\", \"1 teaspoon salt\", \"2 cups chocolate chips\"], \"instructions\": [\"Preheat oven to 375 degrees F (190 degrees C).\", \"In a large bowl, cream together the butter, granulated sugar, and brown sugar until light and fluffy.\", \"Beat in the eggs one at a time, then stir in the vanilla.\", \"In a separate bowl, whisk together the flour, baking soda, and salt.\", \"Gradually add the dry ingredients to the wet ingredients, mixing until just combined.\", \"Stir in the chocolate chips.\", \"Drop by rounded tablespoons onto ungreased baking sheets.\", \"Bake for 9-11 minutes, or until edges are golden brown.\", \"Let cool on baking sheets for a few minutes before transferring to a wire rack to cool completely.\"] }, {\"name\": \"Oatmeal Raisin Cookies\", \"ingredients\": [\"1 cup (2 sticks) unsalted butter, softened\", \"3/4 cup granulated sugar\", \"3/4 cup packed light brown sugar\", \"2 large eggs\", \"1 teaspoon pure vanilla extract\", \"2 1/4 cups all-purpose flour\", \"1 teaspoon baking soda\", \"1 teaspoon salt\", \"1 1/2 cups old-fashioned rolled oats\", \"1 cup raisins\"], \"instructions\": [\"Preheat oven to 350 degrees F (175 degrees C).\", \"In a large bowl, cream together the butter, granulated sugar, and brown sugar until light and fluffy.\", \"Beat in the eggs one at a time, then stir in the vanilla.\", \"In a separate bowl, whisk together the flour, baking soda, and salt.\", \"Gradually add the dry ingredients to the wet ingredients, mixing until just combined.\", \"Stir in the oats and raisins.\", \"Drop by rounded tablespoons onto ungreased baking sheets.\", \"Bake for 10-12 minutes, or until edges are golden brown.\", \"Let cool on baking sheets for a few minutes before transferring to a wire rack to cool completely.\"] }, {\"name\": \"Snickerdoodles\", \"ingredients\": [\"1 cup (2 sticks) unsalted butter, softened\", \"1 1/2 cups granulated sugar\", \"2 large eggs\", \"2 teaspoons pure vanilla extract\", \"3 cups all-purpose flour\", \"1 teaspoon cream of tartar\", \"1/2 teaspoon baking soda\", \"1/4 teaspoon salt\", \"2 tablespoons granulated sugar for rolling\"], \"instructions\": [\"Preheat oven to 400 degrees F (200 degrees C).\", \"In a large bowl, cream together the butter and sugar until light and fluffy.\", \"Beat in the eggs one at a time, then stir in the vanilla.\", \"In a separate bowl, whisk together the flour, cream of tartar, baking soda, and salt.\", \"Gradually add the dry ingredients to the wet ingredients, mixing until just combined.\", \"Roll dough into 1-inch balls.\", \"In a small bowl, combine the 2 tablespoons of granulated sugar for rolling.\", \"Roll each dough ball in the sugar.\", \"Place dough balls 2 inches apart on ungreased baking sheets.\", \"Bake for 8-10 minutes, or until edges are lightly browned.\", \"Let cool on baking sheets for a few minutes before transferring to a wire rack to cool completely.\"] }, {\"name\": \"Peanut Butter Cookies\", \"ingredients\": [\"1 cup (2 sticks) unsalted butter, softened\", \"1 cup creamy peanut butter\", \"1/2 cup granulated sugar\", \"1/2 cup packed light brown sugar\", \"1 large egg\", \"1 teaspoon pure vanilla extract\", \"2 cups all-purpose flour\", \"1 teaspoon baking soda\", \"1/2 teaspoon salt\"], \"instructions\": [\"Preheat oven to 375 degrees F (190 degrees C).\", \"In a large bowl, cream together the butter, peanut butter, granulated sugar, and brown sugar until light and fluffy.\", \"Beat in the egg, then stir in the vanilla.\", \"In a separate bowl, whisk together the flour, baking soda, and salt.\", \"Gradually add the dry ingredients to the wet ingredients, mixing until just combined.\", \"Drop by rounded tablespoons onto ungreased baking sheets.\", \"Bake for 8-10 minutes, or until edges are lightly browned.\", \"Let cool on baking sheets for a few minutes before transferring to a wire rack to cool completely.\"] }, {\"name\": \"Sugar Cookies\", \"ingredients\": [\"1 cup (2 sticks) unsalted butter, softened\", \"1 1/2 cups granulated sugar\", \"2 large eggs\", \"1 teaspoon pure vanilla extract\", \"3 1/2 cups all-purpose flour\", \"1 teaspoon baking powder\", \"1/2 teaspoon salt\"], \"instructions\": [\"Preheat oven to 375 degrees F (190 degrees C).\", \"In a large bowl, cream together the butter and sugar until light and fluffy.\", \"Beat in the eggs one at a time, then stir in the vanilla.\", \"In a separate bowl, whisk together the flour, baking powder, and salt.\", \"Gradually add the dry ingredients to the wet ingredients, mixing until just combined.\", \"Roll out dough on a lightly floured surface to 1/4-inch thickness.\", \"Use cookie cutters to cut out desired shapes.\", \"Place cookies on ungreased baking sheets.\", \"Bake for 6-8 minutes, or until edges are lightly browned.\", \"Let cool on baking sheets for a few minutes before transferring to a wire rack to cool completely.\"] }] }\n", 204 | "\n" 205 | ] 206 | } 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "source": [], 212 | "metadata": { 213 | "id": "KtsBb3neadLJ" 214 | }, 215 | "execution_count": null, 216 | "outputs": [] 217 | } 218 | ] 219 | } --------------------------------------------------------------------------------