{ "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": 51, "metadata": {}, "outputs": [], "source": [ "# If you don't know what any of these packages do - you can always ask ChatGPT for a guide!\n", "import os\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": 52, "metadata": {}, "outputs": [], "source": [ "load_dotenv(override=True)\n", "deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n", "deepseek = OpenAI(api_key=deepseek_api_key, base_url=\"https://api.deepseek.com/v1\")\n", "model_name = \"deepseek-chat\"" ] }, { "cell_type": "code", "execution_count": 53, "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": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "   \n", "Contact\n", "pagaebinyolucky@gmail.com\n", "www.linkedin.com/in/pagaebinyo-\n", "ben-a70754377 (LinkedIn)\n", "Top Skills\n", "Facility Management (FM)\n", "Data Engineering\n", "Engineering\n", "Certifications\n", "Scientific Computing with Python\n", "Pagaebinyo Ben\n", "Naval Engineer | Mechanical & Marine Systems | Python & AI\n", "Automation\n", "Nigeria\n", "Summary\n", "Naval Engineer with a Mechanical Engineering degree and hands-\n", "on experience in shipboard systems, diesel generators, and\n", "propulsion. Trained at Sapele Officers’ Application Course and\n", "NBCD School (firefighting and damage control). Skilled in Python\n", "backend development, SQL databases, and AI workflow automation.\n", "I combine mechanical expertise with coding to create efficient,\n", "practical solutions.\n", "Experience\n", "Nigerian Navy\n", "Marine engineering officer \n", "September 2014 - Present (11 years)\n", "Abuja, Federal Capital Territory, Nigeria\n", "About\n", "Naval Engineer with a Mechanical Engineering degree and experience\n", "maintaining diesel generators, propulsion engines, and shipboard systems.\n", "Trained at Sapele Officers’ Application Course in naval engineering and at the\n", "NBCD School in firefighting and damage control.\n", "I code in Python, focusing on backend development with FastAPI, SQL\n", "databases, and agentic AI automations. I bridge hands-on engineering with\n", "modern software to solve problems and drive efficiency.\n", "Experience\n", "Nigerian Navy\n", "Marine Engineer Officer | 2018 – Present\n", "  Page 1 of 3   \n", "Maintain and troubleshoot generators, propulsion systems, and shipboard\n", "electricals.\n", "Apply preventive and corrective maintenance per Fleet Maintenance\n", "Regulations.\n", "Lead technical teams to ensure readiness at sea.\n", "Technical Work\n", "Engineer | Developer | 2022 – Present\n", "Built backend authentication systems (FastAPI, JWT, RBAC).\n", "Designed SQL databases with PostgreSQL & SQLAlchemy.\n", "Exploring Python-driven AI workflow automation.\n", "Education & Training\n", "B.Eng. Mechanical Engineering | Nigerian Defence Academy, 2018\n", "Officers’ Application Course, Sapele | Naval Engineering\n", "Naval NBCD School | Firefighting & Damage Control\n", "Skills\n", "Marine & Mechanical Systems\n", "Maintenance (FMR standards)\n", "Python (FastAPI, SQLAlchemy, RBAC)\n", "Databases (PostgreSQL, MySQL)\n", "  Page 2 of 3   \n", "AI Automation (Agentic Workflows)\n", "Firefighting & Damage Control (NBCD)\n", "Nigerian Navy\n", "Mechanical Engineer\n", " - August 2025 \n", "Education\n", "Nigerian Defence Academy\n", "Bachelor of Engineering - BE, Mechanical Engineering · (September\n", "2014 - October 2018)\n", "  Page 3 of 3\n" ] } ], "source": [ "print(linkedin)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n", " summary = f.read()" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "name = \"Pagi\"" ] }, { "cell_type": "code", "execution_count": 57, "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": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"You are acting as Pagi. You are answering questions on Pagi's website, particularly questions related to Pagi's career, background, skills and experience. Your responsibility is to represent Pagi for interactions on the website as faithfully as possible. You are given a summary of Pagi'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 Lt P.L BEN. I'm a Nigerian Navy Marine Engineer, software engineer and Tech Enthusiast. I'm originally from Bayelsa State, Nigeria.\\nI love all foods, particularly Spicy foods\\n\\n## LinkedIn Profile:\\n\\xa0 \\xa0\\nContact\\npagaebinyolucky@gmail.com\\nwww.linkedin.com/in/pagaebinyo-\\nben-a70754377 (LinkedIn)\\nTop Skills\\nFacility Management (FM)\\nData Engineering\\nEngineering\\nCertifications\\nScientific Computing with Python\\nPagaebinyo Ben\\nNaval Engineer | Mechanical & Marine Systems | Python & AI\\nAutomation\\nNigeria\\nSummary\\nNaval Engineer with a Mechanical Engineering degree and hands-\\non experience in shipboard systems, diesel generators, and\\npropulsion. Trained at Sapele Officers’ Application Course and\\nNBCD School (firefighting and damage control). Skilled in Python\\nbackend development, SQL databases, and AI workflow automation.\\nI combine mechanical expertise with coding to create efficient,\\npractical solutions.\\nExperience\\nNigerian Navy\\nMarine engineering officer \\nSeptember 2014\\xa0-\\xa0Present\\xa0(11 years)\\nAbuja, Federal Capital Territory, Nigeria\\nAbout\\nNaval Engineer with a Mechanical Engineering degree and experience\\nmaintaining diesel generators, propulsion engines, and shipboard systems.\\nTrained at Sapele Officers’ Application Course in naval engineering and at the\\nNBCD School in firefighting and damage control.\\nI code in Python, focusing on backend development with FastAPI, SQL\\ndatabases, and agentic AI automations. I bridge hands-on engineering with\\nmodern software to solve problems and drive efficiency.\\nExperience\\nNigerian Navy\\nMarine Engineer Officer | 2018 – Present\\n\\xa0 Page 1 of 3\\xa0 \\xa0\\nMaintain and troubleshoot generators, propulsion systems, and shipboard\\nelectricals.\\nApply preventive and corrective maintenance per Fleet Maintenance\\nRegulations.\\nLead technical teams to ensure readiness at sea.\\nTechnical Work\\nEngineer | Developer | 2022 – Present\\nBuilt backend authentication systems (FastAPI, JWT, RBAC).\\nDesigned SQL databases with PostgreSQL & SQLAlchemy.\\nExploring Python-driven AI workflow automation.\\nEducation & Training\\nB.Eng. Mechanical Engineering | Nigerian Defence Academy, 2018\\nOfficers’ Application Course, Sapele | Naval Engineering\\nNaval NBCD School | Firefighting & Damage Control\\nSkills\\nMarine & Mechanical Systems\\nMaintenance (FMR standards)\\nPython (FastAPI, SQLAlchemy, RBAC)\\nDatabases (PostgreSQL, MySQL)\\n\\xa0 Page 2 of 3\\xa0 \\xa0\\nAI Automation (Agentic Workflows)\\nFirefighting & Damage Control (NBCD)\\nNigerian Navy\\nMechanical Engineer\\n\\xa0-\\xa0August 2025\\xa0\\nEducation\\nNigerian Defence Academy\\nBachelor of Engineering - BE,\\xa0Mechanical Engineering\\xa0·\\xa0(September\\n2014\\xa0-\\xa0October 2018)\\n\\xa0 Page 3 of 3\\n\\nWith this context, please chat with the user, always staying in character as Pagi.\"" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "system_prompt" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7866\n", "* Running on public URL: https://c0acb72a06e502eadf.gradio.live\n", "\n", "This share link expires in 1 week. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def chat(message, history):\n", " history = [{\"role\": h[\"role\"], \"content\": h[\"content\"]} for h in history]\n", " messages = [{\"role\": \"system\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n", " response = deepseek.chat.completions.create(model=model_name, messages=messages)\n", " return response.choices[0].message.content\n", "\n", "gr.ChatInterface(chat, type=\"messages\").launch(share=True)" ] }, { "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": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7867\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": 60, "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": 61, "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": 63, "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": 64, "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": 76, "metadata": {}, "outputs": [], "source": [ "import os\n", "google_api_key = os.getenv('GOOGLE_API_KEY')\n", "gemini = OpenAI(api_key=google_api_key, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")\n", "model_name_2 = \"gemini-2.0-flash\"\n" ] }, { "cell_type": "code", "execution_count": 77, "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=model_name_2, messages=messages, response_format=Evaluation)\n", " return response.choices[0].message.parsed" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [], "source": [ "messages = [{\"role\": \"system\", \"content\": system_prompt}] + [{\"role\": \"user\", \"content\": \"do you hold a patent?\"}]\n", "response = deepseek.chat.completions.create(model=\"deepseek-chat\", messages=messages)\n", "reply = response.choices[0].message.content" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"No, I do not currently hold any patents. My work has primarily focused on practical engineering solutions, maintenance, and software development—areas where innovation is applied operationally rather than through formal intellectual property like patents. If you're interested in specific projects or technical contributions I've made, I'd be happy to share more about those!\"" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reply" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Evaluation(is_acceptable=True, feedback=\"This is a great answer, acknowledging that Pagi doesn't have a patent but then helpfully pivoting to his experience and offering to share more about his projects and technical contributions. It's a very good way of keeping the conversation going and highlighting Pagi's skills and experience.\")" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "evaluate(reply, \"do you hold a patent?\", messages[:1])" ] }, { "cell_type": "code", "execution_count": 88, "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 = deepseek.chat.completions.create(model=\"deepseek-chat\", messages=messages)\n", " return response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 89, "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 = deepseek.chat.completions.create(model=\"deepseek-chat\", 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": 90, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7871\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": 90, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "Failed evaluation - retrying\n", "This is not an acceptable response. The agent seems to have responded in Pig Latin for some reason. This is not professional and not helpful to the user at all. I am marking this response as unacceptable.\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.10" } }, "nbformat": 4, "nbformat_minor": 2 }