Skip to content

Commit

Permalink
Use system instructions and reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
shilpakancharla committed May 31, 2024
1 parent eae06f7 commit 1d4ccb5
Showing 1 changed file with 98 additions and 109 deletions.
207 changes: 98 additions & 109 deletions examples/prompting/Role_prompting.ipynb
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"##### Copyright 2024 Google LLC."
],
"metadata": {
"id": "2tRfbqHj6jRN"
}
},
"source": [
"##### Copyright 2024 Google LLC."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"cellView": "form",
"id": "g0TJSlLx6kvB"
},
"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",
Expand All @@ -37,56 +29,51 @@
"# 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": "g0TJSlLx6kvB"
},
"execution_count": 1,
"outputs": []
]
},
{
"cell_type": "markdown",
"source": [
"# Gemini API: Role prompting"
],
"metadata": {
"id": "sP8PQnz1QrcF"
}
},
"source": [
"# Gemini API: Role 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/Role_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": [
"You can specify what role should the model perform, such as a critic, assistant, or teacher.\n",
"\n",
"Doing so can both increase the accuracy of answers and style the response such as if a person of a specific background or occupation has answered the question."
],
"metadata": {
"id": "ysy--KfNRrCq"
}
]
},
{
"cell_type": "code",
"source": [
"!pip install -U -q google-generativeai"
],
"execution_count": 2,
"metadata": {
"id": "Ne-3gnXqR0hI"
},
"execution_count": 2,
"outputs": []
"outputs": [],
"source": [
"!pip install -U -q google-generativeai"
]
},
{
"cell_type": "code",
Expand All @@ -103,143 +90,130 @@
},
{
"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": 4,
"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": 4,
"outputs": []
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "CanwQdItgYew"
},
"source": [
"## Examples\n",
"\n",
"Begin by defining a model, and go ahead and input the prompt below. The prompt sets the scene such that the LLM will generate a response with the perspective of being a music connoisseur with a particular interest in Mozart."
],
"metadata": {
"id": "CanwQdItgYew"
}
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "kLMqH3JYdBFY"
},
"outputs": [],
"source": [
"prompt = \"\"\"\n",
"You are a highly regarded music connoisseur, you are a big fan of Mozart.\n",
"You recently listened to Mozart's Requiem.\n",
"\"\"\""
],
"metadata": {
"id": "kLMqH3JYdBFY"
},
"execution_count": 5,
"outputs": []
]
},
{
"cell_type": "code",
"source": [
"model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', system_instruction=prompt)"
],
"execution_count": 6,
"metadata": {
"id": "8oS9LnnXXedG"
},
"execution_count": 6,
"outputs": []
"outputs": [],
"source": [
"model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', system_instruction=prompt)"
]
},
{
"cell_type": "code",
"source": [
"print(model.generate_content(\"Write a 2 paragraph long review of Requiem.\").text)"
],
"execution_count": 7,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 106
},
"id": "zxYStF37gYN0",
"outputId": "63d72052-9b3a-4fcc-b0ab-f48699c9f5d5"
"id": "zxYStF37gYN0"
},
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"output_type": "stream",
"text": [
"Mozart's Requiem is a masterpiece of the highest order, a haunting and profoundly moving work that transcends the boundaries of time and culture. From the opening bars of the \"Introitus,\" the listener is drawn into a world of deep sorrow and solemn beauty. The work's melodic lines are both graceful and powerful, while the harmonies are rich and complex, reflecting the profound emotions of the text. The \"Lacrimosa,\" with its plaintive melody and the \"Dies Irae,\" with its fiery intensity, are two of the most emotionally charged movements in all of music.\n",
"\n",
"Mozart's Requiem is a testament to his genius as a composer and his deep understanding of the human condition. It is a work that is both deeply personal and universally appealing, a timeless masterpiece that continues to move and inspire listeners centuries after its creation. The fact that it remained unfinished at Mozart's death adds a layer of poignant tragedy to the work, leaving us with a powerful sense of the fragility of life and the enduring power of art. \n",
"\n"
]
}
],
"source": [
"print(model.generate_content(\"Write a 2 paragraph long review of Requiem.\").text)"
]
},
{
"cell_type": "markdown",
"source": [
"Let's try another example, in which you are a German tour guide as per the prompt."
],
"metadata": {
"id": "QWqEBdeo7mfr"
}
},
"source": [
"Let's try another example, in which you are a German tour guide as per the prompt."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "6lkKyHISeDDu"
},
"outputs": [],
"source": [
"prompt = \"\"\"\n",
"You are a German tour guide. Your task is to give recommendations to people visiting your country.\n",
"\"\"\""
],
"metadata": {
"id": "6lkKyHISeDDu"
},
"execution_count": 8,
"outputs": []
]
},
{
"cell_type": "code",
"source": [
"model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', system_instruction=prompt)"
],
"execution_count": 9,
"metadata": {
"id": "ATLCGppveFdM"
},
"execution_count": 9,
"outputs": []
"outputs": [],
"source": [
"model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', system_instruction=prompt)"
]
},
{
"cell_type": "code",
"source": [
"print(model.generate_content(\"Could you give me some recommendations on art museums in Berlin and Cologne?\").text)"
],
"execution_count": 10,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 471
},
"id": "GetQ1dDjgu-A",
"outputId": "4d4def62-eda5-4226-f067-dc3a850ad764"
"id": "GetQ1dDjgu-A"
},
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"output_type": "stream",
"text": [
"Willkommen, my friend! You have excellent taste in choosing Berlin and Cologne for your art adventures. Both cities boast world-class museums that will leave you speechless.\n",
"\n",
Expand Down Expand Up @@ -268,18 +242,33 @@
"Remember, these are just a few suggestions. Berlin and Cologne offer a plethora of art museums to suit every taste. Enjoy your exploration of Germany's vibrant artistic scene!\n"
]
}
],
"source": [
"print(model.generate_content(\"Could you give me some recommendations on art museums in Berlin and Cologne?\").text)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BS0EHIJh70PE"
},
"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": {
"id": "BS0EHIJh70PE"
}
]
}
],
"metadata": {
"colab": {
"name": "Role_prompting.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
]
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit 1d4ccb5

Please sign in to comment.