{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "95b5ffec", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: pypdf in /Users/robinchriqui/project/LLM_projects/venv/lib/python3.12/site-packages (6.8.0)\n", "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m26.0.1\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n" ] } ], "source": [ "!pip install pypdf" ] }, { "cell_type": "code", "execution_count": null, "id": "19c07744", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: requests in /Users/robinchriqui/project/LLM_projects/venv/lib/python3.12/site-packages (2.32.5)\n", "Requirement already satisfied: charset_normalizer<4,>=2 in /Users/robinchriqui/project/LLM_projects/venv/lib/python3.12/site-packages (from requests) (3.4.6)\n", "Requirement already satisfied: idna<4,>=2.5 in /Users/robinchriqui/project/LLM_projects/venv/lib/python3.12/site-packages (from requests) (3.11)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/robinchriqui/project/LLM_projects/venv/lib/python3.12/site-packages (from requests) (2.6.3)\n", "Requirement already satisfied: certifi>=2017.4.17 in /Users/robinchriqui/project/LLM_projects/venv/lib/python3.12/site-packages (from requests) (2026.1.4)\n", "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m26.0.1\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n" ] } ], "source": [ "!pip install requests" ] }, { "cell_type": "code", "execution_count": null, "id": "5e073e58", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/robinchriqui/project/LLM_projects/venv/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "from dotenv import load_dotenv\n", "from openai import OpenAI\n", "from pypdf import PdfReader\n", "import gradio as gr\n", "import os\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": null, "id": "fcaf080c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "load_dotenv(override=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "f7a99d8f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pushover user found and starts with u\n", "Pushover token found and starts with a\n" ] } ], "source": [ "pushover_user = os.getenv(\"PUSHOVER_USER\")\n", "pushover_token = os.getenv(\"PUSHOVER_TOKEN\")\n", "pushover_url = \"https://api.pushover.net/1/messages.json\"\n", "\n", "if pushover_user:\n", " print(f\"Pushover user found and starts with {pushover_user[0]}\")\n", "else:\n", " print(\"Pushover user not found\")\n", "\n", "if pushover_token:\n", " print(f\"Pushover token found and starts with {pushover_token[0]}\")\n", "else:\n", " print(\"Pushover token not found\")" ] }, { "cell_type": "code", "execution_count": null, "id": "c984de99", "metadata": {}, "outputs": [], "source": [ "client = OpenAI(\n", " api_key=os.getenv(\"OPENROUTER_API_KEY\"),\n", " base_url=\"https://openrouter.ai/api/v1\",\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "fc8be1cc", "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": null, "id": "b2e842c4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Push: Comment ça va mec?\n" ] } ], "source": [ "push(\"Comment ça va mec?\")" ] }, { "cell_type": "code", "execution_count": null, "id": "73eab2a4", "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\"}\n", "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": null, "id": "b87c85d0", "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", "}\n", "\n", "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": null, "id": "4f5beb00", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'type': 'function', 'function': {'name': 'record_user_details', 'description': 'Use this tool to record that a user is interested in being in touch and provided an email address', 'parameters': {'type': 'object', 'properties': {'email': {'type': 'string', 'description': 'The email address of this user'}, 'name': {'type': 'string', 'description': \"The user's name, if they provided it\"}, 'notes': {'type': 'string', 'description': \"Any additional information about the conversation that's worth recording to give context\"}}, 'required': ['email'], 'additionalProperties': False}}}, {'type': 'function', 'function': {'name': 'record_unknown_question', 'description': \"Always use this tool to record any question that couldn't be answered as you didn't know the answer\", 'parameters': {'type': 'object', 'properties': {'question': {'type': 'string', 'description': \"The question that couldn't be answered\"}}, 'required': ['question'], 'additionalProperties': False}}}]\n" ] } ], "source": [ "tools = [{\"type\": \"function\", \"function\": record_user_details_json},\n", " {\"type\": \"function\", \"function\": record_unknown_question_json}]\n", "print(tools)" ] }, { "cell_type": "code", "execution_count": null, "id": "6a60d09f", "metadata": {}, "outputs": [], "source": [ "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": null, "id": "458ae6ca", "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": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "globals()[\"record_unknown_question\"](\"this is a really hard question\")" ] }, { "cell_type": "code", "execution_count": null, "id": "b27089d3", "metadata": {}, "outputs": [], "source": [ "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": null, "id": "17eae5d4", "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 = \"Robin Chriqui\"" ] }, { "cell_type": "code", "execution_count": null, "id": "fce937c0", "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 to any question, use your record_unknown_question tool to record the question that you couldn't answer, even if it's about something trivial or unrelated to career. \\\n", "If the user is engaging in discussion, try to steer them towards getting in touch via email; ask for their email and record it using your record_user_details tool. \"\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}.\"" ] }, { "cell_type": "code", "execution_count": null, "id": "13f2a56b", "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 = client.chat.completions.create(model=\"openai/gpt-4o-mini\", messages=messages, tools=tools)\n", "\n", " finish_reason = response.choices[0].finish_reason\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": null, "id": "0c7fd634", "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": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gr.ChatInterface(chat).launch()" ] } ], "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.7" } }, "nbformat": 4, "nbformat_minor": 5 }