Skip to content

Commit

Permalink
Merge branch 'main' into prompting
Browse files Browse the repository at this point in the history
  • Loading branch information
shilpakancharla authored May 30, 2024
2 parents 7c65d32 + b67bf46 commit e4b744a
Show file tree
Hide file tree
Showing 7 changed files with 926 additions and 56 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/stale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This workflow warns and then closes issues and PRs that have had no activity for a specified amount of time.
#
# You can adjust the behavior by modifying this file.
# For more information, see:
# https://github.com/actions/stale
name: Mark stale issues and pull requests

on:
schedule:
# Scheduled to run at 1.30 UTC everyday
- cron: '30 1 * * *'

jobs:
stale:

runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write

steps:
- uses: actions/stale@v5
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
days-before-issue-stale: 14
days-before-issue-close: 14
stale-issue-label: "status:stale"
close-issue-reason: not_planned
any-of-labels: "status:awaiting user response,status:more data needed"
stale-issue-message: >
Marking this issue as stale since it has been open for 14 days with no activity.
This issue will be closed if no further activity occurs.
close-issue-message: >
This issue was closed because it has been inactive for 28 days.
Please post a new issue if you need further assistance. Thanks!
days-before-pr-stale: 14
days-before-pr-close: 14
stale-pr-label: "status:stale"
stale-pr-message: >
Marking this pull request as stale since it has been open for 14 days with no activity.
This PR will be closed if no further activity occurs.
close-pr-message: >
This pull request was closed because it has been inactive for 28 days.
Please open a new pull request if you need further assistance. Thanks!
# Label that can be assigned to issues to exclude them from being marked as stale
exempt-issue-labels: 'override-stale'
# Label that can be assigned to PRs to exclude them from being marked as stale
exempt-pr-labels: "override-stale"
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

0 comments on commit e4b744a

Please sign in to comment.