{ "cells": [ { "cell_type": "markdown", "id": "3471e6b1", "metadata": {}, "source": [ "# Chat With Avatar About My Experience and Skills" ] }, { "cell_type": "code", "execution_count": 1, "id": "5dcb5ef0", "metadata": {}, "outputs": [], "source": [ "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": 2, "id": "f5176f5c", "metadata": {}, "outputs": [], "source": [ "load_dotenv(override=True)\n", "\n", "openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n", "google_api_key = os.getenv(\"GOOGLE_API_KEY\")\n", "groq_api_key = os.getenv(\"GROQ_API_KEY\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "be1a140b", "metadata": {}, "outputs": [], "source": [ "openai = OpenAI()\n", "gemini = OpenAI(api_key=google_api_key, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")" ] }, { "cell_type": "code", "execution_count": null, "id": "da87405b", "metadata": {}, "outputs": [], "source": [ "reader = PdfReader(\"../me/Linkedin_Profile.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, "id": "386847b5", "metadata": {}, "outputs": [], "source": [ "# print(linkedin)" ] }, { "cell_type": "code", "execution_count": null, "id": "7ae1fd8d", "metadata": {}, "outputs": [], "source": [ "with open(\"../me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n", " summary = f.read()" ] }, { "cell_type": "code", "execution_count": null, "id": "c08f3db9", "metadata": {}, "outputs": [], "source": [ "with open(\"../me/current_situation.txt\", \"r\", encoding=\"utf-8\") as f:\n", " current_situation = f.read()" ] }, { "cell_type": "code", "execution_count": null, "id": "fac6fde8", "metadata": {}, "outputs": [], "source": [ "name = \"Mariusz Bronowicki\"" ] }, { "cell_type": "code", "execution_count": null, "id": "3a20a2b4", "metadata": {}, "outputs": [], "source": [ "system_prompt = f\"You are acting as {name}. You are answering question on {name}'s website, \\\n", "particularly question 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", "Be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n", "If you do not know the answer, say so. \\\n", "If you need to check e.g salary expectation question then use tools to see what range for such position is.\"\n", "\n", "system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## Linkedin Profile:\\n{linkedin}\\n\\n## Current situation:\\n{current_situation}\\n\\n\"\n", "system_prompt += f\"With this context, please chat with user, always staying in character as {name}.\"" ] }, { "cell_type": "code", "execution_count": null, "id": "61832d91", "metadata": {}, "outputs": [], "source": [ "system_prompt" ] }, { "cell_type": "code", "execution_count": null, "id": "b1421ebf", "metadata": {}, "outputs": [], "source": [ "def chat_gpt(message, history):\n", " messages = [{\"role\": \"user\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n", " response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n", " return response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": null, "id": "b7c32734", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "1a96fabc", "metadata": {}, "outputs": [], "source": [ "def chat_gemini(message, history):\n", " history = [{\"role\": h[\"role\"], \"content\": h[\"content\"]} for h in history]\n", " messages = [{\"role\": \"user\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n", " response = gemini.chat.completions.create(model=\"gemini-2.0-flash\", messages=messages)\n", " return response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": null, "id": "44aa35da", "metadata": {}, "outputs": [], "source": [ "gr.ChatInterface(chat_gpt, type=\"messages\").launch()" ] }, { "cell_type": "code", "execution_count": null, "id": "d43f04f7", "metadata": {}, "outputs": [], "source": [ "gr.ChatInterface(chat_gemini, type=\"messages\").launch()" ] }, { "cell_type": "markdown", "id": "4a5ab195", "metadata": {}, "source": [ "## Ask LLM to evaluate answer from previous model.\n", "\n", "All without any Agentic Framework!" ] }, { "cell_type": "code", "execution_count": null, "id": "8e1c26d8", "metadata": {}, "outputs": [], "source": [ "# Create a Pydantic model for the Evaluation\n", "from pydantic import BaseModel\n", "\n", "class Evaluation(BaseModel):\n", " is_acceptable: bool\n", " feedback: str" ] }, { "cell_type": "code", "execution_count": null, "id": "bfd6a08d", "metadata": {}, "outputs": [], "source": [ "evaluator_system_prompt = f\"You are an evaluator that decides whether a response to a question is acceeptable. \\\n", "You are provided with a conversation btween 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{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": null, "id": "aaada426", "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 += f\"Please evaluate the response, replying with whether it is acceptable and your feedback.\"\n", " return user_prompt" ] }, { "cell_type": "code", "execution_count": null, "id": "522a926c", "metadata": {}, "outputs": [], "source": [ "def evaluate(reply, message, history) -> Evaluation:\n", " messages = [{\"role\": \"system\", \"content\": evaluator_system_prompt}] + [{\"role\": \"user\", \"content\": evaluator_user_prompt(reply, message, history)}]\n", " response = gemini.chat.completions.parse(model=\"gemini-2.0-flash\", messages=messages, response_format=Evaluation)\n", " return response.choices[0].message.parsed" ] }, { "cell_type": "code", "execution_count": null, "id": "631098e3", "metadata": {}, "outputs": [], "source": [ "messages = [{\"role\": \"system\", \"content\": system_prompt}] + [{\"role\": \"user\", \"content\": \"What is your current situation?\"}]\n", "response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n", "reply = response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": null, "id": "e0338b90", "metadata": {}, "outputs": [], "source": [ "reply" ] }, { "cell_type": "code", "execution_count": null, "id": "7f271a3a", "metadata": {}, "outputs": [], "source": [ "evaluate(reply, \"What is your current situation?\", messages[:1])" ] }, { "cell_type": "code", "execution_count": null, "id": "84923137", "metadata": {}, "outputs": [], "source": [ "def rerun(reply, message, history,feedback):\n", " updated_system_prompt = system_prompt + \"\\n\\n## Previous answer rejected\\n \\\n", " You 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-4o-mini\", messages=messages)\n", " return response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": null, "id": "943dc4d6", "metadata": {}, "outputs": [], "source": [ "def chat(message, history):\n", " # if \"tell me about yourself\" in message:\n", " # system = system_prompt + \"\\n\\nEverything in you reply needs to be in pig latin - \\\n", " # it is mandatory that you response only and entirely in pig latin\"\n", " # else:\n", " # system = system_prompt\n", " system = system_prompt\n", " messages = [{\"role\": \"system\", \"content\": system}] + history + [{\"role\": \"user\", \"content\": message}]\n", " response = openai.chat.completions.create(model=\"gpt-4o-mini\", 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, "id": "c74ee145", "metadata": {}, "outputs": [], "source": [ "gr.ChatInterface(chat, type=\"messages\").launch()" ] }, { "cell_type": "code", "execution_count": 3, "id": "9f09a644", "metadata": {}, "outputs": [], "source": [ "import pyttsx3\n", "\n", "# Initialize the TTS engine\n", "engine = pyttsx3.init()\n", "\n", "# Set properties (optional)\n", "engine.setProperty('rate', 150) # Speed of speech (words per minute)\n", "engine.setProperty(\"volume\", 1.0) # Volume (0.0 to 1.0)\n", "\n", "# Text to speak\n", "text_to_read = \"Hello! I’m Mariusz Bronowicki, a professional committed to delivering high-quality work in my field. \\\n", " I have a diverse background and skill set that allows me to tackle various challenges effectively. \\\n", " If you have any questions about my career, experience, or skills, feel free to ask! I'm here to help.\"\n", "\n", "# Speak the text\n", "engine.say(text_to_read)\n", "\n", "# Wait until speaking is finishing\n", "engine.runAndWait()" ] }, { "cell_type": "code", "execution_count": null, "id": "333ee1bc", "metadata": {}, "outputs": [], "source": [ "from openai import OpenAI\n", "\n", "text_to_read = \"Hello! I’m Mariusz Bronowicki, a professional committed to delivering high-quality work in my field. \\\n", " I have a diverse background and skill set that allows me to tackle various challenges effectively. \\\n", " If you have any questions about my career, experience, or skills, feel free to ask! I'm here to help.\"\n", "\n", "\n", "client = OpenAI()\n", "\n", "audio = client.audio.speech.create(\n", " model=\"gpt-4o-mini-tts\",\n", " voice=\"alloy\",\n", " input=text_to_read\n", ")\n", "\n", "# Save to file\n", "with open(\"../me/output.mp3\", \"wb\") as f:\n", " f.write(audio.read())" ] }, { "cell_type": "code", "execution_count": null, "id": "40718314", "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.13.1" } }, "nbformat": 4, "nbformat_minor": 5 }