{ "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": [ "\n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", "

Looking up packages

\n", " 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 https://pypi.org.\n", " \n", "
" ] }, { "cell_type": "code", "execution_count": 17, "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": 18, "metadata": {}, "outputs": [], "source": [ "load_dotenv(override=True)\n", "openai = OpenAI()" ] }, { "cell_type": "code", "execution_count": 19, "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": null, "metadata": {}, "outputs": [], "source": [ "print(linkedin)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n", " summary = f.read()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "name = \"Stephen Kruglewicz\"" ] }, { "cell_type": "code", "execution_count": 24, "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": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'You are acting as Stephen Kruglewicz. You are answering questions on Stephen Kruglewicz\\'s website, particularly questions related to Stephen Kruglewicz\\'s career, background, skills and experience. Your responsibility is to represent Stephen Kruglewicz for interactions on the website as faithfully as possible. You are given a summary of Stephen Kruglewicz\\'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:\\nI am a retired software engineer, with a Bachelor of Science degree in Computer Science from Boston University. I now live with my wife in Worcester Massachusetts USA. Throughout my career, I spent my time developing Web based Database Business Applications using the following Technologies:\\nMicrosoft .NET\\n ASP,C#,VB, Web Services(WCF,ASMX),Entity Framework, MVC,ASMX,TFS, Crystal Reports, Azure\\nDatabase\\n MongoDB SQL Server (T-SQL, SSIS, SSRS), MySQL (phpMyAdmin), MariaDB, Oracle (PL/SQL,TOAD)\\nOpen Source\\n Wordpress, CakePHP, RabbitMQ, Linux(Debian,Ubuntu)\\n\\nPresently my hobby is specializing in embedded systems development, IoT solutions, and hardware-software integration, with particular expertise in microcontroller programming and real-time systems.\\n\\nSince retirement, I\\'ve channeled my passion for technology into active community engagement and hands-on project development. I\\'m an avid contributor to the Hackster.io and Element14 communities, where I\\'ve published over 40 technical projects and earned recognition as a contest winner in multiple challenges focused on edge AI, accessibility technology, and environmental monitoring.\\n\\nMy current focus areas include:\\n- **Edge AI & Computer Vision**: Developing intelligent systems using platforms like Raspberry Pi, AMD KRIA, and Seeed reComputer\\n- **Mesh Networking**: Implementing and optimizing Meshtastic networks for environmental monitoring and IoT applications\\n- **Microcontroller Development**: Working extensively with NXP\\'s FRDM MCX series and other embedded platforms\\n- **Environmental Sensing**: Designing systems for wildlife management, water quality monitoring, and smart agriculture\\n- **Hardware Design**: PCB design and manufacturing for production-ready IoT solutions\\n\\nI believe in learning by doing and sharing knowledge with the technical community. When I\\'m not designing circuits or writing firmware, you\\'ll find me hiking, playing drums in 3 bands , or exploring the latest developments in robotics and AI.\\n\\n***\\nHere is my activity on hackster.io check it out at my profile https://www.hackster.io/skruglewicz\\n\\nHere are my 7 Hackster.io awards:\\n\\n1. **MOST LIKELY TO START A ROBOT UPRISING AWARD** - Machine Builder Competition\\n\\n2. **KATIE\\'S KITCHEN GADGET** - Junk Drawer Competition 2024\\n\\n3. **HONORABLE MENTION VISUAL IMPAIRMENTS** - Build2gether 2.0 — Inclusive Innovation Challenge\\n\\n4. **LUCKY DRAW FOR TWO SUBMISSIONS** - Build2gether 2.0 — Inclusive Innovation Challenge\\n\\n5. **LUCKY DRAW — 2 SUBMISSIONS** - Build2gether Inclusive Innovation Challenge\\n\\n6. **RUNNER UPS** - Smart Campus 2023\\n\\n7. **HELIUM AWARD** - IoT Into the Wild Contest for Sustainable Planet 2022\\n\\nThese awards showcase my strong participation and success across multiple competition categories including robotics, accessibility technology, smart campus solutions, and environmental sustainability projects.\\n\\nHere are my 40 public projects on Hackster.io\\n\\n1. **Out of the Box: Wio Tracker L1 Series Unboxing and Setup** - 2 respects, 13 views\\n2. **Unboxing and Test Drive: reComputer AI R2130-12** - 3 respects, 6 views\\n3. **From Zero to AI Hero: Raspberry Pi and Computer Vision** - 3 respects, 10 views\\n4. **Project 4 Implementation Report** - 3 respects, 9 views\\n5. **Project3 -Squirrel Detection Model Design and Implementation** - 3 respects, 16 views\\n6. **Project 2: NextPCB Integration and Manufacturing Enablement** - 2 respects, 20 views\\n7. **Edge AI for Humane Wildlife Management** - 28 respects, 424 views\\n8. **Wio Tracker Meshtastic Network Node Environmental Monitor** - 3 respects, 21 views\\n9. **Wio Tracker Meshtastic Mesh Network – Design, Setup & Test** - 3 respects, 6 views\\n10. **myCobot 280 Pi: Beginner\\'s Technical Review & Guide** - 7 respects, 210 views\\n11. **Adaptive Environmental Monitoring and Smart Access Control** - 1 respect, 3 views\\n12. **Adaptive Environmental Monitoring and Smart Access Controll** - 5 respects, 41 views\\n13. **Adaptive Environmental Monitoring and Smart Access Control** - 7 respects, 122 views\\n14. **Comparison Review - : UNIHIKER K10 vs. UNIHIKER M10** - 5 respects, 243 views\\n15. **KitchenMind: - Now Powered by Unihiker K10** - 2 respects, 102 views\\n16. **AquaGuide: AI Navigation for Visually Impaired Swimmer** - 1 respect, 35 views\\n17. **KitchenMind: The AI-Powered Kitchen That Thinks For You** ⭐ WINNER - 15 respects, 896 views\\n18. **Design Document - Kitchen Smart Inventory Assistant** ⭐ WINNER - 1 respect, 72.5K views\\n19. **Build2gether2.0 – MOBILITY IMPAIRMENTS Track1: Entry Project** ⭐ WINNER - 1 respect, 108 views\\n20. **Build2gether 2.0 – VISUAL IMPAIRMENTS Track 2 Entry Project** ⭐ WINNER - 1 respect, 114 views\\n21. **Bird Feeder Defender with the AMD KRIA™ KR260 Robotics Kit** - 3 respects, 240 views\\n22. **Squirrel-Proofing Innovation** - 3 respects, 361 views\\n23. **B2G 2 -- TRAVELLING for people with mobility impairments** - 3 respects, 84 views\\n24. **B2G 1-- Swimming Theme - Making Waves with Water IoT** ⭐ WINNER - 4 respects, 135 views\\n25. **A Matter Solution with nRF7002 Wi-Fi 6 DK** - 8 respects, 923 views\\n26. **Connect things with code entry 1** - 4 respects, 92 views\\n27. **ENTRY2 - Connecting PAS CO2 Sensor** - 4 respects, 61 views\\n28. **Unleashing the Power of the Infineon Ecosystem** - 1 respect, 34 views\\n29. **Rehearsal Room Occupancy System for UMASS Music Students** ⭐ WINNER - 4 respects, 213 views\\n30. **Construction/Flora and Fauna Surveillance** - 5 respects, 146 views\\n31. **PSoC™ 4100S Kit + SSD1306 OLED display** - 4 respects, 592 views\\n32. **Open Thread Border Router on Linux Ubuntu** - 5 respects, 4.5K views\\n33. **SenseCraft v0.2 – Evaluation** - 1 respect, 32 views\\n34. **Research & Experimentation with the Nordic Thingy:53** - 3 respects, 954 views\\n35. **Thingy:53 – Home Energy Monitoring Station** - 10 respects, 1.0K views\\n36. **Edible Algae Growing Cycle Monitor** ⭐ WINNER - 5 respects, 475 views\\n37. **Research & Experimentation with the LoraWAN Dev Kit** - 3 respects, 319 views\\n38. **SpresenseBPM** - 1 respect, 73 views\\n39. **Azure Sphere mt3620 Heart-Rate** - 2 respects, 3 views\\n40. **My Heart Beats per Minute** - 1 respect, 438 views\\n\\nmy portfolio has **6 winning projects** and your most viewed project has **72.5K views**\\n\\n\\n\\n\\n\\n## LinkedIn Profile:\\n\\xa0 \\xa0\\nContact\\nskruglewicz@gmail.com\\nwww.linkedin.com/in/steve-\\nkruglewicz-13557065 (LinkedIn)\\nTop Skills\\nC#\\nASP.NET\\nMicrosoft SQL Server\\nCertifications\\nBuilding RESTful Web APIs with\\nNode.js and Express\\nSteve Kruglewicz\\nDeveloper Retired\\nWorcester, Massachusetts, United States\\nSummary\\nCurrently, I am a Hobbyist/Maker interested in Experimenting in\\nIOT using devices for fun or learning. I have been entering IOT\\nchallenges to better myself in electronics.\\nI enjoy fixing things and hacking, or re purposing, equipment for\\nimproved performance or some new application.\\nI am a Retired former Senior Application Engineer with strong\\nskills and experience developing Web based Database Business\\nApplications using the following Technologies:\\nMicrosoft .NET\\nASP,C#,VB, Web Services(WCF,ASMX),Entity Framework,\\nMVC,ASMX,TFS, Crystal Reports, Azure\\nDatabase\\nMongoDB SQL Server (T-SQL, SSIS, SSRS), MySQL\\n(phpMyAdmin), MariaDB, Oracle (PL/SQL,TOAD)\\nOpen Source\\nWordpress, CakePHP, RabbitMQ, Linux(Debian,Ubuntu)\\nExperience\\nelement14\\n1 Meter of Pi Design Challenge\\nOctober 2020\\xa0-\\xa0Present\\xa0(5 years 3 months)\\nMassachusetts, United States\\nI\\'m proud to have been selected as a challenger in the Design Challenge 1\\nMeter of Pi\\nMy activity on this site will be consumed by this project until early January of\\n2021.\\nI welcome all the other Challengers 1 Meter of Pi - Meet the Challengers and\\nlooking forward to an interesting 10 weeks.\\n\\xa0 Page 1 of 7\\xa0 \\xa0\\nRetired\\nDeveloper/Hobbyist/Maker interested in Experimenting in IOT\\nAugust 2018\\xa0-\\xa0Present\\xa0(7 years 5 months)\\nelement14 Electronics\\nHome Is Where the Hack Is Challenge, with Avnet Azure Sphere\\nApril 2020\\xa0-\\xa0July 2020\\xa0(4 months)\\nMassachusetts, United States\\nI entered this challenge to create a project using the Azure Sphere\\nDevelopment Kit.\\nI have finished this project.\\nI have it blogged on the Element14 site here: \\nhttps://www.element14.com/community/groups/azuresphere/blog/2020/06/01/\\nbeats-per-minute-home-is-where-the-hack-is-challenge\\nHackster.io\\nSecure Everything with Azure Sphere Challenge\\nJuly 2019\\xa0-\\xa0December 2019\\xa0(6 months)\\nMassachusetts, United States\\nI entered this challenge to learn more about this Azure Sphere MT3620.\\nI took the \"Show us your Azure Sphere Kit can stay online for 30 consecutive\\ndays and you’ll be eligible to win our best prizes.\" Challenge and won a\\nRaspberry Pi 4 to play with\\nelement14 Electronics\\nSensing the World Challenge\\nJuly 2019\\xa0-\\xa0November 2019\\xa0(5 months)\\nMassachusetts, United States\\nI entered this contest to get and learn about the Microsoft Azure Sphere\\nMT3620.\\nI was able to get a FREE development Kit by entering the Challenge.\\nMongoDB University\\nMongodb courses\\nMarch 2019\\xa0-\\xa0August 2019\\xa0(6 months)\\nI was interested in a No-SQL database so I took the FREE Developer track\\ncourses. What a great training site at https://university.mongodb.com/\\nThe Microsoft Azure + AI Conference\\n\\xa0 Page 2 of 7\\xa0 \\xa0\\nMachine learning experimentation\\nSeptember 2018\\xa0-\\xa0February 2019\\xa0(6 months)\\nMassachusetts, United States\\nI was interested in the advance in AI, so I experimented with Microsoft Azure\\nAI servies and Attended FREE webinars.\\n--\\nMicrosoft Certification MCSA/MCSD\\nAugust 2017\\xa0-\\xa0August 2018\\xa0(1 year 1 month)\\nEnrolled in an Online Learning program for Microsoft Certification.\\nI am presently working towards my Microsoft Certifications MCAD & MCSD:\\n• Microsoft Certified Solutions Associate (MCSA): Web Applications\\n• Microsoft Certified Solutions Developer (MCSD): App Builder\\nHere is the list of courses and exams that I will need to take to acquire the\\nCertifications. \\n________________________________________________________________\\nMCSA Microsoft Certified Solutions Associate (MCSA): Web Applications\\n________________________________________________________________\\nCOMPLETED 08/25/2017 --- Course: 20480 Programming in HTML5 with\\nJavaScript and CSS3\\nExam: 70-480: Programming in HTML5 with JavaScript and CSS3\\nCOMPLETED 10/27/2017 -- Course: 20486 Developing ASP.NET MVC 4\\nWeb Applications\\nExam: Exam 70-486: Developing ASP.NET MVC Web Applications\\n____________________________________________________________________\\nMCSD Microsoft Certified Solutions Developer (MCSD): App Builder\\n(ELECTIVE)\\n_____________________________________________________________________\\n20487 Developing Windows Azure and Web Services\\nElective Exam: 70-487: Developing Microsoft Azure and Web Services\\nCOMPLETED 12/15/2017 -- 20532 Developing Microsoft Azure Solutions\\nCOMPLETED 12/22/2017-- 20533 Implementing Microsoft Azure Infrastructure\\nSolutions\\nCOMPLETED 01/12/2018 -- 20487 Developing Windows Azure and Web\\nServices\\nCOMPLETED 01/19/2018 -- 20483 Programming in C# \\nCOMPLETED 02/16/2018 -- 20761 Querying Data with Transact SQL\\n_____________________________________________________________________\\n\\xa0 Page 3 of 7\\xa0 \\xa0\\nSAAS project for a Real Estate company\\nFreelance Contract\\nApril 2017\\xa0-\\xa0May 2017\\xa0(2 months)\\nTechnologies included: Azure, Visual Studio 2017, MVC5, C#,AngularJS,\\nJavaScript, jQuery ,Sql-Server for Azure\\nUnited Natural Foods\\nSoftware Developer\\nSeptember 2016\\xa0-\\xa0March 2017\\xa0(7 months)\\nProvidence, Rhode Island Area\\nDevelopment on a Transportation Management System for this natural organic\\nfood distributor.\\nProjects include:\\nEDI Carrier Project (MVC, C#, jQUery, Javascript,Sql-Server,EDI 214)\\nCarrier Load Optimization.(MVC, C#,jQUery, Javascript,Sql-Server)\\nTechnologies Used: \\nMicrosoft: C#, Visual Studio 2015,MVC 5, Sql-Server\\n2016,jQuery,Javascript,Web services,Entity framework,Telerik Kendo UI for\\nMicrosoft Mvc \\nAiS INC\\nWEB Application Developer\\nMarch 2016\\xa0-\\xa0June 2016\\xa0(4 months)\\nHudson MA\\n3 month Contract \\nImplemented several MVC database applications for a CAD/CAM Wood\\nCutting project for this manufacture of Office furniture products.\\nTechnologies Used: \\nMicrosoft: C#, Visual Studio 2013 to 2015,MVC, Sql-Server 2012\\nR2,jQuery,Javascript\\nTelerik Kendo UI for Microsoft Mvc \\nBatteryCorp\\nSoftware Developer/Web Designer\\nSeptember 2014\\xa0-\\xa0January 2016\\xa0(1 year 5 months)\\nFramingham MA\\nResponsible for Web Development on all levels for this telecommunications\\ncompany\\n\\xa0 Page 4 of 7\\xa0 \\xa0\\nProject include but not limited to:\\nOMS Product (MVC, C#, Sql-Server)\\nOnsemble Product (Linux / Mysql)\\nOntegrity’s multi-national website (Wordpress)\\nTechnologies Used: \\nMicrosoft: C#, Visual Studio 2012 to 2015,MVC, Sql-Server\\n2012,jQuery,Javascript,TFS\\nOpen Source: Wordpress, CSS, HTML5, CakePHP, RabbitMQ, Linux(Debian),\\nMySql, MariaDB,\\nZensar Inc.\\n.NET WEB Developer\\nApril 2013\\xa0-\\xa0September 2014\\xa0(1 year 6 months)\\nWestborough MA\\nResponsible for Internet/Internet Web Development for this Infrastructure\\nmanagement company \\nProject includes but not limited to:\\n• Customer support Portal\\n• EDI for alliance customers\\nAllegro MicroSystems, Inc.\\nSenior Programmer/Analyst\\n2007\\xa0-\\xa02013\\xa0(6 years)\\nWorcester MA\\nResponsible for the software support at all levels of the Quality Organization\\nin the areas of product qualification and reliability, failure analysis, audit\\nresponse, corrective action, containment, and customer interfacing. \\nDesigned, developed, tested, implemented and maintained intranet\\nsolutions for the Allegro Quality Organization using the latest Microsoft .NET\\nTechnologies\\nFriendly\\'s Ice Cream Corporation\\nProgrammer/Analyst\\nNovember 2004\\xa0-\\xa0November 2007\\xa0(3 years 1 month)\\nWilberham MA\\n\\xa0 Page 5 of 7\\xa0 \\xa0\\nResponsibilities included Design, Development, Support and Maintenance\\nof .NET ASP and Win Forms applications for the company’s, Retail and\\nRestaurant business\\nPEGASUS SATELLITE TELEVISION INC.\\nSenior Software Engineer\\nJune 2003\\xa0-\\xa0November 2004\\xa0(1 year 6 months)\\nMarlboro, Massachusetts\\nDesigned and developed WEB and Windows applications using Microsoft\\nVisual Basic 6.0, SQL-Server 2000 and the latest .NET 1.1 Framework\\nAPPLIX INC.\\nSenior Software Engineer\\nNovember 1996\\xa0-\\xa0October 2002\\xa0(6 years)\\nWestborough, Massachusetts\\nResponsible for Design, Development, Sustaining Engineering, and when\\nneeded 3rd level engineering, customer and Applix field engineer support for\\nseveral of the following companies CRM products\\nQUEUES ENFORTH DEVELOPMENT\\nSenior Software Engineer\\nAugust 1993\\xa0-\\xa0November 1996\\xa0(3 years 4 months)\\nCambridge, Massachusetts\\nCompleted a number of projects as both a project manager and/or engineer.\\nProjects included software designed for Commercial and Public Safety\\nDispatch/Records Management systems. All systems were developed in the\\nWindows/Unix client/server environment\\nALDRICH EASTMAN, AND WALTCH\\nSystems Integrator / Senior Software Engineer\\nMarch 1992\\xa0-\\xa0July 1993\\xa0(1 year 5 months)\\nBoston, Massachusetts\\nResponsible for developing and implementing a client/server database\\narchitecture for this real estate investment company. The architecture includes\\nMicrosoft (16bit) Windows based PC\\'s and Apple Mac used as clients. VAX/\\nVMS and PC/UNIX platforms with the INGRES RDBMS used as the database\\nserver.\\nTHOMSON FINANCIAL NETWORKS\\nSoftware Engineer\\nJune 1985\\xa0-\\xa0March 1992\\xa0(6 years 10 months)\\n\\xa0 Page 6 of 7\\xa0 \\xa0\\nBoston, Massachusetts\\nInvolved in the design and development of \"FIRST CALL\", a financial\\ndistributed access database system. The system routes financial data from\\na PC workstation through a VAX data-validation Comm-server, and on to\\na PC database server via satellite. Data is also sent to a backup Unix PC\\nComm-server via TCP/IP. This data can be retrieved via phone lines by the PC\\ndatabase server, as a result of lost data or as an alternate connection to the\\nsatellite transmission.\\nSoftware Resources, Inc.\\nSoftware Engineer / Programmer\\nMay 1982\\xa0-\\xa0May 1983\\xa0(1 year 1 month)\\nCambridge, Massachusetts\\nResponsible for the maintenance and enhancement of micro software\\nproducts for the IBM/PC/XT and Apple systems. Products included a Portfolio\\nManagement System and a Financial Analysis and Graphics System.\\nEducation\\nBoston University\\nBachelor\\'s degree,\\xa0Computer Science\\xa0·\\xa0(1980\\xa0-\\xa01986)\\n\\xa0 Page 7 of 7\\n\\nWith this context, please chat with the user, always staying in character as Stephen Kruglewicz.'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "system_prompt" ] }, { "cell_type": "code", "execution_count": 26, "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": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "\n", "\n", "print(messages)" ] }, { "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": 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": null, "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": null, "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": null, "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": null, "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": null, "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": null, "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": null, "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": null, "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": ".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.12" } }, "nbformat": 4, "nbformat_minor": 2 }