From 4f7fd25d7fbc270b7fb138a5f5c7ce2c1629a260 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Wed, 5 Jun 2024 09:35:14 -0700 Subject: [PATCH] Sentiment analysis JSON example (#193) * Add example for adding context information for prompting * delete * add self ask for prompting * Add sentiment analysis JSON example * Update colab link * Update colab link for self ask * License header for self ask fixed --- .../Sentiment_Analysis.ipynb | 305 ++++++++++++++++++ examples/prompting/Self_ask_prompting.ipynb | 188 +++++++++++ 2 files changed, 493 insertions(+) create mode 100644 examples/json_capabilities/Sentiment_Analysis.ipynb create mode 100644 examples/prompting/Self_ask_prompting.ipynb diff --git a/examples/json_capabilities/Sentiment_Analysis.ipynb b/examples/json_capabilities/Sentiment_Analysis.ipynb new file mode 100644 index 000000000..ec9e2ff03 --- /dev/null +++ b/examples/json_capabilities/Sentiment_Analysis.ipynb @@ -0,0 +1,305 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "UvTPRErEO2cl" + }, + "source": [ + "##### Copyright 2024 Google LLC." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "cellView": "form", + "id": "LCJL7_hQO3jW" + }, + "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: Sentiment Analysis" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bxGr_x3MRA0z" + }, + "source": [ + "\n", + " \n", + "
\n", + " Run in Google Colab\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ysy--KfNRrCq" + }, + "source": [ + "You will use the Gemini to extract sentiment scores of reviews." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "Ne-3gnXqR0hI" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\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[32m10.2/158.8 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[90m╺\u001b[0m\u001b[90m━━━━━━━━━━━\u001b[0m \u001b[32m112.6/158.8 kB\u001b[0m \u001b[31m1.9 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[31m1.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25h" + ] + } + ], + "source": [ + "!pip install -U -q google-generativeai" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "id": "EconMHePQHGw" + }, + "outputs": [], + "source": [ + "import google.generativeai as genai\n", + "\n", + "import json\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": 3, + "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": "markdown", + "metadata": { + "id": "cblGFW3VwzyO" + }, + "source": [ + "Start by defining how you want your JSON to be returned and which categories you would like to classify an item by. After that,gGo ahead and define some examples. In this case, you are trying to classify reviews as positive, neutral, or negative." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "id": "QGdJnd0AOKbu" + }, + "outputs": [], + "source": [ + "sentiment_llm = genai.GenerativeModel(model_name='gemini-1.5-flash-latest',\n", + " generation_config={\"response_mime_type\": \"application/json\"})\n", + "prompt = \"\"\"\n", + "Generate each sentiment score probability (positive, negative, or neutral) for the whole text.\n", + "\n", + "Use the following schema to return the sentiment scores:\n", + "\n", + "class Sentiment(TypedDict):\n", + " positive_sentiment_score: float\n", + " negative_sentiment_score: float\n", + " neutral_sentiment_score: float\n", + "\n", + "{review}\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "id": "yRq9PGpVQLUK" + }, + "outputs": [], + "source": [ + "negative_review = \"This establishment is an insult to the culinary arts, with inedible food that left me questioning the chef's sanity and the health inspector's judgment.\"\n", + "positive_review = \"This restaurant is a true gem with impeccable service and a menu that tantalizes the taste buds. Every dish is a culinary masterpiece, crafted with fresh ingredients and bursting with flavor.\"\n", + "neutral_review = \"The restaurant offers a decent dining experience with average food and service, making it a passable choice for a casual meal.\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "aanwkFCtxKCi" + }, + "source": [ + "Here is a helper function to print formatted JSON:" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "id": "xyqeFZ-9QPV4" + }, + "outputs": [], + "source": [ + "def print_json(response):\n", + " print(json.dumps(json.loads(response.text), indent=4))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "z0yHqEJRwp_U" + }, + "source": [ + "Take a look at each of the probabilities returned to see how each of these reviews would be classified by the Gemini model." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "id": "Tz0cDFyD9uUT" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"positive_sentiment_score\": 0.0,\n", + " \"negative_sentiment_score\": 0.9999999403953552,\n", + " \"neutral_sentiment_score\": 5.96046448e-08\n", + "}\n" + ] + } + ], + "source": [ + "response = sentiment_llm.generate_content(prompt.format(review=negative_review))\n", + "print_json(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "id": "bMW3QmYy9uUT" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"positive_sentiment_score\": 0.987756,\n", + " \"negative_sentiment_score\": 0.012244,\n", + " \"neutral_sentiment_score\": 0.0\n", + "}\n" + ] + } + ], + "source": [ + "response = sentiment_llm.generate_content(prompt.format(review=positive_review))\n", + "print_json(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "id": "QTjUYa4J9uUT" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"positive_sentiment_score\": 0.213,\n", + " \"negative_sentiment_score\": 0.193,\n", + " \"neutral_sentiment_score\": 0.594\n", + "}\n" + ] + } + ], + "source": [ + "response = sentiment_llm.generate_content(prompt.format(review=neutral_review))\n", + "print_json(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AGOx4_7r1uN6" + }, + "source": [ + "## Summary\n", + "You have now used Gemini to analyze the sentiment of restaurant reviews with JSON, but you can try it out on other types of texts, such as comments under a video or emails.\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." + ] + } + ], + "metadata": { + "colab": { + "name": "Sentiment_Analysis.ipynb", + "toc_visible": true + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/examples/prompting/Self_ask_prompting.ipynb b/examples/prompting/Self_ask_prompting.ipynb new file mode 100644 index 000000000..ab76eb39e --- /dev/null +++ b/examples/prompting/Self_ask_prompting.ipynb @@ -0,0 +1,188 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "sP8PQnz1QrcF" + }, + "source": [ + "##### Copyright 2024 Google LLC." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "cellView": "form", + "id": "906e07f6e562" + }, + "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": "993efe4a45ee" + }, + "source": [ + "# Gemini API: Self-ask prompting" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bxGr_x3MRA0z" + }, + "source": [ + "\n", + " \n", + "
\n", + " Run in Google Colab\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ysy--KfNRrCq" + }, + "source": [ + "Self ask prompting is similar to chain of thought, but instead of going step by step as one answer, it asks itself questions that will help answer the query. Like the chain of thought, it helps the model to think analytically." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "Ne-3gnXqR0hI" + }, + "outputs": [], + "source": [ + "!pip install -U -q google-generativeai" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "id": "EconMHePQHGw" + }, + "outputs": [], + "source": [ + "import google.generativeai as genai\n", + "\n", + "from IPython.display import Markdown" + ] + }, + { + "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": 3, + "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": "yQnqEPjephXi" + }, + "source": [ + "## Example" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "id": "XEfLLHa7pjC8" + }, + "outputs": [ + { + "data": { + "text/markdown": [ + "Let's break this down step-by-step:\n", + "\n", + "1. **When was Maria Skłodowska born?** We need this information to determine which Emperor of Japan was in power. \n", + "2. **Who was the Emperor of Japan during that time period?** Once we know the year of Maria Skłodowska's birth, we can find the corresponding Emperor.\n", + "3. **Where did that Emperor die?** This is the final piece of information we need to answer the question.\n", + "\n", + "**Please provide the year Maria Skłodowska was born so we can continue!** \n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "prompt = \"\"\"\n", + "Query: Who was the president of the united states when Mozart died?\n", + "Are follow up questions needed?: yes.\n", + "Follow up: When did Mozart died?\n", + "Intermediate answer: 1791.\n", + "Follow up: Who was the president of the united states in 1791?\n", + "Intermediate answer: George Washington.\n", + "Final answer: When Mozart died George Washington was the president of the USA.\n", + "\n", + "Question: where died the Emperor of Japan who ruled when Maria Skłodowska was born?\"\"\"\n", + "model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', generation_config={\"temperature\": 0})\n", + "Markdown(model.generate_content(prompt).text)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1RtZ1y-IpcnV" + }, + "source": [ + "## Additional note\n", + "Self ask prompting works well with function calling. Follow-up questions can be used as input to a function, which e.g. searches the internet. The question and answer from the function can be added back to the prompt. During the next query to the model, it can either create another function call or return the final answer." + ] + } + ], + "metadata": { + "colab": { + "name": "Self_ask_prompting.ipynb", + "toc_visible": true + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}