Skip to content

Commit

Permalink
add basic reasoning prompting example (#150)
Browse files Browse the repository at this point in the history
* add basic reasoning prompting example

* update basic reasoning example

* Remove trailing space from system instructions

* Update word problem,
  • Loading branch information
shilpakancharla committed Jun 11, 2024
1 parent e7b3eec commit b4d8c8c
Showing 1 changed file with 235 additions and 0 deletions.
235 changes: 235 additions & 0 deletions examples/prompting/Basic_Reasoning.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "ATN0AySQa38h"
},
"source": [
"##### Copyright 2024 Google LLC."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "80WmhK32a-_g"
},
"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 reasoning\n",
"\n",
"This notebook demonstrates how to use prompting to perform reasoning tasks using the Gemini API's Python SDK. In this example, you will work through a mathematical word problem using 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/Basic_Reasoning.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": [
"The Gemini API can handle many tasks that involve indirect reasoning, such as solving mathematical or logical proofs.\n",
"\n",
"In this example, you will see how the LLM explains given problems step by step."
]
},
{
"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": "L-Wt23A_uzFZ"
},
"source": [
"## Examples\n",
"\n",
"Begin by defining some system instructions that will be include when you define and choose the model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SabVqFgbu_Hd"
},
"outputs": [],
"source": [
"system_prompt = \"\"\"\n",
"You are a teacher solving mathematical and logical problems. Your task:\n",
"1. Summarize given conditions.\n",
"2. Identify the problem.\n",
"3. Provide a clear, step-by-step solution.\n",
"4. Provide an explanation for each step.\n",
"\n",
"Ensure simplicity, clarity, and correctness in all steps of your explanation.\n",
"Each of your task should be done in order and seperately.\n",
"\"\"\"\n",
"model = genai.GenerativeModel(model_name=\"gemini-1.5-flash-latest\", system_instruction=system_prompt)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HqAQdUvbe_1k"
},
"source": [
"Next, you can define a logical problem such as the one below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "82qEUsfpz8mx"
},
"outputs": [
{
"data": {
"text/markdown": "## Problem Summary:\n\n* **Scenario:** In a world where 1 in 5 dice are weighted to always roll a 6, a person rolls a 6.\n* **Question:** Is it more likely that the person rolled a weighted die or a fair die?\n\n## Identifying the Problem:\n\nWe need to determine the probability of rolling a 6 with a weighted die versus a fair die, given that a 6 was rolled. This is a conditional probability problem.\n\n## Solution:\n\nLet's use Bayes' Theorem to solve this:\n\n**P(Weighted | 6) = [P(6 | Weighted) * P(Weighted)] / P(6)**\n\nWhere:\n\n* **P(Weighted | 6):** The probability that the die is weighted, given that a 6 was rolled.\n* **P(6 | Weighted):** The probability of rolling a 6 given that the die is weighted (which is 1, or 100%).\n* **P(Weighted):** The prior probability of the die being weighted (which is 1/5).\n* **P(6):** The overall probability of rolling a 6 (which we need to calculate).\n\n**Calculating P(6):**\n\n* **P(6) = P(6 | Weighted) * P(Weighted) + P(6 | Fair) * P(Fair)**\n* **P(6) = (1 * 1/5) + (1/6 * 4/5)**\n* **P(6) = 1/5 + 2/15 = 1/3**\n\n**Applying Bayes' Theorem:**\n\n* **P(Weighted | 6) = (1 * 1/5) / (1/3)**\n* **P(Weighted | 6) = 3/5 = 0.6**\n\n## Explanation:\n\n1. **Bayes' Theorem:** This theorem helps us calculate the probability of an event (weighted die) given that another event has occurred (rolling a 6). \n2. **Prior Probabilities:** We know that 1 in 5 dice are weighted (P(Weighted) = 1/5), and therefore 4 in 5 are fair (P(Fair) = 4/5).\n3. **Calculating P(6):** We calculate the probability of rolling a 6 by considering the probability of rolling a 6 with a weighted die and a fair die, and weighting them by their respective probabilities.\n4. **Conditional Probability:** We use the calculated P(6) and the probabilities from step 2 to apply Bayes' Theorem and calculate the probability of the die being weighted given that a 6 was rolled.\n\n## Conclusion:\n\nThe probability of the die being weighted, given that a 6 was rolled, is 0.6 or 60%. This means it is more likely that the person rolled a weighted die than a fair die. \n",
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"logical_problem = \"\"\"\n",
"Assume a world where 1 in 5 dice are weighted and have 100% to roll a 6.\n",
"A person rolled a dice and rolled a 6.\n",
"Is it more likely that the die was weighted or not?\n",
"\"\"\"\n",
"Markdown(model.generate_content(logical_problem).text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "u2ZDzuOYvMMy"
},
"outputs": [
{
"data": {
"text/markdown": "## 1. Summarize given conditions:\n\n* The triangle has a base (b) of 6 units.\n* The triangle has a height (h) of 8 units.\n\n## 2. Identify the problem:\n\nThe problem asks us to calculate the area of the triangle.\n\n## 3. Provide a clear, step-by-step solution:\n\n**Step 1:** Recall the formula for the area of a triangle: \nArea = (1/2) * base * height \n\n**Step 2:** Substitute the given values into the formula:\nArea = (1/2) * 6 * 8\n\n**Step 3:** Simplify the expression:\nArea = 3 * 8\n\n**Step 4:** Calculate the final result:\nArea = 24\n\n## 4. Provide an explanation for each step:\n\n**Step 1:** The formula for the area of a triangle is a fundamental concept in geometry. It states that the area of a triangle is equal to half the product of its base and height.\n\n**Step 2:** We substitute the given values of base (b=6) and height (h=8) into the formula to obtain a specific equation for the area of this particular triangle.\n\n**Step 3:** We perform the multiplication operation according to the order of operations, simplifying the expression to a single multiplication.\n\n**Step 4:** Finally, we perform the last multiplication to arrive at the final answer, which is 24 square units. \n",
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math_problem = \"Given a triangle with base b=6 and height h=8, calculate its area\"\n",
"Markdown(model.generate_content(math_problem).text)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4gJJr5AM5Bwl"
},
"source": [
"## Next steps\n",
"\n",
"Be sure to explore other examples of prompting in the repository. Try creating your own prompts that include instructions on how to solve basic reasoning problems, or use the prompt given in this notebook as a template."
]
}
],
"metadata": {
"colab": {
"name": "Basic_Reasoning.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit b4d8c8c

Please sign in to comment.