File size: 30,228 Bytes
53311da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Welcome to Lab 3 for Week 1 Day 4\n",
    "\n",
    "Today we're going to build something with immediate value!\n",
    "\n",
    "In the folder `me` I've put a single file `linkedin.pdf` - it's a PDF download of my LinkedIn profile.\n",
    "\n",
    "Please replace it with yours!\n",
    "\n",
    "I've also made a file called `summary.txt`\n",
    "\n",
    "We're not going to use Tools just yet - we're going to add the tool tomorrow."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<table style=\"margin: 0; text-align: left; width:100%\">\n",
    "    <tr>\n",
    "        <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
    "            <img src=\"../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
    "        </td>\n",
    "        <td>\n",
    "            <h2 style=\"color:#00bfff;\">Looking up packages</h2>\n",
    "            <span style=\"color:#00bfff;\">In this lab, we're going to use the wonderful Gradio package for building quick UIs, \n",
    "            and we're also going to use the popular PyPDF PDF reader. You can get guides to these packages by asking \n",
    "            ChatGPT or Claude, and you find all open-source packages on the repository <a href=\"https://pypi.org\">https://pypi.org</a>.\n",
    "            </span>\n",
    "        </td>\n",
    "    </tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# If you don't know what any of these packages do - you can always ask ChatGPT for a guide!\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "from openai import OpenAI\n",
    "from pypdf import PdfReader\n",
    "import gradio as gr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "load_dotenv(override=True)\n",
    "openai = OpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "reader = PdfReader(\"me/amitlinkedinProfile.pdf\")\n",
    "linkedin = \"\"\n",
    "for page in reader.pages:\n",
    "    text = page.extract_text()\n",
    "    if text:\n",
    "        linkedin += text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   \n",
      "Contact\n",
      "156/8 Double Storey Govindpuri,\n",
      "Modinagar, Ghaziabad, Uttar\n",
      "Pradesh. Pin-201201\n",
      "7302960092 (Mobile)\n",
      "amit1508g@gmail.com\n",
      "www.linkedin.com/in/amit-\n",
      "kumar-1508g (LinkedIn)\n",
      "partner.cloudskillsboost.google/\n",
      "public_profiles/\n",
      "eb3ebb58-85c7-414d-\n",
      "bf08-2742c4448f56 (Other)\n",
      "Top Skills\n",
      "Docker \n",
      "Microsoft Azure AI\n",
      "Automation testing\n",
      "Languages\n",
      "English (Professional Working)\n",
      "Hindi (Native or Bilingual)\n",
      "Certifications\n",
      "Fundamentals of Agentic AI:\n",
      "Public speaking\n",
      "Defending Against Ransomware\n",
      "Mobile Testing with Appium\n",
      "AWS\n",
      "Amit Kumar\n",
      "Senior Test Engineer at HCLTech 4+ YOE, Noida | Web, Mobile and\n",
      "Desktop Automation Tester | Selenium, Appium, Java l Agentic AI\n",
      "Enthusiast | Devops\n",
      "Ghaziabad, Uttar Pradesh, India\n",
      "Summary\n",
      "1. Software Engineer with 4+ extensive experience in Testing\n",
      "(Manual and Automation)\n",
      "2. Extensive experience of 4+ years in Automating Web Application\n",
      "Testing using Selenium WebDriver with TestNG framework, JUnit,\n",
      "and Cucumber.\n",
      "3. Strong Experience in Java, Appium. Comfortable working with\n",
      "Maven and Jenkins for CI/CD.\n",
      "Well versed with Handling Elements using Page Object Model and\n",
      "experience in Data driven, Hybrid and BDD frameworks.\n",
      "4. Experience in leading the team to build the Automation Projects\n",
      "from scratch.\n",
      "5. Expertise in handling test automation projects from understanding\n",
      "the business scenarios, creating test plan, develop test cases and\n",
      "execute the jobs on continuous integration tools.\n",
      "6. Good knowledge on XML, HTML and CSS. Has the expertise in\n",
      "PostgreSql & MySQL, Microsoft Azure AI. \n",
      "7. Comfortable working in Windows, Mac & Linux environments.\n",
      "8. Has good experience in Integration and Regression Testing,\n",
      "Evaluation of test results, Defect management and coordinating with\n",
      "Product Owners & Dev team.\n",
      "Experience\n",
      "HCLTech\n",
      "  Page 1 of 2   \n",
      "4 years 10 months\n",
      "Senior Test Engineer\n",
      "October 2024 - Present (11 months)\n",
      "Software Engineer\n",
      "November 2020 - Present (4 years 10 months)\n",
      "Noida, Uttar Pradesh, India\n",
      "Education\n",
      "Shanmugha Arts, Science, Technology & Reserch Academy\n",
      "(SASTRA), Thanjavur\n",
      "Master of Computer Applications - MCA, Computer Science · (March\n",
      "2024 - April 2026)\n",
      "Shanmugha Arts, Science, Technology & Reserch Academy\n",
      "(SASTRA), Thanjavur\n",
      "Bachelor of Computer Application , Computer Science  · (March 2021 - March\n",
      "2024)\n",
      "Shiv Nadar University\n",
      "TechBee, HCL TSS Training, Software Testing · (July 2019 - July 2020)\n",
      "Chhaya Public School \n",
      "12 PCM+CS, CS  · (May 2018 - May 2019)\n",
      "  Page 2 of 2\n"
     ]
    }
   ],
   "source": [
    "print(linkedin)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n",
    "    summary = f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"Amit Kumar\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "system_prompt = f\"You are acting as {name}. You are answering questions on {name}'s website, \\\n",
    "particularly questions related to {name}'s career, background, skills and experience. \\\n",
    "Your responsibility is to represent {name} for interactions on the website as faithfully as possible. \\\n",
    "You are given a summary of {name}'s background and LinkedIn profile which you can use to answer questions. \\\n",
    "Be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
    "If you don't know the answer, say so.\"\n",
    "\n",
    "system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
    "system_prompt += f\"With this context, please chat with the user, always staying in character as {name}.\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"You are acting as Amit Kumar. You are answering questions on Amit Kumar's website, particularly questions related to Amit Kumar's career, background, skills and experience. Your responsibility is to represent Amit Kumar for interactions on the website as faithfully as possible. You are given a summary of Amit Kumar's background and LinkedIn profile which you can use to answer questions. Be professional and engaging, as if talking to a potential client or future employer who came across the website. If you don't know the answer, say so.\\n\\n## Summary:\\nMy name is Amit Kumar. I'm a Senior Test Engineer at HCLTech in Noida, with over four years of extensive experience in manual and automation testing. I specialize in web, mobile, and desktop application testing using technologies like Selenium, Appium, and Java.\\n\\nI'm an Agentic AI enthusiast with certifications in AWS, Docker, and Microsoft Azure AI. My professional expertise includes building automation frameworks from the ground up, managing CI/CD pipelines with Maven and Jenkins, and handling defect management. I'm comfortable working across Windows, Mac, and Linux operating systems. I am a native Hindi speaker and am professionally fluent in English.\\nI love automation, and have done hands on automation of web and mobile(android and ios application)\\nI am currently working in HCLTech with 4+ year of experience.\\n\\n\\n\\n## LinkedIn Profile:\\n\\xa0 \\xa0\\nContact\\n156/8 Double Storey Govindpuri,\\nModinagar, Ghaziabad, Uttar\\nPradesh. Pin-201201\\n7302960092 (Mobile)\\namit1508g@gmail.com\\nwww.linkedin.com/in/amit-\\nkumar-1508g (LinkedIn)\\npartner.cloudskillsboost.google/\\npublic_profiles/\\neb3ebb58-85c7-414d-\\nbf08-2742c4448f56 (Other)\\nTop Skills\\nDocker \\nMicrosoft Azure AI\\nAutomation testing\\nLanguages\\nEnglish (Professional Working)\\nHindi (Native or Bilingual)\\nCertifications\\nFundamentals of Agentic AI:\\nPublic speaking\\nDefending Against Ransomware\\nMobile Testing with Appium\\nAWS\\nAmit Kumar\\nSenior Test Engineer at HCLTech 4+ YOE, Noida | Web, Mobile and\\nDesktop Automation Tester | Selenium, Appium, Java l Agentic AI\\nEnthusiast | Devops\\nGhaziabad, Uttar Pradesh, India\\nSummary\\n1. Software Engineer with 4+ extensive experience in Testing\\n(Manual and Automation)\\n2. Extensive experience of 4+ years in Automating Web Application\\nTesting using Selenium WebDriver with TestNG framework, JUnit,\\nand Cucumber.\\n3. Strong Experience in Java, Appium. Comfortable working with\\nMaven and Jenkins for CI/CD.\\nWell versed with Handling Elements using Page Object Model and\\nexperience in Data driven, Hybrid and BDD frameworks.\\n4. Experience in leading the team to build the Automation Projects\\nfrom scratch.\\n5. Expertise in handling test automation projects from understanding\\nthe business scenarios, creating test plan, develop test cases and\\nexecute the jobs on continuous integration tools.\\n6. Good knowledge on XML, HTML and CSS. Has the expertise in\\nPostgreSql & MySQL, Microsoft Azure AI. \\n7. Comfortable working in Windows, Mac & Linux environments.\\n8. Has good experience in Integration and Regression Testing,\\nEvaluation of test results, Defect management and coordinating with\\nProduct Owners & Dev team.\\nExperience\\nHCLTech\\n\\xa0 Page 1 of 2\\xa0 \\xa0\\n4 years 10 months\\nSenior Test Engineer\\nOctober 2024\\xa0-\\xa0Present\\xa0(11 months)\\nSoftware Engineer\\nNovember 2020\\xa0-\\xa0Present\\xa0(4 years 10 months)\\nNoida, Uttar Pradesh, India\\nEducation\\nShanmugha Arts, Science, Technology & Reserch Academy\\n(SASTRA), Thanjavur\\nMaster of Computer Applications - MCA,\\xa0Computer Science\\xa0·\\xa0(March\\n2024\\xa0-\\xa0April 2026)\\nShanmugha Arts, Science, Technology & Reserch Academy\\n(SASTRA), Thanjavur\\nBachelor of Computer Application ,\\xa0Computer Science \\xa0·\\xa0(March 2021\\xa0-\\xa0March\\n2024)\\nShiv Nadar University\\nTechBee, HCL TSS Training, Software Testing\\xa0·\\xa0(July 2019\\xa0-\\xa0July 2020)\\nChhaya Public School \\n12 PCM+CS, CS\\xa0\\xa0·\\xa0(May 2018\\xa0-\\xa0May 2019)\\n\\xa0 Page 2 of 2\\n\\nWith this context, please chat with the user, always staying in character as Amit Kumar.\""
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "system_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "def chat(message, history):\n",
    "    messages = [{\"role\": \"system\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
    "    response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
    "    return response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Special note for people not using OpenAI\n",
    "\n",
    "Some providers, like Groq, might give an error when you send your second message in the chat.\n",
    "\n",
    "This is because Gradio shoves some extra fields into the history object. OpenAI doesn't mind; but some other models complain.\n",
    "\n",
    "If this happens, the solution is to add this first line to the chat() function above. It cleans up the history variable:\n",
    "\n",
    "```python\n",
    "history = [{\"role\": h[\"role\"], \"content\": h[\"content\"]} for h in history]\n",
    "```\n",
    "\n",
    "You may need to add this in other chat() callback functions in the future, too."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "* Running on local URL:  http://127.0.0.1:7864\n",
      "* To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7864/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gr.ChatInterface(chat, type=\"messages\").launch()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A lot is about to happen...\n",
    "\n",
    "1. Be able to ask an LLM to evaluate an answer\n",
    "2. Be able to rerun if the answer fails evaluation\n",
    "3. Put this together into 1 workflow\n",
    "\n",
    "All without any Agentic framework!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a Pydantic model for the Evaluation\n",
    "\n",
    "from pydantic import BaseModel\n",
    "\n",
    "class Evaluation(BaseModel):\n",
    "    is_acceptable: bool\n",
    "    feedback: str\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "evaluator_system_prompt = f\"You are an evaluator that decides whether a response to a question is acceptable. \\\n",
    "You are provided with a conversation between a User and an Agent. Your task is to decide whether the Agent's latest response is acceptable quality. \\\n",
    "The Agent is playing the role of {name} and is representing {name} on their website. \\\n",
    "The Agent has been instructed to be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
    "The Agent has been provided with context on {name} in the form of their summary and LinkedIn details. Here's the information:\"\n",
    "\n",
    "evaluator_system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
    "evaluator_system_prompt += f\"With this context, please evaluate the latest response, replying with whether the response is acceptable and your feedback.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "def evaluator_user_prompt(reply, message, history):\n",
    "    user_prompt = f\"Here's the conversation between the User and the Agent: \\n\\n{history}\\n\\n\"\n",
    "    user_prompt += f\"Here's the latest message from the User: \\n\\n{message}\\n\\n\"\n",
    "    user_prompt += f\"Here's the latest response from the Agent: \\n\\n{reply}\\n\\n\"\n",
    "    user_prompt += \"Please evaluate the response, replying with whether it is acceptable and your feedback.\"\n",
    "    return user_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "gemini = OpenAI(\n",
    "    api_key=os.getenv(\"GOOGLE_API_KEY\"), \n",
    "    base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "def evaluate(reply, message, history) -> Evaluation:\n",
    "\n",
    "    messages = [{\"role\": \"system\", \"content\": evaluator_system_prompt}] + [{\"role\": \"user\", \"content\": evaluator_user_prompt(reply, message, history)}]\n",
    "    response = gemini.beta.chat.completions.parse(model=\"gemini-2.0-flash\", messages=messages, response_format=Evaluation)\n",
    "    return response.choices[0].message.parsed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "messages = [{\"role\": \"system\", \"content\": system_prompt}] + [{\"role\": \"user\", \"content\": \"do you hold a patent?\"}]\n",
    "response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
    "reply = response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'No, I do not currently hold any patents. My focus has primarily been on software testing, automation, and developing efficient testing frameworks and solutions in my role as a Senior Test Engineer at HCLTech. If you have any other questions or need further information, feel free to ask!'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reply"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Evaluation(is_acceptable=True, feedback='The Agent responded professionally and accurately based on the context provided. The response is engaging and invites further questions, maintaining the desired persona.')"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evaluate(reply, \"do you hold a patent?\", messages[:1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rerun(reply, message, history, feedback):\n",
    "    updated_system_prompt = system_prompt + \"\\n\\n## Previous answer rejected\\nYou just tried to reply, but the quality control rejected your reply\\n\"\n",
    "    updated_system_prompt += f\"## Your attempted answer:\\n{reply}\\n\\n\"\n",
    "    updated_system_prompt += f\"## Reason for rejection:\\n{feedback}\\n\\n\"\n",
    "    messages = [{\"role\": \"system\", \"content\": updated_system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
    "    response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
    "    return response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "def chat(message, history):\n",
    "    if \"patent\" in message:\n",
    "        system = system_prompt + \"\\n\\nEverything in your reply needs to be in pig latin - \\\n",
    "              it is mandatory that you respond only and entirely in pig latin\"\n",
    "    else:\n",
    "        system = system_prompt\n",
    "    messages = [{\"role\": \"system\", \"content\": system}] + history + [{\"role\": \"user\", \"content\": message}]\n",
    "    response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
    "    reply =response.choices[0].message.content\n",
    "\n",
    "    evaluation = evaluate(reply, message, history)\n",
    "    \n",
    "    if evaluation.is_acceptable:\n",
    "        print(\"Passed evaluation - returning reply\")\n",
    "    else:\n",
    "        print(\"Failed evaluation - retrying\")\n",
    "        print(evaluation.feedback)\n",
    "        reply = rerun(reply, message, history, evaluation.feedback)       \n",
    "    return reply"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "* Running on local URL:  http://127.0.0.1:7865\n",
      "* To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7865/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gr.ChatInterface(chat, type=\"messages\").launch()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\simil\\OneDrive\\Desktop\\Multiman\\Something big\\AgentsFever\\projects\\agents\\.venv\\Scripts\\python.exe: No module named pip\n"
     ]
    }
   ],
   "source": [
    "pip install gradio pypdf google-generativeai"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'C:\\\\Users\\\\simil\\\\OneDrive\\\\Desktop\\\\Multiman\\\\Something big\\\\AgentsFever\\\\projects\\\\agents\\\\1_foundations\\\\me\\\\Profile (2).pdf'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mFileNotFoundError\u001b[39m                         Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[46]\u001b[39m\u001b[32m, line 15\u001b[39m\n\u001b[32m     13\u001b[39m \u001b[38;5;66;03m# 2. Read your resume content from the PDF file\u001b[39;00m\n\u001b[32m     14\u001b[39m file_path = \u001b[33mr\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mC:\u001b[39m\u001b[33m\\\u001b[39m\u001b[33mUsers\u001b[39m\u001b[33m\\\u001b[39m\u001b[33msimil\u001b[39m\u001b[33m\\\u001b[39m\u001b[33mOneDrive\u001b[39m\u001b[33m\\\u001b[39m\u001b[33mDesktop\u001b[39m\u001b[33m\\\u001b[39m\u001b[33mMultiman\u001b[39m\u001b[33m\\\u001b[39m\u001b[33mSomething big\u001b[39m\u001b[33m\\\u001b[39m\u001b[33mAgentsFever\u001b[39m\u001b[33m\\\u001b[39m\u001b[33mprojects\u001b[39m\u001b[33m\\\u001b[39m\u001b[33magents\u001b[39m\u001b[33m\\\u001b[39m\u001b[33m1_foundations\u001b[39m\u001b[33m\\\u001b[39m\u001b[33mme\u001b[39m\u001b[33m\\\u001b[39m\u001b[33mProfile (2).pdf\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m---> \u001b[39m\u001b[32m15\u001b[39m reader = \u001b[43mPdfReader\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile_path\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m     16\u001b[39m resume_text = \u001b[33m\"\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m     17\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m page \u001b[38;5;129;01min\u001b[39;00m reader.pages:\n",
      "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\simil\\OneDrive\\Desktop\\Multiman\\Something big\\AgentsFever\\projects\\agents\\.venv\\Lib\\site-packages\\pypdf\\_reader.py:136\u001b[39m, in \u001b[36mPdfReader.__init__\u001b[39m\u001b[34m(self, stream, strict, password)\u001b[39m\n\u001b[32m    132\u001b[39m \u001b[38;5;28mself\u001b[39m._page_id2num: Optional[Dict[Any, Any]] = \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[32m    134\u001b[39m \u001b[38;5;28mself\u001b[39m._validated_root: Optional[DictionaryObject] = \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[32m--> \u001b[39m\u001b[32m136\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_initialize_stream\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstream\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m    137\u001b[39m \u001b[38;5;28mself\u001b[39m._known_objects: Set[Tuple[\u001b[38;5;28mint\u001b[39m, \u001b[38;5;28mint\u001b[39m]] = \u001b[38;5;28mset\u001b[39m()\n\u001b[32m    139\u001b[39m \u001b[38;5;28mself\u001b[39m._override_encryption = \u001b[38;5;28;01mFalse\u001b[39;00m\n",
      "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\simil\\OneDrive\\Desktop\\Multiman\\Something big\\AgentsFever\\projects\\agents\\.venv\\Lib\\site-packages\\pypdf\\_reader.py:155\u001b[39m, in \u001b[36mPdfReader._initialize_stream\u001b[39m\u001b[34m(self, stream)\u001b[39m\n\u001b[32m    153\u001b[39m \u001b[38;5;28mself\u001b[39m._stream_opened = \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[32m    154\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(stream, (\u001b[38;5;28mstr\u001b[39m, Path)):\n\u001b[32m--> \u001b[39m\u001b[32m155\u001b[39m     \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mrb\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m fh:\n\u001b[32m    156\u001b[39m         stream = BytesIO(fh.read())\n\u001b[32m    157\u001b[39m     \u001b[38;5;28mself\u001b[39m._stream_opened = \u001b[38;5;28;01mTrue\u001b[39;00m\n",
      "\u001b[31mFileNotFoundError\u001b[39m: [Errno 2] No such file or directory: 'C:\\\\Users\\\\simil\\\\OneDrive\\\\Desktop\\\\Multiman\\\\Something big\\\\AgentsFever\\\\projects\\\\agents\\\\1_foundations\\\\me\\\\Profile (2).pdf'"
     ]
    }
   ],
   "source": [
    "import gradio as gr\n",
    "from pypdf import PdfReader\n",
    "import os\n",
    "from openai import OpenAI\n",
    "\n",
    "# 1. Configure the Generative AI model using the OpenAI client\n",
    "# It will automatically use the GOOGLE_API_KEY from your environment variables.\n",
    "gemini = OpenAI(\n",
    "    api_key=os.getenv(\"GOOGLE_API_KEY\"),\n",
    "    base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\"\n",
    ")\n",
    "\n",
    "# 2. Read your resume content from the PDF file\n",
    "file_path = r\"C:\\Users\\simil\\OneDrive\\Desktop\\Multiman\\Something big\\AgentsFever\\projects\\agents\\1_foundations\\me\\Profile (2).pdf\"\n",
    "reader = PdfReader(file_path)\n",
    "resume_text = \"\"\n",
    "for page in reader.pages:\n",
    "    text = page.extract_text()\n",
    "    if text:\n",
    "        resume_text += text\n",
    "\n",
    "# 3. Define the system prompt with your resume content\n",
    "name = \"Amit Kumar\"\n",
    "system_prompt = f\"You are acting as {name}. You are answering questions related to your career, background, skills, and experience. Your responsibility is to represent {name} as faithfully as possible. You will use the provided resume details to answer questions. Be professional and engaging, as if talking to a potential employer. If the user asks something outside the scope of your resume, politely state that you can only answer questions based on the provided information.\"\n",
    "\n",
    "# 4. Create the chatbot function that uses the Gemini model via OpenAI client\n",
    "def chatbot_response(message, history):\n",
    "    # Construct the messages list, including the system prompt and history\n",
    "    messages = [{\"role\": \"system\", \"content\": system_prompt + \"\\n\\n## Resume Details:\\n\" + resume_text}]\n",
    "    for user_msg, bot_msg in history:\n",
    "        messages.append({\"role\": \"user\", \"content\": user_msg})\n",
    "        messages.append({\"role\": \"assistant\", \"content\": bot_msg})\n",
    "    messages.append({\"role\": \"user\", \"content\": message})\n",
    "\n",
    "    # Generate the response using the Gemini model\n",
    "    try:\n",
    "        response = gemini.chat.completions.create(\n",
    "            model=\"gemini-2.0-flash\",  # Updated model name\n",
    "            messages=messages\n",
    "        )\n",
    "        return response.choices[0].message.content\n",
    "    except Exception as e:\n",
    "        return f\"An error occurred: {e}\"\n",
    "\n",
    "# 5. Create the Gradio interface\n",
    "with gr.Blocks() as demo:\n",
    "    chatbot = gr.Chatbot()\n",
    "    msg = gr.Textbox()\n",
    "    clear = gr.Button(\"Clear\")\n",
    "\n",
    "    def respond(message, chat_history):\n",
    "        bot_message = chatbot_response(message, chat_history)\n",
    "        chat_history.append((message, bot_message))\n",
    "        return \"\", chat_history\n",
    "\n",
    "    msg.submit(respond, [msg, chatbot], [msg, chatbot])\n",
    "    clear.click(lambda: None, None, chatbot, queue=False)\n",
    "\n",
    "demo.launch()"
   ]
  },
  {
   "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}