Skip to content
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

Add entity extraction JSON example #178

Merged
merged 6 commits into from
May 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
222 changes: 222 additions & 0 deletions examples/json_capabilities/Entity_Extraction_JSON.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "T47AX_Is2FjB"
},
"source": [
"##### Copyright 2024 Google LLC."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "M_vx0YO92qlR"
},
"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": {
"id": "sP8PQnz1QrcF"
},
"source": [
"# Gemini API: Entity Extraction"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "bxGr_x3MRA0z"
},
"source": [
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/json_capabilities/Entity_Extraction_JSON.ipynb\"><img src = \"https://www.tensorflow.org/images/colab_logo_32px.png\"/>Run in Google Colab</a>\n",
" </td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ysy--KfNRrCq"
},
"source": [
"You will use Gemini to extract all fields that fit one of the predefined classes and label them."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "Ne-3gnXqR0hI"
},
"outputs": [],
"source": [
"!pip install -U -q google-generativeai"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"id": "EconMHePQHGw"
},
"outputs": [],
"source": [
"import google.generativeai as genai\n",
"\n",
"import json\n",
"from enum import Enum\n",
"from typing_extensions import TypedDict # in python 3.12 replace typing_extensions with typing"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "eomJzCa6lb90"
},
"source": [
"## Configure your API key\n",
"\n",
"To run the following cell, your API key must be stored it 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."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"id": "v-JZzORUpVR2"
},
"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",
"metadata": {
"id": "R3EUoLgJNfe7"
},
"source": [
"## Example"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"id": "QGdJnd0AOKbu"
},
"outputs": [],
"source": [
"entity_recongnition_text = \"John Johnson, the CEO of the Oil Inc. and Coal Inc. companies, has unveiled plans to build a new factory in Houston, Texas.\"\n",
"prompt = f\"\"\"\n",
"Generate list of entities in text based on the following Python class structure:\n",
"\n",
"class CategoryEnum(str, Enum):\n",
" Person = 'Person'\n",
" Company = 'Company'\n",
" State = 'State'\n",
" City = 'City'\n",
"\n",
"class Entity(TypedDict):\n",
" name: str\n",
" category: CategoryEnum\n",
"\n",
"class Entities(TypedDict):\n",
" entities: list[Entity]\n",
"\n",
"{entity_recongnition_text}\"\"\"\n",
"model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', generation_config={\"temperature\": 0})\n",
"response = model.generate_content(prompt, generation_config={\"response_mime_type\": \"application/json\"})"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"id": "d5tOgde6ONo3"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"entities\": [\n",
" {\n",
" \"name\": \"John Johnson\",\n",
" \"category\": \"Person\"\n",
" },\n",
" {\n",
" \"name\": \"Oil Inc.\",\n",
" \"category\": \"Company\"\n",
" },\n",
" {\n",
" \"name\": \"Coal Inc.\",\n",
" \"category\": \"Company\"\n",
" },\n",
" {\n",
" \"name\": \"Houston\",\n",
" \"category\": \"City\"\n",
" },\n",
" {\n",
" \"name\": \"Texas\",\n",
" \"category\": \"State\"\n",
" }\n",
" ]\n",
"}\n"
]
}
],
"source": [
"print(json.dumps(json.loads(response.text), indent=4))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2uv9Rikv27tf"
},
"source": [
"## Summary\n",
"You have used the Gemini API to extract entities of predifined categories with their labels. You extracted every person, company, state, and country. You are not limited to these categories, as this should work with any category of your choice.\n",
"\n",
"Please see the other notebooks in this directory to learn more about how you can use the Gemini API for other JSON related tasks.\n"
]
}
],
"metadata": {
"colab": {
"name": "Entity_Extraction_JSON.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading