From 55662181136c4b8e8cab71bd67decd6144f46739 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Tue, 21 May 2024 14:24:23 -0700 Subject: [PATCH 01/11] Add example for adding context information for prompting --- .../Adding_context_information.ipynb | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 examples/prompting/Adding_context_information.ipynb diff --git a/examples/prompting/Adding_context_information.ipynb b/examples/prompting/Adding_context_information.ipynb new file mode 100644 index 000000000..eb5b62ac6 --- /dev/null +++ b/examples/prompting/Adding_context_information.ipynb @@ -0,0 +1,152 @@ +{ + "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: Adding context information" + ], + "metadata": { + "id": "sP8PQnz1QrcF" + } + }, + { + "cell_type": "markdown", + "source": [ + "\n", + " \n", + "
\n", + " Run in Google Colab\n", + "
" + ], + "metadata": { + "id": "bxGr_x3MRA0z" + } + }, + { + "cell_type": "markdown", + "source": [ + "While LLMs are trained extensively on various documents and data, the LLM does not know everything. New information or information that is not easily accessible cannot be known by the LLM, unless it was specifically added to its corpus of knowledge somehow. For this reason, it is sometimes necessary to provide the LLM, with information and context necessary to answer our queries by providing additional context." + ], + "metadata": { + "id": "ysy--KfNRrCq" + } + }, + { + "cell_type": "code", + "source": [ + "!pip install -U -q google-generativeai" + ], + "metadata": { + "id": "Ne-3gnXqR0hI" + }, + "execution_count": 1, + "outputs": [] + }, + { + "cell_type": "code", + "execution_count": 2, + "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": 3, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Example" + ], + "metadata": { + "id": "JljcHgI2ltTY" + } + }, + { + "cell_type": "code", + "source": [ + "# the list as of April 2024\n", + "prompt = \"\"\"\n", + "QUERY: provide a list of atheletes that competed in olympics exactly 9 times.\n", + "CONTEXT:\n", + "Ian Millar, 10\n", + "Hubert Raudaschl, 9\n", + "Afanasijs Kuzmins, 9\n", + "Nino Salukvadze, 9\n", + "Piero d'Inzeo, 8\n", + "Raimondo d'Inzeo, 8\n", + "Claudia Pechstein, 8\n", + "Jaqueline Mourão, 8\n", + "Ivan Osiier, 7\n", + "François Lafortune, Jr, 7\n", + "\n", + "ANSWER:\"\"\"\n", + "model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', generation_config={\"temperature\": 0})\n", + "Markdown(model.generate_content(prompt).text)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 104 + }, + "id": "uFcm6Dd7ls_F", + "outputId": "37628141-885c-4cc4-dcd4-3c340af3a574" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ], + "text/markdown": "The list you provided already includes all the athletes who competed in the Olympics exactly 9 times:\n\n* **Hubert Raudaschl**\n* **Afanasijs Kuzmins**\n* **Nino Salukvadze** \n" + }, + "metadata": {}, + "execution_count": 5 + } + ] + } + ] +} \ No newline at end of file From ae93721521bd103a8a23cac9b5576e97082c9033 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Tue, 21 May 2024 14:38:48 -0700 Subject: [PATCH 02/11] delete --- .../Adding_context_information.ipynb | 152 ------------------ 1 file changed, 152 deletions(-) delete mode 100644 examples/prompting/Adding_context_information.ipynb diff --git a/examples/prompting/Adding_context_information.ipynb b/examples/prompting/Adding_context_information.ipynb deleted file mode 100644 index eb5b62ac6..000000000 --- a/examples/prompting/Adding_context_information.ipynb +++ /dev/null @@ -1,152 +0,0 @@ -{ - "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: Adding context information" - ], - "metadata": { - "id": "sP8PQnz1QrcF" - } - }, - { - "cell_type": "markdown", - "source": [ - "\n", - " \n", - "
\n", - " Run in Google Colab\n", - "
" - ], - "metadata": { - "id": "bxGr_x3MRA0z" - } - }, - { - "cell_type": "markdown", - "source": [ - "While LLMs are trained extensively on various documents and data, the LLM does not know everything. New information or information that is not easily accessible cannot be known by the LLM, unless it was specifically added to its corpus of knowledge somehow. For this reason, it is sometimes necessary to provide the LLM, with information and context necessary to answer our queries by providing additional context." - ], - "metadata": { - "id": "ysy--KfNRrCq" - } - }, - { - "cell_type": "code", - "source": [ - "!pip install -U -q google-generativeai" - ], - "metadata": { - "id": "Ne-3gnXqR0hI" - }, - "execution_count": 1, - "outputs": [] - }, - { - "cell_type": "code", - "execution_count": 2, - "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": 3, - "outputs": [] - }, - { - "cell_type": "markdown", - "source": [ - "## Example" - ], - "metadata": { - "id": "JljcHgI2ltTY" - } - }, - { - "cell_type": "code", - "source": [ - "# the list as of April 2024\n", - "prompt = \"\"\"\n", - "QUERY: provide a list of atheletes that competed in olympics exactly 9 times.\n", - "CONTEXT:\n", - "Ian Millar, 10\n", - "Hubert Raudaschl, 9\n", - "Afanasijs Kuzmins, 9\n", - "Nino Salukvadze, 9\n", - "Piero d'Inzeo, 8\n", - "Raimondo d'Inzeo, 8\n", - "Claudia Pechstein, 8\n", - "Jaqueline Mourão, 8\n", - "Ivan Osiier, 7\n", - "François Lafortune, Jr, 7\n", - "\n", - "ANSWER:\"\"\"\n", - "model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', generation_config={\"temperature\": 0})\n", - "Markdown(model.generate_content(prompt).text)" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 104 - }, - "id": "uFcm6Dd7ls_F", - "outputId": "37628141-885c-4cc4-dcd4-3c340af3a574" - }, - "execution_count": 5, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "" - ], - "text/markdown": "The list you provided already includes all the athletes who competed in the Olympics exactly 9 times:\n\n* **Hubert Raudaschl**\n* **Afanasijs Kuzmins**\n* **Nino Salukvadze** \n" - }, - "metadata": {}, - "execution_count": 5 - } - ] - } - ] -} \ No newline at end of file From f225e24a0183dfca362f0645daa7c9525b916775 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Tue, 28 May 2024 12:44:28 -0700 Subject: [PATCH 03/11] Add text summarization JSON example --- .../Text_Summarization.ipynb | 270 ++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 examples/json_capabilities/Text_Summarization.ipynb diff --git a/examples/json_capabilities/Text_Summarization.ipynb b/examples/json_capabilities/Text_Summarization.ipynb new file mode 100644 index 000000000..b5aa6477f --- /dev/null +++ b/examples/json_capabilities/Text_Summarization.ipynb @@ -0,0 +1,270 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "sP8PQnz1QrcF" + }, + "source": [ + "# Gemini API: Text Summarization\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bxGr_x3MRA0z" + }, + "source": [ + "\n", + " \n", + "
\n", + " Run in Google Colab\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ysy--KfNRrCq" + }, + "source": [ + "You will use Gemini API's JSON capabilities to extract characters, locations, and summary of the plot from a story." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "Ne-3gnXqR0hI" + }, + "outputs": [], + "source": [ + "!pip install -U -q google-generativeai" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "id": "EconMHePQHGw" + }, + "outputs": [], + "source": [ + "import google.generativeai as genai\n", + "\n", + "import json\n", + "from IPython.display import Markdown\n", + "from typing_extensions import TypedDict # in python 3.12 replace typing_extensions with typing" + ] + }, + { + "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": 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)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kNE5XslpPpT0" + }, + "source": [ + "## Example" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 999 + }, + "id": "8V5lHRmkNejR", + "outputId": "f240c0d1-0afc-43be-c831-4c27b847bc61" + }, + "outputs": [ + { + "data": { + "text/markdown": [ + "The air hung heavy with the scent of pine and damp earth as Elara, a young woman with eyes the color of storm clouds and hair like spun moonlight, navigated the winding path through the Whispering Woods. Her boots, worn and patched, crunched on the fallen leaves, the only sound besides the rustling of the ancient trees. She carried a worn leather satchel, its contents a mystery, but its weight a familiar burden on her shoulder. Elara was on a mission, a quest that had taken her far from her home in the bustling city of Aethel, a place of cobblestone streets and towering spires.\n", + "\n", + "Her destination was the hidden village of Eldoria, nestled deep within the heart of the Whispering Woods. Legend spoke of a powerful artifact, the Heart of Eldoria, a crystal said to hold the very essence of the forest. It was said to be guarded by the ancient tree, the Whispering Elder, a towering oak that had stood for centuries, its branches reaching towards the heavens like gnarled fingers. Elara sought the Heart of Eldoria, not for power or glory, but for a chance to heal her ailing mother, who lay dying in Aethel.\n", + "\n", + "As she walked, the shadows of the trees grew longer, stretching out like grasping claws. The air grew colder, and a chill ran down Elara's spine. She quickened her pace, her heart pounding in her chest. The Whispering Woods was known for its dangers, home to creatures of myth and legend, from mischievous sprites to fearsome wolves. But Elara was not afraid. She had faced danger before, and she knew that the Heart of Eldoria was worth the risk.\n", + "\n", + "After what felt like hours, she emerged from the dense forest and into a clearing. In the center stood a small village, its houses built of wood and stone, nestled amongst the trees. The air here was different, filled with a sense of peace and tranquility. Elara felt a sense of relief wash over her. She had reached Eldoria.\n", + "\n", + "As she approached the village, she was greeted by a young man named Kael, his eyes as bright as the sun and his hair the color of burnished copper. He was a woodcarver, his hands calloused and strong, and he wore a smile that could light up the darkest night. He welcomed Elara to Eldoria, offering her food and shelter. He told her of the Whispering Elder and the Heart of Eldoria, and he warned her of the dangers that lurked in the forest.\n", + "\n", + "Elara listened intently, her heart filled with hope. She knew that the Heart of Eldoria was her only chance to save her mother. She thanked Kael for his hospitality and told him of her quest. Kael, touched by her story, offered to help her. He knew the forest well, and he promised to guide her to the Whispering Elder.\n", + "\n", + "Together, they set out into the forest, following a winding path that led them deeper into the woods. The trees grew taller and thicker, their branches intertwining overhead, creating a canopy of darkness. The air grew heavy with the scent of pine and damp earth, and the only sound was the rustling of leaves and the occasional chirp of a bird.\n", + "\n", + "As they walked, Kael told Elara stories of the forest, tales of ancient spirits and forgotten magic. He spoke of the Whispering Elder, a tree that was said to be wise beyond measure, its roots reaching deep into the earth, its branches touching the sky. He told her that the Heart of Eldoria was a powerful artifact, capable of healing the sick and restoring life to the dying.\n", + "\n", + "Elara listened intently, her heart filled with hope. She knew that the Heart of Eldoria was her only chance to save her mother. She thanked Kael for his stories and his guidance, and she vowed to find the Heart of Eldoria and bring it back to Aethel.\n", + "\n", + "Finally, they reached the Whispering Elder, a towering oak that stood in the center of a clearing. Its branches were gnarled and twisted, its bark rough and ancient. The tree seemed to hum with a life force, its leaves rustling softly in the breeze. Elara felt a sense of awe wash over her as she stood before the Whispering Elder.\n", + "\n", + "Kael stepped back, leaving Elara to face the ancient tree alone. He knew that the Heart of Eldoria was guarded by the Whispering Elder, and that only those who were pure of heart could approach it. Elara closed her eyes and took a deep breath. She thought of her mother, her face pale and drawn, her voice weak and raspy. She thought of the love she had for her mother, and the pain she felt at seeing her suffer.\n", + "\n", + "When she opened her eyes, she saw a faint glow emanating from the base of the Whispering Elder. It was the Heart of Eldoria, a crystal that pulsed with a soft, ethereal light. Elara reached out and touched the crystal, feeling a surge of energy flow through her. She knew that the Heart of Eldoria was real, and that it held the power to heal her mother.\n", + "\n", + "But as she reached for the crystal, a shadow fell over her. A creature emerged from the darkness, its eyes glowing red, its teeth bared in a snarl. It was a creature of the forest, a guardian of the Whispering Elder, and it was determined to prevent Elara from taking the Heart of Eldoria.\n", + "\n", + "Elara knew that she had to fight. She drew her sword, its blade gleaming in the dim light of the forest. She faced the creature, her heart pounding in her chest. She knew that she was outmatched, but she was determined to protect the Heart of Eldoria and save her mother.\n", + "\n", + "The battle was fierce, the creature's claws slashing at Elara, its teeth snapping at her throat. But Elara fought back with all her strength, her sword flashing in the dim light. She dodged and weaved, her movements swift and precise. She knew that she had to defeat the creature, or she would never be able to take the Heart of Eldoria.\n", + "\n", + "Finally, with a surge of adrenaline, Elara struck. Her sword pierced the creature's heart, and it let out a roar of pain before collapsing to the ground. Elara stood over the creature, her breath coming in ragged gasps. She had won, but the victory came at a cost. She was wounded, her body aching, her clothes torn.\n", + "\n", + "But she had the Heart of Eldoria. She picked up the crystal, its light warming her hand. She knew that she had to return to Aethel, to her mother, and to use the Heart of Eldoria to heal her. She turned and walked away from the Whispering Elder, her heart filled with hope and determination. She knew that she had faced danger and overcome it, and that she would do whatever it took to save her mother.\n", + "\n", + "As she walked back through the Whispering Woods, the shadows seemed to recede, the air grew warmer, and the forest seemed to sigh with relief. Elara knew that she had done something important, something that would change her life forever. She had faced her fears, she had overcome her doubts, and she had found the Heart of Eldoria. Now, she had to bring it back to Aethel and use its power to heal her mother. She knew that the journey would be long and difficult, but she was ready. She was ready to face whatever challenges lay ahead, for she had the Heart of Eldoria, and she had the love of her mother to guide her. \n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', generation_config={\"temperature\": 0})\n", + "prompt = \"Generate a 10 paragraph fantasy story. Include at least 2 named characters and 2 named locations. Give as much detail in the story as possible.\"\n", + "response = model.generate_content(prompt)\n", + "story = response.text\n", + "Markdown(story)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "id": "sShzxm3JNm6M" + }, + "outputs": [], + "source": [ + "class Character(TypedDict):\n", + " name: str\n", + " description: str\n", + " alignment: str\n", + "\n", + "class Location(TypedDict):\n", + " name: str\n", + " description: str\n", + "\n", + "class TextSummary(TypedDict):\n", + " synopsis: str\n", + " genres: list[str]\n", + " locations: list[Location]\n", + " characters: list[Character]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "id": "QGdJnd0AOKbu" + }, + "outputs": [], + "source": [ + "prompt = f\"\"\"\n", + "Generate summary of the story. With a list of genres locations and characters.\n", + "\n", + "{story}\"\"\"\n", + "response = model.generate_content(prompt, generation_config={\"response_mime_type\": \"application/json\", \"response_schema\": TextSummary})" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "d5tOgde6ONo3", + "outputId": "0aa8ce30-c5d3-4177-8a0a-c2d8581685f3" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"summary\": \"Elara, a young woman from the city of Aethel, embarks on a perilous journey to the hidden village of Eldoria in the Whispering Woods. She seeks the Heart of Eldoria, a powerful artifact said to hold the essence of the forest, in hopes of healing her ailing mother. Guided by Kael, a kind woodcarver, Elara faces dangers and battles a guardian creature to obtain the Heart. Ultimately, she triumphs and returns to Aethel with the artifact, ready to face the challenges of healing her mother.\",\n", + " \"genres\": [\n", + " \"Fantasy\",\n", + " \"Adventure\",\n", + " \"Romance\"\n", + " ],\n", + " \"locations\": [\n", + " \"Aethel\",\n", + " \"Whispering Woods\",\n", + " \"Eldoria\"\n", + " ],\n", + " \"characters\": [\n", + " \"Elara\",\n", + " \"Kael\",\n", + " \"Whispering Elder\",\n", + " \"Guardian Creature\"\n", + " ]\n", + "}\n" + ] + } + ], + "source": [ + "print(json.dumps(json.loads(response.text), indent=4))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FhFfzDMk36aI" + }, + "source": [ + "## Summary\n", + "We used Gemini API to extract various information from the story. In this case, we could have done it without the help of the model in no time, but imagine how much time it would save if the text would be 10 times as long or even longer.\n", + "\n", + "Please see the other notebooks in this directory to learn more about how you can use the Gemini API for other JSON related tasks." + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} From a2c334af301e587820f247e0e8a2a61904f7a401 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Tue, 28 May 2024 12:56:27 -0700 Subject: [PATCH 04/11] Reformat notebook --- .../Text_Summarization.ipynb | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/examples/json_capabilities/Text_Summarization.ipynb b/examples/json_capabilities/Text_Summarization.ipynb index b5aa6477f..03b27b041 100644 --- a/examples/json_capabilities/Text_Summarization.ipynb +++ b/examples/json_capabilities/Text_Summarization.ipynb @@ -6,8 +6,7 @@ "id": "sP8PQnz1QrcF" }, "source": [ - "# Gemini API: Text Summarization\n", - "\n" + "# Gemini API: Text Summarization\n" ] }, { @@ -96,12 +95,7 @@ "cell_type": "code", "execution_count": 4, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 999 - }, - "id": "8V5lHRmkNejR", - "outputId": "f240c0d1-0afc-43be-c831-4c27b847bc61" + "id": "8V5lHRmkNejR" }, "outputs": [ { @@ -203,11 +197,7 @@ "cell_type": "code", "execution_count": 7, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "d5tOgde6ONo3", - "outputId": "0aa8ce30-c5d3-4177-8a0a-c2d8581685f3" + "id": "d5tOgde6ONo3" }, "outputs": [ { @@ -255,14 +245,12 @@ ], "metadata": { "colab": { - "provenance": [] + "name": "Text_Summarization.ipynb", + "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" - }, - "language_info": { - "name": "python" } }, "nbformat": 4, From f657cd58747d25b0d48b72cd4febcbe244fdeedf Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Tue, 28 May 2024 12:59:25 -0700 Subject: [PATCH 05/11] Replace we with you --- examples/json_capabilities/Text_Summarization.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/json_capabilities/Text_Summarization.ipynb b/examples/json_capabilities/Text_Summarization.ipynb index 03b27b041..c0d5346aa 100644 --- a/examples/json_capabilities/Text_Summarization.ipynb +++ b/examples/json_capabilities/Text_Summarization.ipynb @@ -237,7 +237,7 @@ }, "source": [ "## Summary\n", - "We used Gemini API to extract various information from the story. In this case, we could have done it without the help of the model in no time, but imagine how much time it would save if the text would be 10 times as long or even longer.\n", + "In this example, you used Gemini API to extract various information from the story. In this case, you could have done it without the help of the model in no time, but imagine how much time it would save if the text would be 10 times as long or even longer.\n", "\n", "Please see the other notebooks in this directory to learn more about how you can use the Gemini API for other JSON related tasks." ] From 7776cac5f90bb101f5fa4dda34b6671723693058 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Tue, 28 May 2024 13:09:58 -0700 Subject: [PATCH 06/11] Add licensing --- .../Text_Summarization.ipynb | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/examples/json_capabilities/Text_Summarization.ipynb b/examples/json_capabilities/Text_Summarization.ipynb index c0d5346aa..ded636d8b 100644 --- a/examples/json_capabilities/Text_Summarization.ipynb +++ b/examples/json_capabilities/Text_Summarization.ipynb @@ -6,7 +6,26 @@ "id": "sP8PQnz1QrcF" }, "source": [ - "# Gemini API: Text Summarization\n" + "##### Copyright 2024 Google LLC." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "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." ] }, { @@ -15,6 +34,8 @@ "id": "bxGr_x3MRA0z" }, "source": [ + "# Gemini API: Text Summarization\n", + "\n", "\n", "
\n", " Run in Google Colab\n", @@ -42,6 +63,13 @@ "!pip install -U -q google-generativeai" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": 2, From 97146b429ffa8776375b38ba1c4fee04f90f1fc4 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Tue, 28 May 2024 13:10:37 -0700 Subject: [PATCH 07/11] update colab button link --- examples/json_capabilities/Text_Summarization.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/json_capabilities/Text_Summarization.ipynb b/examples/json_capabilities/Text_Summarization.ipynb index ded636d8b..f561e0e31 100644 --- a/examples/json_capabilities/Text_Summarization.ipynb +++ b/examples/json_capabilities/Text_Summarization.ipynb @@ -38,7 +38,7 @@ "\n", "\n", " \n", "
\n", - " Run in Google Colab\n", + " Run in Google Colab\n", "
" ] From 94e73e87bd8f99c00da08a1b593db13ddc431663 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Tue, 28 May 2024 13:15:45 -0700 Subject: [PATCH 08/11] Reformat notebook --- examples/json_capabilities/Text_Summarization.ipynb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/examples/json_capabilities/Text_Summarization.ipynb b/examples/json_capabilities/Text_Summarization.ipynb index f561e0e31..3a8212856 100644 --- a/examples/json_capabilities/Text_Summarization.ipynb +++ b/examples/json_capabilities/Text_Summarization.ipynb @@ -12,7 +12,10 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "cellView": "form", + "id": "ca23c3f523a7" + }, "outputs": [], "source": [ "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n", @@ -63,13 +66,6 @@ "!pip install -U -q google-generativeai" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "code", "execution_count": 2, From e1e0285140ec8699747d02c0a21ad7fdf699ef7a Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Tue, 28 May 2024 13:22:19 -0700 Subject: [PATCH 09/11] Reformat notebook --- examples/json_capabilities/Text_Summarization.ipynb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/examples/json_capabilities/Text_Summarization.ipynb b/examples/json_capabilities/Text_Summarization.ipynb index 3a8212856..89e490f3e 100644 --- a/examples/json_capabilities/Text_Summarization.ipynb +++ b/examples/json_capabilities/Text_Summarization.ipynb @@ -46,6 +46,13 @@ "
" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": { From ad8b07a4e63b8be6ae9effbac569f37d45bf993c Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Tue, 28 May 2024 13:25:18 -0700 Subject: [PATCH 10/11] Reformat notebook --- examples/json_capabilities/Text_Summarization.ipynb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/examples/json_capabilities/Text_Summarization.ipynb b/examples/json_capabilities/Text_Summarization.ipynb index 89e490f3e..3a8212856 100644 --- a/examples/json_capabilities/Text_Summarization.ipynb +++ b/examples/json_capabilities/Text_Summarization.ipynb @@ -46,13 +46,6 @@ "" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "metadata": { From dcc89c8120b50e7db77618de82049d534af240e1 Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Wed, 29 May 2024 13:40:49 -0700 Subject: [PATCH 11/11] Use default temperature, use pro --- .../Text_Summarization.ipynb | 99 ++++++++++--------- 1 file changed, 53 insertions(+), 46 deletions(-) diff --git a/examples/json_capabilities/Text_Summarization.ipynb b/examples/json_capabilities/Text_Summarization.ipynb index 3a8212856..7d951120f 100644 --- a/examples/json_capabilities/Text_Summarization.ipynb +++ b/examples/json_capabilities/Text_Summarization.ipynb @@ -61,7 +61,16 @@ "metadata": { "id": "Ne-3gnXqR0hI" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mWARNING: There was an error checking the latest version of pip.\u001b[0m\u001b[33m\n", + "\u001b[0m" + ] + } + ], "source": [ "!pip install -U -q google-generativeai" ] @@ -117,7 +126,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 8, "metadata": { "id": "8V5lHRmkNejR" }, @@ -125,53 +134,39 @@ { "data": { "text/markdown": [ - "The air hung heavy with the scent of pine and damp earth as Elara, a young woman with eyes the color of storm clouds and hair like spun moonlight, navigated the winding path through the Whispering Woods. Her boots, worn and patched, crunched on the fallen leaves, the only sound besides the rustling of the ancient trees. She carried a worn leather satchel, its contents a mystery, but its weight a familiar burden on her shoulder. Elara was on a mission, a quest that had taken her far from her home in the bustling city of Aethel, a place of cobblestone streets and towering spires.\n", - "\n", - "Her destination was the hidden village of Eldoria, nestled deep within the heart of the Whispering Woods. Legend spoke of a powerful artifact, the Heart of Eldoria, a crystal said to hold the very essence of the forest. It was said to be guarded by the ancient tree, the Whispering Elder, a towering oak that had stood for centuries, its branches reaching towards the heavens like gnarled fingers. Elara sought the Heart of Eldoria, not for power or glory, but for a chance to heal her ailing mother, who lay dying in Aethel.\n", - "\n", - "As she walked, the shadows of the trees grew longer, stretching out like grasping claws. The air grew colder, and a chill ran down Elara's spine. She quickened her pace, her heart pounding in her chest. The Whispering Woods was known for its dangers, home to creatures of myth and legend, from mischievous sprites to fearsome wolves. But Elara was not afraid. She had faced danger before, and she knew that the Heart of Eldoria was worth the risk.\n", - "\n", - "After what felt like hours, she emerged from the dense forest and into a clearing. In the center stood a small village, its houses built of wood and stone, nestled amongst the trees. The air here was different, filled with a sense of peace and tranquility. Elara felt a sense of relief wash over her. She had reached Eldoria.\n", - "\n", - "As she approached the village, she was greeted by a young man named Kael, his eyes as bright as the sun and his hair the color of burnished copper. He was a woodcarver, his hands calloused and strong, and he wore a smile that could light up the darkest night. He welcomed Elara to Eldoria, offering her food and shelter. He told her of the Whispering Elder and the Heart of Eldoria, and he warned her of the dangers that lurked in the forest.\n", + "Anya gripped the worn leather strap of her satchel, her knuckles white as pearls against the tanned hide. Wind whipped strands of her raven hair across her face, obscuring the treacherous path that snaked its way down the cliff face towards the Whisperwood below. Legend claimed the forest whispered secrets to those who dared to listen, secrets both wondrous and terrible. Anya, however, only heard the insistent shriek of the wind, a chilling reminder of the urgency of her mission. \n", "\n", - "Elara listened intently, her heart filled with hope. She knew that the Heart of Eldoria was her only chance to save her mother. She thanked Kael for his hospitality and told him of her quest. Kael, touched by her story, offered to help her. He knew the forest well, and he promised to guide her to the Whispering Elder.\n", + "The fate of Eldoria, her homeland, rested on her slender shoulders. She carried within her satchel a vial containing the last shimmering drop of Moonsilver, a mystical elixir capable of healing the Blight that was slowly strangling the life from their world. The Blight, a creeping darkness that withered everything it touched, had originated in the Shadowlands, a desolate realm ruled by the tyrannical sorcerer, Malkor. \n", "\n", - "Together, they set out into the forest, following a winding path that led them deeper into the woods. The trees grew taller and thicker, their branches intertwining overhead, creating a canopy of darkness. The air grew heavy with the scent of pine and damp earth, and the only sound was the rustling of leaves and the occasional chirp of a bird.\n", + "Anya's destination lay hidden deep within the Whisperwood: the Grove of Whispers, a sanctuary guarded by the enigmatic Whisperers, an ancient order sworn to protect the balance of nature. Legend claimed they held the knowledge to activate the Moonsilver, knowledge lost to the rest of Eldoria. Anya prayed the legends held true. She had witnessed firsthand the devastating effects of the Blight: the once vibrant fields now barren wastelands, the vibrant forests reduced to skeletal husks. \n", "\n", - "As they walked, Kael told Elara stories of the forest, tales of ancient spirits and forgotten magic. He spoke of the Whispering Elder, a tree that was said to be wise beyond measure, its roots reaching deep into the earth, its branches touching the sky. He told her that the Heart of Eldoria was a powerful artifact, capable of healing the sick and restoring life to the dying.\n", + "Reaching the forest floor, she pushed through a tangle of thorny vines, their leaves the color of dried blood. An oppressive silence descended, the wind's mournful song swallowed by the dense canopy overhead. Sunlight struggled to penetrate the thick foliage, casting the forest floor in an eerie, perpetual twilight. Anya shivered, pulling her cloak tighter around her against the unnatural chill.\n", "\n", - "Elara listened intently, her heart filled with hope. She knew that the Heart of Eldoria was her only chance to save her mother. She thanked Kael for his stories and his guidance, and she vowed to find the Heart of Eldoria and bring it back to Aethel.\n", + "The forest pulsed with an unseen energy, a living entity that observed her every move with unseen eyes. She could almost feel the whispers brushing against her skin, whispers that seemed to slither into her mind, whispering doubts, fears, temptations. She pressed on, clutching the vial in her satchel like a talisman, her resolve hardening with each step. \n", "\n", - "Finally, they reached the Whispering Elder, a towering oak that stood in the center of a clearing. Its branches were gnarled and twisted, its bark rough and ancient. The tree seemed to hum with a life force, its leaves rustling softly in the breeze. Elara felt a sense of awe wash over her as she stood before the Whispering Elder.\n", + "Days bled into nights and still Anya journeyed deeper into the Whisperwood, her supplies dwindling, her body weary. She subsisted on the meager berries she could find, the sweet juice tinged with a strange bitterness that lingered on her tongue. Sleep offered little respite, haunted by nightmares of the Blight consuming everything she held dear, the chilling laughter of Malkor echoing in her ears. \n", "\n", - "Kael stepped back, leaving Elara to face the ancient tree alone. He knew that the Heart of Eldoria was guarded by the Whispering Elder, and that only those who were pure of heart could approach it. Elara closed her eyes and took a deep breath. She thought of her mother, her face pale and drawn, her voice weak and raspy. She thought of the love she had for her mother, and the pain she felt at seeing her suffer.\n", + "One evening, as the last rays of sunlight painted the forest in hues of gold and crimson, Anya stumbled upon a clearing. In the center stood a majestic oak, its branches reaching skyward like gnarled fingers, its leaves rustling softly despite the absence of wind. At the base of the tree, a lone figure sat cross-legged, seemingly carved from the very wood itself. He wore a robe of woven leaves and bark, his face obscured by a deep hood. \n", "\n", - "When she opened her eyes, she saw a faint glow emanating from the base of the Whispering Elder. It was the Heart of Eldoria, a crystal that pulsed with a soft, ethereal light. Elara reached out and touched the crystal, feeling a surge of energy flow through her. She knew that the Heart of Eldoria was real, and that it held the power to heal her mother.\n", + "\"You seek the Whisperers,\" the figure rasped, his voice like the rustling of dry leaves.\n", "\n", - "But as she reached for the crystal, a shadow fell over her. A creature emerged from the darkness, its eyes glowing red, its teeth bared in a snarl. It was a creature of the forest, a guardian of the Whispering Elder, and it was determined to prevent Elara from taking the Heart of Eldoria.\n", + "Anya's heart leaped. \"I do,\" she breathed, her voice hoarse from exhaustion. \"I am Anya, and I bring with me the last of the Moonsilver. Eldoria… my people… they are dying. We need your help.\"\n", "\n", - "Elara knew that she had to fight. She drew her sword, its blade gleaming in the dim light of the forest. She faced the creature, her heart pounding in her chest. She knew that she was outmatched, but she was determined to protect the Heart of Eldoria and save her mother.\n", + "The figure slowly raised his head, and Anya found herself staring into eyes that glowed with an otherworldly light, eyes as ancient as the forest itself. \"The Grove of Whispers is not a place you find, child,\" he intoned. \"It is a place that finds you.\"\n", "\n", - "The battle was fierce, the creature's claws slashing at Elara, its teeth snapping at her throat. But Elara fought back with all her strength, her sword flashing in the dim light. She dodged and weaved, her movements swift and precise. She knew that she had to defeat the creature, or she would never be able to take the Heart of Eldoria.\n", - "\n", - "Finally, with a surge of adrenaline, Elara struck. Her sword pierced the creature's heart, and it let out a roar of pain before collapsing to the ground. Elara stood over the creature, her breath coming in ragged gasps. She had won, but the victory came at a cost. She was wounded, her body aching, her clothes torn.\n", - "\n", - "But she had the Heart of Eldoria. She picked up the crystal, its light warming her hand. She knew that she had to return to Aethel, to her mother, and to use the Heart of Eldoria to heal her. She turned and walked away from the Whispering Elder, her heart filled with hope and determination. She knew that she had faced danger and overcome it, and that she would do whatever it took to save her mother.\n", - "\n", - "As she walked back through the Whispering Woods, the shadows seemed to recede, the air grew warmer, and the forest seemed to sigh with relief. Elara knew that she had done something important, something that would change her life forever. She had faced her fears, she had overcome her doubts, and she had found the Heart of Eldoria. Now, she had to bring it back to Aethel and use its power to heal her mother. She knew that the journey would be long and difficult, but she was ready. She was ready to face whatever challenges lay ahead, for she had the Heart of Eldoria, and she had the love of her mother to guide her. \n" + "And as the figure, whose name was Silvanus, spoke these words, the forest around them shimmered and shifted. The trees surrounding the clearing parted as if alive, revealing a hidden path, bathed in an ethereal, silver light. Anya's breath caught in her throat, hope blooming in her chest like a fragile flower pushing through the barren earth. This was it. This was her chance to save her world. \n" ], "text/plain": [ "" ] }, - "execution_count": 4, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', generation_config={\"temperature\": 0})\n", + "model = genai.GenerativeModel(model_name='gemini-1.5-pro-latest')\n", "prompt = \"Generate a 10 paragraph fantasy story. Include at least 2 named characters and 2 named locations. Give as much detail in the story as possible.\"\n", "response = model.generate_content(prompt)\n", "story = response.text\n", @@ -180,7 +175,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 9, "metadata": { "id": "sShzxm3JNm6M" }, @@ -204,12 +199,22 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 10, "metadata": { "id": "QGdJnd0AOKbu" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 10.4 ms, sys: 5.78 ms, total: 16.2 ms\n", + "Wall time: 6.02 s\n" + ] + } + ], "source": [ + "%%time\n", "prompt = f\"\"\"\n", "Generate summary of the story. With a list of genres locations and characters.\n", "\n", @@ -219,7 +224,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 12, "metadata": { "id": "d5tOgde6ONo3" }, @@ -229,23 +234,25 @@ "output_type": "stream", "text": [ "{\n", - " \"summary\": \"Elara, a young woman from the city of Aethel, embarks on a perilous journey to the hidden village of Eldoria in the Whispering Woods. She seeks the Heart of Eldoria, a powerful artifact said to hold the essence of the forest, in hopes of healing her ailing mother. Guided by Kael, a kind woodcarver, Elara faces dangers and battles a guardian creature to obtain the Heart. Ultimately, she triumphs and returns to Aethel with the artifact, ready to face the challenges of healing her mother.\",\n", " \"genres\": [\n", - " \"Fantasy\",\n", - " \"Adventure\",\n", - " \"Romance\"\n", + " \"fantasy\",\n", + " \"adventure\"\n", " ],\n", " \"locations\": [\n", - " \"Aethel\",\n", - " \"Whispering Woods\",\n", - " \"Eldoria\"\n", + " {\n", + " \"name\": \"Eldoria\"\n", + " },\n", + " {\n", + " \"name\": \"Whisperwood\"\n", + " },\n", + " {\n", + " \"name\": \"Shadowlands\"\n", + " },\n", + " {\n", + " \"name\": \"Grove of Whispers\"\n", + " }\n", " ],\n", - " \"characters\": [\n", - " \"Elara\",\n", - " \"Kael\",\n", - " \"Whispering Elder\",\n", - " \"Guardian Creature\"\n", - " ]\n", + " \"synopsis\": \"Anya, a young woman from Eldoria, embarks on a perilous journey to the Whisperwood, a mystical forest, to find the Grove of Whispers and seek help from the Whisperers, an ancient order. Her homeland is being consumed by the Blight, a creeping darkness originating from the Shadowlands ruled by the evil sorcerer Malkor. Anya carries the last hope for Eldoria, a vial of Moonsilver, which needs the Whisperers' knowledge to activate. Facing perilous paths, strange whispers, and her own fears, Anya's determination leads her to Silvanus, a figure seemingly a part of the forest itself. Silvanus guides her towards the hidden Grove of Whispers, offering a glimmer of hope for Anya's mission to save her people and her world.\"\n", "}\n" ] }