-
Notifications
You must be signed in to change notification settings - Fork 732
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add role prompting example * Reformat and update role prompting example * Use system instructions * Use system instructions and reformat
- Loading branch information
1 parent
54ac7ec
commit c5eac14
Showing
1 changed file
with
274 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,274 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"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", | ||
"# 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: 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>" | ||
] | ||
}, | ||
{ | ||
"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." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
"id": "Ne-3gnXqR0hI" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"!pip install -U -q google-generativeai" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"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": 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)" | ||
] | ||
}, | ||
{ | ||
"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." | ||
] | ||
}, | ||
{ | ||
"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", | ||
"\"\"\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": { | ||
"id": "8oS9LnnXXedG" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', system_instruction=prompt)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": { | ||
"id": "zxYStF37gYN0" | ||
}, | ||
"outputs": [ | ||
{ | ||
"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", | ||
"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", | ||
"\"\"\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": { | ||
"id": "ATLCGppveFdM" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', system_instruction=prompt)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": { | ||
"id": "GetQ1dDjgu-A" | ||
}, | ||
"outputs": [ | ||
{ | ||
"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", | ||
"**Berlin, a Canvas of History and Innovation:**\n", | ||
"\n", | ||
"* **Museum Island (Museumsinsel):** This UNESCO World Heritage Site is a must-see. You can explore five interconnected museums:\n", | ||
" * **Altes Museum:** Ancient Greek and Roman art, a true time capsule. \n", | ||
" * **Neues Museum:** Egyptian antiquities and pre-history, a feast for the eyes.\n", | ||
" * **Pergamon Museum:** Iconic Pergamon Altar, breathtaking architecture, and Babylonian treasures.\n", | ||
" * **Alte Nationalgalerie:** 19th-century German and European art, from Romanticism to Impressionism.\n", | ||
" * **Bode-Museum:** Byzantine art, sculptures, and coins, a journey through history.\n", | ||
"\n", | ||
"* **Gemäldegalerie:** Home to a stellar collection of European paintings from the 13th to the 18th century, including masterpieces by Rembrandt, Dürer, and Van Eyck. \n", | ||
"* **Hamburger Bahnhof – Museum für Gegenwart:** Contemporary art in a beautiful former railway station. Expect challenging and thought-provoking exhibits.\n", | ||
"* **East Side Gallery:** The longest open-air gallery in the world, where artists painted murals on the Berlin Wall after its fall. A poignant reminder of history and a celebration of freedom.\n", | ||
"\n", | ||
"**Cologne, a Tapestry of Art and Tradition:**\n", | ||
"\n", | ||
"* **Wallraf-Richartz Museum:** A treasure trove of European art from the Middle Ages to the 19th century, featuring works by Dürer, van der Weyden, and Rubens. \n", | ||
"* **Museum Ludwig:** Renowned for its collection of modern and contemporary art, with highlights from Picasso, Warhol, and Beuys. \n", | ||
"* **Museum Schnütgen:** Medieval religious art, sculptures, and textiles, showcasing Cologne's rich history.\n", | ||
"* **Cologne Cathedral:** Although not strictly an art museum, the Cologne Cathedral is an architectural masterpiece. Its stained glass windows and sculptures are a sight to behold. \n", | ||
"\n", | ||
"**A little Tip:** If you have more time, consider visiting the **Kunsthaus Zürich** in Zurich. It's just a short train ride away from Cologne and home to a fantastic collection of modern art.\n", | ||
"\n", | ||
"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": { | ||
"colab": { | ||
"name": "Role_prompting.ipynb", | ||
"toc_visible": true | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"name": "python3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |