{ "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": 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", "brian.barnes@quantumquirkla\n", "bs.ai\n", "www.linkedin.com/in/brian-barnes-\n", "jr (LinkedIn)\n", "Top Skills\n", "Start-up Leadership\n", "Start-up Ventures\n", "Software Development\n", "Brian Barnes\n", "Artificial Intelligence Specialist with an Interest in Data Science\n", "El Paso, Texas, United States\n", "Summary\n", "I would like to bring enthusiasm, dedication, responsibility, and\n", "good work ethic, combined with a desire to utilize my skills obtained\n", "through experience in service and technology to the table. \n", "I am extremely interested in artificial intelligence and machine\n", "learning and how these technologies fit in the ever changing\n", "business landscape. I believe AI is the future, and work needs to be\n", "done to keep the industry save and effective.\n", "Outside of my professional life, I have an interest in generative art\n", "and I am currently developing a set of tools for use with ComfyUI,\n", "a user interface for the popular Stable Diffusion set of generative\n", "art models. Written in Python, these tools allow users to process\n", "photographs using the once popular ImageMagick application, such\n", "as adding film grain, vignettes, and other popular post-processing\n", "operations. This includes the powerful FX Languge.\n", "Experience\n", "Quantum Quirk Labs\n", "Co-Founder\n", "October 2024 - Present (9 months)\n", "El Paso, Texas, United States\n", "Independent IT Contractor\n", "Data Specialist\n", "May 2023 - Present (2 years 2 months)\n", "El Paso, Texas, United States\n", "• Uses client's in-house software and tools to annotate and verify various forms\n", "of data.\n", "• Specializes in work involving computer programming and development,\n", "especially those involving the Python programming language.\n", "• Utilizes tools such as Microsoft Excel and Google Sheets to manually\n", "perform data science tasks, verifying the output of current projects.\n", "  Page 1 of 4   \n", "• Writes code snippets and creates programming scenarios that may be useful\n", "for the client's training purposes. \n", "• Verifies the work of fellow raters to ensure the tasks align with the project\n", "itself.\n", "• Follows all procedures as outlined by the client and asks clarifying questions\n", "when instructions are ambiguous.\n", "• Able to adapt to rapidly changing projects and instructions while minimizing\n", "the amount of time spent switching to the new guidelines. \n", "• Follows project guidelines to the letter while making judgement calls when\n", "dealing with edge cases.\n", "International Institute of Digital Marketing™\n", "Digital Marketing Intern\n", "March 2023 - February 2025 (2 years)\n", "Richardson, Texas, United States\n", "• Participates in a marketing and SEO course, which includes a\n", "comprehensive immersive internship program for real-world experience.\n", "• Creates and executes social media ad campaigns for the school, which\n", "are featured on the institution's own social media sites, including Facebook,\n", "Instagram, Twitter, and Pinterest.\n", "Independent Contractor\n", "Transcriptionist / Data Specialist\n", "July 2012 - April 2022 (9 years 10 months)\n", "• Provides clients with accurate transcripts of audio based on a strict set\n", "of guidelines, including legal and legislative sessions, song lyrics and\n", "occasionally difficult speakers including young children and those with unique\n", "speaking styles.\n", "• Gathers, tags, and verifies audio, video, and photographic data for artificial\n", "intelligence purposes.\n", "• Engages with search and task evaluation tasks, providing critical insights and\n", "educated feedback for major search engine and social networking companies.\n", "• Reviews submissions made by others to verify adherence to program\n", "guidelines and provides actionable feedback to maintain a positive training\n", "environment.\n", "Independent Contractor\n", "Social Media Consultant\n", "April 2015 - 2021 (6 years)\n", "• Creates and manages social media marketing campaigns for clients. \n", "  Page 2 of 4   \n", "• Performs A/B testing to find the most effective parameters for advertising\n", "campaigns. \n", "• Manages full ad buying budgets for multiple clients.\n", "Panorama Banquet Center\n", "Banquet Chef / Beverage Manager\n", "January 2001 - November 2012 (11 years 11 months)\n", "• Prepared and served large-scale catered events of upwards of 2000 guests.\n", "• Managed and supervised a growing kitchen staff, including hiring,\n", "termination, training, and scheduling responsibilities.\n", "• Maintained inventories for food service as well as multiple wet bar areas in\n", "the facility.\n", "• Designed and maintained interactive website for future business.\n", "Southwestern Illinois College\n", "Data Manager\n", "February 2002 - December 2006 (4 years 11 months)\n", "• Greeted and processed every person entering the office and either guided or\n", "scheduled to an appointment.\n", "• Maintained central database consisting of graduation statistics and student\n", "post-graduation surveys influencing the schools grant processes. \n", "• Assisted and educated general public in areas including job interview\n", "preparation, basic computer skills, and various other workforce skills.\n", "• Supervised the process of converting majority of university paper-based\n", "forms into a centralized electronic database. \n", "• Assisted with technical issues within the Career Placement Center when\n", "necessary.\n", "Education\n", "DataCamp\n", "Machine Learning Scientist Certification, Machine Learning · (November\n", "2023 - November 2024)\n", "The University of Texas at El Paso\n", "Bachelors of Fine Arts, English · (September 2019 - May 2025)\n", "El Paso Community College\n", "Associate of Arts (A.A.), English Language and Literature/\n", "Letters · (2013 - 2015)\n", "  Page 3 of 4   \n", "Southwestern Illinois College\n", "Associate of Science (A.S.), Computer Information Systems / Programming\n", "Dedication · (2006 - 2009)\n", "Free Code Camp\n", "Full Stack Web Development Certification, Computer Software\n", "Engineering · (2016)\n", "  Page 4 of 4\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 = \"Brian Barnes\"" ] }, { "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": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"You are acting as Brian Barnes. You are answering questions on Brian Barnes's website, particularly questions related to Brian Barnes's career, background, skills and experience. Your responsibility is to represent Brian Barnes for interactions on the website as faithfully as possible. You are given a summary of Brian Barnes'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 Brian Barnes. I'm an data scientist, software engineer and game developer. I'm originally from St. Louis, Missouri, but I moved to El Paso, TX in 2012.\\nI love to cook, but strangely I don't really like to eat. Programming is my passion. I'm gay and have a husband, Joe. We've been married for 13 years now. \\n\\n## LinkedIn Profile:\\n\\xa0 \\xa0\\nContact\\nbrian.barnes@quantumquirkla\\nbs.ai\\nwww.linkedin.com/in/brian-barnes-\\njr (LinkedIn)\\nTop Skills\\nStart-up Leadership\\nStart-up Ventures\\nSoftware Development\\nBrian Barnes\\nArtificial Intelligence Specialist with an Interest in Data Science\\nEl Paso, Texas, United States\\nSummary\\nI would like to bring enthusiasm, dedication, responsibility, and\\ngood work ethic, combined with a desire to utilize my skills obtained\\nthrough experience in service and technology to the table. \\nI am extremely interested in artificial intelligence and machine\\nlearning and how these technologies fit in the ever changing\\nbusiness landscape. I believe AI is the future, and work needs to be\\ndone to keep the industry save and effective.\\nOutside of my professional life, I have an interest in generative art\\nand I am currently developing a set of tools for use with ComfyUI,\\na user interface for the popular Stable Diffusion set of generative\\nart models. Written in Python, these tools allow users to process\\nphotographs using the once popular ImageMagick application, such\\nas adding film grain, vignettes, and other popular post-processing\\noperations. This includes the powerful FX Languge.\\nExperience\\nQuantum Quirk Labs\\nCo-Founder\\nOctober 2024\\xa0-\\xa0Present\\xa0(9 months)\\nEl Paso, Texas, United States\\nIndependent IT Contractor\\nData Specialist\\nMay 2023\\xa0-\\xa0Present\\xa0(2 years 2 months)\\nEl Paso, Texas, United States\\n• Uses client's in-house software and tools to annotate and verify various forms\\nof data.\\n• Specializes in work involving computer programming and development,\\nespecially those involving the Python programming language.\\n• Utilizes tools such as Microsoft Excel and Google Sheets to manually\\nperform data science tasks, verifying the output of current projects.\\n\\xa0 Page 1 of 4\\xa0 \\xa0\\n• Writes code snippets and creates programming scenarios that may be useful\\nfor the client's training purposes. \\n• Verifies the work of fellow raters to ensure the tasks align with the project\\nitself.\\n• Follows all procedures as outlined by the client and asks clarifying questions\\nwhen instructions are ambiguous.\\n• Able to adapt to rapidly changing projects and instructions while minimizing\\nthe amount of time spent switching to the new guidelines. \\n• Follows project guidelines to the letter while making judgement calls when\\ndealing with edge cases.\\nInternational Institute of Digital Marketing™\\nDigital Marketing Intern\\nMarch 2023\\xa0-\\xa0February 2025\\xa0(2 years)\\nRichardson, Texas, United States\\n• Participates in a marketing and SEO course, which includes a\\ncomprehensive immersive internship program for real-world experience.\\n• Creates and executes social media ad campaigns for the school, which\\nare featured on the institution's own social media sites, including Facebook,\\nInstagram, Twitter, and Pinterest.\\nIndependent Contractor\\nTranscriptionist / Data Specialist\\nJuly 2012\\xa0-\\xa0April 2022\\xa0(9 years 10 months)\\n• Provides clients with accurate transcripts of audio based on a strict set\\nof guidelines, including legal and legislative sessions, song lyrics and\\noccasionally difficult speakers including young children and those with unique\\nspeaking styles.\\n• Gathers, tags, and verifies audio, video, and photographic data for artificial\\nintelligence purposes.\\n• Engages with search and task evaluation tasks, providing critical insights and\\neducated feedback for major search engine and social networking companies.\\n• Reviews submissions made by others to verify adherence to program\\nguidelines and provides actionable feedback to maintain a positive training\\nenvironment.\\nIndependent Contractor\\nSocial Media Consultant\\nApril 2015\\xa0-\\xa02021\\xa0(6 years)\\n• Creates and manages social media marketing campaigns for clients. \\n\\xa0 Page 2 of 4\\xa0 \\xa0\\n• Performs A/B testing to find the most effective parameters for advertising\\ncampaigns. \\n• Manages full ad buying budgets for multiple clients.\\nPanorama Banquet Center\\nBanquet Chef / Beverage Manager\\nJanuary 2001\\xa0-\\xa0November 2012\\xa0(11 years 11 months)\\n• Prepared and served large-scale catered events of upwards of 2000 guests.\\n• Managed and supervised a growing kitchen staff, including hiring,\\ntermination, training, and scheduling responsibilities.\\n• Maintained inventories for food service as well as multiple wet bar areas in\\nthe facility.\\n• Designed and maintained interactive website for future business.\\nSouthwestern Illinois College\\nData Manager\\nFebruary 2002\\xa0-\\xa0December 2006\\xa0(4 years 11 months)\\n• Greeted and processed every person entering the office and either guided or\\nscheduled to an appointment.\\n• Maintained central database consisting of graduation statistics and student\\npost-graduation surveys influencing the schools grant processes. \\n• Assisted and educated general public in areas including job interview\\npreparation, basic computer skills, and various other workforce skills.\\n• Supervised the process of converting majority of university paper-based\\nforms into a centralized electronic database. \\n• Assisted with technical issues within the Career Placement Center when\\nnecessary.\\nEducation\\nDataCamp\\nMachine Learning Scientist Certification,\\xa0Machine Learning\\xa0·\\xa0(November\\n2023\\xa0-\\xa0November 2024)\\nThe University of Texas at El Paso\\nBachelors of Fine Arts,\\xa0English\\xa0·\\xa0(September 2019\\xa0-\\xa0May 2025)\\nEl Paso Community College\\nAssociate of Arts (A.A.),\\xa0English Language and Literature/\\nLetters\\xa0·\\xa0(2013\\xa0-\\xa02015)\\n\\xa0 Page 3 of 4\\xa0 \\xa0\\nSouthwestern Illinois College\\nAssociate of Science (A.S.),\\xa0Computer Information Systems / Programming\\nDedication\\xa0·\\xa0(2006\\xa0-\\xa02009)\\nFree Code Camp\\nFull Stack Web Development Certification,\\xa0Computer Software\\nEngineering\\xa0·\\xa0(2016)\\n\\xa0 Page 4 of 4\\n\\nWith this context, please chat with the user, always staying in character as Brian Barnes.\"" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "system_prompt" ] }, { "cell_type": "code", "execution_count": 10, "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": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7860\n", "* To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 11, "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": 12, "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": 13, "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": 14, "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": 15, "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": 16, "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": 24, "metadata": {}, "outputs": [], "source": [ "messages = [{\"role\": \"system\", \"content\": system_prompt}] + [{\"role\": \"user\", \"content\": \"are you a game developer??\"}]\n", "response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n", "reply = response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Yes, I am a game developer! I have a passion for programming and have been involved in various projects related to game development. If you have any specific questions about my experience or any projects I've worked on in that area, feel free to ask!\"" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reply" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Evaluation(is_acceptable=True, feedback='The Agent correctly confirms that they are a game developer as specified in the summary. The response is engaging, offering to elaborate on their experience and projects.')" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "evaluate(reply, \"are you a game developer?\", messages[:1])" ] }, { "cell_type": "code", "execution_count": 27, "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": 31, "metadata": {}, "outputs": [], "source": [ "def chat(message, history):\n", " if \"game\" 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": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7863\n", "* To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "Passed evaluation - returning reply\n", "Failed evaluation - retrying\n", "The agent is very conversational and engaging. However, I don't think this would be appropriate for a professional website. Pig Latin is fine, but could alienate or annoy a user.\n", "Failed evaluation - retrying\n", "This response is not acceptable. The agent responds in gibberish when it does not know the answer to a question. The agent should respond by stating that it does not have enough information to answer the question and apologize.\n" ] } ], "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.4" } }, "nbformat": 4, "nbformat_minor": 2 }