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 chain of thought prompting example #164

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Changes from 2 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
223 changes: 223 additions & 0 deletions examples/prompting/Chain_of_thought_prompting.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
{
shilpakancharla marked this conversation as resolved.
Show resolved Hide resolved
shilpakancharla marked this conversation as resolved.
Show resolved Hide resolved
shilpakancharla marked this conversation as resolved.
Show resolved Hide resolved
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "fzRFGWxAsTm2"
},
"source": [
"##### Copyright 2024 Google LLC."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "Y0nQsAf2sSfs"
},
"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: Chain of thought prompting"
]
},
{
"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/Chain_of_thought_prompting.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": [
"Using chain of thought helps the LLM take a logical and arithmetic approach. Instead of outputting the answer immediately, the LLM uses smaller and easier steps to get to the answer."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Ne-3gnXqR0hI"
},
"outputs": [],
"source": [
"!pip install -U -q google-generativeai"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": null,
"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": "LlRYq9JSeRzR"
},
"source": [
"## Example\n",
"\n",
"Here is an example with chain of thought prompting. Take a look at what the result would be without chain of thought implemented."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "8oS9LnnXXedG"
},
"outputs": [],
"source": [
"model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', generation_config={\"temperature\": 0})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "0u1IjBOmeQgG"
},
"outputs": [
{
"data": {
"text/markdown": "Here's how to solve this problem:\n\n**Understanding the Relationship**\n\n* **People and Donuts:** If 5 people make 5 donuts, that means each person makes 1 donut in 5 minutes.\n* **Time:** The time it takes to make a donut doesn't change with more people.\n\n**Solving the Problem**\n\n1. **Individual Rate:** Each person makes 1 donut every 5 minutes.\n2. **Total Donuts:** With 25 people, they can make 25 donuts every 5 minutes (25 people * 1 donut/person = 25 donuts).\n3. **Time for 100 Donuts:** To make 100 donuts, it would take 20 minutes (100 donuts / 25 donuts/5 minutes = 20 minutes).\n\n**Answer:** It would take 25 people **20 minutes** to make 100 donuts. \n",
shilpakancharla marked this conversation as resolved.
Show resolved Hide resolved
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt = \"\"\"\n",
"Q: 5 people can create 5 donuts every 5 minutes. How much time would it take 25 people to make 100 donuts?\n",
"A:\"\"\"\n",
"Markdown(model.generate_content(prompt).text)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kCFJ2dvBubSm"
},
"source": [
"Now, implement chain of thought into your prompt and look at the difference in the response. Note the multiple steps implemented within the prompt."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "HdMNHASyeWoK"
},
"outputs": [
{
"data": {
"text/markdown": "Here's how to solve this problem:\n\n**1. Find the individual production rate:**\n\n* 5 people make 5 donuts in 5 minutes, meaning one person makes 1 donut in 5 minutes.\n\n**2. Calculate the total production rate with 25 people:**\n\n* With 25 people, they can make 25 donuts every 5 minutes (25 people * 1 donut/person/5 minutes = 25 donuts/5 minutes).\n\n**3. Determine the time to make 100 donuts:**\n\n* Since they make 25 donuts every 5 minutes, they make 5 sets of 25 donuts in 25 minutes (5 sets * 5 minutes/set = 25 minutes).\n\n**Answer:** It would take 25 people **25 minutes** to make 100 donuts. \n",
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt = \"\"\"\n",
"Q: 11 factories can make 22 cars per hour. How much time would it take 22 factories to make 88 cars?\n",
"A: A factory can make 22/11=2 cars per hour. 22 factories can make 22*2=44 cars per hour. Making 88 cars would take 88/44=2 hours. The answer is 2 hours.\n",
"Q: 5 people can create 5 donuts every 5 minutes. How much time would it take 25 people to make 100 donuts?\n",
"A:\"\"\"\n",
"Markdown(model.generate_content(prompt).text)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "w_K9Sn3Yu9eL"
},
"source": [
"## Next steps\n",
"\n",
"Be sure to explore other examples of prompting in the repository. Try writing prompts about classifying your own data, or try some of the other prompting techniques such as few-shot prompting."
]
}
],
"metadata": {
"colab": {
"name": "Chain_of_thought_prompting.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading