File size: 22,437 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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ์ด ๋ฌธ์„œ๋ฅผ ์ˆ˜์ •ํ•  ๋‹น์‹ ์—๊ฒŒ...\n",
    "#### ํ˜„์žฌ ์ƒํ™ฉ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.\n",
    "1. ์•„๋ž˜ 4๊ฐ€์ง€์˜ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.\n",
    "- ํ•„ํ„ฐ query๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ •๋ณด ์ถ”์ถœ(์ถ”์ฒœ์„ ์œ„ํ•จ): chroma๋ฅผ ํ™œ์šฉํ•ด ์ˆ˜์ง‘๋œ wine database์— ํ•„ํ„ฐ๋ฅผ ์ ์šฉํ•˜์—ฌ ํŠน์ • ์™€์ธ๋งŒ ๋ฝ‘์•„์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.    \n",
    "    - input: {body:3, price:100000~120000}\n",
    "    - output: [Document(page_content='ํฌ์ง€์˜ค ๋ฐ์ด ์ ค์‹œ', metadata={'price': '30000', ...}),]\n",
    "- ์ž์—ฐ์–ด query๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ •๋ณด ์ถ”์ถœ(์ถ”์ฒœ์„ ์œ„ํ•จ): DocArrayInMemorySearch์„ ํ™œ์šฉํ•ด wine database์— ์ž์—ฐ์–ด๋กœ ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘์•„ ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.\n",
    "    - input: \"ํ”„๋ž‘์Šค์—์„œ ๋งŒ๋“ค์–ด์ง„ ์™€์ธ์€?\"\n",
    "- ํŠน์ • URL query๋กœ ์›น ํŽ˜์ด์ง€์—์„œ ์ •๋ณด ์ถ”์ถœ(์™€์ธ ๊ตฌ๋งค๋งํฌ์—์„œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์‚ฌ์šฉ): SeleniumURLLoader๋ฅผ ์ด์šฉํ•ด ํŠน์ • url๋กœ ๋ถ€ํ„ฐ ์›นํŽ˜์ด์ง€ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.\n",
    "- ์ž์—ฐ์–ด ๊ตฌ๊ธ€ ๊ฒ€์ƒ‰์œผ๋กœ ์›น ํŽ˜์ด์ง€์—์„œ ์ •๋ณด ์ถ”์ถœ(Q&A์ฒ˜๋Ÿผ ๊ทธ ์™ธ ๊ฒ€์ƒ‰์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉ): SerpAPI๋ฅผ ์ด์šฉํ•ด ๊ตฌ๊ธ€์—์„œ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.\n",
    "\n",
    "#### ๋‹น์‹ ์˜ ๋ชฉํ‘œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.\n",
    "1. ๋‹ค์–‘ํ•œ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํƒ๊ตฌํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ์ข‹์€ ๋ฐฉ์‹์„ ์ฐพ์Šต๋‹ˆ๋‹ค. (์˜ˆ๋ฅผ ๋“ค๋ฉด ์™€์ธ๋ฐ”์˜ ์ƒ์„ธ์ •๋ณด๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์œ„ํ•œ ์นด์นด์˜ค ์œ„์น˜ ์ •๋ณด API๋“ฑ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์„ ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค.)\n",
    "2. ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ ์ ˆํ•œ format์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ์ค๋‹ˆ๋‹ค. ๊ฐ ๋„๊ตฌ๋“ค์— ๋Œ€ํ•œ ์ ์ ˆํ•œ input format์„ ์ฐพ์Šต๋‹ˆ๋‹ค.\n",
    "\n",
    "\n",
    "#### ์ฐธ๊ณ ์‚ฌํ•ญ\n",
    "ํ˜„์žฌ ๋ง์ด ๋„ˆ๋ฌด ๋ชจํ˜ธํ•˜๊ฒŒ ์จ์žˆ๊ณ , ๋„๊ตฌ์˜ ์‚ฌ์šฉ์ฒ˜๊ฐ€ ํ™•์‹คํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ตฌ์ฒดํ™”ํ•˜๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "-------------------------------------------------------------------------------"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### API ํ‚ค ๋ถˆ๋Ÿฌ์˜ค๊ธฐ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import configparser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['./secrets.ini']"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "config = configparser.ConfigParser()\n",
    "config.read('./secrets.ini')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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, 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": [
    "### Load wine database json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_json('./data/unified_wine_data.json', encoding='utf-8', lines=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>url</th>\n",
       "      <th>site_name</th>\n",
       "      <th>price</th>\n",
       "      <th>name</th>\n",
       "      <th>en_name</th>\n",
       "      <th>img_url</th>\n",
       "      <th>body</th>\n",
       "      <th>acidity</th>\n",
       "      <th>tannin</th>\n",
       "      <th>sweetness</th>\n",
       "      <th>alcohol</th>\n",
       "      <th>wine_type</th>\n",
       "      <th>country</th>\n",
       "      <th>grape</th>\n",
       "      <th>rating</th>\n",
       "      <th>pickup_location</th>\n",
       "      <th>vivino_link</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>https://www.winenara.com/shop/product/product_...</td>\n",
       "      <td>winenara</td>\n",
       "      <td>49000</td>\n",
       "      <td>๋ชจ๋…ธํด ํด๋ผ์‹œ์ฝ”</td>\n",
       "      <td>Monopole Classico</td>\n",
       "      <td>https://www.winenara.com/uploads/product/550/1...</td>\n",
       "      <td>3</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>ํ™”์ดํŠธ</td>\n",
       "      <td>์ŠคํŽ˜์ธ</td>\n",
       "      <td></td>\n",
       "      <td>3.8</td>\n",
       "      <td></td>\n",
       "      <td>https://www.vivino.com/monopole-la-rioja-blanc...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>https://www.winenara.com/shop/product/product_...</td>\n",
       "      <td>winenara</td>\n",
       "      <td>32000</td>\n",
       "      <td>์Š๋Ÿผ๋ฒ ๋ฅด๊ฑฐ ๋กœ์ œ ์ŠคํŽ˜์…œ ๋ธŒ๋คผ</td>\n",
       "      <td>Schlumberger Rose Special Brut</td>\n",
       "      <td>https://www.winenara.com/uploads/product/550/d...</td>\n",
       "      <td>3</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>์ŠคํŒŒํด๋ง</td>\n",
       "      <td>๋…์ผ</td>\n",
       "      <td></td>\n",
       "      <td>3.8</td>\n",
       "      <td></td>\n",
       "      <td>https://www.vivino.com/schlumberger-spring-edi...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>https://www.winenara.com/shop/product/product_...</td>\n",
       "      <td>winenara</td>\n",
       "      <td>50000</td>\n",
       "      <td>SET)ํŽ˜๋ฐ๋Ÿด๋ฆฌ์ŠคํŠธ ์ƒค๋ฅด๋„๋„ค ์›ํ†ต ํŒจํ‚ค์ง€</td>\n",
       "      <td>SET)THE FEDERALIST CHARDONNAY</td>\n",
       "      <td>https://www.winenara.com/uploads/product/550/d...</td>\n",
       "      <td>3</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>ํ™”์ดํŠธ</td>\n",
       "      <td>๋ฏธ๊ตญ</td>\n",
       "      <td></td>\n",
       "      <td>3.7</td>\n",
       "      <td></td>\n",
       "      <td>https://www.vivino.com/federalist-chardonnay-m...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>https://www.winenara.com/shop/product/product_...</td>\n",
       "      <td>winenara</td>\n",
       "      <td>55000</td>\n",
       "      <td>๋ฒ ๋‹ˆ์นด ํŠธ๋ ˆ ๋น„๋‹ˆ์Šค</td>\n",
       "      <td>VENICA TRE VIGNIS</td>\n",
       "      <td>https://www.winenara.com/uploads/product/550/c...</td>\n",
       "      <td>4</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>ํ™”์ดํŠธ</td>\n",
       "      <td>์ดํƒˆ๋ฆฌ์•„</td>\n",
       "      <td></td>\n",
       "      <td>3.9</td>\n",
       "      <td></td>\n",
       "      <td>https://www.vivino.com/US-CA/en/venica-venica-...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>https://www.winenara.com/shop/product/product_...</td>\n",
       "      <td>winenara</td>\n",
       "      <td>24900</td>\n",
       "      <td>SET)๋นŒ๋ผ์— ๋น„์•™์ฝ” + ๊ธ€๋ผ์Šค2๊ฐœ ์œˆํ„ฐํŒจํ‚ค์ง€</td>\n",
       "      <td>SET)VILLA M Bianco + GLASS WINTER PACKAGE</td>\n",
       "      <td>https://www.winenara.com/uploads/product/550/a...</td>\n",
       "      <td>-1</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>4</td>\n",
       "      <td>-1</td>\n",
       "      <td>๋””์ €ํŠธ</td>\n",
       "      <td>์ดํƒˆ๋ฆฌ์•„</td>\n",
       "      <td></td>\n",
       "      <td>3.9</td>\n",
       "      <td></td>\n",
       "      <td>https://www.vivino.com/villa-m-bianco/w/1774733</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 url site_name  price  \\\n",
       "0  https://www.winenara.com/shop/product/product_...  winenara  49000   \n",
       "1  https://www.winenara.com/shop/product/product_...  winenara  32000   \n",
       "2  https://www.winenara.com/shop/product/product_...  winenara  50000   \n",
       "3  https://www.winenara.com/shop/product/product_...  winenara  55000   \n",
       "4  https://www.winenara.com/shop/product/product_...  winenara  24900   \n",
       "\n",
       "                       name                                    en_name  \\\n",
       "0                  ๋ชจ๋…ธํด ํด๋ผ์‹œ์ฝ”                          Monopole Classico   \n",
       "1           ์Š๋Ÿผ๋ฒ ๋ฅด๊ฑฐ ๋กœ์ œ ์ŠคํŽ˜์…œ ๋ธŒ๋คผ             Schlumberger Rose Special Brut   \n",
       "2    SET)ํŽ˜๋ฐ๋Ÿด๋ฆฌ์ŠคํŠธ ์ƒค๋ฅด๋„๋„ค ์›ํ†ต ํŒจํ‚ค์ง€              SET)THE FEDERALIST CHARDONNAY   \n",
       "3                ๋ฒ ๋‹ˆ์นด ํŠธ๋ ˆ ๋น„๋‹ˆ์Šค                          VENICA TRE VIGNIS   \n",
       "4  SET)๋นŒ๋ผ์— ๋น„์•™์ฝ” + ๊ธ€๋ผ์Šค2๊ฐœ ์œˆํ„ฐํŒจํ‚ค์ง€  SET)VILLA M Bianco + GLASS WINTER PACKAGE   \n",
       "\n",
       "                                             img_url  body acidity tannin  \\\n",
       "0  https://www.winenara.com/uploads/product/550/1...     3                  \n",
       "1  https://www.winenara.com/uploads/product/550/d...     3                  \n",
       "2  https://www.winenara.com/uploads/product/550/d...     3                  \n",
       "3  https://www.winenara.com/uploads/product/550/c...     4                  \n",
       "4  https://www.winenara.com/uploads/product/550/a...    -1                  \n",
       "\n",
       "   sweetness  alcohol wine_type country grape  rating pickup_location  \\\n",
       "0         -1       -1       ํ™”์ดํŠธ     ์ŠคํŽ˜์ธ           3.8                   \n",
       "1         -1       -1      ์ŠคํŒŒํด๋ง      ๋…์ผ           3.8                   \n",
       "2         -1       -1       ํ™”์ดํŠธ      ๋ฏธ๊ตญ           3.7                   \n",
       "3         -1       -1       ํ™”์ดํŠธ    ์ดํƒˆ๋ฆฌ์•„           3.9                   \n",
       "4          4       -1       ๋””์ €ํŠธ    ์ดํƒˆ๋ฆฌ์•„           3.9                   \n",
       "\n",
       "                                         vivino_link  \n",
       "0  https://www.vivino.com/monopole-la-rioja-blanc...  \n",
       "1  https://www.vivino.com/schlumberger-spring-edi...  \n",
       "2  https://www.vivino.com/federalist-chardonnay-m...  \n",
       "3  https://www.vivino.com/US-CA/en/venica-venica-...  \n",
       "4    https://www.vivino.com/villa-m-bianco/w/1774733  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prepare Langchain Tool"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Tool1: Wine database 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "loader =DataFrameLoader(data_frame=df, page_content_column='name')\n",
    "docs = loader.load()\n",
    "embeddings = OpenAIEmbeddings()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "์•„๋ž˜๋Š” wine database1์— metadata_field Attribute์ด๋‹ค. ์•„๋ž˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์„œ์น˜๋ฅผ ์ง„ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "metadata_field_info = [\n",
    "    AttributeInfo(\n",
    "        name=\"body\",\n",
    "        description=\"1-5 rating for the body of wine\",\n",
    "        type=\"int\",\n",
    "    ),\n",
    "    AttributeInfo(\n",
    "        name=\"sweetness\",\n",
    "        description=\"1-5 rating for the sweetness of wine\",\n",
    "        type=\"int\",\n",
    "    ),\n",
    "    AttributeInfo(\n",
    "        name=\"alcohol\",\n",
    "        description=\"1-5 rating for the alcohol of wine\",\n",
    "        type=\"int\",\n",
    "    ),\n",
    "    AttributeInfo(\n",
    "        name=\"price\",\n",
    "        description=\"The price of the wine\",\n",
    "        type=\"int\",\n",
    "    ),\n",
    "    AttributeInfo(\n",
    "        name=\"rating\", \n",
    "        description=\"1-5 rating for the wine\", \n",
    "        type=\"float\"\n",
    "    ),\n",
    "    AttributeInfo(\n",
    "        name=\"wine_type\", \n",
    "        description=\"The type of wine. It can be '๋ ˆ๋“œ', '๋กœ์ œ', '์ŠคํŒŒํด๋ง', 'ํ™”์ดํŠธ', '๋””์ €ํŠธ', '์ฃผ์ •๊ฐ•ํ™”'\", \n",
    "        type=\"string\"\n",
    "    ),\n",
    "    AttributeInfo(\n",
    "        name=\"country\", \n",
    "        description=\"The country of wine. It can be '๊ธฐํƒ€ ์‹ ๋Œ€๋ฅ™', '๊ธฐํƒ€๊ตฌ๋Œ€๋ฅ™', '๋‰ด์งˆ๋žœ๋“œ', '๋…์ผ', '๋ฏธ๊ตญ', '์ŠคํŽ˜์ธ', '์•„๋ฅดํ—จํ‹ฐ๋‚˜', '์ดํƒˆ๋ฆฌ์•„', '์น ๋ ˆ', 'ํฌ๋ฃจํˆฌ์นผ', 'ํ”„๋ž‘์Šค', 'ํ˜ธ์ฃผ'\", \n",
    "        type=\"float\"\n",
    "    ),\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vectorstore = Chroma.from_documents(docs, embeddings)\n",
    "document_content_description = \"Database of a wine\"\n",
    "llm = OpenAI(temperature=0)\n",
    "retriever = SelfQueryRetriever.from_llm(\n",
    "    llm, vectorstore, document_content_description, metadata_field_info, verbose=True\n",
    ")  # Added missing closing parenthesis\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "query=' ' filter=Operation(operator=<Operator.AND: 'and'>, arguments=[Comparison(comparator=<Comparator.EQ: 'eq'>, attribute='wine_type', value='๋ ˆ๋“œ'), Operation(operator=<Operator.AND: 'and'>, arguments=[Comparison(comparator=<Comparator.GT: 'gt'>, attribute='body', value=0), Comparison(comparator=<Comparator.LT: 'lt'>, attribute='body', value=3)])]) limit=None\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[Document(page_content='๋ฐ” ๋… ํ”ผ๋…ธ๋ˆ„์•„', metadata={'url': 'https://www.winenara.com/shop/product/product_view?product_cd=03P976', 'site_name': 'winenara', 'price': 29000, 'en_name': 'BAR DOG PINOT NOIR', 'img_url': 'https://www.winenara.com/uploads/product/550/1936_detail_084.png', 'body': 2, 'acidity': '', 'tannin': '', 'sweetness': -1, 'alcohol': -1, 'wine_type': '๋ ˆ๋“œ', 'country': '๋ฏธ๊ตญ', 'grape': '', 'rating': 3.6, 'pickup_location': '', 'vivino_link': 'https://www.vivino.com/US-CA/en/bar-dog-pinot-noir/w/7129723'}),\n",
       " Document(page_content='๋ฃจ์ด๋ผ๋šœ๋ฅด ํ”ผ๋…ธ๋ˆ„์•„', metadata={'url': 'https://www.winenara.com/shop/product/product_view?product_cd=03H965', 'site_name': 'winenara', 'price': 52000, 'en_name': 'LOUIS LATOUR PINOT NOIR', 'img_url': 'https://www.winenara.com/uploads/product/550/493_detail_025.png', 'body': 2, 'acidity': '', 'tannin': '', 'sweetness': -1, 'alcohol': -1, 'wine_type': '๋ ˆ๋“œ', 'country': 'ํ”„๋ž‘์Šค', 'grape': '', 'rating': 3.6, 'pickup_location': '', 'vivino_link': 'https://www.vivino.com/GB/en/louis-latour-bourgogne-pinot-noir/w/7343'}),\n",
       " Document(page_content='๋ฃจ์ด๋ผ๋šœ๋ฅด ์ƒ๋œจ๋„ค', metadata={'url': 'https://www.winenara.com/shop/product/product_view?product_cd=03P299', 'site_name': 'winenara', 'price': 79000, 'en_name': 'LOUIS LATOUR SANTENAY', 'img_url': 'https://www.winenara.com/uploads/product/550/489_detail_096.png', 'body': 2, 'acidity': '', 'tannin': '', 'sweetness': -1, 'alcohol': -1, 'wine_type': '๋ ˆ๋“œ', 'country': 'ํ”„๋ž‘์Šค', 'grape': '', 'rating': 3.8, 'pickup_location': '', 'vivino_link': 'https://www.vivino.com/louis-latour-santenay-rouge/w/7369'}),\n",
       " Document(page_content='๋น„์•Œ๋ ˆ๋˜ ๋กœ์†Œ', metadata={'url': 'https://www.winenara.com/shop/product/product_view?product_cd=033704', 'site_name': 'winenara', 'price': 12000, 'en_name': 'VIALETTO ROSSO', 'img_url': 'https://www.winenara.com/uploads/product/550/d1ef6058de3661b565084b815e359852.png', 'body': 2, 'acidity': '', 'tannin': '', 'sweetness': -1, 'alcohol': -1, 'wine_type': '๋ ˆ๋“œ', 'country': '์ดํƒˆ๋ฆฌ์•„', 'grape': '', 'rating': 3.1, 'pickup_location': '', 'vivino_link': 'https://www.vivino.com/US-CA/en/vialetto-rosso-dolce/w/2213764'})]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "retriever.get_relevant_documents('{\"wine_type\":\"๋ ˆ๋“œ\", \"body\": \"lt 3 gt 0\"}') # gt means greater than, lt means less than, eq means equal to"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Tool2: Search specific wine with url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def search_with_url(query):\n",
    "    return SeleniumURLLoader(urls=[query]).load()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Tool3: Wine database 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "index = VectorstoreIndexCreator(\n",
    "    vectorstore_cls=DocArrayInMemorySearch\n",
    ").from_loaders([loader])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Tool4: Search in Google"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "search = SerpAPIWrapper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tools = [\n",
    "    Tool(\n",
    "        name=\"Wine database\",\n",
    "        func=retriever.get_relevant_documents,\n",
    "        description=\"\"\"\n",
    "Database about the wines in wine store. You can get information such as the price of the wine, purchase URL, features, rating information, and more.\n",
    "You can search wines with the following attributes:\n",
    "- body: 1-5 rating int for the body of wine. You have to specify greater than or less than. For example, if you want to search for wines with a body rating of less than 3, enter 'body: gt 0 lt 3'\n",
    "- price: The price range of the wine. Please enter the price range in the form of range. For example, if you want to search for wines that cost less than 20,000 won, enter 'price: gt 0 lt20000'\n",
    "- rating: 1-5 rating float for the wine. You have to specify greater than or less than. For example, if you want to search for wines with a rating of less than 3, enter 'rating: gt 0 lt 3'\n",
    "- wine_type: The type of wine. It can be '๋ ˆ๋“œ', '๋กœ์ œ', '์ŠคํŒŒํด๋ง', 'ํ™”์ดํŠธ', '๋””์ €ํŠธ', '์ฃผ์ •๊ฐ•ํ™”'\n",
    "- name: The name of wine. ์ž…๋ ฅํ•  ๋•Œ๋Š” '์™€์ธ ์ด๋ฆ„์€ \"๋น„๋ƒ ์กฐ์ž˜\" ์ž…๋‹ˆ๋‹ค' ์ด๋Ÿฐ ์‹์œผ๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.\n",
    "\"\"\"\n",
    "    ),\n",
    "    Tool(\n",
    "        name = \"Search specific wine with url\",\n",
    "        func=search_with_url,\n",
    "        description=\"Search specific wine with url. Query must be url\"\n",
    "    ),\n",
    "    Tool(\n",
    "        name = \"Wine database 2\",\n",
    "        func=index.query,\n",
    "        description=\"Database about the wines in wine store. You can use this tool if you're having trouble getting information from the wine database tool above. Query must be in String\"\n",
    "    ),\n",
    "    Tool(\n",
    "        name = \"Search\",\n",
    "        func=search.run,\n",
    "        description=\"Useful for when you need to ask with search. Search in English only.\"\n",
    "    ),\n",
    "]"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}