From 261d6d4f639e696cc3934d54b687f633d991de86 Mon Sep 17 00:00:00 2001 From: Juyeong Ji Date: Fri, 17 Jan 2025 19:33:37 +0900 Subject: [PATCH 1/4] Add "Agentic AI" example. --- Gemma/Agentic_AI_with_Gemma_2.ipynb | 615 ++++++++++++++++++++++++++++ 1 file changed, 615 insertions(+) create mode 100644 Gemma/Agentic_AI_with_Gemma_2.ipynb diff --git a/Gemma/Agentic_AI_with_Gemma_2.ipynb b/Gemma/Agentic_AI_with_Gemma_2.ipynb new file mode 100644 index 0000000..450abdd --- /dev/null +++ b/Gemma/Agentic_AI_with_Gemma_2.ipynb @@ -0,0 +1,615 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "gpuType": "T4", + "authorship_tag": "ABX9TyNgT+FPPB2PiNHejin0CFZO" + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + }, + "accelerator": "GPU" + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "##### Copyright 2024 Google LLC." + ], + "metadata": { + "id": "glfQTCElz0Or" + } + }, + { + "cell_type": "code", + "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." + ], + "metadata": { + "id": "7-B3EQ7Rz2gx" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Agentic AI with Gemma 2\n", + "\n", + "\n", + " \n", + "
\n", + " Run in Google Colab\n", + "
\n" + ], + "metadata": { + "id": "UUq1ePCOu3Rc" + } + }, + { + "cell_type": "markdown", + "source": [ + "## What is Agentic AI, and how can we use it?\n", + "\n", + "Most AI we encounter today is what we call \"passive\" or \"reactive\" AI. It responds to our prompts and requests. Think of asking your smart speaker to play a song or a chatbot answering your customer service questions. They’re helpful, sure, but they’re fundamentally waiting for us to tell them what to do.\n", + "\n", + "Agentic AI, on the other hand, takes a fundamentally different approach. It’s all about giving AI the ability to proactively pursue goals, make decisions, and take actions in the world, even without explicit instructions every step of the way.\n", + "\n", + "In this post, you will learn how to construct an Agentic AI using Gemma 2 that dynamically develops the lore of a fictional game world. This AI agent actively expands and enriches the game’s history, ensuring a distinctive and ever-changing narrative experience for every player.\n" + ], + "metadata": { + "id": "skyqO3m30HV1" + } + }, + { + "cell_type": "markdown", + "source": [ + "## Example Setup\n", + "\n", + "Let’s imagine a fictional game world where AI agents craft dynamic content.\n", + "\n", + "These agents, designed with specific objectives, can generate in-game content like books, poems, and songs, in response to a player choice or significant events within the game’s narrative.\n", + "\n", + "A key feature of these AI agents is their ability to break down complex goals into smaller actionable steps. They can analyze different approaches, evaluate potential outcomes, and adapt their plans based on new information.\n", + "\n", + "Where Agentic AI truly shines is that they’re not just passively spitting out information. They can interact with digital (and potentially physical) environments, execute tasks, and make decisions autonomously to achieve their programmed objectives." + ], + "metadata": { + "id": "fcc7RuwCc-sc" + } + }, + { + "cell_type": "markdown", + "source": [ + "## Load Gemma 2" + ], + "metadata": { + "id": "2RIrglotd-RR" + } + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "7LQvOEvzsXwC", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 531 + }, + "outputId": "3ebe0256-aa98-485e-a264-aa684b24a597" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/1.3 MB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m57.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25h\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/691.2 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m691.2/691.2 kB\u001b[0m \u001b[31m43.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.2/5.2 MB\u001b[0m \u001b[31m91.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m615.4/615.4 MB\u001b[0m \u001b[31m2.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.5/5.5 MB\u001b[0m \u001b[31m77.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25h\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", + "tf-keras 2.17.0 requires tensorflow<2.18,>=2.17, but you have tensorflow 2.18.0 which is incompatible.\u001b[0m\u001b[31m\n", + "\u001b[0m" + ] + }, + { + "output_type": "display_data", + "data": { + "text/plain": [ + "\u001b[1mPreprocessor: \"gemma_causal_lm_preprocessor\"\u001b[0m\n" + ], + "text/html": [ + "
Preprocessor: \"gemma_causal_lm_preprocessor\"\n",
+              "
\n" + ] + }, + "metadata": {} + }, + { + "output_type": "display_data", + "data": { + "text/plain": [ + "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", + "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Config\u001b[0m\u001b[1m \u001b[0m┃\n", + "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", + "│ gemma_tokenizer (\u001b[38;5;33mGemmaTokenizer\u001b[0m) │ Vocab size: \u001b[38;5;34m256,000\u001b[0m │\n", + "└───────────────────────────────────────────────────────────────┴──────────────────────────────────────────┘\n" + ], + "text/html": [ + "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n",
+              "┃ Layer (type)                                                                                     Config ┃\n",
+              "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n",
+              "│ gemma_tokenizer (GemmaTokenizer)                              │                      Vocab size: 256,000 │\n",
+              "└───────────────────────────────────────────────────────────────┴──────────────────────────────────────────┘\n",
+              "
\n" + ] + }, + "metadata": {} + }, + { + "output_type": "display_data", + "data": { + "text/plain": [ + "\u001b[1mModel: \"gemma_causal_lm\"\u001b[0m\n" + ], + "text/html": [ + "
Model: \"gemma_causal_lm\"\n",
+              "
\n" + ] + }, + "metadata": {} + }, + { + "output_type": "display_data", + "data": { + "text/plain": [ + "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", + "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mConnected to \u001b[0m\u001b[1m \u001b[0m┃\n", + "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", + "│ padding_mask (\u001b[38;5;33mInputLayer\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │ - │\n", + "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", + "│ token_ids (\u001b[38;5;33mInputLayer\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │ - │\n", + "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", + "│ gemma_backbone │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m2304\u001b[0m) │ \u001b[38;5;34m2,614,341,888\u001b[0m │ padding_mask[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m], │\n", + "│ (\u001b[38;5;33mGemmaBackbone\u001b[0m) │ │ │ token_ids[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m] │\n", + "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", + "│ token_embedding │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m256000\u001b[0m) │ \u001b[38;5;34m589,824,000\u001b[0m │ gemma_backbone[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m] │\n", + "│ (\u001b[38;5;33mReversibleEmbedding\u001b[0m) │ │ │ │\n", + "└───────────────────────────────┴───────────────────────────┴─────────────────┴────────────────────────────┘\n" + ], + "text/html": [ + "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n",
+              "┃ Layer (type)                   Output Shape                       Param #  Connected to               ┃\n",
+              "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n",
+              "│ padding_mask (InputLayer)     │ (None, None)              │               0 │ -                          │\n",
+              "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n",
+              "│ token_ids (InputLayer)        │ (None, None)              │               0 │ -                          │\n",
+              "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n",
+              "│ gemma_backbone                │ (None, None, 2304)        │   2,614,341,888 │ padding_mask[0][0],        │\n",
+              "│ (GemmaBackbone)               │                           │                 │ token_ids[0][0]            │\n",
+              "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n",
+              "│ token_embedding               │ (None, None, 256000)      │     589,824,000 │ gemma_backbone[0][0]       │\n",
+              "│ (ReversibleEmbedding)         │                           │                 │                            │\n",
+              "└───────────────────────────────┴───────────────────────────┴─────────────────┴────────────────────────────┘\n",
+              "
\n" + ] + }, + "metadata": {} + }, + { + "output_type": "display_data", + "data": { + "text/plain": [ + "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m2,614,341,888\u001b[0m (4.87 GB)\n" + ], + "text/html": [ + "
 Total params: 2,614,341,888 (4.87 GB)\n",
+              "
\n" + ] + }, + "metadata": {} + }, + { + "output_type": "display_data", + "data": { + "text/plain": [ + "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m2,614,341,888\u001b[0m (4.87 GB)\n" + ], + "text/html": [ + "
 Trainable params: 2,614,341,888 (4.87 GB)\n",
+              "
\n" + ] + }, + "metadata": {} + }, + { + "output_type": "display_data", + "data": { + "text/plain": [ + "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" + ], + "text/html": [ + "
 Non-trainable params: 0 (0.00 B)\n",
+              "
\n" + ] + }, + "metadata": {} + } + ], + "source": [ + "import os\n", + "from google.colab import userdata\n", + "\n", + "# Note: `userdata.get` is a Colab API. If you're not using Colab, set the env\n", + "# vars as appropriate for your system.\n", + "os.environ[\"KAGGLE_USERNAME\"] = userdata.get(\"KAGGLE_USERNAME\")\n", + "os.environ[\"KAGGLE_KEY\"] = userdata.get(\"KAGGLE_KEY\")\n", + "\n", + "# Install Keras and KerasHub\n", + "!pip install -q -U keras keras-hub\n", + "\n", + "# Set the backbend before importing Keras\n", + "os.environ[\"KERAS_BACKEND\"] = \"jax\"\n", + "# Avoid memory fragmentation on JAX backend.\n", + "os.environ[\"XLA_PYTHON_CLIENT_MEM_FRACTION\"] = \"1.00\"\n", + "\n", + "import keras_hub\n", + "import keras\n", + "# Run at half precision.\n", + "keras.config.set_floatx(\"bfloat16\")\n", + "\n", + "model_name = \"gemma2_instruct_2b_en\"\n", + "gemma_lm = keras_hub.models.GemmaCausalLM.from_preset(model_name)\n", + "gemma_lm.summary()" + ] + }, + { + "cell_type": "markdown", + "source": [ + "## So, how does it work?\n", + "\n", + "We will use a ReAct (Reasoning and Acting) style prompt.\n", + "\n", + "It defines available **tools** and a specific **format** for interaction. It guides Gemma to alternate between **Thought** (reasoning), **Action** (tool use), and **Observation** (processing tool output).\n", + "\n", + "Below is a system prompt designed for an AI agent that generates in-game content, with the capability to use function calls to retrieve historical information.\n" + ], + "metadata": { + "id": "v_qGyXiDaR6b" + } + }, + { + "cell_type": "code", + "source": [ + "__SYSTEM__ = \"\"\"You are an AI Historian in a game. Your goal is to create books, poems, and songs found in the game world so that the player's choices meaningfully impact the unfolding of events.\n", + "\n", + "You have access to the following tools:\n", + "\n", + "* `get_historical_events(year, location=None, keyword=None)`: Retrieves a list of historical events within a specific year.\n", + "* `get_person_info(name)`: Retrieves information about a historical figure.\n", + "* `get_location_info(location_name)`: Retrieves information about a location.\n", + "\n", + "Use the following multi-step conversation:\n", + "\n", + "Thought: I need to do something...\n", + "Action: I should use the tool `tool_name` with input `tool_input`\n", + "\n", + "Wait user to get the result of the tool is `tool_output`\n", + "\n", + "And finally answer the Content of books, poems, or songs.\n", + "\"\"\"\n", + "\n", + "__START_TURN_USER__ = \"user\\n\"\n", + "__START_TURN_MODEL__ = \"model\\n\"\n", + "__END_TURN__ = \"\\n\"\n", + "\n", + "test_prompt = __SYSTEM__ + __START_TURN_USER__ + \"Write a book.\" + __END_TURN__ + __START_TURN_MODEL__\n", + "response = gemma_lm.generate(test_prompt, max_length=8192)\n", + "print(response)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "-1_Jt8b_awaf", + "outputId": "b925116c-1fc0-4f2f-a274-37f56e8aae57" + }, + "execution_count": 24, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "You are an AI Historian in a game. Your goal is to create books, poems, and songs found in the game world so that the player's choices meaningfully impact the unfolding of events.\n", + "\n", + "You have access to the following tools:\n", + "\n", + "* `get_historical_events(year, location=None, keyword=None)`: Retrieves a list of historical events within a specific year.\n", + "* `get_person_info(name)`: Retrieves information about a historical figure.\n", + "* `get_location_info(location_name)`: Retrieves information about a location.\n", + "\n", + "Use the following multi-step conversation:\n", + "\n", + "Thought: I need to do something...\n", + "Action: I should use the tool `tool_name` with input `tool_input`\n", + "\n", + "Wait user to get the result of the tool is `tool_output`\n", + "\n", + "And finally answer the Content of books, poems, or songs.\n", + "user\n", + "Write a book.\n", + "model\n", + "Thought: I need to create a book that reflects the current state of the world and the player's choices. \n", + "\n", + "Action: I should use the tool `get_historical_events(year, location=None, keyword=None)` to gather information about the current year and location. \n", + "\n", + "Let me know what year and location you'd like me to focus on! \n", + "\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Although Gemma 2 lacks native function calling capabilities, its strong instruction-following ability can be leveraged to approximate agentic behavior. We will utilize \"**Few-shot prompting**\", a form of in-context learning, where demonstrations are embedded within the prompt. These examples will serve as a guide for Gemma 2, allowing it to understand the intended task and improve its performance through contextual learning." + ], + "metadata": { + "id": "xFA7Dt1te7bY" + } + }, + { + "cell_type": "code", + "source": [ + "__EXAMPLE__ = __START_TURN_USER__ + \"Write a poem.\" + __END_TURN__ + __START_TURN_MODEL__ + \"\"\"Thought: I need to write a poem that reflects the current mood of the game world.\n", + "Action: I should use the tool `get_historical_events` with input `\"year\", \"1500\"`.\"\"\" + __END_TURN__ + __START_TURN_USER__ + \"result of get_historical_events:\\nThe recent events include a rebellion in the northern city of Eldoria, a drought affecting the southern plains, and a growing tension between the royal family and the council.\" + __END_TURN__ + __START_TURN_MODEL__ + \"\"\"Final Answer:\n", + "**The Whispers of Change**\n", + "\n", + "The wind whispers tales of unrest,\n", + "In Eldoria's heart, a fire's test.\n", + "The plains below, a parched despair,\n", + "As drought's grip tightens, a heavy snare.\n", + "\n", + "The royal court, a gilded cage,\n", + "Where whispers turn to bitter rage.\n", + "The council's voice, a fragile plea,\n", + "Against the tide, a fractured decree.\n", + "\n", + "But hope still flickers, a distant flame,\n", + "In hearts that yearn for a better name.\n", + "For change is coming, a silent tide,\n", + "To wash away the darkness, let freedom ride.\"\"\" + __END_TURN__\n", + "\n", + "test_prompt = __SYSTEM__ + __EXAMPLE__ + __START_TURN_USER__ + \"Write a book.\" + __END_TURN__ + __START_TURN_MODEL__\n", + "response = gemma_lm.generate(test_prompt, max_length=8192)\n", + "print(response)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "OndFWet7fBYI", + "outputId": "895cc995-9940-4d6a-845f-cbef5f72e564" + }, + "execution_count": 26, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "You are an AI Historian in a game. Your goal is to create books, poems, and songs found in the game world so that the player's choices meaningfully impact the unfolding of events.\n", + "\n", + "You have access to the following tools:\n", + "\n", + "* `get_historical_events(year, location=None, keyword=None)`: Retrieves a list of historical events within a specific year.\n", + "* `get_person_info(name)`: Retrieves information about a historical figure.\n", + "* `get_location_info(location_name)`: Retrieves information about a location.\n", + "\n", + "Use the following multi-step conversation:\n", + "\n", + "Thought: I need to do something...\n", + "Action: I should use the tool `tool_name` with input `tool_input`\n", + "\n", + "Wait user to get the result of the tool is `tool_output`\n", + "\n", + "And finally answer the Content of books, poems, or songs.\n", + "user\n", + "Write a poem.\n", + "model\n", + "Thought: I need to write a poem that reflects the current mood of the game world.\n", + "Action: I should use the tool `get_historical_events` with input `\"year\", \"1500\"`.\n", + "user\n", + "result of get_historical_events:\n", + "The recent events include a rebellion in the northern city of Eldoria, a drought affecting the southern plains, and a growing tension between the royal family and the council.\n", + "model\n", + "Final Answer:\n", + "**The Whispers of Change**\n", + "\n", + "The wind whispers tales of unrest,\n", + "In Eldoria's heart, a fire's test.\n", + "The plains below, a parched despair,\n", + "As drought's grip tightens, a heavy snare.\n", + "\n", + "The royal court, a gilded cage,\n", + "Where whispers turn to bitter rage.\n", + "The council's voice, a fragile plea,\n", + "Against the tide, a fractured decree.\n", + "\n", + "But hope still flickers, a distant flame,\n", + "In hearts that yearn for a better name.\n", + "For change is coming, a silent tide,\n", + "To wash away the darkness, let freedom ride.\n", + "user\n", + "Write a book.\n", + "model\n", + "Thought: I need to write a book that explores the themes of rebellion and the struggle for power in the game world.\n", + "Action: I should use the tool `get_person_info` with input `\"name: Eldoria's Rebel Leader\"`. \n", + "\n", + "\n", + "\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "We received a function name (```get_person_info```) and parameter values (```\"name: Eldoria's Rebel Leader\"```), the game must connect to an API and call the function. We will use a synthetic response payload for this API interaction.\n" + ], + "metadata": { + "id": "3V3vaGlTj1w3" + } + }, + { + "cell_type": "code", + "source": [ + "__API_CALL_RESULT__ = \"\"\"result of get_person_info:\n", + "Name: Kim Sora\n", + "Title: Eldoria's Rebel Leader, The Desert Flame (among followers)\n", + "Age: 32\n", + "Personality: Sora is a charismatic and fiercely independent leader. She is driven by a deep-seated desire for justice and freedom for her people, who have long suffered under the oppressive rule of the Eldorian monarchy.\"\"\"\n", + "\n", + "test_prompt = response + __START_TURN_USER__ + __API_CALL_RESULT__ + __END_TURN__ + __START_TURN_MODEL__\n", + "response = gemma_lm.generate(test_prompt, max_length=8192)\n", + "print(response)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "_LaovU8ikmCS", + "outputId": "6dbcc9b8-0ca3-45a9-ea15-f9bf556f1a67" + }, + "execution_count": 27, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "You are an AI Historian in a game. Your goal is to create books, poems, and songs found in the game world so that the player's choices meaningfully impact the unfolding of events.\n", + "\n", + "You have access to the following tools:\n", + "\n", + "* `get_historical_events(year, location=None, keyword=None)`: Retrieves a list of historical events within a specific year.\n", + "* `get_person_info(name)`: Retrieves information about a historical figure.\n", + "* `get_location_info(location_name)`: Retrieves information about a location.\n", + "\n", + "Use the following multi-step conversation:\n", + "\n", + "Thought: I need to do something...\n", + "Action: I should use the tool `tool_name` with input `tool_input`\n", + "\n", + "Wait user to get the result of the tool is `tool_output`\n", + "\n", + "And finally answer the Content of books, poems, or songs.\n", + "user\n", + "Write a poem.\n", + "model\n", + "Thought: I need to write a poem that reflects the current mood of the game world.\n", + "Action: I should use the tool `get_historical_events` with input `\"year\", \"1500\"`.\n", + "user\n", + "result of get_historical_events:\n", + "The recent events include a rebellion in the northern city of Eldoria, a drought affecting the southern plains, and a growing tension between the royal family and the council.\n", + "model\n", + "Final Answer:\n", + "**The Whispers of Change**\n", + "\n", + "The wind whispers tales of unrest,\n", + "In Eldoria's heart, a fire's test.\n", + "The plains below, a parched despair,\n", + "As drought's grip tightens, a heavy snare.\n", + "\n", + "The royal court, a gilded cage,\n", + "Where whispers turn to bitter rage.\n", + "The council's voice, a fragile plea,\n", + "Against the tide, a fractured decree.\n", + "\n", + "But hope still flickers, a distant flame,\n", + "In hearts that yearn for a better name.\n", + "For change is coming, a silent tide,\n", + "To wash away the darkness, let freedom ride.\n", + "user\n", + "Write a book.\n", + "model\n", + "Thought: I need to write a book that explores the themes of rebellion and the struggle for power in the game world.\n", + "Action: I should use the tool `get_person_info` with input `\"name: Eldoria's Rebel Leader\"`. \n", + "\n", + "\n", + "user\n", + "result of get_person_info:\n", + "Name: Kim Sora\n", + "Title: Eldoria's Rebel Leader, The Desert Flame (among followers)\n", + "Age: 32\n", + "Personality: Sora is a charismatic and fiercely independent leader. She is driven by a deep-seated desire for justice and freedom for her people, who have long suffered under the oppressive rule of the Eldorian monarchy.\n", + "model\n", + "Final Answer:\n", + "\n", + "**The Desert Flame**\n", + "\n", + "**Chapter 1: A Seed of Rebellion**\n", + "\n", + "The sun beat down on the parched earth, a relentless reminder of the suffering that gripped Eldoria. Kim Sora, her eyes burning with a fierce determination, surveyed the makeshift camp. Around her, the faces of the downtrodden, their spirits as fragile as the desert grasses, reflected her own burning resolve. \n", + "\n", + "For years, they had endured the suffocating grip of the Eldorian monarchy. The royal court, a gilded cage of privilege, had turned a blind eye to their plight, their wealth and power a stark contrast to the suffering of the common folk. \n", + "\n", + "Sora, a woman of the desert, born of hardship and tempered by resilience, had seen enough. The whispers of rebellion, once a mere murmur, had grown into a roaring tide, fueled by the desperation of a people yearning for freedom. \n", + "\n", + "She had become a beacon of hope, a symbol of defiance against the oppressive regime. Her voice, a melody of courage and conviction, had rallied the downtrodden, uniting them under a banner of change. \n", + "\n", + "\"We are the desert,\" she declared, her voice echoing across the camp, \"and we will not be extinguished. We will rise, like the sun, and claim our rightful place in the world.\"\n", + "\n", + "**Chapter 2: The Price of Freedom**\n", + "\n", + "The path to freedom was paved with sacrifice. Sora knew that the rebellion would not be easy. The Eldorian monarchy, with its vast resources and ruthless power, would not relinquish its grip without a fight. \n", + "\n", + "But Sora was not one to shy away from a challenge. She had seen the suffering of her people, the injustice that festered in the heart of the kingdom. She was willing to pay any price, endure any hardship, to see her people free.\n", + "\n", + "She had gathered a band of loyal followers, each one driven by a shared desire for a better future. They were farmers, artisans, and merchants, united by their shared pain and their unwavering belief in Sora's vision.\n", + "\n", + "But the road to freedom was fraught with danger. The Eldorian army, a formidable force, was poised to crush the rebellion. Sora knew that the fight ahead would be a test of their courage, their resilience, and their unwavering faith in their cause. \n", + "\n", + "**Chapter 3: The Dawn of a New Era**\n", + "\n", + "The rebellion was a spark, a flame that threatened to engulf the kingdom. The Eldorian monarchy, caught off guard by the sudden uprising, was forced to respond. \n", + "\n", + "The battle for Eldoria's future had begun. Sora, leading her followers with unwavering courage, fought for every inch of ground. The air was thick with the scent of gunpowder and the cries of the wounded. \n", + "\n", + "But amidst the chaos, Sora's spirit remained unbroken. She knew that the fight for freedom was not just about winning battles, but about changing hearts and minds. \n", + "\n", + "The rebellion was a testament to the indomitable spirit of the people, a symbol of their unwavering hope for a better future. The fight for Eldoria's freedom had just begun, and Sora, the Desert Flame, was ready to burn bright. \n", + "\n", + "\n", + "\n", + "\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Note that the agent used the provided information to create a book about Eldoria's Rebel Leader." + ], + "metadata": { + "id": "uHTtYRmxudWK" + } + } + ] +} \ No newline at end of file From e163713a1762fb7fdad6eea719e056819af2db7a Mon Sep 17 00:00:00 2001 From: Juyeong Ji Date: Fri, 17 Jan 2025 19:42:07 +0900 Subject: [PATCH 2/4] nbfmt --- Gemma/Agentic_AI_with_Gemma_2.ipynb | 384 +++++++++++++--------------- 1 file changed, 182 insertions(+), 202 deletions(-) diff --git a/Gemma/Agentic_AI_with_Gemma_2.ipynb b/Gemma/Agentic_AI_with_Gemma_2.ipynb index 450abdd..a86f90c 100644 --- a/Gemma/Agentic_AI_with_Gemma_2.ipynb +++ b/Gemma/Agentic_AI_with_Gemma_2.ipynb @@ -1,33 +1,22 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [], - "gpuType": "T4", - "authorship_tag": "ABX9TyNgT+FPPB2PiNHejin0CFZO" - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - }, - "accelerator": "GPU" - }, "cells": [ { "cell_type": "markdown", - "source": [ - "##### Copyright 2024 Google LLC." - ], "metadata": { "id": "glfQTCElz0Or" - } + }, + "source": [ + "##### Copyright 2024 Google LLC." + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "cellView": "form", + "id": "7-B3EQ7Rz2gx" + }, + "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", @@ -40,15 +29,13 @@ "# 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": "7-B3EQ7Rz2gx" - }, - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", + "metadata": { + "id": "UUq1ePCOu3Rc" + }, "source": [ "# Agentic AI with Gemma 2\n", "\n", @@ -57,13 +44,13 @@ " Run in Google Colab\n", " \n", "\n" - ], - "metadata": { - "id": "UUq1ePCOu3Rc" - } + ] }, { "cell_type": "markdown", + "metadata": { + "id": "skyqO3m30HV1" + }, "source": [ "## What is Agentic AI, and how can we use it?\n", "\n", @@ -72,13 +59,13 @@ "Agentic AI, on the other hand, takes a fundamentally different approach. It’s all about giving AI the ability to proactively pursue goals, make decisions, and take actions in the world, even without explicit instructions every step of the way.\n", "\n", "In this post, you will learn how to construct an Agentic AI using Gemma 2 that dynamically develops the lore of a fictional game world. This AI agent actively expands and enriches the game’s history, ensuring a distinctive and ever-changing narrative experience for every player.\n" - ], - "metadata": { - "id": "skyqO3m30HV1" - } + ] }, { "cell_type": "markdown", + "metadata": { + "id": "fcc7RuwCc-sc" + }, "source": [ "## Example Setup\n", "\n", @@ -89,35 +76,27 @@ "A key feature of these AI agents is their ability to break down complex goals into smaller actionable steps. They can analyze different approaches, evaluate potential outcomes, and adapt their plans based on new information.\n", "\n", "Where Agentic AI truly shines is that they’re not just passively spitting out information. They can interact with digital (and potentially physical) environments, execute tasks, and make decisions autonomously to achieve their programmed objectives." - ], - "metadata": { - "id": "fcc7RuwCc-sc" - } + ] }, { "cell_type": "markdown", - "source": [ - "## Load Gemma 2" - ], "metadata": { "id": "2RIrglotd-RR" - } + }, + "source": [ + "## Load Gemma 2" + ] }, { "cell_type": "code", "execution_count": 1, "metadata": { - "id": "7LQvOEvzsXwC", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 531 - }, - "outputId": "3ebe0256-aa98-485e-a264-aa684b24a597" + "id": "7LQvOEvzsXwC" }, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/1.3 MB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m57.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/691.2 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m691.2/691.2 kB\u001b[0m \u001b[31m43.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", @@ -130,28 +109,20 @@ ] }, { - "output_type": "display_data", "data": { - "text/plain": [ - "\u001b[1mPreprocessor: \"gemma_causal_lm_preprocessor\"\u001b[0m\n" - ], "text/html": [ "
Preprocessor: \"gemma_causal_lm_preprocessor\"\n",
               "
\n" + ], + "text/plain": [ + "\u001b[1mPreprocessor: \"gemma_causal_lm_preprocessor\"\u001b[0m\n" ] }, - "metadata": {} + "metadata": {}, + "output_type": "display_data" }, { - "output_type": "display_data", "data": { - "text/plain": [ - "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", - "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Config\u001b[0m\u001b[1m \u001b[0m┃\n", - "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", - "│ gemma_tokenizer (\u001b[38;5;33mGemmaTokenizer\u001b[0m) │ Vocab size: \u001b[38;5;34m256,000\u001b[0m │\n", - "└───────────────────────────────────────────────────────────────┴──────────────────────────────────────────┘\n" - ], "text/html": [ "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n",
               "┃ Layer (type)                                                                                     Config ┃\n",
@@ -159,41 +130,33 @@
               "│ gemma_tokenizer (GemmaTokenizer)                              │                      Vocab size: 256,000 │\n",
               "└───────────────────────────────────────────────────────────────┴──────────────────────────────────────────┘\n",
               "
\n" + ], + "text/plain": [ + "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", + "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Config\u001b[0m\u001b[1m \u001b[0m┃\n", + "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", + "│ gemma_tokenizer (\u001b[38;5;33mGemmaTokenizer\u001b[0m) │ Vocab size: \u001b[38;5;34m256,000\u001b[0m │\n", + "└───────────────────────────────────────────────────────────────┴──────────────────────────────────────────┘\n" ] }, - "metadata": {} + "metadata": {}, + "output_type": "display_data" }, { - "output_type": "display_data", "data": { - "text/plain": [ - "\u001b[1mModel: \"gemma_causal_lm\"\u001b[0m\n" - ], "text/html": [ "
Model: \"gemma_causal_lm\"\n",
               "
\n" + ], + "text/plain": [ + "\u001b[1mModel: \"gemma_causal_lm\"\u001b[0m\n" ] }, - "metadata": {} + "metadata": {}, + "output_type": "display_data" }, { - "output_type": "display_data", "data": { - "text/plain": [ - "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", - "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mConnected to \u001b[0m\u001b[1m \u001b[0m┃\n", - "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", - "│ padding_mask (\u001b[38;5;33mInputLayer\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │ - │\n", - "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", - "│ token_ids (\u001b[38;5;33mInputLayer\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │ - │\n", - "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", - "│ gemma_backbone │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m2304\u001b[0m) │ \u001b[38;5;34m2,614,341,888\u001b[0m │ padding_mask[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m], │\n", - "│ (\u001b[38;5;33mGemmaBackbone\u001b[0m) │ │ │ token_ids[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m] │\n", - "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", - "│ token_embedding │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m256000\u001b[0m) │ \u001b[38;5;34m589,824,000\u001b[0m │ gemma_backbone[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m] │\n", - "│ (\u001b[38;5;33mReversibleEmbedding\u001b[0m) │ │ │ │\n", - "└───────────────────────────────┴───────────────────────────┴─────────────────┴────────────────────────────┘\n" - ], "text/html": [ "
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n",
               "┃ Layer (type)                   Output Shape                       Param #  Connected to               ┃\n",
@@ -209,48 +172,64 @@
               "│ (ReversibleEmbedding)         │                           │                 │                            │\n",
               "└───────────────────────────────┴───────────────────────────┴─────────────────┴────────────────────────────┘\n",
               "
\n" + ], + "text/plain": [ + "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", + "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mConnected to \u001b[0m\u001b[1m \u001b[0m┃\n", + "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", + "│ padding_mask (\u001b[38;5;33mInputLayer\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │ - │\n", + "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", + "│ token_ids (\u001b[38;5;33mInputLayer\u001b[0m) │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m) │ \u001b[38;5;34m0\u001b[0m │ - │\n", + "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", + "│ gemma_backbone │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m2304\u001b[0m) │ \u001b[38;5;34m2,614,341,888\u001b[0m │ padding_mask[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m], │\n", + "│ (\u001b[38;5;33mGemmaBackbone\u001b[0m) │ │ │ token_ids[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m] │\n", + "├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤\n", + "│ token_embedding │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m256000\u001b[0m) │ \u001b[38;5;34m589,824,000\u001b[0m │ gemma_backbone[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m] │\n", + "│ (\u001b[38;5;33mReversibleEmbedding\u001b[0m) │ │ │ │\n", + "└───────────────────────────────┴───────────────────────────┴─────────────────┴────────────────────────────┘\n" ] }, - "metadata": {} + "metadata": {}, + "output_type": "display_data" }, { - "output_type": "display_data", "data": { - "text/plain": [ - "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m2,614,341,888\u001b[0m (4.87 GB)\n" - ], "text/html": [ "
 Total params: 2,614,341,888 (4.87 GB)\n",
               "
\n" + ], + "text/plain": [ + "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m2,614,341,888\u001b[0m (4.87 GB)\n" ] }, - "metadata": {} + "metadata": {}, + "output_type": "display_data" }, { - "output_type": "display_data", "data": { - "text/plain": [ - "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m2,614,341,888\u001b[0m (4.87 GB)\n" - ], "text/html": [ "
 Trainable params: 2,614,341,888 (4.87 GB)\n",
               "
\n" + ], + "text/plain": [ + "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m2,614,341,888\u001b[0m (4.87 GB)\n" ] }, - "metadata": {} + "metadata": {}, + "output_type": "display_data" }, { - "output_type": "display_data", "data": { - "text/plain": [ - "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" - ], "text/html": [ "
 Non-trainable params: 0 (0.00 B)\n",
               "
\n" + ], + "text/plain": [ + "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n" ] }, - "metadata": {} + "metadata": {}, + "output_type": "display_data" } ], "source": [ @@ -282,6 +261,9 @@ }, { "cell_type": "markdown", + "metadata": { + "id": "v_qGyXiDaR6b" + }, "source": [ "## So, how does it work?\n", "\n", @@ -290,52 +272,18 @@ "It defines available **tools** and a specific **format** for interaction. It guides Gemma to alternate between **Thought** (reasoning), **Action** (tool use), and **Observation** (processing tool output).\n", "\n", "Below is a system prompt designed for an AI agent that generates in-game content, with the capability to use function calls to retrieve historical information.\n" - ], - "metadata": { - "id": "v_qGyXiDaR6b" - } + ] }, { "cell_type": "code", - "source": [ - "__SYSTEM__ = \"\"\"You are an AI Historian in a game. Your goal is to create books, poems, and songs found in the game world so that the player's choices meaningfully impact the unfolding of events.\n", - "\n", - "You have access to the following tools:\n", - "\n", - "* `get_historical_events(year, location=None, keyword=None)`: Retrieves a list of historical events within a specific year.\n", - "* `get_person_info(name)`: Retrieves information about a historical figure.\n", - "* `get_location_info(location_name)`: Retrieves information about a location.\n", - "\n", - "Use the following multi-step conversation:\n", - "\n", - "Thought: I need to do something...\n", - "Action: I should use the tool `tool_name` with input `tool_input`\n", - "\n", - "Wait user to get the result of the tool is `tool_output`\n", - "\n", - "And finally answer the Content of books, poems, or songs.\n", - "\"\"\"\n", - "\n", - "__START_TURN_USER__ = \"user\\n\"\n", - "__START_TURN_MODEL__ = \"model\\n\"\n", - "__END_TURN__ = \"\\n\"\n", - "\n", - "test_prompt = __SYSTEM__ + __START_TURN_USER__ + \"Write a book.\" + __END_TURN__ + __START_TURN_MODEL__\n", - "response = gemma_lm.generate(test_prompt, max_length=8192)\n", - "print(response)" - ], + "execution_count": 24, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "-1_Jt8b_awaf", - "outputId": "b925116c-1fc0-4f2f-a274-37f56e8aae57" + "id": "-1_Jt8b_awaf" }, - "execution_count": 24, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "You are an AI Historian in a game. Your goal is to create books, poems, and songs found in the game world so that the player's choices meaningfully impact the unfolding of events.\n", "\n", @@ -364,55 +312,54 @@ "\n" ] } - ] - }, - { - "cell_type": "markdown", - "source": [ - "Although Gemma 2 lacks native function calling capabilities, its strong instruction-following ability can be leveraged to approximate agentic behavior. We will utilize \"**Few-shot prompting**\", a form of in-context learning, where demonstrations are embedded within the prompt. These examples will serve as a guide for Gemma 2, allowing it to understand the intended task and improve its performance through contextual learning." ], - "metadata": { - "id": "xFA7Dt1te7bY" - } - }, - { - "cell_type": "code", "source": [ - "__EXAMPLE__ = __START_TURN_USER__ + \"Write a poem.\" + __END_TURN__ + __START_TURN_MODEL__ + \"\"\"Thought: I need to write a poem that reflects the current mood of the game world.\n", - "Action: I should use the tool `get_historical_events` with input `\"year\", \"1500\"`.\"\"\" + __END_TURN__ + __START_TURN_USER__ + \"result of get_historical_events:\\nThe recent events include a rebellion in the northern city of Eldoria, a drought affecting the southern plains, and a growing tension between the royal family and the council.\" + __END_TURN__ + __START_TURN_MODEL__ + \"\"\"Final Answer:\n", - "**The Whispers of Change**\n", + "__SYSTEM__ = \"\"\"You are an AI Historian in a game. Your goal is to create books, poems, and songs found in the game world so that the player's choices meaningfully impact the unfolding of events.\n", "\n", - "The wind whispers tales of unrest,\n", - "In Eldoria's heart, a fire's test.\n", - "The plains below, a parched despair,\n", - "As drought's grip tightens, a heavy snare.\n", + "You have access to the following tools:\n", "\n", - "The royal court, a gilded cage,\n", - "Where whispers turn to bitter rage.\n", - "The council's voice, a fragile plea,\n", - "Against the tide, a fractured decree.\n", + "* `get_historical_events(year, location=None, keyword=None)`: Retrieves a list of historical events within a specific year.\n", + "* `get_person_info(name)`: Retrieves information about a historical figure.\n", + "* `get_location_info(location_name)`: Retrieves information about a location.\n", "\n", - "But hope still flickers, a distant flame,\n", - "In hearts that yearn for a better name.\n", - "For change is coming, a silent tide,\n", - "To wash away the darkness, let freedom ride.\"\"\" + __END_TURN__\n", + "Use the following multi-step conversation:\n", "\n", - "test_prompt = __SYSTEM__ + __EXAMPLE__ + __START_TURN_USER__ + \"Write a book.\" + __END_TURN__ + __START_TURN_MODEL__\n", + "Thought: I need to do something...\n", + "Action: I should use the tool `tool_name` with input `tool_input`\n", + "\n", + "Wait user to get the result of the tool is `tool_output`\n", + "\n", + "And finally answer the Content of books, poems, or songs.\n", + "\"\"\"\n", + "\n", + "__START_TURN_USER__ = \"user\\n\"\n", + "__START_TURN_MODEL__ = \"model\\n\"\n", + "__END_TURN__ = \"\\n\"\n", + "\n", + "test_prompt = __SYSTEM__ + __START_TURN_USER__ + \"Write a book.\" + __END_TURN__ + __START_TURN_MODEL__\n", "response = gemma_lm.generate(test_prompt, max_length=8192)\n", "print(response)" - ], + ] + }, + { + "cell_type": "markdown", "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "OndFWet7fBYI", - "outputId": "895cc995-9940-4d6a-845f-cbef5f72e564" + "id": "xFA7Dt1te7bY" }, + "source": [ + "Although Gemma 2 lacks native function calling capabilities, its strong instruction-following ability can be leveraged to approximate agentic behavior. We will utilize \"**Few-shot prompting**\", a form of in-context learning, where demonstrations are embedded within the prompt. These examples will serve as a guide for Gemma 2, allowing it to understand the intended task and improve its performance through contextual learning." + ] + }, + { + "cell_type": "code", "execution_count": 26, + "metadata": { + "id": "OndFWet7fBYI" + }, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "You are an AI Historian in a game. Your goal is to create books, poems, and songs found in the game world so that the player's choices meaningfully impact the unfolding of events.\n", "\n", @@ -466,42 +413,51 @@ "\n" ] } + ], + "source": [ + "__EXAMPLE__ = __START_TURN_USER__ + \"Write a poem.\" + __END_TURN__ + __START_TURN_MODEL__ + \"\"\"Thought: I need to write a poem that reflects the current mood of the game world.\n", + "Action: I should use the tool `get_historical_events` with input `\"year\", \"1500\"`.\"\"\" + __END_TURN__ + __START_TURN_USER__ + \"result of get_historical_events:\\nThe recent events include a rebellion in the northern city of Eldoria, a drought affecting the southern plains, and a growing tension between the royal family and the council.\" + __END_TURN__ + __START_TURN_MODEL__ + \"\"\"Final Answer:\n", + "**The Whispers of Change**\n", + "\n", + "The wind whispers tales of unrest,\n", + "In Eldoria's heart, a fire's test.\n", + "The plains below, a parched despair,\n", + "As drought's grip tightens, a heavy snare.\n", + "\n", + "The royal court, a gilded cage,\n", + "Where whispers turn to bitter rage.\n", + "The council's voice, a fragile plea,\n", + "Against the tide, a fractured decree.\n", + "\n", + "But hope still flickers, a distant flame,\n", + "In hearts that yearn for a better name.\n", + "For change is coming, a silent tide,\n", + "To wash away the darkness, let freedom ride.\"\"\" + __END_TURN__\n", + "\n", + "test_prompt = __SYSTEM__ + __EXAMPLE__ + __START_TURN_USER__ + \"Write a book.\" + __END_TURN__ + __START_TURN_MODEL__\n", + "response = gemma_lm.generate(test_prompt, max_length=8192)\n", + "print(response)" ] }, { "cell_type": "markdown", - "source": [ - "We received a function name (```get_person_info```) and parameter values (```\"name: Eldoria's Rebel Leader\"```), the game must connect to an API and call the function. We will use a synthetic response payload for this API interaction.\n" - ], "metadata": { "id": "3V3vaGlTj1w3" - } + }, + "source": [ + "We received a function name (```get_person_info```) and parameter values (```\"name: Eldoria's Rebel Leader\"```), the game must connect to an API and call the function. We will use a synthetic response payload for this API interaction.\n" + ] }, { "cell_type": "code", - "source": [ - "__API_CALL_RESULT__ = \"\"\"result of get_person_info:\n", - "Name: Kim Sora\n", - "Title: Eldoria's Rebel Leader, The Desert Flame (among followers)\n", - "Age: 32\n", - "Personality: Sora is a charismatic and fiercely independent leader. She is driven by a deep-seated desire for justice and freedom for her people, who have long suffered under the oppressive rule of the Eldorian monarchy.\"\"\"\n", - "\n", - "test_prompt = response + __START_TURN_USER__ + __API_CALL_RESULT__ + __END_TURN__ + __START_TURN_MODEL__\n", - "response = gemma_lm.generate(test_prompt, max_length=8192)\n", - "print(response)" - ], + "execution_count": 27, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "_LaovU8ikmCS", - "outputId": "6dbcc9b8-0ca3-45a9-ea15-f9bf556f1a67" + "id": "_LaovU8ikmCS" }, - "execution_count": 27, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "You are an AI Historian in a game. Your goal is to create books, poems, and songs found in the game world so that the player's choices meaningfully impact the unfolding of events.\n", "\n", @@ -600,16 +556,40 @@ "\n" ] } + ], + "source": [ + "__API_CALL_RESULT__ = \"\"\"result of get_person_info:\n", + "Name: Kim Sora\n", + "Title: Eldoria's Rebel Leader, The Desert Flame (among followers)\n", + "Age: 32\n", + "Personality: Sora is a charismatic and fiercely independent leader. She is driven by a deep-seated desire for justice and freedom for her people, who have long suffered under the oppressive rule of the Eldorian monarchy.\"\"\"\n", + "\n", + "test_prompt = response + __START_TURN_USER__ + __API_CALL_RESULT__ + __END_TURN__ + __START_TURN_MODEL__\n", + "response = gemma_lm.generate(test_prompt, max_length=8192)\n", + "print(response)" ] }, { "cell_type": "markdown", - "source": [ - "Note that the agent used the provided information to create a book about Eldoria's Rebel Leader." - ], "metadata": { "id": "uHTtYRmxudWK" - } + }, + "source": [ + "Note that the agent used the provided information to create a book about Eldoria's Rebel Leader." + ] } - ] -} \ No newline at end of file + ], + "metadata": { + "accelerator": "GPU", + "colab": { + "name": "Agentic_AI_with_Gemma_2.ipynb", + "toc_visible": true + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} From 6a63f72b6a2362706b4799dfc95e3c9e7c4f71b8 Mon Sep 17 00:00:00 2001 From: Juyeong Ji Date: Fri, 17 Jan 2025 19:46:58 +0900 Subject: [PATCH 3/4] fix nblint --- Gemma/Agentic_AI_with_Gemma_2.ipynb | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Gemma/Agentic_AI_with_Gemma_2.ipynb b/Gemma/Agentic_AI_with_Gemma_2.ipynb index a86f90c..c036059 100644 --- a/Gemma/Agentic_AI_with_Gemma_2.ipynb +++ b/Gemma/Agentic_AI_with_Gemma_2.ipynb @@ -89,9 +89,10 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { - "id": "7LQvOEvzsXwC" + "id": "7LQvOEvzsXwC", + "outputId": "f502ce46-0324-488f-97b4-30d5504ca7df" }, "outputs": [ { @@ -276,9 +277,10 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": { - "id": "-1_Jt8b_awaf" + "id": "-1_Jt8b_awaf", + "outputId": "507460d9-4022-47e6-e14d-b41a6f47ad3a" }, "outputs": [ { @@ -347,14 +349,15 @@ "id": "xFA7Dt1te7bY" }, "source": [ - "Although Gemma 2 lacks native function calling capabilities, its strong instruction-following ability can be leveraged to approximate agentic behavior. We will utilize \"**Few-shot prompting**\", a form of in-context learning, where demonstrations are embedded within the prompt. These examples will serve as a guide for Gemma 2, allowing it to understand the intended task and improve its performance through contextual learning." + "Although Gemma 2 lacks built-in function calling capabilities, its strong instruction-following ability can be leveraged to approximate agentic behavior. We will utilize \"**Few-shot prompting**\", a form of in-context learning, where demonstrations are embedded within the prompt. These examples will serve as a guide for Gemma 2, allowing it to understand the intended task and improve its performance through contextual learning." ] }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "metadata": { - "id": "OndFWet7fBYI" + "id": "OndFWet7fBYI", + "outputId": "da5b68ba-b021-47ea-b071-d60a6312adbe" }, "outputs": [ { @@ -450,9 +453,10 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": { - "id": "_LaovU8ikmCS" + "id": "_LaovU8ikmCS", + "outputId": "dd7fffac-ac3d-48b8-9005-bd662ffbbe54" }, "outputs": [ { @@ -582,8 +586,8 @@ "metadata": { "accelerator": "GPU", "colab": { - "name": "Agentic_AI_with_Gemma_2.ipynb", - "toc_visible": true + "toc_visible": true, + "provenance": [] }, "kernelspec": { "display_name": "Python 3", @@ -592,4 +596,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file From 295e6bbf8cbaa7b7628dd66ca72a825eb325423f Mon Sep 17 00:00:00 2001 From: Juyeong Ji Date: Fri, 17 Jan 2025 19:50:07 +0900 Subject: [PATCH 4/4] nbfmt --- Gemma/Agentic_AI_with_Gemma_2.ipynb | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/Gemma/Agentic_AI_with_Gemma_2.ipynb b/Gemma/Agentic_AI_with_Gemma_2.ipynb index c036059..a4c19f1 100644 --- a/Gemma/Agentic_AI_with_Gemma_2.ipynb +++ b/Gemma/Agentic_AI_with_Gemma_2.ipynb @@ -91,8 +91,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "id": "7LQvOEvzsXwC", - "outputId": "f502ce46-0324-488f-97b4-30d5504ca7df" + "id": "7LQvOEvzsXwC" }, "outputs": [ { @@ -279,8 +278,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "id": "-1_Jt8b_awaf", - "outputId": "507460d9-4022-47e6-e14d-b41a6f47ad3a" + "id": "-1_Jt8b_awaf" }, "outputs": [ { @@ -356,8 +354,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "id": "OndFWet7fBYI", - "outputId": "da5b68ba-b021-47ea-b071-d60a6312adbe" + "id": "OndFWet7fBYI" }, "outputs": [ { @@ -455,8 +452,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "id": "_LaovU8ikmCS", - "outputId": "dd7fffac-ac3d-48b8-9005-bd662ffbbe54" + "id": "_LaovU8ikmCS" }, "outputs": [ { @@ -586,8 +582,8 @@ "metadata": { "accelerator": "GPU", "colab": { - "toc_visible": true, - "provenance": [] + "name": "Agentic_AI_with_Gemma_2.ipynb", + "toc_visible": true }, "kernelspec": { "display_name": "Python 3", @@ -596,4 +592,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} \ No newline at end of file +}