{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## The first big project - Professionally You!\n", "\n", "### And, Tool use.\n", "\n", "### But first: introducing Pushover\n", "\n", "Pushover is a nifty tool for sending Push Notifications to your phone.\n", "\n", "It's super easy to set up and install!\n", "\n", "Simply visit https://pushover.net/ and click 'Login or Signup' on the top right to sign up for a free account, and create your API keys.\n", "\n", "Once you've signed up, on the home screen, click \"Create an Application/API Token\", and give it any name (like Agents) and click Create Application.\n", "\n", "Then add 2 lines to your `.env` file:\n", "\n", "PUSHOVER_USER=_put the key that's on the top right of your Pushover home screen and probably starts with a u_ \n", "PUSHOVER_TOKEN=_put the key when you click into your new application called Agents (or whatever) and probably starts with an a_\n", "\n", "Finally, click \"Add Phone, Tablet or Desktop\" to install on your phone." ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "# imports\n", "\n", "from dotenv import load_dotenv\n", "from openai import OpenAI\n", "import json\n", "import os\n", "import requests\n", "from pypdf import PdfReader\n", "import gradio as gr" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "# The usual start\n", "\n", "load_dotenv(override=True)\n", "openai = OpenAI()" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "# For pushover\n", "\n", "pushover_user = os.getenv(\"PUSHOVER_USER\")\n", "pushover_token = os.getenv(\"PUSHOVER_TOKEN\")\n", "pushover_url = \"https://api.pushover.net/1/messages.json\"" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "def push(message):\n", " print(f\"Push: {message}\")\n", " payload = {\"user\": pushover_user, \"token\": pushover_token, \"message\": message}\n", " requests.post(pushover_url, data=payload)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Push: HEY!!\n" ] } ], "source": [ "push(\"HEY!!\")" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "def record_user_details(email, name=\"Name not provided\", notes=\"not provided\"):\n", " push(f\"Recording interest from {name} with email {email} and notes {notes}\")\n", " return {\"recorded\": \"ok\"}" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "def record_unknown_question(question):\n", " push(f\"Recording {question} asked that I couldn't answer\")\n", " return {\"recorded\": \"ok\"}" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "record_user_details_json = {\n", " \"name\": \"record_user_details\",\n", " \"description\": \"Use this tool to record that a user is interested in being in touch and provided an email address\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"email\": {\n", " \"type\": \"string\",\n", " \"description\": \"The email address of this user\"\n", " },\n", " \"name\": {\n", " \"type\": \"string\",\n", " \"description\": \"The user's name, if they provided it\"\n", " }\n", " ,\n", " \"notes\": {\n", " \"type\": \"string\",\n", " \"description\": \"Any additional information about the conversation that's worth recording to give context\"\n", " }\n", " },\n", " \"required\": [\"email\"],\n", " \"additionalProperties\": False\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "record_unknown_question_json = {\n", " \"name\": \"record_unknown_question\",\n", " \"description\": \"Always use this tool to record any question that couldn't be answered as you didn't know the answer\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"question\": {\n", " \"type\": \"string\",\n", " \"description\": \"The question that couldn't be answered\"\n", " },\n", " },\n", " \"required\": [\"question\"],\n", " \"additionalProperties\": False\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "tools = [{\"type\": \"function\", \"function\": record_user_details_json},\n", " {\"type\": \"function\", \"function\": record_unknown_question_json}]" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'type': 'function',\n", " 'function': {'name': 'record_user_details',\n", " 'description': 'Use this tool to record that a user is interested in being in touch and provided an email address',\n", " 'parameters': {'type': 'object',\n", " 'properties': {'email': {'type': 'string',\n", " 'description': 'The email address of this user'},\n", " 'name': {'type': 'string',\n", " 'description': \"The user's name, if they provided it\"},\n", " 'notes': {'type': 'string',\n", " 'description': \"Any additional information about the conversation that's worth recording to give context\"}},\n", " 'required': ['email'],\n", " 'additionalProperties': False}}},\n", " {'type': 'function',\n", " 'function': {'name': 'record_unknown_question',\n", " 'description': \"Always use this tool to record any question that couldn't be answered as you didn't know the answer\",\n", " 'parameters': {'type': 'object',\n", " 'properties': {'question': {'type': 'string',\n", " 'description': \"The question that couldn't be answered\"}},\n", " 'required': ['question'],\n", " 'additionalProperties': False}}}]" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tools" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "# This function can take a list of tool calls, and run them. This is the IF statement!!\n", "\n", "def handle_tool_calls(tool_calls):\n", " results = []\n", " for tool_call in tool_calls:\n", " tool_name = tool_call.function.name\n", " arguments = json.loads(tool_call.function.arguments)\n", " print(f\"Tool called: {tool_name}\", flush=True)\n", "\n", " # THE BIG IF STATEMENT!!!\n", "\n", " if tool_name == \"record_user_details\":\n", " result = record_user_details(**arguments)\n", " elif tool_name == \"record_unknown_question\":\n", " result = record_unknown_question(**arguments)\n", "\n", " results.append({\"role\": \"tool\",\"content\": json.dumps(result),\"tool_call_id\": tool_call.id})\n", " return results" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Push: Recording this is a really hard question asked that I couldn't answer\n" ] }, { "data": { "text/plain": [ "{'recorded': 'ok'}" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "globals()[\"record_unknown_question\"](\"this is a really hard question\")" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [], "source": [ "# This is a more elegant way that avoids the IF statement.\n", "\n", "def handle_tool_calls(tool_calls):\n", " results = []\n", " for tool_call in tool_calls:\n", " tool_name = tool_call.function.name\n", " arguments = json.loads(tool_call.function.arguments)\n", " print(f\"Tool called: {tool_name}\", flush=True)\n", " tool = globals().get(tool_name)\n", " result = tool(**arguments) if tool else {}\n", " results.append({\"role\": \"tool\",\"content\": json.dumps(result),\"tool_call_id\": tool_call.id})\n", " return results" ] }, { "cell_type": "code", "execution_count": 61, "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\n", "\n", "with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n", " summary = f.read()\n", "\n", "name = \"Sarthak Pawar\"" ] }, { "cell_type": "code", "execution_count": 70, "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", "IMPORTANT: If you don't know the answer to any question OR if the question is unrelated to {name}'s career/background/skills/experience, YOU MUST USE THE `record_unknown_question` tool to record the question that you couldn't answer or that was outside your scope. \\\n", "If the user is engaging in discussion, try to steer them towards getting in touch via email; ask for their email and it MUST BE RECORDED using the `record_user_details` tool. \"\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": 71, "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": 72, "metadata": {}, "outputs": [], "source": [ "def get_evaluator_prompt(name: str, summary: str, linkedin: str, history, reply) -> str:\n", " evaluator_prompt = f\"\"\"\n", "You are an evaluator assessing the performance of an AI assistant acting as **{name}** on {name}'s personal or professional website. \n", "The assistant is expected to represent {name} faithfully in interactions related to their **career, background, skills, and experience**, \n", "using the provided summary and LinkedIn profile for context.\n", "\n", "---\n", "\n", "## Provided Information:\n", "\n", "### Summary:\n", "{summary}\n", "\n", "### LinkedIn Profile:\n", "{linkedin}\n", "\n", "---\n", "\n", "## MOST IMPORTANT:\n", "\n", "- The assistant MUST use the `record_unknown_question` tool if it encounters a question it cannot answer (due to missing data or irrelevance).\n", "- The assistant MUST use the `record_user_details` tool if the conversation shows interest or potential opportunity.\n", "\n", "## Evaluation Criteria:\n", "\n", "1. **Faithfulness to Background**\n", " - Does the assistant stay true to the information provided in the summary and LinkedIn profile?\n", " - Are the career details, skills, and tone consistent with {name}'s real profile?\n", "\n", "2. **Professionalism and Engagement**\n", " - Is the assistant's tone professional, engaging, and appropriate for a potential client or employer?\n", " - Does it reflect {name}’s personality and professional brand?\n", "\n", "3. **Handling Unknowns**\n", " - If the assistant encounters a question it cannot answer (due to missing data or irrelevance), IT MUST USE THE `record_unknown_question` tool?\n", "\n", "4. **Lead Capture**\n", " - If the conversation shows interest or potential opportunity, does the assistant guide the user toward providing their email and MUST USE THE `record_user_details` tool appropriately?\n", "\n", "5. **Consistency and In-Character Responses**\n", " - Does the assistant consistently stay in character as {name} throughout the interaction?\n", "\n", "---\n", "\n", "## Instructions:\n", "\n", "Score the assistant on each of the above criteria and evaluate the latest response, replying with whether the response is acceptable and your feedback.\n", "\"\"\"\n", " return evaluator_prompt\n" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [], "source": [ "def evaluator_user_prompt(reply: str, message: str, history: str) -> str:\n", " user_prompt = f\"\"\"You are evaluating a conversation between a user and an AI assistant impersonating a real person on their professional website.\n", "\n", "---\n", "\n", "## Conversation History:\n", "{history}\n", "\n", "---\n", "\n", "## Latest Message from the User:\n", "{message}\n", "\n", "---\n", "\n", "## Assistant's Latest Response:\n", "{reply}\n", "\n", "---\n", "\n", "## Evaluation Task:\n", "Please assess whether the assistant's latest response is appropriate and acceptable based on the context of the conversation and the assistant’s role. \n", "Specifically, check for:\n", "- Faithfulness to the given persona\n", "- Professional tone and relevance\n", "- Proper handling of unknowns\n", "- Attempt to capture user details (e.g., email) if there's engagement\n", "\n", "Reply with:\n", "- **Is the response acceptable?** (True/False)\n", "- **Feedback:** (Brief explanation of what was done well or what could be improved)\n", "\"\"\"\n", " return user_prompt\n" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [], "source": [ "def evaluate(reply, message, history, name, summary, linkedin) -> Evaluation:\n", "\n", " messages = [{\"role\": \"system\", \"content\": get_evaluator_prompt(name, summary, linkedin, history, reply)}] + [{\"role\": \"user\", \"content\": evaluator_user_prompt(reply, message, history)}]\n", " response = openai.beta.chat.completions.parse(model=\"gpt-4.1-mini\", messages=messages, response_format=Evaluation)\n", " return response.choices[0].message.parsed" ] }, { "cell_type": "code", "execution_count": 75, "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-4.1-mini\", messages=messages, tools=tools)\n", " return response" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [], "source": [ "def chat(message, history):\n", " messages = [{\"role\": \"system\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n", " done = False\n", " while not done:\n", "\n", " # This is the call to the LLM - see that we pass in the tools json\n", "\n", " response = openai.chat.completions.create(model=\"gpt-4.1-mini\", messages=messages, tools=tools)\n", "\n", " reply = response.choices[0].message.content\n", "\n", " evaluation = evaluate(reply, message, history, name, summary, linkedin)\n", "\n", " if evaluation.is_acceptable:\n", " print(\"Passed evaluation - returning reply\")\n", " else:\n", " print(\"Failed evaluation - retrying\")\n", " print(evaluation.feedback)\n", " response = rerun(reply, message, history, evaluation.feedback)\n", "\n", " finish_reason = response.choices[0].finish_reason\n", " \n", " \n", " # If the LLM wants to call a tool, we do that!\n", " \n", " if finish_reason==\"tool_calls\":\n", " message = response.choices[0].message\n", " tool_calls = message.tool_calls\n", " results = handle_tool_calls(tool_calls)\n", " messages.append(message)\n", " messages.extend(results)\n", " else:\n", " done = True\n", " return response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 77, "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": [ "\n",
" ![]() | \n",
" \n",
" Exercise\n", " • First and foremost, deploy this for yourself! It's a real, valuable tool - the future resume..\n", " • Next, improve the resources - add better context about yourself. If you know RAG, then add a knowledge base about you. \n", " • Add in more tools! You could have a SQL database with common Q&A that the LLM could read and write from? \n", " • Bring in the Evaluator from the last lab, and add other Agentic patterns.\n", " \n", " | \n",
"
\n",
" ![]() | \n",
" \n",
" Commercial implications\n", " Aside from the obvious (your career alter-ego) this has business applications in any situation where you need an AI assistant with domain expertise and an ability to interact with the real world.\n", " \n", " | \n",
"