File size: 29,453 Bytes
7c9b59a |
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 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
{
"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 PyPDF2 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": 1,
"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": 2,
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)\n",
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"reader = PdfReader(\"me/linkedin.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": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" \n",
"Contact\n",
"+918420117409 (Mobile)\n",
"[email protected]\n",
"www.linkedin.com/in/sagarnildas\n",
"(LinkedIn)\n",
"sagarnildas.teachable.com/\n",
"(Personal)\n",
"Top Skills\n",
"Machine Learning\n",
"Deep Learning\n",
"Monte Carlo Simulation\n",
"Languages\n",
"English (Full Professional)\n",
"Hindi (Full Professional)\n",
"Bengali (Full Professional)\n",
"Certifications\n",
"GAN Specialization\n",
"Become a Data Scientist\n",
"Data Science Foundations: Data\n",
"Mining\n",
"Data Visualization: Storytelling\n",
"Statistics Foundations: 1\n",
"Honors-Awards\n",
"Dean's Fellowhip\n",
"Publications\n",
"Simultaneous Localization and\n",
"Mapping (SLAM) using RTAB-MAP\n",
"Enhancing Early Diabetic\n",
"Retinopathy Detection through\n",
"Synthetic DR1 Image Generation: A\n",
"StyleGAN3 Approach\n",
"Calculating Customer Lifetime Value\n",
"and Churn Using Beta Geometric\n",
"Negative Binomial and Gamma-\n",
"Gamma Distribution in an Nft-Based\n",
"Setting\n",
"Robot localization in a mapped\n",
"environment using Adaptive Monte\n",
"Carlo algorithmSagarnil Das\n",
"Machine Learning & Deep Learning Engineer | Intel Edge AI\n",
"Scholarship Winner | Ex Udacity Machine Learning Mentor | Ex\n",
"NASA Researcher | Kaggle Expert\n",
"Kolkata, West Bengal, India\n",
"Summary\n",
"Machine Learning & Deep Learning Innovator | Transforming\n",
"Industries with Cutting-Edge AI Solutions\n",
"With over a decade of experience in Machine Learning (ML) and\n",
"Deep Learning (DL), I have consistently delivered impactful AI\n",
"solutions across healthcare, retail, education, and government\n",
"industries. My career journey includes pivotal roles in renowned\n",
"organizations such as:\n",
"✅ New York State Department of Health (Affordable Care Act -\n",
"OBAMACARE)\n",
"✅ Computer Science Corporation (Fortune 500 Company)\n",
"✅ Udacity (Fortune 500 Company) as a Machine Learning Mentor\n",
"✅ Future Group India (India's leading $130Bn retail giant) as a\n",
"Machine Learning Manager\n",
"✅ Hopscotch (Leading child retail company in India) as a Data\n",
"Science Lead\n",
"Key Achievements in ML & DL\n",
"• Developed scalable AI ecosystems, driving adoption of healthcare\n",
"AI at Artelus, achieving groundbreaking sensitivity rates for Diabetic\n",
"Retinopathy (DR) detection (98.7%).\n",
"• Engineered unsupervised learning pipelines leveraging frameworks\n",
"like SimCLR and BYOL to pre-train robust medical foundational\n",
"models.\n",
"• Innovated deep learning architectures such as U-NET and\n",
"StyleGAN3, delivering superior segmentation and synthetic data\n",
"generation capabilities.\n",
"• Pioneered retail solutions, including dynamic pricing models,\n",
"recommender systems, and Bayesian optimization, boosting\n",
"operational efficiency and revenue margins.\n",
" Page 1 of 8 \n",
"AI-Driven Diabetic Retinopathy\n",
"Screening: Multicentric Validation of\n",
"AIDRSS in India• Created no-code ML platforms with seamless MLOps integration,\n",
"empowering over 7,000 users.\n",
"Certifications & Education\n",
"• Andrew Ng's Deep Learning Specialization\n",
"• MIT MicroMasters in Data Science\n",
"• Udacity Flying Car & Robotics Nanodegrees\n",
"• 40+ additional certifications in ML/DL, TensorFlow, and PyTorch\n",
"As a research contributor, I’ve published in international journals on\n",
"topics like robot localization and SLAM (Simultaneous Localization\n",
"and Mapping).\n",
"Technical Expertise\n",
"• Deep Learning: TensorFlow, PyTorch, GANs, U-NET,\n",
"Transformers, LLMs\n",
"• Applied ML: Bayesian Optimization, Time-Series Forecasting,\n",
"Reinforcement Learning\n",
"• Full-Stack Deployment: Kubernetes, PostgreSQL, MongoDB,\n",
"React, NextJS\n",
"• AI in Healthcare: Retinal Imaging Analysis, RNFL Thickness\n",
"Measurement\n",
"Let’s Collaborate\n",
"I’m passionate about harnessing the power of AI/ML to solve\n",
"real-world challenges, whether in healthcare, retail, or any other\n",
"domain. Open to sharing knowledge, and building connections with\n",
"professionals in the Machine Learning and Deep Learning space.\n",
"Feel free to connect at [email protected]\n",
"✅MachineLearning ✅DeepLearning ✅AI\n",
"Experience\n",
"Artelus India\n",
" Page 2 of 8 \n",
"Director & Chief Technology Officer\n",
"March 2023 - Present (2 years 3 months)\n",
"Kolkata, West Bengal, India\n",
"• Spearheaded the architectural design and strategic roadmap of the Artelus\n",
"Technology Ecosystem, creating an integrated, scalable infrastructure for AI\n",
"driven healthcare solutions. Defined long-term product vision and technology\n",
"strategy to position Artelus as an industry leader.\n",
"• Achieved significant increases in product adoption and client retention by\n",
"leading the development of the ARTELUS Technology Ecosystem, steering\n",
"scalable AI model deployment on cloud platforms like AWS and Azure.\n",
"• Conceptualised LLM-based mental health app, incorporating an AI-enabled\n",
"Patient-360 engine for holistic diagnosis, including emotion detection and\n",
"suicidal tendency analysis.\n",
"• Engineered a solution that runs 8 LLM models in parallel and in real-time,\n",
"significantly enhancing the\n",
"diagnostic capabilities of healthcare providers.\n",
"• Created a framework combining natural data and unlabelled fundus images\n",
"for unsupervised pre-training of robust medical foundational models using\n",
"SimCLR and BYOL, enabling data-efficient generalization across various\n",
"medical tasks.\n",
"• Innovated synthetic data generation using StyleGAN3 model, enhancing DR1\n",
"image datasets. Conducted multiple statistical tests like Chi-Square, Mann-\n",
"Whitney U-Test.\n",
"• Constructed Inference Engine SaaS app, enabling project and point-and-click\n",
"model training with\n",
"deployment to Kubernetes servers via FastAPI and Next JS based interface.\n",
"• Refined U-NET architecture deep learning model, achieving accurate\n",
"segmentation of retinal layers for precise RNFL thickness and CDR\n",
"measurements.\n",
"CyberDeck AI\n",
"Founder & Machine Learning Engineer\n",
"March 2022 - March 2023 (1 year 1 month)\n",
"India\n",
"• Founded and created a complete no-code platform for end-to-end Data\n",
"Science and Machine Learning. This platform is capable of performing Data\n",
"Processing, Exploratory Data Analysis, Inferential Statistical Tests, Machine\n",
"Learning, Univariate and multivariate Time-Series Forecasting, Clustering and\n",
"MLOPS at the click of a mouse. Wrote the code for the whole machine learning\n",
"and deep learning backend of this platform.\n",
" Page 3 of 8 \n",
"• Responsible for the complete cloud deployment and management of the app.\n",
"The app currently runs in a Kubernetes cluster as a microservice on AWS. Set\n",
"up the Database structure, Load Balancers, Auto Scaling, Grafana and ELK\n",
"stack for monitoring. S3 and RDS for storage. Numerous Lambda functions\n",
"also run for different in-app purposes.\n",
"• CyberDeck got selected in Startup-India, an initiative by the honorable Prime\n",
"Minister of India to help boost the most promising startups in India.\n",
"• We also got selected in Microsoft Startup Founders Hub and received\n",
"$25000 in credit in Azure cloud.\n",
"• We also got selected in AWS activate and received a $10000 credit in AWS.\n",
"• Onboarded and managed around 7000 users for the free tier of CyberDeck.\n",
"Hopscotch\n",
"Data Science and Machine Learning Lead\n",
"September 2020 - March 2022 (1 year 7 months)\n",
"Bengaluru, Karnataka, India\n",
"• Improved CTR, RPI, and UPI by creating an optimisation model for Product\n",
"Listing Page Sort, utilising a tree-based ML model to predict a weighted sum of\n",
"CTR, RPI, and UPI, and incorporating SHAP values for feature weighting in the\n",
"ranking framework. Implemented multi-task neural networks with multimodal\n",
"feature spaces for ranking the products. Implemented Boltzman exploration\n",
"methods for mitigating positional bias. Created A/B testing frameworks along\n",
"with team-draft interleaving (with Bootstrap resampling) to quickly iterate\n",
"through multiple models and select the best model to productionize.\n",
"• Implemented a Dynamic Pricing model based on Bayesian Optimization\n",
"and Markov chain Monte Carlo Simulation. The pricing model initially predicts\n",
"an optimized price based on historical price, demand and traffic data. After\n",
"the initial price is predicted, a second demand forecasting model is used to\n",
"understand the seasonality of the product and adjust the price accordingly.\n",
"Within 2 months of implementation for 10 product types, the total Margin has\n",
"gone up by 4% with Individual margins at a product type level increasing by\n",
"30% all the way upto 172%.\n",
"• Created a purchase/demographics based user segmentation model with an\n",
"unsupervised ML algorithm (Gaussian Mixture model) for better targeting of\n",
"users. \n",
"• In order to increase the LTV of customers, currently building a customer\n",
"survival analysis with Kaplan Meier and Cox’s proportional Hazard models\n",
" Page 4 of 8 \n",
"as these works much better with censored data than a regression model.\n",
"Created a CLTV model using Beta Geometric Negative Binomial Distribution\n",
"and Gamma-Gamma mixture models.\n",
"• Implemented multiple recommender systems for Product to Product\n",
"(Word2Vec and BERT embedding method) and Customer to Product (Two\n",
"Tower Neural Networks, Deep Retrieval, Bandit Based algorithms like UCB\n",
"and Thompson sampling). This increased the PDP Click-Through Rate from\n",
"1.3% to 3.2%.\n",
"Tathastu\n",
"Manager, Data Science and Machine Learning\n",
"October 2017 - September 2020 (3 years)\n",
"Kolkata Area, India\n",
"• Implemented a virtual try on pipeline, where given a person’s image and a\n",
"cloth image, the model generates a new image of the person dressed with the\n",
"new cloth. Implemented using JPPNet, OpenPose and novel Virtual Try On\n",
"module. This model also has the ability to generate limbs e.g if the person was\n",
"originally dressed in full sleeve dress and he/she chooses a sleeveless one.\n",
"• Created a novel Encoder – Decoder deep learning architecture chained\n",
"with Monte Carlo based error correction and Attention mechanism to predict\n",
"which customers will visit in the following week. Effectively targeting only those\n",
"customers reduced the campaign operation cost by 4%. Presently finishing up\n",
"the paper on this and awaiting publication.\n",
"• Created a supervised machine learning model (Xgboost) to predict how much\n",
"a customer will spend in his/her next visit resulting in a margin of 7%.\n",
"• Created an unsupervised machine learning model (Gaussian Mixture model\n",
"coupled with PCA) to segment customers based on purchase pattern and\n",
"demographics for effective campaign management.\n",
"• Created a deep neural product embedding model to find the best potential\n",
"customers for a product promotion campaign. Combined with the visit\n",
"prediction model, this resulted in a 4% reduction in operation cost.\n",
"• Created a LSTM model to predict the gender of a customer from his/her\n",
"name. The same mechanism was scaled to predict mother tongue, religion,\n",
"community etc in order to complete the Member 360 Framework.\n",
"• Created a Deep learning model (YOLO/Faster RCNN) to predict customer’s\n",
"age, gender and emotion from live video feed. Use case still being developed\n",
"for this work.\n",
"• Campaign Budget Optimization with Machine Learning and Linear\n",
"Optimization method.\n",
" Page 5 of 8 \n",
"• Dynamic price optimization at a sku level. The method taken is a Bayesian\n",
"optimization method coupled with Markov Chain Monte Carlo (MCMC).\n",
"EduPristine Inc.\n",
"Big Data and Data Science faculty \n",
"May 2017 - October 2017 (6 months)\n",
"India\n",
"• Worked as an active faculty member for the Big Data and Data Science\n",
"Certification Course with batches of 40 - 50 students on average.\n",
"• Organizing interactive sessions, demos and real-life project solving in\n",
"Banking domain, retail sector and social media. The main challenge in this part\n",
"is conveying the goal effectively to a huge batch of students and successfully\n",
"accomplishing them together.\n",
"New York State Department of Health\n",
"2 years 4 months\n",
"Senior Data Specialist\n",
"April 2016 - May 2017 (1 year 2 months)\n",
"Albany, New York Area\n",
"• Migrated all data from Oracle to Hadoop framework, data analysis and\n",
"modeling with Pig, Hive and Python .\n",
"• Created unsupervised learning models to cluster different groups of Medical\n",
"facilities together based on locations.\n",
"Junior Data Specialist\n",
"February 2015 - April 2016 (1 year 3 months)\n",
"Albany, New York Area\n",
"• Reduced the rejection rate by 5% and incidence of defects by 20% through\n",
"measures such as proactive data analysis and implementing machine learning\n",
"models for anomaly detection.\n",
"Computer Sciences Corporation (CSC)\n",
"Database Developer\n",
"December 2013 - December 2014 (1 year 1 month)\n",
"Albany, NY\n",
"• Developed for the original Affordable Care Act team (ObamaCare), which\n",
"provided healthcare to 32 million Americans.\n",
"• Developed SQL utilities and scripts to monitor database performance.\n",
"UCF\n",
"Research Assistant\n",
" Page 6 of 8 \n",
"May 2012 - May 2013 (1 year 1 month)\n",
"Orlando, Florida Area\n",
"• Performed baseline measurement tests on the PV modules like IV\n",
"measurements, Dry leakage Tests, Electroluminescence, IR, and Wet Leakage\n",
"Tests. \n",
"• Studied the evolution of residual stress in the TBC system using in-situ\n",
"transmission synchrotron X-ray diffraction in Argonne National Lab, Chicago.\n",
"• Worked with the NASA funded SBIR project regarding the CMAS and the\n",
"overlay coatings (La2Zr2O7). \n",
"• Developed models which produced 50K x 50K dense / partial dense /\n",
"sparse matrix to assess state of the art mitigation solutions to the thermal and\n",
"radiation problems.\n",
"NASA - National Aeronautics and Space Administration\n",
"Research Scholar\n",
"August 2012 - April 2013 (9 months)\n",
"United States\n",
"• Computed the residual stress in the TGO layer by Photoluminescence and\n",
"Raman spectroscopy.\n",
"• Studied the evolution of strain in the TBC system by taking depth\n",
"measurements using in-situ transmission synchrotron X-ray diffraction.\n",
"• Used the measured strain values to understand the critical stages of strain\n",
"evolution within the TGO.\n",
"• Worked with the NASA funded SBIR project regarding the CMAS and the\n",
"overlay coatings (La2Zr2O7)\n",
"Florida Solar Energy Center\n",
"Research Assistant\n",
"July 2011 - May 2012 (11 months)\n",
"Orlando, Florida Area\n",
"• Worked on a project of a Glass Manufacturing company and performed\n",
"baseline measurement tests on the PV modules like IV measurements, Dry\n",
"leakage Tests, Electroluminescence, IR, and Wet Leakage Tests.\n",
"• Performed Rapid Thermal Processing of CIGS solar cells, measurement of\n",
"sheet resistance of ZnO layer, and UV-vis measurements.\n",
"• Published a paper titled \"Outdoor high-voltage study of commercially\n",
"available PV modules from leading manufacturers in hot and humid conditions\"\n",
"Education\n",
" Page 7 of 8 \n",
"University of Central Florida\n",
"Master of Science - MS, Materials Science · (2011 - 2013)\n",
"Udacity\n",
"Deep Reinforcement Learning Nanodegree, Artificial Intelligence · (December\n",
"2022 - December 2022)\n",
"Udacity\n",
"Flying Car and Autonomous Flight Engineer Nanodegree, Artificial\n",
"Intelligence · (April 2021 - May 2021)\n",
"Udacity\n",
"Robotics Software Engineering Nanodegree, Robotics · (2018 - 2018)\n",
"Udacity\n",
"Artificial Intelligence nanodegree, Artificial Intelligence · (2017 - 2018)\n",
" Page 8 of 8\n"
]
}
],
"source": [
"print(linkedin)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n",
" summary = f.read()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"name = \"Sagarnil Das\""
]
},
{
"cell_type": "code",
"execution_count": 7,
"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": null,
"metadata": {},
"outputs": [],
"source": [
"system_prompt"
]
},
{
"cell_type": "code",
"execution_count": 9,
"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": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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": 11,
"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": 23,
"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": 24,
"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 += f\"Please evaluate the response, replying with whether it is acceptable and your feedback.\"\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": 25,
"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": 26,
"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": 27,
"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": null,
"metadata": {},
"outputs": [],
"source": [
"reply"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"evaluate(reply, \"do you hold a patent?\", messages[:1])"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"def rerun(reply, message, history, feedback):\n",
" updated_system_prompt = system_prompt + f\"\\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": 35,
"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": null,
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(chat, type=\"messages\").launch()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "artelus",
"language": "python",
"name": "artelus"
},
"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.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|