forked from google-gemini/cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic classification prompting tutorial (google-gemini#149)
* Add basic classification prompting tutorial * Update basic classification example
- Loading branch information
1 parent
13b87b1
commit 74094b2
Showing
1 changed file
with
253 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,253 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "6YVFzUGtYdOk" | ||
}, | ||
"source": [ | ||
"##### Copyright 2024 Google LLC." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"cellView": "form", | ||
"id": "eCxW9moPYbOO" | ||
}, | ||
"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: Basic classification\n", | ||
"\n", | ||
"This notebook demonstrates how to use prompting to perform classification tasks using the Gemini API's Python SDK." | ||
] | ||
}, | ||
{ | ||
"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/prompting/Basic_Classification.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": [ | ||
"LLMs can be used in tasks that require classifying content into predefined categories. This business case shows how it categorizes user messages under the blog topic. It can classify replies in the following categories: spam, abusive comments, and offensive messages." | ||
] | ||
}, | ||
{ | ||
"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": [ | ||
"## Examples" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": { | ||
"id": "C-1hwJCcV-EZ" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"classification_system_prompt = \"\"\"\n", | ||
"\n", | ||
"As a social media moderation system, your task is to categorize user comments under a post.\n", | ||
"Analyze the comment related to the topic and classify it into one of the following categories:\n", | ||
"\n", | ||
"Abusive\n", | ||
"Spam\n", | ||
"Offensive\n", | ||
"\n", | ||
"If the comment does not fit any of the above categories, classify it as: Neutral.\n", | ||
"\n", | ||
"Provide only the category as a response without explanations.\n", | ||
"\"\"\"\n", | ||
"model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', generation_config={\"temperature\": 0},\n", | ||
" system_instruction=classification_system_prompt)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": { | ||
"id": "VvMhQfbYpNXN" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Define a template that you will reuse in examples below\n", | ||
"classification_template = \"\"\"\n", | ||
"Topic: What can I do after highschool?\n", | ||
"Comment: You should do a gap year!\n", | ||
"Class: Neutral\n", | ||
"\n", | ||
"Topic: Where can I buy a cheap phone?\n", | ||
"Comment: You have just won an IPhone 15 Pro Max!!! Click the link to receive the prize!!!\n", | ||
"Class: Spam\n", | ||
"\n", | ||
"Topic: How long do you boil eggs?\n", | ||
"Comment: Are you stupid?\n", | ||
"Class: Offensive\n", | ||
"\n", | ||
"Topic: {topic}\n", | ||
"Comment: {comment}\n", | ||
"Class: \"\"\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": { | ||
"id": "L8gQM5GwsM2k" | ||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/markdown": "Spam \n", | ||
"text/plain": [ | ||
"<IPython.core.display.Markdown object>" | ||
] | ||
}, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"spam_topic = \"I am looking for a vet in our neighbourhood. Can anyone reccomend someone good? Thanks.\"\n", | ||
"spam_comment = \"You can win 1000$ by just following me!\"\n", | ||
"spam_prompt = classification_template.format(topic=spam_topic, comment=spam_comment)\n", | ||
"Markdown(model.generate_content(spam_prompt).text)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": { | ||
"id": "AITEHd2jsiAO" | ||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/markdown": "Neutral \n", | ||
"text/plain": [ | ||
"<IPython.core.display.Markdown object>" | ||
] | ||
}, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"neutral_topic = \"My computer froze. What should I do?\"\n", | ||
"neutral_comment = \"Try turning it off and on.\"\n", | ||
"neutral_prompt = classification_template.format(topic=neutral_topic, comment=neutral_comment)\n", | ||
"Markdown(model.generate_content(neutral_prompt).text)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "mwN1x_tn24xt" | ||
}, | ||
"source": [ | ||
"## Next steps\n", | ||
"\n", | ||
"Be sure to explore other examples of prompting in the repository. Try writing prompts about classifying your own datasets." | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"colab": { | ||
"name": "Basic_Classification.ipynb", | ||
"toc_visible": true | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"name": "python3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |