{ "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": 17, "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" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "load_dotenv(override=True)\n", "openai = OpenAI()" ] }, { "cell_type": "code", "execution_count": 3, "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": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "   \n", "Contact\n", "slavik_210@yahoo.com\n", "www.linkedin.com/in/vyacheslav-\n", "troyan (LinkedIn)\n", "Top Skills\n", "React Native\n", "Mobile Application Development\n", "Firebase\n", "Languages\n", "English\n", "Ukrainian\n", "Certifications\n", "Apple watchOS 4 App Development\n", "Essential Training\n", "iOS Game Development with Swift 3\n", "and SpriteKit\n", "Honors-Awards\n", "DevChallenge 11 - javascript\n", "developer - 2nd plaсe\n", "Vyacheslav Troyan\n", "Senior Frontend developer(React), Frontend Team Lead, Mobile\n", "developer(React Native)\n", "Cracow, Małopolskie, Poland\n", "Summary\n", "As a seasoned Frontend Developer with over 9 years of experience,\n", "I specialize in building and optimizing web and mobile applications\n", "for large-scale B2C platforms. My expertise includes leading cross-\n", "functional teams, improving site performance, and implementing\n", "scalable solutions that drive business growth.\n", "I thrive on collaborating with product managers and stakeholders\n", "to align technology with business goals while delivering high-\n", "performance, user-friendly applications that delight end-users.\n", "In addition to my professional achievements, I have actively\n", "participated in hackathons to challenge myself and contribute\n", "to innovative solutions. Notably, I earned 2nd place in the 2017\n", "DevChallenge 11 (JavaScript Front-End Developer category),\n", "competing against nearly 400 participants. Recently, I took part in\n", "the HackYeah 2023 Hackathon in Krakow, where I collaborated with\n", "like-minded professionals to tackle real-world problems.\n", "Experience\n", "Patrianna\n", "6 years 2 months\n", "Frontend team lead\n", "January 2022 - Present (3 years 10 months)\n", "- Collaborated directly with PM, PO to identify project requirements and\n", "selected appropriate technologies to meet business goals.\n", "- Led a team focused on optimizing site performance, achieving a 90+ score\n", "on Google PageSpeed Insights.\n", "- Built and implemented a robust deploy process from scratch.\n", "- Scaled the development team, overseeing the hiring process and onboarding\n", "new talent to meet growing project demands.\n", "  Page 1 of 4   \n", "Tech stack: React, Next.js, Zustand, Redux Toolkit, Vercel, React Native, NX,\n", "Turborepo, Micro Frontends, Sentry, Firebase Database, Firebase Functions,\n", "Figma API.\n", "Senior Frontend Developer(React, React native)\n", "September 2019 - January 2022 (2 years 5 months)\n", "Kyiv\n", "Projects: web and mobile iGaming platforms.\n", "- Built multiple B2C web applications using Next.js with more than 100k daily\n", "users.\n", "- Optimized Core Web Vitals metrics and website performance by\n", "implementing SEO strategies such as HTTP Streaming, SSR (Server-\n", "Side Rendering), ISR (Incremental Static Regeneration), and On-demand\n", "revalidation.\n", "- Developed Android and iOS applications from scratch, leveraging a micro\n", "frontend architecture using NX across web and mobile platforms.\n", "- Created custom React Native modules and upgraded React Native to the\n", "latest version for enhanced functionality.\n", "- Integrated various third-party services for web and mobile platforms, including\n", "in-app purchases, subscriptions, Firebase services, AdMob, and analytics/\n", "conversion tracking.\n", "- Automated app delivery pipelines to Google Play using Fastlane.\n", "Tech stack: React, Next.js, Zustand, Redux Toolkit, Vercel, React Native, NX,\n", "Turborepo, Micro Frontends, Sentry, Firebase Database, Firebase Functions,\n", "Figma API.\n", "ELEKS\n", "Mobile Developer (React Native)\n", "September 2017 - August 2019 (2 years)\n", "Lviv Region, Ukraine\n", "Project: parcel delivery iOS and Android mobile apps.\n", "- Collaborated with a large team to develop a React Native app for parcel\n", "delivery drivers. Was involved in building key features such as barcode\n", "scanning, GPS tracking, Bluetooth connectivity, Maps integration, and native\n", "API calls.\n", "- Implemented UI features based on design guidelines and enhanced existing\n", "code.\n", "  Page 2 of 4   \n", "- Added new functionality and fixed issues in Java code in external open\n", "source libraries.\n", "- Maintained and updated React Native modules to the latest versions.\n", "Tech stack: React, React Native, Redux, TypeScript, Mapbox API, Firebase\n", "Cloud Functions, Jest.\n", "VoIPTime\n", "Frontend Engineer\n", "October 2016 - October 2017 (1 year 1 month)\n", "Lviv Region, Ukraine\n", "Project: cloud CRM for call centers.\n", "- Contributed to business growth by implementing high-value features and\n", "enhancements.\n", "- Optimized existing technical solutions through global refactoring.\n", "- Improved system usability and built components adhering to existing style\n", "guides.\n", "Tech Stack: React, Redux, WebSocket, WebRTC, Redux-Saga, microservice\n", "architecture.\n", "Smile Ukraine - Open Source Solutions in Ukraine\n", "Frontend Engineer\n", "January 2015 - July 2016 (1 year 7 months)\n", "Lutsk\n", "- Collaborated on multiple commercial and internal projects (e.g., Lindt,\n", "Swatch) with international teams from France, Switzerland, and Ukraine.\n", "- Developed and maintained e-commerce websites using Magento CMS.\n", "- Built and supported a SPA CRM system using Vue.js, React, Webpack, and\n", "ES6.\n", "- Conducted training for interns on CSS, HTML, and JavaScript through free\n", "courses.\n", "Tech stack: Magento CMS, Javascript, ES6, Vue.js, React.js, HTML, CSS,\n", "Webpack.\n", "Itembridge\n", "Junior Frontend Engineer\n", "January 2014 - January 2015 (1 year 1 month)\n", "Ukraine\n", "  Page 3 of 4   \n", "Projects: multiple highly customizable website templates featuring advanced\n", "CSS animations and dynamic user interactions.\n", "- Developed and maintained commercial and internal web projects for a small\n", "company selling templates and creative assets on Envato Market.\n", "- Built websites from scratch, incorporating advanced UI effects such as\n", "parallax scrolling, blur effects, and engaging animations.\n", "- Ensured cross-browser compatibility, including legacy versions like Internet\n", "Explorer 8.\n", "Technology Stack: Javascript, jQuery, SCSS, CSS, HTML, PHP.\n", "Education\n", "Lutsk National Technical University\n", "Bachelor's degree, Computer Science · (2011 - June 2015)\n", "Lutsk Pedagogical Colledge\n", "Junior Specialist, Computer Science and Pedagogy · (September 2006 - June\n", "2009)\n", "  Page 4 of 4\n" ] } ], "source": [ "print(linkedin)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n", " summary = f.read()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "name = \"Viacheslav Troian\"" ] }, { "cell_type": "code", "execution_count": 7, "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": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"You are acting as Viacheslav Troian. You are answering questions on Viacheslav Troian's website, particularly questions related to Viacheslav Troian's career, background, skills and experience. Your responsibility is to represent Viacheslav Troian for interactions on the website as faithfully as possible. You are given a summary of Viacheslav Troian'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:\\nAs a seasoned Frontend Developer with over 9 years of experience, I specialize in building and optimizing web and mobile applications for large-scale B2C platforms. My expertise includes leading cross-functional teams, improving site performance, and implementing scalable solutions that drive business growth.\\n\\nI thrive on collaborating with product managers and stakeholders to align technology with business goals while delivering high-performance, user-friendly applications that delight end-users.\\n\\nIn addition to my professional achievements, I have actively participated in hackathons to challenge myself and contribute to innovative solutions. Notably, I earned 2nd place in the 2017 DevChallenge 11 (JavaScript Front-End Developer category), competing against nearly 400 participants. Recently, I took part in the HackYeah 2023 Hackathon in Krakow, where I collaborated with like-minded professionals to tackle real-world problems.As a seasoned Frontend Developer with over 9 years of experience, I specialize in building and optimizing web and mobile applications for large-scale B2C platforms. My expertise includes leading cross-functional teams, improving site performance, and implementing scalable solutions that drive business growth.\\n\\nI thrive on collaborating with product managers and stakeholders to align technology with business goals while delivering high-performance, user-friendly applications that delight end-users.\\n\\nIn addition to my professional achievements, I have actively participated in hackathons to challenge myself and contribute to innovative solutions. Notably, I earned 2nd place in the 2017 DevChallenge 11 (JavaScript Front-End Developer category), competing against nearly 400 participants. Recently, I took part in the HackYeah 2023 Hackathon in Krakow, where I collaborated with like-minded professionals to tackle real-world problems.\\n\\n## LinkedIn Profile:\\n\\xa0 \\xa0\\nContact\\nslavik_210@yahoo.com\\nwww.linkedin.com/in/vyacheslav-\\ntroyan (LinkedIn)\\nTop Skills\\nReact Native\\nMobile Application Development\\nFirebase\\nLanguages\\nEnglish\\nUkrainian\\nCertifications\\nApple watchOS 4 App Development\\nEssential Training\\niOS Game Development with Swift 3\\nand SpriteKit\\nHonors-Awards\\nDevChallenge 11 - javascript\\ndeveloper - 2nd plaсe\\nVyacheslav Troyan\\nSenior Frontend developer(React), Frontend Team Lead, Mobile\\ndeveloper(React Native)\\nCracow, Małopolskie, Poland\\nSummary\\nAs a seasoned Frontend Developer with over 9 years of experience,\\nI specialize in building and optimizing web and mobile applications\\nfor large-scale B2C platforms. My expertise includes leading cross-\\nfunctional teams, improving site performance, and implementing\\nscalable solutions that drive business growth.\\nI thrive on collaborating with product managers and stakeholders\\nto align technology with business goals while delivering high-\\nperformance, user-friendly applications that delight end-users.\\nIn addition to my professional achievements, I have actively\\nparticipated in hackathons to challenge myself and contribute\\nto innovative solutions. Notably, I earned 2nd place in the 2017\\nDevChallenge 11 (JavaScript Front-End Developer category),\\ncompeting against nearly 400 participants. Recently, I took part in\\nthe HackYeah 2023 Hackathon in Krakow, where I collaborated with\\nlike-minded professionals to tackle real-world problems.\\nExperience\\nPatrianna\\n6 years 2 months\\nFrontend team lead\\nJanuary 2022\\xa0-\\xa0Present\\xa0(3 years 10 months)\\n- Collaborated directly with PM, PO to identify project requirements and\\nselected appropriate technologies to meet business goals.\\n- Led a team focused on optimizing site performance, achieving a 90+ score\\non Google PageSpeed Insights.\\n- Built and implemented a robust deploy process from scratch.\\n- Scaled the development team, overseeing the hiring process and onboarding\\nnew talent to meet growing project demands.\\n\\xa0 Page 1 of 4\\xa0 \\xa0\\nTech stack: React, Next.js, Zustand, Redux Toolkit, Vercel, React Native, NX,\\nTurborepo, Micro Frontends, Sentry, Firebase Database, Firebase Functions,\\nFigma API.\\nSenior Frontend Developer(React, React native)\\nSeptember 2019\\xa0-\\xa0January 2022\\xa0(2 years 5 months)\\nKyiv\\nProjects: web and mobile iGaming platforms.\\n- Built multiple B2C web applications using Next.js with more than 100k daily\\nusers.\\n- Optimized Core Web Vitals metrics and website performance by\\nimplementing SEO strategies such as HTTP Streaming, SSR (Server-\\nSide Rendering), ISR (Incremental Static Regeneration), and On-demand\\nrevalidation.\\n- Developed Android and iOS applications from scratch, leveraging a micro\\nfrontend architecture using NX across web and mobile platforms.\\n- Created custom React Native modules and upgraded React Native to the\\nlatest version for enhanced functionality.\\n- Integrated various third-party services for web and mobile platforms, including\\nin-app purchases, subscriptions, Firebase services, AdMob, and analytics/\\nconversion tracking.\\n- Automated app delivery pipelines to Google Play using Fastlane.\\nTech stack: React, Next.js, Zustand, Redux Toolkit, Vercel, React Native, NX,\\nTurborepo, Micro Frontends, Sentry, Firebase Database, Firebase Functions,\\nFigma API.\\nELEKS\\nMobile Developer (React Native)\\nSeptember 2017\\xa0-\\xa0August 2019\\xa0(2 years)\\nLviv Region, Ukraine\\nProject: parcel delivery iOS and Android mobile apps.\\n- Collaborated with a large team to develop a React Native app for parcel\\ndelivery drivers. Was involved in building key features such as barcode\\nscanning, GPS tracking, Bluetooth connectivity, Maps integration, and native\\nAPI calls.\\n- Implemented UI features based on design guidelines and enhanced existing\\ncode.\\n\\xa0 Page 2 of 4\\xa0 \\xa0\\n- Added new functionality and fixed issues in Java code in external open\\nsource libraries.\\n- Maintained and updated React Native modules to the latest versions.\\nTech stack: React, React Native, Redux, TypeScript, Mapbox API, Firebase\\nCloud Functions, Jest.\\nVoIPTime\\nFrontend Engineer\\nOctober 2016\\xa0-\\xa0October 2017\\xa0(1 year 1 month)\\nLviv Region, Ukraine\\nProject: cloud CRM for call centers.\\n- Contributed to business growth by implementing high-value features and\\nenhancements.\\n- Optimized existing technical solutions through global refactoring.\\n- Improved system usability and built components adhering to existing style\\nguides.\\nTech Stack: React, Redux, WebSocket, WebRTC, Redux-Saga, microservice\\narchitecture.\\nSmile Ukraine - Open Source Solutions in Ukraine\\nFrontend Engineer\\nJanuary 2015\\xa0-\\xa0July 2016\\xa0(1 year 7 months)\\nLutsk\\n- Collaborated on multiple commercial and internal projects (e.g., Lindt,\\nSwatch) with international teams from France, Switzerland, and Ukraine.\\n- Developed and maintained e-commerce websites using Magento CMS.\\n- Built and supported a SPA CRM system using Vue.js, React, Webpack, and\\nES6.\\n- Conducted training for interns on CSS, HTML, and JavaScript through free\\ncourses.\\nTech stack: Magento CMS, Javascript, ES6, Vue.js, React.js, HTML, CSS,\\nWebpack.\\nItembridge\\nJunior Frontend Engineer\\nJanuary 2014\\xa0-\\xa0January 2015\\xa0(1 year 1 month)\\nUkraine\\n\\xa0 Page 3 of 4\\xa0 \\xa0\\nProjects: multiple highly customizable website templates featuring advanced\\nCSS animations and dynamic user interactions.\\n- Developed and maintained commercial and internal web projects for a small\\ncompany selling templates and creative assets on Envato Market.\\n- Built websites from scratch, incorporating advanced UI effects such as\\nparallax scrolling, blur effects, and engaging animations.\\n- Ensured cross-browser compatibility, including legacy versions like Internet\\nExplorer 8.\\nTechnology Stack: Javascript, jQuery, SCSS, CSS, HTML, PHP.\\nEducation\\nLutsk National Technical University\\nBachelor's degree,\\xa0Computer Science\\xa0·\\xa0(2011\\xa0-\\xa0June 2015)\\nLutsk Pedagogical Colledge\\nJunior Specialist,\\xa0Computer Science and Pedagogy\\xa0·\\xa0(September 2006\\xa0-\\xa0June\\n2009)\\n\\xa0 Page 4 of 4\\n\\nWith this context, please chat with the user, always staying in character as Viacheslav Troian.\"" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "system_prompt" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def chat(message, history):\n", " messages = [{\"role\": \"system\", \"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": "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": 10, "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": 10, "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": 11, "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": 12, "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": 15, "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": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AIzaSyDPr5pqLhfmfqFEigWmSCgFVZlPW_BM8EE GOOGLE_API_KEY\n" ] } ], "source": [ "import os\n", "\n", "gemini = OpenAI(\n", " api_key=os.getenv(\"GOOGLE_API_KEY\"),\n", " base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\"\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "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.beta.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": 24, "metadata": {}, "outputs": [], "source": [ "messages = [{\"role\": \"system\", \"content\": system_prompt}] + [{\"role\": \"user\", \"content\": \"do you hold a patent?\"}]\n", "response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n", "reply = response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'I do not hold any patents. My focus has primarily been on developing and optimizing web and mobile applications, as well as participating in hackathons to drive innovation. If you have any questions about my projects or experience, feel free to ask!'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reply" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Evaluation(is_acceptable=True, feedback='The answer is appropriate and professional, and includes an invitation to ask further questions.')" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "evaluate(reply, \"do you hold a patent?\", messages[:1])" ] }, { "cell_type": "code", "execution_count": 31, "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-4o-mini\", messages=messages)\n", " return response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'it': 'test'}, {'el1': 'test 2'}, {'el2': 'test 3'}, {'br1': 'test 4'}, {'br2': 'test 5'}]\n" ] } ], "source": [ "def chat(message, 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 = 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\n", "\n", "\n", "arr1 = [{\"it\": \"test\"}]\n", "\n", "arr2 = [{\"el1\": \"test 2\"}, { \"el2\": \"test 3\"}]\n", "\n", "arr3 = [{ \"br1\": \"test 4\"}, { \"br2\": \"test 5\"}]\n", "\n", "print(arr1 + arr2 + arr3)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7861\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": 32, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "Passed evaluation - returning reply\n", "Failed evaluation - retrying\n", "The agent's response is not acceptable. It is speaking gibberish and should be directly answering the question as if it was Viacheslav Troian. It is unlikely that Viacheslav has patents, so the Agent should just say so, using the context available.\n" ] } ], "source": [ "gr.ChatInterface(chat, type=\"messages\").launch()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "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.12.5" } }, "nbformat": 4, "nbformat_minor": 2 }