File size: 6,397 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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.1.1\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "! pip3 install -qU  markdownify  langchain-upstage rank_bm25 python-dotenv langchain_chroma langchain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext dotenv\n",
    "%dotenv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_chroma import Chroma\n",
    "from langchain_upstage import UpstageEmbeddings\n",
    "from langchain.docstore.document import Document\n",
    "\n",
    "sample_text_list = [\n",
    "  \"λ§ˆλ²•μ‚¬μ˜ λ‚˜λ¬΄λŠ” λ§ˆλ²• μ§€νŒ‘μ΄μ˜ 재료 쀑 μ΅œμƒκΈ‰μ˜ 재료둜 μ•Œλ €μ Έ μžˆλ‹€\",\n",
    "  \"There are Weasleys' Wizard Wheezes nearby the Diagon Alley\",\n",
    "  \"Diangon Alley is a place where you can buy magic wands\",\n",
    "  \"Hogwart is a school for witches and wizards\",\n",
    "]\n",
    "sample_docs = [Document(page_content=text) for text in sample_text_list]\n",
    "vectorstore = Chroma.from_documents(\n",
    "    documents=sample_docs,\n",
    "    embedding=UpstageEmbeddings(model=\"solar-embedding-1-large\"),\n",
    ")\n",
    "retriever = vectorstore.as_retriever()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import PromptTemplate\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain_upstage import ChatUpstage\n",
    "\n",
    "\n",
    "llm = ChatUpstage()\n",
    "\n",
    "prompt_template = PromptTemplate.from_template(\n",
    "    \"\"\"\n",
    "    You are best D&D host. And user is playing D&D game with you.\n",
    "    Please answer in a exciting tone as a host.\n",
    "    User has to make choices in the game.\n",
    "    User has 10 life points and 10 gold coins.\n",
    "    Please make scenario of most interesting event from the following context, and provide diverse choices which user can select.\n",
    "    For each round, you should provide at least 3 choices and total round should be less than 50.\n",
    "    You should explain situation and choices in detail.\n",
    "    If someone's conversation is included in the scenario, please express it in colloquial terms that fit the person's character as much as possible.\n",
    "    Choices should be diverse and interesting, and each choice will reduce or increase user's life points or gold coins.\n",
    "    If user make appropriate decision, user's life points or gold coins will increase.\n",
    "    If user make inappropriate decision, user's life points or gold coins will decrease and decreasing amount should not be larger than amount of user owned.\n",
    "    When user select a choice, you should provide the result of the choice, but you should not show result before user select the choice.\n",
    "    You should consider previous conversation and context to make the scenario.\n",
    "    If user's life points or gold coins are less than 0, user will lose the game and get bad ending.\n",
    "    Also when total round is more than 50, user will lose the game and get bad ending.\n",
    "    If user's life points or gold coins are more than 20, user will win the game and get good ending.\n",
    "    ---\n",
    "    User Choice: {user_choice}\n",
    "    ---\n",
    "    Previous Conversation: {previous_conversation}\n",
    "    ---\n",
    "    Context: {context}\n",
    "    \"\"\"\n",
    ")\n",
    "chain = prompt_template | llm | StrOutputParser()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'λ§ˆλ²•μ‚¬μ˜ λ‚˜λ¬΄μ™€ 용의 심μž₯으둜 λ§Œλ“  μ§€νŒ‘μ΄λ₯Ό 사고 μ‹Άλ‹€λŠ”κ΅°μš”! 그것은 κ°•λ ₯ν•œ λ§ˆλ²• μ•„μ΄ν…œμž…λ‹ˆλ‹€. 이제 μ„ νƒμ˜ μ‹œκ°„μ΄ μ™”μŠ΅λ‹ˆλ‹€. 이 μ§€νŒ‘μ΄λ₯Ό μ–΄λ–»κ²Œ 얻을 κ²ƒμΈκ°€μš”?\\n\\n1. **λ§ˆλ²• 상점을 λ°©λ¬Έν•˜μ„Έμš”:** λ§ˆμ„μ˜ λ§ˆλ²• 상점을 μ°Ύμ•„κ°€μ„œ μ§€νŒ‘μ΄λ₯Ό κ΅¬λ§€ν•΄λ³΄μ„Έμš”. 가격은 8κ³¨λ“œ μ½”μΈμž…λ‹ˆλ‹€.\\n2. **λ§ˆλ²•μ‚¬λ‘œλΆ€ν„° κ΅¬λ§€ν•˜μ„Έμš”:** λ§ˆμ„μ— μžˆλŠ” λ§ˆλ²•μ‚¬μ—κ²Œ μ§€νŒ‘μ΄λ₯Ό ꡬ맀할 수 μžˆλŠ”μ§€ λ¬Όμ–΄λ³΄μ„Έμš”. ν•˜μ§€λ§Œ 가격은 10κ³¨λ“œ μ½”μΈμœΌλ‘œ λΉ„μ‹Έμ§‘λ‹ˆλ‹€.\\n3. **μ§€νŒ‘μ΄λ₯Ό μ œμž‘ν•˜μ„Έμš”:** μž¬λ£Œμ™€ λ§ˆλ²• 지식을 κ°€μ§€κ³  μ§€νŒ‘μ΄λ₯Ό 직접 μ œμž‘ν•΄λ³΄μ„Έμš”. ν•˜μ§€λ§Œ 이 선택은 5κ³¨λ“œ 코인을 μ†ŒλΉ„ν•˜κ³ , μ œμž‘μ— 성곡할 ν™•λ₯ μ€ 50%μž…λ‹ˆλ‹€.\\n\\nμ–΄λ–€ 선택을 ν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_choice = \"μ €λŠ” λ§ˆλ²•μ‚¬μ˜ λ‚˜λ¬΄μ™€ 용의 심μž₯으둜 λ§Œλ“  μ§€νŒ‘μ΄λ₯Ό 사고 μ‹Άμ–΄μš”.\"\n",
    "sample_context = \"What is related information about {user_choice}?\"\n",
    "result_docs = retriever.invoke(sample_context)\n",
    "chain.invoke({ \"user_choice\": user_choice, \"previous_conversation\": \"\", \"context\": sample_context })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}