{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "6fea6a2d", "metadata": {}, "outputs": [], "source": [ "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": 2, "id": "6da108f6", "metadata": {}, "outputs": [], "source": [ "# The usual start\n", "\n", "load_dotenv(override=True)\n", "openai = OpenAI()" ] }, { "cell_type": "code", "execution_count": 3, "id": "f8413e24", "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": "markdown", "id": "a62f99a3", "metadata": {}, "source": [ "### THis is how you push a message using PushOver" ] }, { "cell_type": "code", "execution_count": 4, "id": "c82f6a4e", "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": 5, "id": "7f3d1952", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Push : Everyday is a new fresh start, make it count\n" ] } ], "source": [ "push(\"Everyday is a new fresh start, make it count\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "e087b2f1", "metadata": {}, "outputs": [], "source": [ "# This is generally used to record user details if user wants to get in touch.\n", "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": 7, "id": "036d3d3f", "metadata": {}, "outputs": [], "source": [ "# this is when user ask something out of the box question, then it will send you a notification.\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": 8, "id": "685c4f7e", "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": 9, "id": "c6b45cc5", "metadata": {}, "outputs": [], "source": [ "tools = [{\"type\": \"function\", \"function\": record_user_details_json},\n", " {\"type\": \"function\", \"function\": record_unknown_question_json}]" ] }, { "cell_type": "code", "execution_count": 10, "id": "7483b361", "metadata": {}, "outputs": [], "source": [ "def handle_tools_calls(tool_calls):\n", " results = []\n", "\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", " 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": 11, "id": "511c6475", "metadata": {}, "outputs": [], "source": [ "reader = PdfReader(\"me/VVJ.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 = \"Venkata Vikranth Jannatha\"" ] }, { "cell_type": "code", "execution_count": 12, "id": "bcb7a7ad", "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}.\"\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "e175f369", "metadata": {}, "outputs": [], "source": [ "def chat(message, history):\n", " # Convert Gradio history (list of (user, assistant) tuples) into OpenAI chat format\n", " messages = [{\"role\": \"system\", \"content\": system_prompt}]\n", " for user_msg, assistant_msg in history:\n", " if user_msg:\n", " messages.append({\"role\": \"user\", \"content\": user_msg})\n", " if assistant_msg:\n", " messages.append({\"role\": \"assistant\", \"content\": assistant_msg})\n", "\n", " messages.append({\"role\": \"user\", \"content\": message})\n", " done = False\n", "\n", " while not done:\n", " response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages, tools=tools)\n", " finish_reason = response.choices[0].finish_reason\n", " print(finish_reason)\n", "\n", " if finish_reason == \"tool_calls\":\n", " message_obj = response.choices[0].message\n", " tool_calls = message_obj.tool_calls\n", " results = handle_tools_calls(tool_calls)\n", " messages.append(message_obj)\n", " messages.extend(results)\n", " else:\n", " done = True\n", " return response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": null, "id": "01e8c54b", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Personal\\Work\\agents\\.venv\\Lib\\site-packages\\gradio\\chat_interface.py:347: UserWarning: The 'tuples' format for chatbot messages is deprecated and will be removed in a future version of Gradio. Please set type='messages' instead, which uses openai-style 'role' and 'content' keys.\n", " self.chatbot = Chatbot(\n" ] }, { "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": 14, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "stop\n", "stop\n", "stop\n", "stop\n" ] } ], "source": [ "gr.ChatInterface(chat).launch()" ] }, { "cell_type": "code", "execution_count": null, "id": "5a6b4926", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "agents (3.12.5)", "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.5" } }, "nbformat": 4, "nbformat_minor": 5 }