{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2bvOXqHoa8fo", "outputId": "1ad82e02-6539-4b37-8baf-e12e1db315ce" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.4/50.4 kB\u001b[0m \u001b[31m440.1 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m377.3/377.3 kB\u001b[0m \u001b[31m11.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m47.1/47.1 kB\u001b[0m \u001b[31m2.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m295.8/295.8 kB\u001b[0m \u001b[31m13.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m139.8/139.8 kB\u001b[0m \u001b[31m9.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m337.0/337.0 kB\u001b[0m \u001b[31m17.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m29.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m5.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m5.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m141.1/141.1 kB\u001b[0m \u001b[31m8.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m2.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Building wheel for clipboard (setup.py) ... \u001b[?25l\u001b[?25hdone\n" ] } ], "source": [ "! pip3 install -qU markdownify langchain-upstage rank_bm25 getpass4" ] }, { "cell_type": "code", "source": [ "import os\n", "import getpass\n", "import warnings\n", "warnings.filterwarnings(\"ignore\")\n", "\n", "UPSTAGE_API_KEY = getpass.getpass('Enter your API Key')\n", "_ = os.environ.setdefault(\"UPSTAGE_API_KEY\", UPSTAGE_API_KEY)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2iCnlIWUbJ7H", "outputId": "94843282-0e8d-4d7c-ba04-5b461422e9f9" }, "execution_count": 2, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your API Key··········\n" ] } ] }, { "cell_type": "code", "source": [ "with open(\"/content/Harry_Potter_summary.txt\", \"r\", encoding=\"utf-8\") as file:\n", " novel = file.read()" ], "metadata": { "id": "r5ViKrajcj7P" }, "execution_count": 13, "outputs": [] }, { "cell_type": "code", "source": [ "print(novel[:1000])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "HwIdL3NMcxID", "outputId": "539b5d57-d994-4b21-b3d6-d9b9c4f76fc2" }, "execution_count": 15, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Harry Potter summary\n", "\n", "1. Harry Potter and the Philosopher's Stone (Sorcerer's Stone in the U.S.):\n", " - Harry Potter, an orphaned boy living with his cruel aunt and uncle, discovers on his 11th birthday that he is a wizard. He is invited to attend Hogwarts School of Witchcraft and Wizardry. At Hogwarts, he makes friends, including Ron Weasley and Hermione Granger, and learns about his past and the dark wizard Voldemort, who killed his parents. Harry, Ron, and Hermione prevent Voldemort from stealing the Philosopher's Stone, a magical object that grants immortality.\n", "\n", "2. Harry Potter and the Chamber of Secrets:\n", " - In his second year at Hogwarts, Harry encounters a series of mysterious attacks on students, caused by the opening of the Chamber of Secrets. He discovers that a memory of Voldemort, preserved in a diary, is controlling a giant serpent (the basilisk) to attack students. With Ron and Hermione's help, Harry defeats the basilisk and destroys the diary.\n", "\n", "3. Harry Potter and the Pr\n" ] } ] }, { "cell_type": "code", "source": [ "from langchain_core.prompts import PromptTemplate\n", "from langchain_core.output_parsers import StrOutputParser\n", "from langchain_upstage import ChatUpstage\n", "\n", "llm = ChatUpstage()\n", "\n", "prompt_template = PromptTemplate.from_template(\n", " \"\"\"\n", " Based on the novel context provided and considering the player's personal traits: gender {gender}, MBTI personality {mbti}, hobbies {hobbies},\n", " create a detailed character for a fantasy tabletop RPG. Provide the character's name, gender, age, race and unique abilities.\n", " Specify the following exact 4 skills with values from 1 to 100 reflecting their capabilities:\n", " - Physical Strength\n", " - Intelligence\n", " - Combat Power\n", " - Agility\n", " Use creativity to infer and create a character that would fit within the universe described in the context, aligning with the player's traits.\n", " ---\n", " Novel Context: {Context}\n", " \"\"\"\n", ")\n", "chain = prompt_template | llm | StrOutputParser()\n" ], "metadata": { "id": "ipyJ9lRobCMF" }, "execution_count": 11, "outputs": [] }, { "cell_type": "code", "source": [ "# Example Context from a novel\n", "novel_context = novel\n", "\n", "# Your gender, MBTI type, and hobbies (input actual values relevant to you)\n", "my_gender = \"Male\"\n", "my_mbti = \"INTJ\"\n", "my_hobbies = \"strategy games, history, and hiking\"\n", "\n", "# Invocation of the chain to generate an RPG character\n", "response = chain.invoke({\n", " \"Context\": novel_context,\n", " \"gender\": my_gender,\n", " \"mbti\": my_mbti,\n", " \"hobbies\": my_hobbies\n", "})\n", "\n", "# Print the response to see the detailed RPG character attributes\n", "print(response)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "z0__GWNjbDue", "outputId": "89bf0c8b-8081-4cc9-a271-d54b15205946" }, "execution_count": 16, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Character Name: Alaric Thorne\n", "\n", "Gender: Male\n", "Age: 18\n", "Race: Half-Blood Wizard (Muggle-born)\n", "\n", "Unique Abilities:\n", "\n", "1. Legilimency: Alaric has a natural talent for Legilimency, the magical practice of reading thoughts and memories. This ability has been enhanced by his INTJ personality, making him a formidable opponent in mind games and a valuable asset in solving complex problems.\n", "\n", "2. Strategic Combat: Alaric's love for strategy games has translated into a unique combat style. He is able to anticipate his opponent's moves and counter them effectively, making him a dangerous opponent in duels.\n", "\n", "3. Enhanced Memory: Alaric's love for history has given him an enhanced memory, allowing him to recall information quickly and accurately when needed.\n", "\n", "Skills:\n", "\n", "1. Physical Strength: 60\n", "2. Intelligence: 90\n", "3. Combat Power: 75\n", "4. Agility: 80\n", "\n", "Alaric Thorne is a thoughtful and strategic wizard, who prefers to think his way out of problems rather than relying solely on magic. He is often found in the library, studying ancient texts and learning about the magical world's history. His love for strategy games has made him a skilled dueler, and his natural talent for Legilimency allows him to outsmart his opponents. Despite his introverted nature, Alaric has a strong sense of loyalty to his friends and will do whatever it takes to protect them.\n" ] } ] } ] }