File size: 24,636 Bytes
5838cbb
 
 
 
7976fe2
5838cbb
 
 
15a19f2
5838cbb
7976fe2
1b5e4ab
5838cbb
 
1b5e4ab
 
5838cbb
 
 
 
 
 
7976fe2
6ee98e6
 
 
7da8c71
8c3c6c0
15a19f2
 
6ee98e6
 
 
 
7976fe2
5838cbb
 
 
 
 
 
 
 
 
 
8c3c6c0
 
 
 
 
 
 
 
 
 
7976fe2
8c3c6c0
 
 
 
15a19f2
8c3c6c0
 
 
6ee98e6
 
 
 
 
 
 
 
5838cbb
 
7976fe2
1b5e4ab
 
 
 
 
 
8c3c6c0
1b5e4ab
 
 
 
7da8c71
 
1b5e4ab
 
 
 
 
 
15a19f2
 
 
 
 
 
 
 
1b5e4ab
 
 
 
 
7976fe2
6ee98e6
7da8c71
 
8c3c6c0
 
15a19f2
 
 
 
 
 
 
 
 
 
8c3c6c0
 
7976fe2
8c3c6c0
 
7da8c71
 
1b5e4ab
 
8c3c6c0
 
1b5e4ab
 
6ee98e6
 
 
 
 
15a19f2
6ee98e6
 
1b5e4ab
 
7976fe2
5838cbb
 
 
15a19f2
 
5838cbb
15a19f2
 
 
 
 
 
1b5e4ab
15a19f2
 
8c3c6c0
15a19f2
 
7976fe2
6ee98e6
7976fe2
15a19f2
 
 
 
 
 
 
 
 
 
 
 
1b5e4ab
 
 
 
7976fe2
1b5e4ab
15a19f2
 
 
 
 
7976fe2
15a19f2
 
 
1b5e4ab
15a19f2
6ee98e6
 
7976fe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ee98e6
 
 
 
 
7da8c71
1b5e4ab
 
 
 
7976fe2
1b5e4ab
6ee98e6
1b5e4ab
15a19f2
 
 
 
 
 
 
 
 
 
 
 
 
8c3c6c0
 
 
 
7976fe2
1b5e4ab
7da8c71
 
 
 
 
8c3c6c0
7da8c71
 
 
1b5e4ab
15a19f2
8c3c6c0
 
 
 
7976fe2
8c3c6c0
 
 
15a19f2
 
 
8c3c6c0
 
 
 
7976fe2
8c3c6c0
 
 
15a19f2
 
1b5e4ab
 
 
 
7976fe2
8c3c6c0
 
 
 
 
 
 
 
7976fe2
1b5e4ab
7da8c71
 
8c3c6c0
 
7976fe2
8c3c6c0
 
7976fe2
8c3c6c0
 
 
 
 
 
 
 
 
 
7976fe2
8c3c6c0
 
 
 
 
7976fe2
 
15a19f2
 
7976fe2
15a19f2
 
 
 
7976fe2
8c3c6c0
 
7976fe2
8c3c6c0
 
7da8c71
 
1b5e4ab
8c3c6c0
1b5e4ab
7976fe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5838cbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7da8c71
5838cbb
 
 
 
 
 
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm\n",
    "import pandas as pd\n",
    "import datetime\n",
    "import openai\n",
    "import praw\n",
    "import os\n",
    "import re\n",
    "\n",
    "pd.set_option('max_colwidth', 100)\n",
    "pd.set_option('display.max_columns', None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.vectorstores import Chroma\n",
    "from langchain.chains import ConversationalRetrievalChain\n",
    "from langchain.llms import OpenAI\n",
    "from langchain.embeddings import TensorflowHubEmbeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "REDDIT_CLIENT_ID = os.environ.get('REDDIT_CLIENT_ID')\n",
    "REDDIT_CLIENT_SECRET = os.environ.get('REDDIT_CLIENT_SECRET')\n",
    "REDDIT_USER_AGENT = os.environ.get('REDDIT_USER_AGENT')\n",
    "\n",
    "reddit = praw.Reddit(client_id=REDDIT_CLIENT_ID, client_secret=REDDIT_CLIENT_SECRET, user_agent=REDDIT_USER_AGENT)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "query = '''\n",
    "I got laid off last week. How should I go about finding a new job?\n",
    "'''"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Topic Retrieval"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_topics(query, model=\"gpt-3.5-turbo\"):\n",
    "\n",
    "    messages = [\n",
    "        {\"role\": \"user\", \"content\": f\"Take this query '{query}' and return a list of 10 simple to understand topics (4 words or less) to input in Search so it returns good results.\"},\n",
    "    ]\n",
    "\n",
    "    response = openai.ChatCompletion.create(\n",
    "        model=model,\n",
    "        messages=messages,\n",
    "        temperature=0\n",
    "    )\n",
    "\n",
    "    response_message = response[\"choices\"][0][\"message\"][\"content\"]\n",
    "\n",
    "    topics = re.sub(r'^\\d+\\.\\s*', '', response_message, flags=re.MULTILINE).split(\"\\n\")\n",
    "\n",
    "    # Post-processing GPT output\n",
    "\n",
    "    topics = [topic.strip() for topic in topics]\n",
    "\n",
    "    topics = [topic[1:-1] if (topic.startswith('\"') and topic.endswith('\"')) or (topic.startswith(\"'\") and topic.endswith(\"'\")) else topic for topic in topics]\n",
    "\n",
    "    topics = [re.sub(r'[^a-zA-Z0-9\\s]', ' ', topic) for topic in topics]\n",
    "\n",
    "    return topics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Job search tips',\n",
       " 'Resume writing advice',\n",
       " 'Networking strategies',\n",
       " 'Interview preparation tips',\n",
       " 'Online job boards',\n",
       " 'Career counseling services',\n",
       " 'Job fairs near me',\n",
       " 'Freelance opportunities',\n",
       " 'Remote work options',\n",
       " 'Industry specific job listings']"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "topics = generate_topics(query)\n",
    "\n",
    "topics"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Relevant Comments Retrieval"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_relevant_subreddits(topics):\n",
    "    comments = []\n",
    "\n",
    "    for topic in tqdm(topics):\n",
    "        for post in reddit.subreddit(\"all\").search(\n",
    "        topic, limit=10):\n",
    "            \n",
    "            post.comment_limit = 20\n",
    "            post.comment_sort = \"top\"\n",
    "\n",
    "            # Top level comments only\n",
    "            post.comments.replace_more(limit=0)\n",
    "\n",
    "            for comment in post.comments:\n",
    "                author = comment.author.name if comment.author else '[deleted]'\n",
    "                comments.append([post.id, comment.id, post.subreddit.display_name, post.title, author, comment.body, datetime.datetime.fromtimestamp(comment.created).strftime('%Y-%m')])\n",
    "\n",
    "    comments = pd.DataFrame(comments,columns=['source', 'comment_id', 'subreddit', 'title', 'author', 'text', 'date'])\n",
    "\n",
    "    # Drop empty texts or [\"deleted\"] texts\n",
    "    comments = comments[comments['text'].str.len() > 0]\n",
    "    comments = comments[comments['text'] != \"[deleted]\"]\n",
    "\n",
    "    # Drop comments with None authors\n",
    "    comments = comments[comments['author'] != \"AutoModerator\"]\n",
    "\n",
    "    # Drop duplicate ids\n",
    "    comments = comments.drop_duplicates(subset=['source'])\n",
    "\n",
    "    return comments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:40<00:00,  4.05s/it]\n"
     ]
    }
   ],
   "source": [
    "comments = get_relevant_subreddits(topics)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "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>source</th>\n",
       "      <th>comment_id</th>\n",
       "      <th>subreddit</th>\n",
       "      <th>title</th>\n",
       "      <th>author</th>\n",
       "      <th>text</th>\n",
       "      <th>date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>106m8e3</td>\n",
       "      <td>j3hcif8</td>\n",
       "      <td>funny</td>\n",
       "      <td>Job search tips, update your profile.</td>\n",
       "      <td>saltinstiens_monster</td>\n",
       "      <td>\"Assistant Emperor?\"\\n\\n\"Assistant *to the* Emperor.\"</td>\n",
       "      <td>2023-01-08 17:15:27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10q4pf6</td>\n",
       "      <td>j6nz560</td>\n",
       "      <td>recruitinghell</td>\n",
       "      <td>“Job Search Tips From A Recruiter” thoughts?</td>\n",
       "      <td>[deleted]</td>\n",
       "      <td>1. I work in tech. 99% of the time, tech recruiters don't know anything about the skills the tea...</td>\n",
       "      <td>2023-01-31 19:00:15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>134wnu5</td>\n",
       "      <td>jih9zzp</td>\n",
       "      <td>blackladies</td>\n",
       "      <td>Does anyone have any good job search tips?</td>\n",
       "      <td>Carolinablue87</td>\n",
       "      <td>I recommend keeping an up to date LinkedIn profile that indicates you're actively searching for ...</td>\n",
       "      <td>2023-05-01 22:52:38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>12h0uya</td>\n",
       "      <td>jfmy0a1</td>\n",
       "      <td>physicaltherapy</td>\n",
       "      <td>Job search tips</td>\n",
       "      <td>tunaman4u2</td>\n",
       "      <td>Indeed yes. Sell yourself and don’t take the first offer, be prepared to negotiate. Companies wi...</td>\n",
       "      <td>2023-04-10 02:18:46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>11uekx5</td>\n",
       "      <td>jcnvfnx</td>\n",
       "      <td>respiratorytherapy</td>\n",
       "      <td>Job search tips</td>\n",
       "      <td>Crass_Cameron</td>\n",
       "      <td>Just apply, covid has made super easy for RTs to get jobs</td>\n",
       "      <td>2023-03-18 06:02:52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>251</th>\n",
       "      <td>12d4zd4</td>\n",
       "      <td>jf4w9av</td>\n",
       "      <td>Birmingham</td>\n",
       "      <td>Bartender moving from outta state. Does bham restaurant industry use any specific online job sou...</td>\n",
       "      <td>minorujco</td>\n",
       "      <td>Croux app used to find some gigs in Birmingham before covid, but haven't used it since. I heard ...</td>\n",
       "      <td>2023-04-06 04:13:35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>260</th>\n",
       "      <td>yn7bze</td>\n",
       "      <td>iv7ekcc</td>\n",
       "      <td>biotech</td>\n",
       "      <td>Best listings to find a job in biotech/pharma industry?</td>\n",
       "      <td>tomatotornado420</td>\n",
       "      <td>Linked/indeed. Early career process engineer.</td>\n",
       "      <td>2022-11-05 22:50:07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>269</th>\n",
       "      <td>13bhzdm</td>\n",
       "      <td>jjbacyo</td>\n",
       "      <td>singapore</td>\n",
       "      <td>Industries must show support for local training for jobs to be added to shortage occupation list...</td>\n",
       "      <td>worldcitizensg</td>\n",
       "      <td>Sorry.. Why ? If industry want more \"EP\" then more incentive not to train the locals so the job ...</td>\n",
       "      <td>2023-05-08 10:34:15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>273</th>\n",
       "      <td>138m3nb</td>\n",
       "      <td>jiypppa</td>\n",
       "      <td>dataisbeautiful</td>\n",
       "      <td>[OC] Analyzing 15,963 Job Listings to Uncover the Top Skills for Data Analysts (update)</td>\n",
       "      <td>restore_democracy</td>\n",
       "      <td>Excel and SQL?  We haven’t come far in 30 years.</td>\n",
       "      <td>2023-05-05 16:02:36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>281</th>\n",
       "      <td>10dvylw</td>\n",
       "      <td>j4nkg28</td>\n",
       "      <td>vfx</td>\n",
       "      <td>Is specializing in a specific type of VFX important to get a better chance at finding a job in t...</td>\n",
       "      <td>redddcrow</td>\n",
       "      <td>Most likely yes. unless you want to be a generalist in which case you need to be good at everyth...</td>\n",
       "      <td>2023-01-17 00:42:44</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>61 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      source comment_id           subreddit  \\\n",
       "0    106m8e3    j3hcif8               funny   \n",
       "9    10q4pf6    j6nz560      recruitinghell   \n",
       "19   134wnu5    jih9zzp         blackladies   \n",
       "23   12h0uya    jfmy0a1     physicaltherapy   \n",
       "28   11uekx5    jcnvfnx  respiratorytherapy   \n",
       "..       ...        ...                 ...   \n",
       "251  12d4zd4    jf4w9av          Birmingham   \n",
       "260   yn7bze    iv7ekcc             biotech   \n",
       "269  13bhzdm    jjbacyo           singapore   \n",
       "273  138m3nb    jiypppa     dataisbeautiful   \n",
       "281  10dvylw    j4nkg28                 vfx   \n",
       "\n",
       "                                                                                                   title  \\\n",
       "0                                                                  Job search tips, update your profile.   \n",
       "9                                                           “Job Search Tips From A Recruiter” thoughts?   \n",
       "19                                                            Does anyone have any good job search tips?   \n",
       "23                                                                                       Job search tips   \n",
       "28                                                                                       Job search tips   \n",
       "..                                                                                                   ...   \n",
       "251  Bartender moving from outta state. Does bham restaurant industry use any specific online job sou...   \n",
       "260                                              Best listings to find a job in biotech/pharma industry?   \n",
       "269  Industries must show support for local training for jobs to be added to shortage occupation list...   \n",
       "273              [OC] Analyzing 15,963 Job Listings to Uncover the Top Skills for Data Analysts (update)   \n",
       "281  Is specializing in a specific type of VFX important to get a better chance at finding a job in t...   \n",
       "\n",
       "                   author  \\\n",
       "0    saltinstiens_monster   \n",
       "9               [deleted]   \n",
       "19         Carolinablue87   \n",
       "23             tunaman4u2   \n",
       "28          Crass_Cameron   \n",
       "..                    ...   \n",
       "251             minorujco   \n",
       "260      tomatotornado420   \n",
       "269        worldcitizensg   \n",
       "273     restore_democracy   \n",
       "281             redddcrow   \n",
       "\n",
       "                                                                                                    text  \\\n",
       "0                                                  \"Assistant Emperor?\"\\n\\n\"Assistant *to the* Emperor.\"   \n",
       "9    1. I work in tech. 99% of the time, tech recruiters don't know anything about the skills the tea...   \n",
       "19   I recommend keeping an up to date LinkedIn profile that indicates you're actively searching for ...   \n",
       "23   Indeed yes. Sell yourself and don’t take the first offer, be prepared to negotiate. Companies wi...   \n",
       "28                                             Just apply, covid has made super easy for RTs to get jobs   \n",
       "..                                                                                                   ...   \n",
       "251  Croux app used to find some gigs in Birmingham before covid, but haven't used it since. I heard ...   \n",
       "260                                                        Linked/indeed. Early career process engineer.   \n",
       "269  Sorry.. Why ? If industry want more \"EP\" then more incentive not to train the locals so the job ...   \n",
       "273                                                     Excel and SQL?  We haven’t come far in 30 years.   \n",
       "281  Most likely yes. unless you want to be a generalist in which case you need to be good at everyth...   \n",
       "\n",
       "                   date  \n",
       "0   2023-01-08 17:15:27  \n",
       "9   2023-01-31 19:00:15  \n",
       "19  2023-05-01 22:52:38  \n",
       "23  2023-04-10 02:18:46  \n",
       "28  2023-03-18 06:02:52  \n",
       "..                  ...  \n",
       "251 2023-04-06 04:13:35  \n",
       "260 2022-11-05 22:50:07  \n",
       "269 2023-05-08 10:34:15  \n",
       "273 2023-05-05 16:02:36  \n",
       "281 2023-01-17 00:42:44  \n",
       "\n",
       "[61 rows x 7 columns]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "comments"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answering Query with Langchain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def construct_retriever(comments, k=10):\n",
    "\n",
    "    # Convert comments dataframe to a dictionary\n",
    "    comments = comments.to_dict('records')\n",
    "\n",
    "    # Convert comments[\"text\"] to a list of strings\n",
    "    texts = [comment[\"title\"] + \" \" + comment[\"text\"] for comment in comments]\n",
    "\n",
    "    db = Chroma.from_texts(texts, TensorflowHubEmbeddings(model_url=\"https://tfhub.dev/google/universal-sentence-encoder/4\"), metadatas=[{\"source\": comment[\"source\"], \"comment_id\": comment[\"comment_id\"], \"author\": comment[\"author\"]} for comment in comments])\n",
    "\n",
    "    retriever = db.as_retriever(search_type=\"similarity\", search_kwargs={\"k\": k})\n",
    "\n",
    "    return retriever"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:chromadb:Using embedded DuckDB without persistence: data will be transient\n"
     ]
    }
   ],
   "source": [
    "retriever = construct_retriever(comments)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def construct_qa(retriever):\n",
    "    qa = ConversationalRetrievalChain.from_llm(OpenAI(temperature=0), retriever, return_source_documents=True)\n",
    "    return qa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "chat_history = []\n",
    "qa = construct_qa(retriever)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = qa({\"question\": query, \"chat_history\": chat_history})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"\\nFinding a new job can be a daunting task, especially in the current climate. My best advice is to start by updating your resume and LinkedIn profile to make sure they are up to date and reflect your current skills and experience. You should also reach out to your network of contacts to let them know you are looking for a new job and ask if they know of any opportunities. Additionally, you should look into job search websites such as Indeed and Craigslist, as well as staffing agencies that specialize in your field. Finally, don't forget to take advantage of any career counseling services that may be available to you. Good luck!\""
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[\"answer\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(page_content=\"Would a staffing agency be able to provide me with stable income, even if any given employer doesn't decide to hire me after the temp period? It's entirely possible to still go without a job while with a staffing agency. Just depends on what they have available.\", metadata={'source': '12vixm8', 'comment_id': 'jhc0br3', 'author': 'whotiesyourshoes'}),\n",
       " Document(page_content='Request: job search tips WAITING FOR THE RIGHT JOB WITH DECENT PAY > QUICK JOB', metadata={'source': '122mdcc', 'comment_id': 'jdv57nv', 'author': 'notenoughbeds'}),\n",
       " Document(page_content='Where to look for jobs? Online job boards? LinkedIn\\n\\nIndeed\\n\\nIf you need a... beginners job. Craigslist has a work section', metadata={'source': 'uj35l8', 'comment_id': 'i7gbuat', 'author': 'No-Statement-3019'}),\n",
       " Document(page_content='Job search tips in Canada You’re selling yourself way too cheap. Look for a senior position and then people will want to hire you more too', metadata={'source': '1156zsa', 'comment_id': 'j91mo9r', 'author': 'pxpxy'}),\n",
       " Document(page_content='What temp agencies hire for UPS and was it a good experience? I saw they will be hiring 95K people for the holidays. I did that gig. I went on for 6 1/2 more years.    Just go down to UPS and apply.  The holiday help means you\\'ll out with the driver.  His truck is gonna be stupid full and you two will work until its done. I live in Minnesota, so mine wasn\\'t an \"easy\" experience. Multiple pants were not even keeping my ass warm.            After that I applied to become a loader. They are more apt to hire you since you did the holiday help. The driver put in a good word for me too.          The pay is good for a temp gig, and you\\'ll get good exercise.', metadata={'source': '2gnjhi', 'comment_id': 'ckks20u', 'author': 'seathian'}),\n",
       " Document(page_content='Job search tips Just apply, covid has made super easy for RTs to get jobs', metadata={'source': '11uekx5', 'comment_id': 'jcnvfnx', 'author': 'Crass_Cameron'}),\n",
       " Document(page_content='Looking for freelance opportunities >\\tWhere would be the best place to look?\\n\\nPeople you’ve worked with professionally before. Either for jobs for them or for them to refer you to people they know.', metadata={'source': '11q0h1u', 'comment_id': 'jc0w8rp', 'author': 'dataguy24'}),\n",
       " Document(page_content='Did Career Counseling services help you land a job after you graduated? I found it to be helpful in polishing my resume', metadata={'source': 'xbql4p', 'comment_id': 'io1141n', 'author': 'avo_cado'}),\n",
       " Document(page_content=\"Does anyone have any good job search tips? I recommend keeping an up to date LinkedIn profile that indicates you're actively searching for roles. I've also had luck with Indeed.\\n\\nDepending on your field, I recommend a staffing agency.  They can vouch for you and place you with a company that's a great fit.\", metadata={'source': '134wnu5', 'comment_id': 'jih9zzp', 'author': 'Carolinablue87'}),\n",
       " Document(page_content=\"Even jobs at temp agencies aren't hiring This is a hard question to answer without knowing more about your experience but I can try and help. \\n\\nIf I’m reading this correctly there are two jobs, the first one you interviewed for and they clearly decided it wasn’t the right fit. The second one, if it can be either FT or PT, my guess is the agency is probably prioritizing finding someone full-time because they’ll get more financial return on it.\\n\\nTo answer the bigger question though, like I said it is tough without knowing more about your experience but you’ll probably need to find a agency that specializes in staffing PT work. Most firms aren’t going to work on many PT jobs for the reason above from my experience. And most companies aren’t going to pay a firm to find someone to do basic office tasks for 15-20 hours a week. Honest opinion, hope that helps.\", metadata={'source': '114q8oh', 'comment_id': 'j903ge4', 'author': 'Rhombus-Lion-1'})]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[\"source_documents\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[\"chat_history\"]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.10.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}