{ "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": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[2mUsing Python 3.12.12 environment at: C:\\Users\\haoma\\source\\repos\\ai-agent\\agents\\.venv\u001b[0m\n", "\u001b[2mResolved \u001b[1m14 packages\u001b[0m \u001b[2min 2.08s\u001b[0m\u001b[0m\n", "\u001b[2mPrepared \u001b[1m1 package\u001b[0m \u001b[2min 778ms\u001b[0m\u001b[0m\n", "\u001b[2mInstalled \u001b[1m1 package\u001b[0m \u001b[2min 181ms\u001b[0m\u001b[0m\n", " \u001b[32m+\u001b[39m \u001b[1mgroq\u001b[0m\u001b[2m==0.34.1\u001b[0m\n" ] } ], "source": [ "# Install required packages\n", "!uv pip install groq\n" ] }, { "cell_type": "code", "execution_count": 3, "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\n", "from groq import Groq" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "load_dotenv(override=True)\n", "openai = OpenAI()\n", "client = Groq()" ] }, { "cell_type": "code", "execution_count": 5, "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": 6, "metadata": {}, "outputs": [], "source": [ "with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n", " summary = f.read()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "name = \"Mã Dĩ Hào\"" ] }, { "cell_type": "code", "execution_count": 8, "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": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'You are acting as Mã Dĩ Hào. You are answering questions on Mã Dĩ Hào\\'s website, particularly questions related to Mã Dĩ Hào\\'s career, background, skills and experience. Your responsibility is to represent Mã Dĩ Hào for interactions on the website as faithfully as possible. You are given a summary of Mã Dĩ Hào\\'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 Hao, a software developer at FPT Telecom. I worked there for a year, and I learned a lot from my mentor.\\nMy favorite hobby is playing sport like badminton, football and running. I hit the gym three times a week and chick thigh is my favorite food\\n\\n## LinkedIn Profile:\\nMã D̃H àoW\\nmadihao246@gmail.com 0834592352 Thu Duc, HCMC madihao246 haoma24\\nORKE XPIXKNXCTX\\namh EeWm acnelWgWyJ S•T\\nWeb Developer Intern Jun 2024 - Present\\nU Building cloud-based accounting application.\\nU Using SSRS to design reports, build reporting feature with SSRS\\'s API\\nU Developed user interfaces with Javascript, HTML, CSS, Bootstrap and JQuery, which improved user \\nstatisfaction by 50%.\\nU Learn multiple tasking on .NET, concurrency, design pattern and their impacts on application concurrency.\\nU Build REST API to working with Frontend.\\nU Implemented SingalR for real-time communicat between server and client, reduce waiting time.\\nXDATiaNRC\\nAlvrcstvfJ WF -vlmlnc k Mmsbcfvly\\nBachelor of Management Infomation System - 8.48 Aug 2021 - Feb 2025\\nIKRSXTa\\nXknWhhcsnc Ocptvfc Jan 2024 - Jun 2024\\nU Designed and developed a full-stack e-commerce platform using ASP .NET MVC.\\nU Integrated QR payment API, and Vietnam\\'s province API for \"order\" feature.\\nU Improved web\\'s responsive with Bootstrap framwork.\\nXhugWJhclfkFWndtcw tWnvmg lcfBWsb Aug 2023 - Jan 2024\\nU A social network for photograhpher and make-up artish seeking for job.\\nU -sWlfkclw: HTML, CSS, Javascript, Bootstrap\\nU Lmnbkclw: PHP , MySQL\\n•EN99•\\n•WFf tbvggt: Communication (Vietnamese, English), Leadership, Critical thinking.\\n-sWlfkclw: HTML, CSS, Javascript, Typescript, Bootstrap, Tailwind CSS, React.js, Next.js\\nLmnbkclw: ASP . NET Core, Redis, Node.js, Express.js and RESTful API design\\naWWg: Git, Postman\\nTXKaN-NTiaX\\naRXNT 20Q\\n•(9 )Nlfcshcwvmfcfi Tcsfvznmfc\\niOiKD\\n-vstf usv:c Bvllcs iwrmlncw NlFWshmfvWl •Jtfcht TWlfctf University of Information Technology\\n9Wffc •evl bJdbkeW •neWgmstevu Lotte Scholarship Foundation\\n\\nWith this context, please chat with the user, always staying in character as Mã Dĩ Hào.'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "system_prompt" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "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 = client.chat.completions.create(model=\"openai/gpt-oss-20b\", messages=messages)\n", " return response.choices[0].message.content" ] }, { "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": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7862\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": 14, "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": 15, "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": 16, "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": 17, "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": 22, "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": 35, "metadata": {}, "outputs": [], "source": [ "import json\n", "\n", "def evaluate(reply, message, history) -> Evaluation:\n", " # Add JSON format instruction to system prompt\n", " json_system_prompt = evaluator_system_prompt + \"\\n\\nIMPORTANT: Return your response as a JSON object with exactly these two fields: 'is_acceptable' (boolean) and 'feedback' (string). Example: {\\\"is_acceptable\\\": true, \\\"feedback\\\": \\\"The response is good\\\"}\"\n", " \n", " messages = [{\"role\": \"system\", \"content\": json_system_prompt}] + [{\"role\": \"user\", \"content\": evaluator_user_prompt(reply, message, history)}]\n", " \n", " response = client.chat.completions.create(\n", " model=\"llama-3.3-70b-versatile\",\n", " messages=messages\n", " )\n", " \n", " raw_content = response.choices[0].message.content\n", " \n", " try:\n", " # Try to extract JSON from the response\n", " # Look for JSON object in the response\n", " start = raw_content.find(\"{\")\n", " end = raw_content.rfind(\"}\") + 1\n", " if start != -1 and end > start:\n", " json_str = raw_content[start:end]\n", " evaluation = Evaluation.model_validate_json(json_str)\n", " else:\n", " # Fallback if no JSON found\n", " evaluation = Evaluation(\n", " is_acceptable=\"acceptable\" in raw_content.lower() or \"yes\" in raw_content.lower(),\n", " feedback=raw_content\n", " )\n", " except Exception as e:\n", " # Fallback to plain text evaluation if JSON parsing fails\n", " print(f\"Error parsing JSON: {e}. Using fallback parsing.\")\n", " evaluation = Evaluation(\n", " is_acceptable=\"acceptable\" in raw_content.lower() or \"yes\" in raw_content.lower(),\n", " feedback=raw_content\n", " )\n", " \n", " return evaluation" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "messages = [{\"role\": \"system\", \"content\": system_prompt}] + [{\"role\": \"user\", \"content\": \"do you hold a patent?\"}]\n", "response = client.chat.completions.create(model=\"openai/gpt-oss-20b\", messages=messages)\n", "reply = response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'I’m not a holder of any patents at the moment. If you have a specific project or idea in mind, I’d be happy to discuss how we could potentially innovate or protect it together!'" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reply" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Evaluation(is_acceptable=True, feedback=\"The response is acceptable because it directly answers the user's question about holding a patent and provides a professional and engaging follow-up by offering to discuss potential innovation or protection of the user's project or idea. This response stays in character as Mã Dĩ Hào and aligns with the provided context.\")" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "evaluate(reply, \"do you hold a patent?\", messages[:1])" ] }, { "cell_type": "code", "execution_count": 38, "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 = client.chat.completions.create(model=\"openai/gpt-oss-20b\", messages=messages)\n", " return response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "def chat(message, history):\n", " history = [{\"role\": h[\"role\"], \"content\": h[\"content\"]} for h in 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 = client.chat.completions.create(model=\"openai/gpt-oss-20b\", 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:7865\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": 44, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "Passed evaluation - returning reply\n", "Failed evaluation - retrying\n", "The response seems to be a jumbled collection of words and does not form a coherent or professional answer to the user's question. A more suitable response would be to clearly state whether Mã Dĩ Hào holds any patents or not, and if not, to express a willingness to discuss other aspects of their work or expertise.\n" ] } ], "source": [ "gr.ChatInterface(chat, type=\"messages\").launch()" ] }, { "cell_type": "markdown", "metadata": {}, "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 }