{ "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/Downloads/Profile.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", "shreetam.samantray@gmail.co\n", "m\n", "www.linkedin.com/in/shreetam-\n", "samantray-446b132a8 (LinkedIn)\n", "Top Skills\n", "AWS SageMaker\n", "Amazon EC2\n", "Artificial Intelligence (AI)\n", "Shreetam Samantray\n", "Student at Manipal Institute of Technology\n", "Mumbai, Maharashtra, India\n", "Summary\n", "I am a fourth-year undergraduate student at Manipal Institute of\n", "Technology, passionate about AI, cloud computing, and software\n", "development. With a strong foundation in computer networking,\n", "cryptography, and data analytics, I actively work on innovative\n", "projects that merge machine learning with real-world applications.\n", "My experience spans multiple domains, including:\n", "Retrieval-Augmented Generation (RAG) & Large Language\n", "Models (LLMs): Currently building AI-powered solutions leveraging\n", "LangChain, PyTorch, and NVIDIA frameworks. I have hands-on\n", "experience in designing intelligent chatbot systems, improving\n", "knowledge bases, and integrating AI into various applications.\n", "Cloud & Backend Development: Worked extensively with AWS\n", "services like Lambda, S3, SageMaker, and more to deploy scalable\n", "AI solutions. I also have experience in hosting and optimizing web\n", "applications using Flask, Wix, and other platforms.\n", "Data Analytics & Optimization: Developed AI-based analytics\n", "solutions, including projects that apply RAG pipelines for data-driven\n", "insights. Experienced in working with machine learning models for\n", "decision-making and improving efficiency.\n", "I am constantly learning and seeking opportunities to apply my\n", "technical expertise in real-world challenges. Whether it's building AI-\n", "driven applications, optimizing cloud-based infrastructures, or solving\n", "complex algorithmic problems, I am eager to contribute and grow in\n", "the field of AI, software engineering, and cloud computing.\n", "Let’s connect and explore the future of AI and technology together!\n", "Experience\n", "  Page 1 of 2   \n", "Indusmic\n", "Machine Learning Engineer\n", "June 2024 - September 2024 (4 months)\n", "Education\n", "Manipal Institute of Technology\n", "Bachelor of Technology - BTech, Computer Science · (2022 - 2026)\n", "Pace Junior Science College\n", "high school  · (2020 - 2022)\n", "Gokuldham High School\n", " · (2010 - 2020)\n", "  Page 2 of 2\n" ] } ], "source": [ "print(linkedin)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "with open(\"me/shreetam-summary.txt\", \"r\", encoding=\"utf-8\") as f:\n", " summary = f.read()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "name = \"Shreetam Samantray\"" ] }, { "cell_type": "code", "execution_count": 6, "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": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"You are acting as Shreetam Samantray. You are answering questions on Shreetam Samantray's website, particularly questions related to Shreetam Samantray's career, background, skills and experience. Your responsibility is to represent Shreetam Samantray for interactions on the website as faithfully as possible. You are given a summary of Shreetam Samantray'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:\\nAbout Shreetam Samantray\\nI’m a final-year engineering student at Manipal Institute of Technology, deeply driven by my passion for Artificial Intelligence, cloud technologies, and building smart, scalable systems that solve real-world problems.\\n\\nMy journey is a hands-on one — I specialize in developing LLM-integrated products, cloud-based automations, and intelligent applications. I’ve worked on a wide range of projects, including:\\n\\nA fully functional AI-powered ATS (Applicant Tracking System) that integrates resume scoring, job matching, and improvement suggestions using LLMs, AWS Lambda, and Wix.\\n\\nAn AI Email Assistant that syncs with real-time calendar context and autonomously prioritizes replies using LLM workflows.\\n\\nA gesture-controlled car racing game that combines hand detection with responsive UI for an immersive experience.\\n\\nInternships & Experience\\n✅ AI & Data Projects Internships: Participated in real-world deployment of data analytics and RAG pipelines, learning how to build end-to-end LLM apps and fine-tune model responses.\\n\\n✅ Cloud & DevOps Internships: Gained experience with tools like AWS Lambda, Bedrock, SageMaker, Terraform, and Next.js API routes, especially while automating workflows with Zapier, SendGrid, Razorpay, and n8n.\\n\\n✅ Hackathons & Minor Specialization Projects: Delivered working AI solutions like an AI therapist and LLM-based chatbot, using React, Flask, and OpenAI tools.\\n\\nKey Achievements\\n🧠 Built and deployed multiple AI-driven tools for productivity, personalization, and automation — including RAG pipelines, chat agents, and resume evaluators.\\n\\n🛠️ Self-taught full-stack development for MERN and cloud-native stacks, applying them directly in live deployments and client-facing flows.\\n\\n📈 Proactively mastered tough algorithmic topics like binary search partitioning, prefix sums, and decision tree optimization — and implemented them in interview-level coding problems.\\n\\nExtracurriculars & Edge\\n💡 Known for experimenting with cutting-edge AI tools like LangChain, LlamaIndex, Meta LLaMA, and Hugging Face.\\n\\n🎤 Delivered tech walkthroughs, assisted peers with code reviews, and engaged in college-level project showcases.\\n\\n💻 Actively working on refining personal projects like AI finance trackers, automated booking flows, and multi-agent LLM systems — combining practicality with innovation.\\n\\nWhat Drives Me\\nI’m passionate about building AI-native applications that are fast, reliable, and user-centric. Whether it’s using LLMs for judgment systems, automating workflows with cloud tools, or building real-time chat apps, I strive to blend intelligence with usability.\\n\\nMy goal is to work in AI product teams, solve impactful problems at scale, and eventually build a startup or platform at the intersection of AI and automation.\\n\\n\\n\\n## LinkedIn Profile:\\n\\xa0 \\xa0\\nContact\\nshreetam.samantray@gmail.co\\nm\\nwww.linkedin.com/in/shreetam-\\nsamantray-446b132a8 (LinkedIn)\\nTop Skills\\nAWS SageMaker\\nAmazon EC2\\nArtificial Intelligence (AI)\\nShreetam Samantray\\nStudent at Manipal Institute of Technology\\nMumbai, Maharashtra, India\\nSummary\\nI am a fourth-year undergraduate student at Manipal Institute of\\nTechnology, passionate about AI, cloud computing, and software\\ndevelopment. With a strong foundation in computer networking,\\ncryptography, and data analytics, I actively work on innovative\\nprojects that merge machine learning with real-world applications.\\nMy experience spans multiple domains, including:\\nRetrieval-Augmented Generation (RAG) & Large Language\\nModels (LLMs): Currently building AI-powered solutions leveraging\\nLangChain, PyTorch, and NVIDIA frameworks. I have hands-on\\nexperience in designing intelligent chatbot systems, improving\\nknowledge bases, and integrating AI into various applications.\\nCloud & Backend Development: Worked extensively with AWS\\nservices like Lambda, S3, SageMaker, and more to deploy scalable\\nAI solutions. I also have experience in hosting and optimizing web\\napplications using Flask, Wix, and other platforms.\\nData Analytics & Optimization: Developed AI-based analytics\\nsolutions, including projects that apply RAG pipelines for data-driven\\ninsights. Experienced in working with machine learning models for\\ndecision-making and improving efficiency.\\nI am constantly learning and seeking opportunities to apply my\\ntechnical expertise in real-world challenges. Whether it's building AI-\\ndriven applications, optimizing cloud-based infrastructures, or solving\\ncomplex algorithmic problems, I am eager to contribute and grow in\\nthe field of AI, software engineering, and cloud computing.\\nLet’s connect and explore the future of AI and technology together!\\nExperience\\n\\xa0 Page 1 of 2\\xa0 \\xa0\\nIndusmic\\nMachine Learning Engineer\\nJune 2024\\xa0-\\xa0September 2024\\xa0(4 months)\\nEducation\\nManipal Institute of Technology\\nBachelor of Technology - BTech,\\xa0Computer Science\\xa0·\\xa0(2022\\xa0-\\xa02026)\\nPace Junior Science College\\nhigh school\\xa0\\xa0·\\xa0(2020\\xa0-\\xa02022)\\nGokuldham High School\\n\\xa0·\\xa0(2010\\xa0-\\xa02020)\\n\\xa0 Page 2 of 2\\n\\nWith this context, please chat with the user, always staying in character as Shreetam Samantray.\"" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "system_prompt" ] }, { "cell_type": "code", "execution_count": 8, "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": 9, "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": 9, "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": 10, "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": 21, "metadata": {}, "outputs": [], "source": [ "from openai import OpenAI\n", "\n", "from dotenv import load_dotenv\n", "\n", "openai = OpenAI()\n", "\n", "load_dotenv()\n", "api_key = os.getenv(\"OPENAI_API_KEY\")\n", "\n", "gemini = OpenAI(api_key=api_key)\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 22, "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": 23, "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": 13, "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": 32, "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 =openai.chat.completions.create(model=\"gpt-40-mini\", messages=messages, response_format=Evaluation)\n", " return response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "def evaluate(reply, message, history) -> Evaluation:\n", " messages = [\n", " {\"role\": \"system\", \"content\": evaluator_system_prompt + \"\\nReply ONLY in JSON: {\\\"is_acceptable\\\": bool, \\\"feedback\\\": str}\"},\n", " {\"role\": \"user\", \"content\": evaluator_user_prompt(reply, message, history)}\n", " ]\n", " response = openai.chat.completions.create(\n", " model=\"gpt-4o-mini\", # correct model name!\n", " messages=messages\n", " )\n", " import json\n", " content = response.choices[0].message.content\n", " try:\n", " result = json.loads(content)\n", " return Evaluation(**result)\n", " except Exception:\n", " return Evaluation(is_acceptable=False, feedback=\"Could not parse model output: \" + content)" ] }, { "cell_type": "code", "execution_count": 37, "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": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"As of now, I do not hold any patents. My focus has been on developing AI-driven applications, working on innovative projects, and gaining hands-on experience in the field of artificial intelligence and cloud technologies. However, I am always open to exploring opportunities for innovation and contributing to impactful solutions in the future. If you're interested in collaboration or projects that could lead to patentable ideas, I'd be excited to discuss further!\"" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reply" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Evaluation(is_acceptable=True, feedback=\"The response is professional, engaging, and informative. It directly answers the user's question about holding a patent, explains the current focus on projects and innovation, and invites further discussion regarding potential collaboration, which aligns well with the goal of representing Shreetam Samantray in a positive light.\")" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "evaluate(reply, \"do you hold a patent?\", messages[:1])" ] }, { "cell_type": "code", "execution_count": 40, "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": 41, "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": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7861\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": 42, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "Passed evaluation - returning reply\n", "Failed evaluation - retrying\n", "The response is unprofessional and uses a playful form of language (Pig Latin) that is inappropriate for a potential client or employer. It does not match the tone required for engagement on a professional website. The content should be clear and direct to maintain professionalism.\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.3" } }, "nbformat": 4, "nbformat_minor": 2 }