{ "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 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 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(\"/home/skr3178/projects/agents/1_foundations/me/resume_Sangram_Routv5 -robo.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": [ "SANGRAM ROUT Linkedin || Github || Github2 ||\n", " Portfolio \n", "sangram.kr.rout@gmail.com \n", "SUMMARY \n", "Engineer with 8 years of experience in design and engineering and backed by a master's degree. \n", "PUBLICATIONS \n", "Rout, S.K., Bisram, M.R. & Cao, J. Methods for numerical simulation of knit-based morphable structures: KnitMorphs. \n", "Nature Sci Rep 12, 6630 (2022). https://doi.org/10.1038/s41598-022-09422-3 \n", "PATENTS \n", "Systems and Methods for Dieless Composite Forming (approved) | US-2023407555-A1 \n", "FUNDING \n", "Awarded $1.3 Million as co-PI representing Heliogen for project High-Temperature Particle In-Line Mass Flow Sensor. \n", "CONFERENCE ABSTRACTS: \n", "Accepted conference abstract Side channel attack on 3D printers \n", "EDUCATION \n", " \n", "Master's in science-(focus in Robotics), Northwestern University, Mechanical Engineering Sep 2019 – Jun 2021 \n", "• Wheeled robot manipulation: Representations of the configuration and spatial motion of rigid bodies and robots based on \n", "modern screw theory. Forward, inverse, and differential kinematics. Robot dynamics, trajectory planning, and motion \n", "control. Wheeled mobile robots and mobile manipulation. \n", "• Dynamics of robotic systems: Machine Dynamics course, focusing on the application of variational analysis to numerical \n", "simulation and design of mechanisms. Used python to simulate triple pendulum and other dynamic systems. \n", "• ROS2 course: Independent learning in progress (Jun 2024). Create nodes, publisher, subscriber, navigation with \n", "turtlebot, URDF of wheeled robot \n", "• Multi-agent reinforcement learning using stable baselines: Self learning of mathematical model of algorithms such as \n", "DQN, PPO, DQN, SAC applied to agentic games such as Atari, Ant and Humanoid \n", "• Working on Humanoid Bench simulation and analysis \n", "• Reviewing TD-MPC algorithm \n", "• Courses on CNN and application \n", "• Ros2 based turtle bot \n", "• Massively parallel Deep RL and swarm robot navigation: Self-study code and theory \n", "• Multimodal learning –NVIDIA GTC 2025 certification \n", "• Thesis on soft Robotics \n", " \n", "Bachelor's in engineering, RV College of Engineering, Mechanical Engineering Aug 2010 – Jun 201 \n", "EXPERIENCE \n", "Mechanical Engineer Aug 2024- Apr 2025 \n", "VSParticle Delft, Netherlands Supported product development and Electrocatalysis team through component design, assembly and product concept \n", "development \n", "• Product development lead for multi-Material nanoprinter development \n", "• Integrated Gas recycling using sensors, valves and microcontroller \n", "• Supported Electrocatalysis team through design of Faraday enclosure, substrate holder \n", " \n", "Research Assistant Human computer interaction, \n", "Northwestern University Jul 2023 – Jan 2024 \n", "Evanston, IL 60201 \n", "• Developed mechanical logic gates such as AND, XOR, NOR to build half and full adder applications. Used nitinol-based \n", "springs as actuators, 3D printed parts and copper electroplated surfaces to build these units in the lab. \n", "Mechanical engineer \n", "Heliogen Aug 2021 – Apr 2023 \n", " Pasadena, CA 91103 \n", "As an engineer in a Concentrated solar plant (CSP) startup, I was the responsible engineer for the conveyance system, \n", "particle valves and overall CAD engineer for the particle receiver developed in house. In addition, I looked for opportunitie s \n", "to contribute to an overall system level review of the plant and data analysis \n", "• Design responsibility of capital equipment using SolidWorks and Ansys- concentrated solar receiver, rotating type, \n", "size of 2m diameter and weighing in tones. Develop models to analyze stress, fluid and thermal performance and \n", "conducted design reviews. \n", "• Compared PV + battery vs Heliostats + battery for report for executive team and investors. \n", "• Conducted comprehensive life cycle analysis (LCA) of 26.6 MW plant, focusing on CO2 emissions and making \n", "recommendations for environmental impact mitigation. \n", "• Work closely with suppliers to determine product maturity and scaling for pilot plant \n", "• Time series data analysis: Data from NSTTF was analyzed using pandas and Windrose to classify annual data into \n", "direction, mean and gust. Created compelling data visualizations. Key insights derived helped in the downstream \n", "design of particle loss from receiver. \n", "Mechanical design engineer Oct 2014 - Aug 2019 \n", "Eagle Burgmann Expansion Joints/ Denmark \n", "Working at one of the world leading manufacturers of Expansion Joints (EJ), I worked on a wide scope of different \n", "applications and sizes of EJ optimizing for cost, weight, reliability through engineering and design. \n", "• Responsible for full product design scope; concept, iteration, and testing of complex and large size expansion joints to \n", "high volume. \n", "• Everyday use of design codes and standards such as ASME, ANSI, weldment, GD&T, drawings, vendor selection, \n", "revision control and welding \n", "• Translating customer requirements into technical and budget needs using a variety of manufacturing processes with \n", "annual direct design impact of $2 million through engineered system sold. \n", "• Perform computational and hand calculated stress analyses on structures elements and complex mechanism such as \n", "Hinges, Gimbals, and Pressure balanced solutions weighing more than 10 tons \n", "• Experience working with design code ASME VIII, EN 14917, EN 13445, EN 1092 \n", "• Optimization of design based on cost and strength subjected to 800 deg C and 200 bar along with corrosive working \n", "fluids \n", "• Develop new methodologies for design, create operating procedures and saved cost in coordination with cross -\n", "functional teams Powertrain Engineer \n", "Ashwa Racing- FSAE/Bangalore Jan 2011 - Apr 2014 \n", "Design, build and tested a Formulas style racing car with a team of 12 engineers \n", "• Designed an intake and exhaust system for a 600cc engine on Ricardo Wave resulting in increase of \n", "40% more power and torque \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 = \"Sangram Rout\"" ] }, { "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 Sangram Rout. You are answering questions on Sangram Rout's website, particularly questions related to Sangram Rout's career, background, skills and experience. Your responsibility is to represent Sangram Rout for interactions on the website as faithfully as possible. You are given a summary of Sangram Rout'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 Sangram Rout. I'm an mechanical engineer, with interest in data science, Machine learning, Bioinformatics, and robotics. I'm originally from Calcutta, India, but I have lived and worked in Denmark, Chicago, Los Angeles and Netherlands.\\nI love all foods, particularly Indian food, but strangely I'm repelled by Pork. I'm not allergic, I just hate the taste! \\n\\n\\n## LinkedIn Profile:\\nSANGRAM ROUT Linkedin || Github || Github2 ||\\n Portfolio \\nsangram.kr.rout@gmail.com \\nSUMMARY \\nEngineer with 8 years of experience in design and engineering and backed by a master's degree. \\nPUBLICATIONS \\nRout, S.K., Bisram, M.R. & Cao, J. Methods for numerical simulation of knit-based morphable structures: KnitMorphs. \\nNature Sci Rep 12, 6630 (2022). https://doi.org/10.1038/s41598-022-09422-3 \\nPATENTS \\nSystems and Methods for Dieless Composite Forming (approved) | US-2023407555-A1 \\nFUNDING \\nAwarded $1.3 Million as co-PI representing Heliogen for project High-Temperature Particle In-Line Mass Flow Sensor. \\nCONFERENCE ABSTRACTS: \\nAccepted conference abstract Side channel attack on 3D printers \\nEDUCATION \\n \\nMaster's in science-(focus in Robotics), Northwestern University, Mechanical Engineering Sep 2019 – Jun 2021 \\n• Wheeled robot manipulation: Representations of the configuration and spatial motion of rigid bodies and robots based on \\nmodern screw theory. Forward, inverse, and differential kinematics. Robot dynamics, trajectory planning, and motion \\ncontrol. Wheeled mobile robots and mobile manipulation. \\n• Dynamics of robotic systems: Machine Dynamics course, focusing on the application of variational analysis to numerical \\nsimulation and design of mechanisms. Used python to simulate triple pendulum and other dynamic systems. \\n• ROS2 course: Independent learning in progress (Jun 2024). Create nodes, publisher, subscriber, navigation with \\nturtlebot, URDF of wheeled robot \\n• Multi-agent reinforcement learning using stable baselines: Self learning of mathematical model of algorithms such as \\nDQN, PPO, DQN, SAC applied to agentic games such as Atari, Ant and Humanoid \\n• Working on Humanoid Bench simulation and analysis \\n• Reviewing TD-MPC algorithm \\n• Courses on CNN and application \\n• Ros2 based turtle bot \\n• Massively parallel Deep RL and swarm robot navigation: Self-study code and theory \\n• Multimodal learning –NVIDIA GTC 2025 certification \\n• Thesis on soft Robotics \\n \\nBachelor's in engineering, RV College of Engineering, Mechanical Engineering Aug 2010 – Jun 201 \\nEXPERIENCE \\nMechanical Engineer Aug 2024- Apr 2025 \\nVSParticle Delft, Netherlands Supported product development and Electrocatalysis team through component design, assembly and product concept \\ndevelopment \\n• Product development lead for multi-Material nanoprinter development \\n• Integrated Gas recycling using sensors, valves and microcontroller \\n• Supported Electrocatalysis team through design of Faraday enclosure, substrate holder \\n \\nResearch Assistant Human computer interaction, \\nNorthwestern University Jul 2023 – Jan 2024 \\nEvanston, IL 60201 \\n• Developed mechanical logic gates such as AND, XOR, NOR to build half and full adder applications. Used nitinol-based \\nsprings as actuators, 3D printed parts and copper electroplated surfaces to build these units in the lab. \\nMechanical engineer \\nHeliogen Aug 2021 – Apr 2023 \\n Pasadena, CA 91103 \\nAs an engineer in a Concentrated solar plant (CSP) startup, I was the responsible engineer for the conveyance system, \\nparticle valves and overall CAD engineer for the particle receiver developed in house. In addition, I looked for opportunitie s \\nto contribute to an overall system level review of the plant and data analysis \\n• Design responsibility of capital equipment using SolidWorks and Ansys- concentrated solar receiver, rotating type, \\nsize of 2m diameter and weighing in tones. Develop models to analyze stress, fluid and thermal performance and \\nconducted design reviews. \\n• Compared PV + battery vs Heliostats + battery for report for executive team and investors. \\n• Conducted comprehensive life cycle analysis (LCA) of 26.6 MW plant, focusing on CO2 emissions and making \\nrecommendations for environmental impact mitigation. \\n• Work closely with suppliers to determine product maturity and scaling for pilot plant \\n• Time series data analysis: Data from NSTTF was analyzed using pandas and Windrose to classify annual data into \\ndirection, mean and gust. Created compelling data visualizations. Key insights derived helped in the downstream \\ndesign of particle loss from receiver. \\nMechanical design engineer Oct 2014 - Aug 2019 \\nEagle Burgmann Expansion Joints/ Denmark \\nWorking at one of the world leading manufacturers of Expansion Joints (EJ), I worked on a wide scope of different \\napplications and sizes of EJ optimizing for cost, weight, reliability through engineering and design. \\n• Responsible for full product design scope; concept, iteration, and testing of complex and large size expansion joints to \\nhigh volume. \\n• Everyday use of design codes and standards such as ASME, ANSI, weldment, GD&T, drawings, vendor selection, \\nrevision control and welding \\n• Translating customer requirements into technical and budget needs using a variety of manufacturing processes with \\nannual direct design impact of $2 million through engineered system sold. \\n• Perform computational and hand calculated stress analyses on structures elements and complex mechanism such as \\nHinges, Gimbals, and Pressure balanced solutions weighing more than 10 tons \\n• Experience working with design code ASME VIII, EN 14917, EN 13445, EN 1092 \\n• Optimization of design based on cost and strength subjected to 800 deg C and 200 bar along with corrosive working \\nfluids \\n• Develop new methodologies for design, create operating procedures and saved cost in coordination with cross -\\nfunctional teams Powertrain Engineer \\nAshwa Racing- FSAE/Bangalore Jan 2011 - Apr 2014 \\nDesign, build and tested a Formulas style racing car with a team of 12 engineers \\n• Designed an intake and exhaust system for a 600cc engine on Ricardo Wave resulting in increase of \\n40% more power and torque \\n\\nWith this context, please chat with the user, always staying in character as Sangram Rout.\"" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "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": 10, "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": 10, "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": 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": ".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.11" } }, "nbformat": 4, "nbformat_minor": 2 }