Skip to content

Commit

Permalink
Add prompting example for self-ask (#168)
Browse files Browse the repository at this point in the history
* Add example for adding context information for prompting

* delete

* add self ask for prompting

* Update self ask notebook to have an answer
  • Loading branch information
shilpakancharla committed Jun 18, 2024
1 parent 01ac3d0 commit ea90527
Showing 1 changed file with 104 additions and 90 deletions.
194 changes: 104 additions & 90 deletions examples/prompting/Self_ask_prompting.ipynb
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "sP8PQnz1QrcF"
},
"source": [
"##### Copyright 2024 Google LLC."
]
],
"metadata": {
"id": "d2PtAueID-nE"
}
},
{
"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",
"# @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",
Expand All @@ -29,48 +37,66 @@
"# 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."
]
],
"metadata": {
"id": "bipCP1xBEAJF"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "993efe4a45ee"
},
"source": [
"# Gemini API: Self-ask prompting"
]
],
"metadata": {
"id": "sP8PQnz1QrcF"
}
},
{
"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/Self_ask_prompting.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",
"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."
]
],
"metadata": {
"id": "ysy--KfNRrCq"
}
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "Ne-3gnXqR0hI"
},
"outputs": [],
"source": [
"!pip install -U -q google-generativeai"
],
"metadata": {
"id": "Ne-3gnXqR0hI",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "047a609f-de91-4143-aadf-faf231671d79"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"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[32m30.7/158.8 kB\u001b[0m \u001b[31m829.7 kB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━\u001b[0m \u001b[32m153.6/158.8 kB\u001b[0m \u001b[31m2.4 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"
]
}
]
},
{
Expand All @@ -88,101 +114,89 @@
},
{
"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."
]
],
"metadata": {
"id": "eomJzCa6lb90"
}
},
{
"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)"
]
],
"metadata": {
"id": "v-JZzORUpVR2"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "yQnqEPjephXi"
},
"source": [
"## Example"
]
],
"metadata": {
"id": "yQnqEPjephXi"
}
},
{
"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": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt = \"\"\"\n",
"Query: Who was the president of the united states when Mozart died?\n",
"Question: 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",
"Question: Where did the Emperor of Japan, who ruled the year Maria Skłodowska was born, die?\"\"\"\n",
"model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest')\n",
"Markdown(model.generate_content(prompt).text)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 210
},
"id": "XEfLLHa7pjC8",
"outputId": "fcba7e92-32ae-49a9-ca75-3da2137351b3"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "Let's break this down step by step:\n\n**1. When was Maria Skłodowska born?** \n\n* Maria Skłodowska (Marie Curie) was born on November 7, 1867.\n\n**2. Who was the Emperor of Japan in 1867?**\n\n* The Emperor of Japan in 1867 was **Emperor Meiji**, who reigned from 1867 to 1912.\n\n**3. Where did Emperor Meiji die?**\n\n* Emperor Meiji died in **Tokyo, Japan** on July 30, 1912.\n\n**Final Answer:** The Emperor of Japan who ruled the year Maria Skłodowska was born, Emperor Meiji, died in Tokyo, Japan. \n"
},
"metadata": {},
"execution_count": 8
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1RtZ1y-IpcnV"
},
"source": [
"\n",
"## 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"
"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.\n",
"\n",
"For a related example, please see the [Search re-ranking using Gemini embeddings](https://github.com/google-gemini/cookbook/blob/22ba52659005defc53ce2d6717fb9fedf1d661f1/examples/Search_reranking_using_embeddings.ipynb) example in the Gemini Cookbook."
],
"metadata": {
"id": "1RtZ1y-IpcnV"
}
}
},
"nbformat": 4,
"nbformat_minor": 0
}
]
}

0 comments on commit ea90527

Please sign in to comment.