File size: 21,219 Bytes
4a51346
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 이 λ¬Έμ„œλ₯Ό μˆ˜μ •ν•  λ‹Ήμ‹ μ—κ²Œ...\n",
    "#### ν˜„μž¬ 상황은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.\n",
    "1. ν˜„μž¬ Agent의 action을 7λ‹¨κ³„λ‘œ λ‚˜λˆ„μ–΄ λ†“μ•˜μ§€λ§Œ 이것이 μ΅œμ„ μ€ μ•„λ‹ˆλΌλŠ” 생각이 λ“­λ‹ˆλ‹€. λ˜ν•œ 와인바 μΆ”μ²œλ“±μ— λŒ€ν•œ λ‹¨κ³„λŠ” κ΅¬ν˜„λ˜μ–΄ μžˆμ§€ μ•ŠμŠ΅λ‹ˆλ‹€.\n",
    "2. Assistantκ°€ Agentκ°€ λ‹€μŒμ— μ·¨ν•΄μ•Όν•  Actionμ˜ˆμΈ‘μ„ 잘 λͺ»ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. μ΄λ‘œμΈν•΄μ„œ Agent의 μ„±λŠ₯이 크게 떨어지고 μžˆμŠ΅λ‹ˆλ‹€. \n",
    "\n",
    "#### λ‹Ήμ‹ μ˜ λͺ©ν‘œλŠ” μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.\n",
    "1. ν˜„μž¬μ˜ 7λ‹¨κ³„λ‘œ λ‚˜λˆ„μ–΄λ†“μ€ 것을 와인바 μΆ”μ²œ 단계 등을 μΆ”κ°€ν•˜κ³ , 적절히 μˆ˜μ •ν•©λ‹ˆλ‹€. 그리고 이λ₯Ό [ν”„λ‘¬ν”„νŠΈ](templates/stage_analyzer_inception_prompt_template.txt)에 λ°˜μ˜ν•©λ‹ˆλ‹€.\n",
    "2. μ΅œλŒ€ν•œ Action 예츑의 μ„±λŠ₯을 올리기.(이λ₯Ό μΈ‘μ •ν•˜κΈ° μœ„ν•œ evaluation set을 μ œκ³΅ν•  것 μž…λ‹ˆλ‹€. ν•˜μ§€λ§Œ κ·Έλ₯Ό μœ„ν•΄μ„œλŠ” λ¨Όμ € Agent action의 step을 μ •ν•΄μ„œ μ•Œλ €μ£Όμ…”μ•Ό ν•©λ‹ˆλ‹€.) μ΅œμ’… λͺ©ν‘œλŠ” evaluation set에 λŒ€ν•΄ 95% μ΄μƒμœΌλ‘œ Agent의 λ‹€μŒ action을 μ˜ˆμΈ‘ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.\n",
    "\n",
    "#### 생각할 수 μžˆλŠ” ν•΄κ²° 방식은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.\n",
    "문제 1에 λŒ€ν•˜μ—¬....\n",
    "- μ œκ³΅λ˜μ–΄ μžˆλŠ” μ™€μΈλ°”μ—μ„œ 일어날 수 μžˆλŠ” λŒ€ν™”μ…‹μœΌλ‘œλΆ€ν„° Agentκ°€ μ·¨ν•  action을 μ„ΈλΆ„ν™”ν•©λ‹ˆλ‹€. 이후 이에 따라 ν”„λ‘¬ν”„νŠΈλ₯Ό μˆ˜μ •ν•©λ‹ˆλ‹€.\n",
    "\n",
    "문제 2에 λŒ€ν•˜μ—¬....\n",
    "- Trial and Errorλ₯Ό ν†΅ν•œ ν”„λ‘¬ν”„νŠΈ μˆ˜μ •\n",
    "- μœ μ‚¬ν•œ example ν”„λ‘¬ν”„νŠΈμ— λ„£μ–΄μ£ΌκΈ°: ν˜„μž¬λŠ” ν”„λ‘¬ν”„νŠΈμ— κ³ μ •λœ action 예츑 μ˜ˆμ‹œλ₯Ό μ μ–΄λ†“μ•˜μŠ΅λ‹ˆλ‹€. 이λ₯Ό ν˜„μž¬ conversationκ³Ό μœ μ‚¬λ„κ°€ 높은 μ˜ˆμ‹œλ₯Ό 뽑아 ν”„λ‘¬ν”„νŠΈμ— λ™μ μœΌλ‘œ λ„£μ–΄μ£ΌλŠ” κ²ƒμž…λ‹ˆλ‹€. λ‹€μŒ 링크λ₯Ό μ°Έκ³ ν•΄λ³΄μ„Έμš”. [langchain select by similarity 링크](https://python.langchain.com/docs/modules/model_io/prompts/example_selectors/similarity)\n",
    "- Self Refine μ‚¬μš©ν•˜κΈ°: self refine은 μƒμ„±ν•œ 결과에 λŒ€ν•΄μ„œ 슀슀둜 ν”Όλ“œλ°±μ„ μ£Όκ³ , 이 ν”Όλ“œλ°±μ„ μ΄μš©ν•΄ λ‹€μ‹œ κ°œμ„ ν•˜λŠ” 과정을 λ°˜λ³΅ν•˜λŠ” λ°©μ‹μž…λ‹ˆλ‹€. 이 방식은 μ μš©ν•˜λŠ”λ° μ‹œκ°„μ΄ μ’€ 걸릴듯 ν•˜λ‹ˆ ν›„μˆœμœ„λ‘œ μƒκ°ν•˜μ„Έμš”. [λ…Όλ¬Έ](https://arxiv.org/pdf/2303.17651.pdf)\n",
    "\n",
    "\n",
    "#### μ°Έκ³  사항\n",
    "ν”„λ‘¬ν”„νŠΈ μˆ˜μ •μ€ 고치고, μ‹œν–‰ν•˜κ³ λ₯Ό λ°˜λ³΅ν•˜λŠ” κ³ ν†΅μŠ€λŸ¬μš΄ λ…Έκ°€λ‹€ κ³Όμ •μž…λ‹ˆλ‹€. 정말 νž˜λ“€κ² μ§€λ§Œ ν™”μ΄νŒ… ν•΄μ£Όμ„Έμš”..\n",
    "\n",
    "μ•„λž˜λŠ” μœ„μ˜ μž‘μ—…μ„ μˆ˜ν–‰ν•˜κΈ° μœ„ν•œ 쒋은 μžλ£Œλ“€μž…λ‹ˆλ‹€. μœ„μ˜ λ¬Έμ œν•΄κ²° 전에 μ•„λž˜ μžλ£Œλ“€μ„ λ¨Όμ € ν•™μŠ΅ν•˜λŠ” 것을 맀우 κ°•λ ₯ν•˜κ²Œ μΆ”μ²œν•©λ‹ˆλ‹€.\n",
    "- [ν”„λ‘¬ν”„νŠΈ μ—”μ§€λ‹ˆμ–΄λ§ κ°•μ˜ 2μ‹œκ°„](https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/)\n",
    "- [ν”„λ‘¬ν”„νŠΈ μ—”μ§€λ‹ˆμ–΄λ§ κ°€μ΄λ“œ documentation](https://www.promptingguide.ai/techniques)\n",
    "- [Langchain μ˜ˆμ‹œ: Sales GPT](https://python.langchain.com/docs/use_cases/agents/sales_agent_with_context)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----------------------------------------------------------------"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "AssistantλŠ” λŒ€ν™”κΈ°λ‘μ„ 보고, λ‹€μŒμ— Agentκ°€ μ·¨ν•΄μ•Όν•  κ°€μž₯ μ μ ˆν•œ 행동을 μ„ νƒν•˜λŠ” 것을 λͺ©ν‘œλ‘œ ν•œλ‹€.\n",
    "\n",
    "Agent의 행동은 7κ°€μ§€λ‘œ κ΅¬μ„±λœλ‹€.\n",
    "1. μ‹œμž‘ν•˜κΈ°: λŒ€ν™”λ₯Ό μ‹œμž‘ν•˜κ±°λ‚˜ μ‚¬μš©μžμ˜ 첫 번째 응닡에 응닡할 λ•Œ μ·¨ν•΄μ•Ό ν•  첫 번째 λ‹¨κ³„μž…λ‹ˆλ‹€. μžμ‹ μ„ μ†Œκ°œν•˜λŠ” κ²ƒμœΌλ‘œ λŒ€ν™”λ₯Ό μ‹œμž‘ν•©λ‹ˆλ‹€. 전문적인 λŒ€ν™” 톀을 μœ μ§€ν•˜λ©΄μ„œ μ •μ€‘ν•˜κ³  μ‘΄μ€‘ν•˜λŠ” νƒœλ„λ₯Ό μ·¨ν•˜μ„Έμš”.\n",
    "2. 뢄석: 고객이 μΆ”μ²œμ„ 원할 경우 μΆ”μ²œ 전에 이 단계λ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€. 이 λ‹¨κ³„λŠ” μ‚¬μš©μžμ˜ μ„ ν˜Έλ„λ₯Ό νŒŒμ•…ν•˜λŠ” λ‹¨κ³„μž…λ‹ˆλ‹€. μ‚¬μš©μžμ˜ μ„ ν˜Έλ„λ₯Ό νŒŒμ•…ν•˜κΈ° μœ„ν•΄ μΆ©λΆ„ν•œ μ§ˆλ¬Έμ„ ν•˜μ„Έμš”.\n",
    "3. μΆ”μ²œ: μ‚¬μš©μžμ˜ μ„ ν˜Έλ„λ₯Ό νŒŒμ•…ν•˜λ©΄ 그에 따라 μ ν•©ν•œ 와인을 μΆ”μ²œν•  수 μžˆμŠ΅λ‹ˆλ‹€. μΆ”μ²œμ€ 와인 λ°μ΄ν„°λ² μ΄μŠ€μ— μžˆλŠ” μ™€μΈμœΌλ‘œ μ œν•œν•΄μ•Ό ν•˜λ©°, 이λ₯Ό μœ„ν•΄ 도ꡬλ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.\n",
    "4. μΆ”μ²œ ν›„: 와인 μΆ”μ²œ ν›„ μ‚¬μš©μžκ°€ μΆ”μ²œν•œ 와인이 λ§ˆμŒμ— λ“œλŠ”μ§€ 물어보고 λ§ˆμŒμ— λ“€λ©΄ ν•΄λ‹Ή 와인에 λŒ€ν•œ 링크λ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€. 그렇지 μ•ŠμœΌλ©΄ μΆ”μ²œ λ‹¨κ³„λ‘œ λŒμ•„κ°‘λ‹ˆλ‹€.\n",
    "5. λ§ˆλ¬΄λ¦¬ν•˜κΈ°: μž‘μ—…μ„ 마치면 μ‚¬μš©μžμ—κ²Œ μž‘λ³„ 인사λ₯Ό ν•©λ‹ˆλ‹€.\n",
    "6. 질문 및 λ‹΅λ³€: μ‚¬μš©μžμ˜ μ§ˆλ¬Έμ— λ‹΅λ³€ν•˜λŠ” κ³³μž…λ‹ˆλ‹€.\n",
    "7. 주어진 단계에 ν•΄λ‹Ήν•˜μ§€ μ•ŠμŒ: 1~6단계 쀑 μ–΄λŠ 단계에도 ν•΄λ‹Ήλ˜μ§€ μ•ŠλŠ” 경우 이 단계λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### API ν‚€ 뢈러였기"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import configparser\n",
    "import time\n",
    "import copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['./secrets.ini']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "config = configparser.ConfigParser()\n",
    "config.read('./secrets.ini')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "openai_api_key = config['OPENAI']['OPENAI_API_KEY']\n",
    "serper_api_key = config['SERPER']['SERPER_API_KEY']\n",
    "serp_api_key = config['SERPAPI']['SERPAPI_API_KEY']\n",
    "os.environ.update({'OPENAI_API_KEY': openai_api_key})\n",
    "os.environ.update({'SERPER_API_KEY': serper_api_key})\n",
    "os.environ.update({'SERPAPI_API_KEY': serp_api_key})"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Get Stage Analyzer(Assistant) Prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List, Union\n",
    "import re\n",
    "import json\n",
    "\n",
    "import pandas as pd\n",
    "from langchain import SerpAPIWrapper, LLMChain\n",
    "from langchain.agents import Tool, AgentType, AgentExecutor, LLMSingleActionAgent, AgentOutputParser\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.chains import LLMChain, SimpleSequentialChain\n",
    "from langchain.chains.query_constructor.base import AttributeInfo\n",
    "from langchain.document_loaders import DataFrameLoader, SeleniumURLLoader\n",
    "from langchain.embeddings import OpenAIEmbeddings\n",
    "from langchain.indexes import VectorstoreIndexCreator\n",
    "from langchain.prompts import PromptTemplate, StringPromptTemplate\n",
    "from langchain.prompts import load_prompt, BaseChatPromptTemplate\n",
    "from langchain.llms import OpenAI\n",
    "from langchain.retrievers.self_query.base import SelfQueryRetriever\n",
    "from langchain.schema import AgentAction, AgentFinish, HumanMessage\n",
    "from langchain.vectorstores import DocArrayInMemorySearch, Chroma"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ν”„λ‘¬ν”„νŠΈλ₯Ό μ„ μ–Έν•˜λŠ” 방식은 파일둜 λΆ€ν„° λΆˆλŸ¬μ˜€κ±°λ‚˜ 직접 μ„ μ–Έν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ•„λž˜μ—λŠ” 두 방식 λͺ¨λ‘ κ΅¬ν˜„λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.\n",
    "- 직접 μ„ μ–Έν•˜κΈ° μœ„ν•΄μ„œλŠ” prompt_template을 λ¨Όμ € μž‘μ„±ν•΄μ•Όν•©λ‹ˆλ‹€.\n",
    "- νŒŒμΌλ‘œλΆ€ν„° λΆˆλŸ¬μ˜€λŠ” 것은 ./templates/stage_analyzer_inception_prompt_template.json νŒŒμΌμ„ λΆˆλŸ¬μ˜€λŠ” κ²ƒμž…λ‹ˆλ‹€."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 직접 ν”„λ‘¬ν”„νŠΈ μ„ μ–Έν•˜κΈ°μœ„ν•œ ν…œν”Œλ¦Ώ μž‘μ„±\n",
    "stage_analyzer_inception_prompt_template = \"\"\"\n",
    "You are an assistant decide which stage of the conversation to move to or which stage to stay at.\n",
    "Following '===' is the conversation history. \n",
    "Use conversation history to select the next step the agent should take.\n",
    "\n",
    "Below are the stages of the conversation that the agent can take.\n",
    "1. Start: This is the first step to take when starting a conversation or responding to a user's first response. Start the conversation by introducing yourself. Be polite and respectful while maintaining a professional tone of conversation.\n",
    "2. Analyze: When a customer wants a recommendation, run this step before recommendation. This is the step where you identify the user's preferences. Ask enough questions to understand your users' preferences.\n",
    "3. Recommendation: Once you know the preference of user, you can recommend suitable wines accordingly. Recommendations should be limited to wines in your wine database, and you can use tools for this.\n",
    "4. After recommendation: After making a wine recommendation, it asks if the user likes the wine you recommended, and if they do, it provides a link to it. Otherwise, it takes you back to the recommendation stage.\n",
    "5. Close: When you're done, say goodbye to the user.\n",
    "6. Question and Answering: This is where you answer the user's questions.\n",
    "7. Not in the given steps: This step is for when none of the steps between 1 and 6 apply.\n",
    "\n",
    "Only answer with a number between 1 through 7 with a best guess of what stage should the conversation continue with. \n",
    "The answer needs to be one number only, no words.\n",
    "Once again, we need to understand the user's preferences (STEP 2) before we can make a recommendation (STEP 3), and we need to understand the user's preferences (STEP 2) about 2 times.\n",
    "Do not answer anything else nor add anything to you answer.\n",
    "\n",
    "Below is four examples of how to do this task.\n",
    "Example1:\n",
    "conversation history:\n",
    "    User: μ•ˆλ…•ν•˜μ„Έμš”.\n",
    "stage history: \n",
    "Answer: 1\n",
    "\n",
    "Example2:\n",
    "conversation history:\n",
    "User: μ•ˆλ…•ν•˜μ„Έμš”. <END_OF_TURN>\n",
    "μ΄μš°μ„ : 무엇을 λ„μ™€λ“œλ¦΄κΉŒμš”? <END_OF_TURN>\n",
    "User: 와인 μΆ”μ²œν•΄μ£Όμ„Έμš”. <END_OF_TURN>\n",
    "stage history: 1\n",
    "Answer: 2\n",
    "\n",
    "Example3:\n",
    "conversation history:\n",
    "User: μ•ˆλ…•ν•˜μ„Έμš”. <END_OF_TURN>\n",
    "μ΄μš°μ„ : 무엇을 λ„μ™€λ“œλ¦΄κΉŒμš”? <END_OF_TURN>\n",
    "User: μ™€μΈμ˜ ν¬λ„λŠ” μ–΄λ–€ μ’…λ₯˜κ°€ μžˆλ‚˜μš”?. <END_OF_TURN>\n",
    "stage history: 1\n",
    "Answer: 6\n",
    "\n",
    "Example4:\n",
    "conversation history:\n",
    "User: μ•ˆλ…•ν•˜μ„Έμš”. <END_OF_TURN>\n",
    "μ΄μš°μ„ : 무엇을 λ„μ™€λ“œλ¦΄κΉŒμš”? <END_OF_TURN>\n",
    "User: 와인 μΆ”μ²œν•΄μ£Όμ„Έμš”. <END_OF_TURN>\n",
    "μ΄μš°μ„ : μ–΄λ–€ ν–‰μ‚¬λ‚˜ 기념일을 μœ„ν•΄ 와인을 μ°ΎμœΌμ‹œλŠ”μ§€ μ•Œλ €μ£Όμ‹€ 수 μžˆμœΌμ‹ κ°€μš”? <END_OF_TURN>\n",
    "User: μ΄λ²ˆμ£Όμ— κ²°ν˜ΌκΈ°λ…μΌμ΄ μžˆμ–΄μ„œμš”. <END_OF_TURN>\n",
    "μ΄μš°μ„ : κ·Έλ ‡κ΅°μš”. κ°€κ²©λŒ€λŠ” μ–΄λŠμ •λ„λ‘œ μƒκ°ν•˜κ³  κ³„μ‹ κ°€μš”?  <END_OF_TURN>\n",
    "User: 20λ§Œμ› μ •λ„μš” <END_OF_TURN>\n",
    "μ΄μš°μ„ : κ·Έλ ‡κ΅°μš”. λ‹¬λ‹¬ν•œ 와인을 μ„ ν˜Έν•˜μ‹œλ‚˜μš”? μ•„λ‹ˆλ©΄ μ•½κ°„ μ‹  와인을 μ„ ν˜Έν•˜μ‹œλ‚˜μš”? <END_OF_TURN>\n",
    "User: λ‹¬λ‹¬ν•œ μ™€μΈμ΄μš” <END_OF_TURN>\n",
    "stage history: 1 2 2 2\n",
    "Thought: There are three '2's in the stage history. So the next stage should be 3.\n",
    "Answer: 3\n",
    "\n",
    "Now determine what should be the next immediate conversation stage for the agent in the conversation by selecting one from the following options:\n",
    "Use the conversation history between first and second '======' and stage history between first and second '######' to accomplish the task above.\n",
    "If conversation history is empty, output 1.\n",
    "\n",
    "conversation history:\n",
    "======\n",
    "{conversation_history}\n",
    "======\n",
    "\n",
    "stage history:\n",
    "######\n",
    "{stage_history}\n",
    "######\n",
    "\n",
    "Answer: \n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 직접 ν”„λ‘¬ν”„νŠΈ μ„ μ–Έν•˜κΈ°\n",
    "stage_analyzer_inception_prompt = PromptTemplate(\n",
    "    input_variables=[\"conversation_history\", \"stage_history\"], \n",
    "    template=stage_analyzer_inception_prompt_template,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 파일둜 λΆ€ν„° ν”„λ‘¬ν”„νŠΈ 뢈러였기\n",
    "stage_analyzer_inception_prompt = load_prompt(\"./templates/stage_analyzer_inception_prompt_template.json\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "μ•„λž˜μ™€ 같이 format λ©”μ„œλ“œλ‘œ ν”„λ‘¬ν”„νŠΈλ₯Ό 확인할 수 μžˆμŠ΅λ‹ˆλ‹€."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "example_conversation_history = \"\"\"\n",
    "User: μ•ˆλ…•ν•˜μ„Έμš”. <END_OF_TURN>\n",
    "μ΄μš°μ„ : 무엇을 λ„μ™€λ“œλ¦΄κΉŒμš”? <END_OF_TURN>\n",
    "User: 와인 μΆ”μ²œν•΄μ£Όμ„Έμš”. <END_OF_TURN>\n",
    "μ΄μš°μ„ : μ–΄λ–€ ν–‰μ‚¬λ‚˜ 기념일을 μœ„ν•΄ 와인을 μ°ΎμœΌμ‹œλŠ”μ§€ μ•Œλ €μ£Όμ‹€ 수 μžˆμœΌμ‹ κ°€μš”? <END_OF_TURN>\n",
    "User: μ΄λ²ˆμ£Όμ— κ²°ν˜ΌκΈ°λ…μΌμ΄ μžˆμ–΄μ„œμš”. <END_OF_TURN>\n",
    "\"\"\"\n",
    "example_stage_history = \"1 2\"\n",
    "example_answer = \"2\"\n",
    "# μ—¬κΈ°μ„œ μš°λ¦¬λŠ” μ—μ΄μ „νŠΈκ°€ 와인 μΆ”μ²œμ„ μœ„ν•΄ μœ μ €μ˜ 더 λ§Žμ€ 정보λ₯Ό μ–»κΈ°λ₯Ό μ›ν•œλ‹€. λ”°λΌμ„œ 2(Analyze)κ°€ λ‹΅λ³€μœΌλ‘œ λ‚˜μ˜€κΈΈ μ›ν•œλ‹€."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "You are an assistant decide which stage of the conversation to move to or which stage to stay at.\n",
      "Following '===' is the conversation history. \n",
      "Use conversation history to select the next step the agent should take.\n",
      "\n",
      "Below are the stages of the conversation that the agent can take.\n",
      "1. Start: This is the first step to take when starting a conversation or responding to a user's first response. Start the conversation by introducing yourself. Be polite and respectful while maintaining a professional tone of conversation.\n",
      "2. Analyze: When a customer wants a recommendation, run this step before recommendation. This is the step where you identify the user's preferences. Ask enough questions to understand your users' preferences.\n",
      "3. Recommendation: Once you know the preference of user, you can recommend suitable wines accordingly. Recommendations should be limited to wines in your wine database, and you can use tools for this.\n",
      "4. After recommendation: After making a wine recommendation, it asks if the user likes the wine you recommended, and if they do, it provides a link to it. Otherwise, it takes you back to the recommendation stage.\n",
      "5. Close: When you're done, say goodbye to the user.\n",
      "6. Question and Answering: This is where you answer the user's questions.\n",
      "7. Not in the given steps: This step is for when none of the steps between 1 and 6 apply.\n",
      "\n",
      "Only answer with a number between 1 through 7 with a best guess of what stage should the conversation continue with. \n",
      "The answer needs to be one number only, no words.\n",
      "Once again, we need to understand the user's preferences (STEP 2) before we can make a recommendation (STEP 3), and we need to understand the user's preferences (STEP 2) about 2 times.\n",
      "Do not answer anything else nor add anything to you answer.\n",
      "\n",
      "Below is four examples of how to do this task.\n",
      "Example1:\n",
      "conversation history:\n",
      "    User: μ•ˆλ…•ν•˜μ„Έμš”.\n",
      "stage history: \n",
      "Answer: 1\n",
      "\n",
      "Example2:\n",
      "conversation history:\n",
      "User: μ•ˆλ…•ν•˜μ„Έμš”. <END_OF_TURN>\n",
      "μ΄μš°μ„ : 무엇을 λ„μ™€λ“œλ¦΄κΉŒμš”? <END_OF_TURN>\n",
      "User: 와인 μΆ”μ²œν•΄μ£Όμ„Έμš”. <END_OF_TURN>\n",
      "stage history: 1\n",
      "Answer: 2\n",
      "\n",
      "Example3:\n",
      "conversation history:\n",
      "User: μ•ˆλ…•ν•˜μ„Έμš”. <END_OF_TURN>\n",
      "μ΄μš°μ„ : 무엇을 λ„μ™€λ“œλ¦΄κΉŒμš”? <END_OF_TURN>\n",
      "User: μ™€μΈμ˜ ν¬λ„λŠ” μ–΄λ–€ μ’…λ₯˜κ°€ μžˆλ‚˜μš”?. <END_OF_TURN>\n",
      "stage history: 1\n",
      "Answer: 6\n",
      "\n",
      "Example4:\n",
      "conversation history:\n",
      "User: μ•ˆλ…•ν•˜μ„Έμš”. <END_OF_TURN>\n",
      "μ΄μš°μ„ : 무엇을 λ„μ™€λ“œλ¦΄κΉŒμš”? <END_OF_TURN>\n",
      "User: 와인 μΆ”μ²œν•΄μ£Όμ„Έμš”. <END_OF_TURN>\n",
      "μ΄μš°μ„ : μ–΄λ–€ ν–‰μ‚¬λ‚˜ 기념일을 μœ„ν•΄ 와인을 μ°ΎμœΌμ‹œλŠ”μ§€ μ•Œλ €μ£Όμ‹€ 수 μžˆμœΌμ‹ κ°€μš”? <END_OF_TURN>\n",
      "User: μ΄λ²ˆμ£Όμ— κ²°ν˜ΌκΈ°λ…μΌμ΄ μžˆμ–΄μ„œμš”. <END_OF_TURN>\n",
      "μ΄μš°μ„ : κ·Έλ ‡κ΅°μš”. κ°€κ²©λŒ€λŠ” μ–΄λŠμ •λ„λ‘œ μƒκ°ν•˜κ³  κ³„μ‹ κ°€μš”?  <END_OF_TURN>\n",
      "User: 20λ§Œμ› μ •λ„μš” <END_OF_TURN>\n",
      "μ΄μš°μ„ : κ·Έλ ‡κ΅°μš”. λ‹¬λ‹¬ν•œ 와인을 μ„ ν˜Έν•˜μ‹œλ‚˜μš”? μ•„λ‹ˆλ©΄ μ•½κ°„ μ‹  와인을 μ„ ν˜Έν•˜μ‹œλ‚˜μš”? <END_OF_TURN>\n",
      "User: λ‹¬λ‹¬ν•œ μ™€μΈμ΄μš” <END_OF_TURN>\n",
      "stage history: 1 2 2 2\n",
      "Thought: There are three '2's in the stage history. So the next stage should be 3.\n",
      "Answer: 3\n",
      "\n",
      "Now determine what should be the next immediate conversation stage for the agent in the conversation by selecting one from the following options:\n",
      "Use the conversation history between first and second '======' and stage history between first and second '######' to accomplish the task above.\n",
      "If conversation history is empty, output 1.\n",
      "\n",
      "conversation history:\n",
      "======\n",
      "\n",
      "User: μ•ˆλ…•ν•˜μ„Έμš”. <END_OF_TURN>\n",
      "μ΄μš°μ„ : 무엇을 λ„μ™€λ“œλ¦΄κΉŒμš”? <END_OF_TURN>\n",
      "User: 와인 μΆ”μ²œν•΄μ£Όμ„Έμš”. <END_OF_TURN>\n",
      "μ΄μš°μ„ : μ–΄λ–€ ν–‰μ‚¬λ‚˜ 기념일을 μœ„ν•΄ 와인을 μ°ΎμœΌμ‹œλŠ”μ§€ μ•Œλ €μ£Όμ‹€ 수 μžˆμœΌμ‹ κ°€μš”? <END_OF_TURN>\n",
      "User: μ΄λ²ˆμ£Όμ— κ²°ν˜ΌκΈ°λ…μΌμ΄ μžˆμ–΄μ„œμš”. <END_OF_TURN>\n",
      "\n",
      "======\n",
      "\n",
      "stage history:\n",
      "######\n",
      "1 2\n",
      "######\n",
      "\n",
      "Answer: \n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\n",
    "    stage_analyzer_inception_prompt.format(\n",
    "        conversation_history=example_conversation_history,\n",
    "        stage_history=example_stage_history\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 랭체인 λͺ¨λΈ μ„ μ–Έ, λž­μ²΄μΈμ€ μ–Έμ–΄λͺ¨λΈκ³Ό ν”„λ‘¬ν”„νŠΈλ‘œ κ΅¬μ„±λ©λ‹ˆλ‹€.\n",
    "llm = ChatOpenAI(model='gpt-3.5-turbo', temperature=0.0)\n",
    "stage_analyzer_chain = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=stage_analyzer_inception_prompt, \n",
    "    verbose=False, # 과정을 좜λ ₯할지\n",
    "    output_key=\"stage_number\" # 좜λ ₯κ°’μ˜ λ³€μˆ˜λͺ…\n",
    "    )"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Chain을 μ‹€ν–‰ν•˜κΈ° μœ„ν•΄μ„œλŠ” run λ©”μ„œλ“œλ₯Ό μ‹€ν–‰ν•œλ‹€."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "stage_number = stage_analyzer_chain.run(\n",
    "    {'conversation_history': example_conversation_history, \n",
    "    'stage_history': example_stage_history}\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "κ²°κ³Όλ₯Ό 확인해보면 μ›ν•˜λŠ” κ²°κ³Ό(2. Analyze)κ°€ λ‚˜μ˜€λŠ” 것을 확인할 수 μžˆλ‹€."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(stage_number)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "nemo",
   "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.8.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}