-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basic reasoning prompting example
- Loading branch information
1 parent
02c1720
commit 5c330a5
Showing
1 changed file
with
217 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,217 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"provenance": [] | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"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 demonstration, you will work through a mathematical word problem using prompting." | ||
], | ||
"metadata": { | ||
"id": "sP8PQnz1QrcF" | ||
} | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"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/quickstarts/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>" | ||
], | ||
"metadata": { | ||
"id": "bxGr_x3MRA0z" | ||
} | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"Gemini can handle many tasks that involve indirect reasoning, such as solving mathematical or logical proofs.\n", | ||
"\n", | ||
"In this example, we show how Gemini explains given problems step by step." | ||
], | ||
"metadata": { | ||
"id": "ysy--KfNRrCq" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"!pip install -U -q google-generativeai" | ||
], | ||
"metadata": { | ||
"id": "Ne-3gnXqR0hI" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"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", | ||
"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." | ||
], | ||
"metadata": { | ||
"id": "eomJzCa6lb90" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"from google.colab import userdata\n", | ||
"GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n", | ||
"\n", | ||
"genai.configure(api_key=GOOGLE_API_KEY)" | ||
], | ||
"metadata": { | ||
"id": "v-JZzORUpVR2" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"## Examples" | ||
], | ||
"metadata": { | ||
"id": "L-Wt23A_uzFZ" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"model = genai.GenerativeModel(model_name='gemini-pro', generation_config={\"temperature\": 0})" | ||
], | ||
"metadata": { | ||
"id": "FCp3Mmx-uyjw" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"template = \"\"\"\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", | ||
"\n", | ||
"Ensure simplicity and clarity in all steps of your explanation.\n", | ||
"\n", | ||
"Problem: {problem}\n", | ||
"\"\"\"" | ||
], | ||
"metadata": { | ||
"id": "SabVqFgbu_Hd" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"logical_problem = template.format(problem=\"\"\"\n", | ||
"A farmer wants to cross a river and take with him a wolf, a goat and a cabbage.\n", | ||
"He has a boat, but it can only fit himself plus either the wolf, the goat or the cabbage.\n", | ||
"If the wolf and the goat are alone on one shore, the wolf will eat the goat.\n", | ||
"If the goat and the cabbage are alone on the shore, the goat will eat the cabbage.\n", | ||
"How can the farmer bring the wolf, the goat and the cabbage across the river without anything being eaten?\n", | ||
"\"\"\")\n", | ||
"Markdown(model.generate_content(logical_problem).text)" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/", | ||
"height": 510 | ||
}, | ||
"id": "RtT7kGHxvEB_", | ||
"outputId": "984b3113-e59a-4ddf-e993-78fb931a9564" | ||
}, | ||
"execution_count": null, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"<IPython.core.display.Markdown object>" | ||
], | ||
"text/markdown": "**1. Summarize given conditions:**\n\n* The farmer has a wolf, a goat, and a cabbage.\n* The boat can only fit the farmer and one other item (wolf, goat, or cabbage).\n* The wolf will eat the goat if they are alone on one shore.\n* The goat will eat the cabbage if they are alone on one shore.\n\n**2. Identify the problem:**\n\nThe farmer needs to transport the wolf, the goat, and the cabbage across the river without any of them being eaten.\n\n**3. Provide a clear, step-by-step solution:**\n\n1. The farmer takes the goat across the river.\n2. The farmer returns alone.\n3. The farmer takes the cabbage across the river.\n4. The farmer returns with the goat.\n5. The farmer takes the wolf across the river.\n6. The farmer returns alone.\n7. The farmer takes the goat across the river again.\n\n**Explanation:**\n\n* In step 1, the farmer takes the goat across to establish a presence on the other side.\n* In step 2, the farmer returns alone to retrieve the cabbage.\n* In step 3, the farmer takes the cabbage across, leaving the goat on the other side.\n* In step 4, the farmer returns with the goat to bring it back to the original side.\n* In step 5, the farmer takes the wolf across, leaving the cabbage on the other side.\n* In step 6, the farmer returns alone to retrieve the goat.\n* In step 7, the farmer takes the goat across again to reunite it with the wolf and the cabbage on the other side.\n\nBy following these steps, the farmer successfully transports the wolf, the goat, and the cabbage across the river without any of them being eaten." | ||
}, | ||
"metadata": {}, | ||
"execution_count": 6 | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"math_problem = template.format(problem=\"Given a triangle with base 𝑏=6 and height h=8, calculate its area\")\n", | ||
"Markdown(model.generate_content(math_problem).text)" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/", | ||
"height": 256 | ||
}, | ||
"id": "u2ZDzuOYvMMy", | ||
"outputId": "a3885a3f-3811-4327-9ee2-11c510780054" | ||
}, | ||
"execution_count": null, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"<IPython.core.display.Markdown object>" | ||
], | ||
"text/markdown": "**1. Summarize given conditions:**\n- Triangle with base 𝑏=6\n- Height h=8\n\n**2. Identify the problem:**\n- Calculate the area of the triangle\n\n**3. Provide a clear, step-by-step solution:**\n- The area of a triangle is given by the formula: Area = (1/2) * base * height\n- Substituting the given values: Area = (1/2) * 6 * 8\n- Simplifying: Area = 3 * 8\n- Area = 24 square units\n\n**Therefore, the area of the triangle is 24 square units.**" | ||
}, | ||
"metadata": {}, | ||
"execution_count": 7 | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"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": { | ||
"id": "4gJJr5AM5Bwl" | ||
} | ||
} | ||
] | ||
} |