File size: 10,788 Bytes
0b75c79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
{
  "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"
          ]
        }
      ]
    }
  ]
}