-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1f4a4b
commit e57e264
Showing
1 changed file
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"# Gemini API: JSON Schema\n", | ||
"\n", | ||
"The Gemini API can be used to generate a JSON output if we set the schema that we would like to use." | ||
], | ||
"metadata": { | ||
"id": "GAsiP4mohC2_" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "qLuL9m7KhvxR", | ||
"outputId": "86c668b8-f3a0-40b0-e832-76bb4f82368f" | ||
}, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
" Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n", | ||
" Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n", | ||
" Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", | ||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m679.1/679.1 kB\u001b[0m \u001b[31m7.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | ||
"\u001b[?25h Building wheel for google-generativeai (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"!pip install -U -q google-generativeai" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
"id": "ATIbQM0NHhkj" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import google.generativeai as genai\n", | ||
"\n", | ||
"import dataclasses\n", | ||
"import typing_extensions as typing" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"## Configure your API key\n", | ||
"\n", | ||
"To run the following cell, your API key must be stored in a Colab Secret named `GOOGLE_API_KEY`. If you don't already have an API key, or you're not sure how to create a Colab Secret, see [Authentication](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb) for an example." | ||
], | ||
"metadata": { | ||
"id": "B-axqBTM8Lbd" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": { | ||
"id": "d6lYXRcjthKV" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from google.colab import userdata\n", | ||
"GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n", | ||
"\n", | ||
"genai.configure(api_key=GOOGLE_API_KEY)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"## Generate JSON\n", | ||
"\n", | ||
"We can take a Python class, for instance, and use it as our schema for generating JSON." | ||
], | ||
"metadata": { | ||
"id": "K9nIks0R-tIa" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"class Person(typing.TypedDict):\n", | ||
" family_name: str\n", | ||
" favorite_food: str\n" | ||
], | ||
"metadata": { | ||
"id": "vP6teXff_H_L" | ||
}, | ||
"execution_count": 5, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"prompt = \"Hello, describe a person, all fields are required.\"" | ||
], | ||
"metadata": { | ||
"id": "p0gxz0NNr8si" | ||
}, | ||
"execution_count": 6, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"Using `generate_content`, we pass in the Python class `Person` defined above into the `generation_config`'s `response_schema` field." | ||
], | ||
"metadata": { | ||
"id": "vBlWzt6M-2oM" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": { | ||
"id": "8oe-tL8MDGtx" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"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\": Person\n", | ||
" },\n", | ||
" request_options={\"timeout\": 600},\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"print(result.text)" | ||
], | ||
"metadata": { | ||
"id": "slYcVAcqaDQY", | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"outputId": "468d90b2-b7a9-4eca-a09e-4d9b46bcc741" | ||
}, | ||
"execution_count": 8, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"{\"family_name\": \"Smith\", \"favorite_food\": \"Pizza\" } \n" | ||
] | ||
} | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"colab": { | ||
"provenance": [] | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |