|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "id": "2ce61bb5-1d5b-43b8-b5bb-6aeae91c7574", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "import os\n", |
| 11 | + "from dotenv import load_dotenv\n", |
| 12 | + "from openai import OpenAI\n", |
| 13 | + "from IPython.display import Markdown, display" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": 2, |
| 19 | + "id": "3399686d-5f14-4fb2-8939-fd2401be3007", |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "MODEL = \"gpt-4o-mini\"\n", |
| 24 | + "SYSTEM_PROMPT_PATH = \"Chat_Summary_Data/System_Prompt.txt\"\n", |
| 25 | + "CHATS_PATH = \"Chat_Summary_Data/Chat_Examples/\"" |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "code", |
| 30 | + "execution_count": 3, |
| 31 | + "id": "d97b8374-a161-435c-8317-1d0ecaaa9b71", |
| 32 | + "metadata": {}, |
| 33 | + "outputs": [ |
| 34 | + { |
| 35 | + "name": "stdout", |
| 36 | + "output_type": "stream", |
| 37 | + "text": [ |
| 38 | + "API key found and looks good so far!\n" |
| 39 | + ] |
| 40 | + } |
| 41 | + ], |
| 42 | + "source": [ |
| 43 | + "# Load environment variables in a file called .env\n", |
| 44 | + "\n", |
| 45 | + "load_dotenv(override=True)\n", |
| 46 | + "api_key = os.getenv('OPENAI_API_KEY')\n", |
| 47 | + "\n", |
| 48 | + "# Check the key\n", |
| 49 | + "\n", |
| 50 | + "if not api_key:\n", |
| 51 | + " print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n", |
| 52 | + "elif not api_key.startswith(\"sk-proj-\"):\n", |
| 53 | + " print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\")\n", |
| 54 | + "elif api_key.strip() != api_key:\n", |
| 55 | + " print(\"An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook\")\n", |
| 56 | + "else:\n", |
| 57 | + " print(\"API key found and looks good so far!\")\n" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "code", |
| 62 | + "execution_count": 4, |
| 63 | + "id": "b3f4afb4-2e4a-4971-915e-a8634a17eda8", |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [], |
| 66 | + "source": [ |
| 67 | + "class ChatAI:\n", |
| 68 | + " def __init__(self, system_prompt_path=SYSTEM_PROMPT_PATH, model=MODEL):\n", |
| 69 | + " with open(system_prompt_path, \"r\") as file:\n", |
| 70 | + " self.system_prompt = file.read()\n", |
| 71 | + "\n", |
| 72 | + " self.openai = OpenAI()\n", |
| 73 | + " self.model = model\n", |
| 74 | + " \n", |
| 75 | + " @staticmethod\n", |
| 76 | + " def _get_user_prompt(chat_txt):\n", |
| 77 | + " with open(chat_txt, \"r\") as file:\n", |
| 78 | + " user_prompt_str = file.read()\n", |
| 79 | + " return user_prompt_str\n", |
| 80 | + " \n", |
| 81 | + " def generate(self, chat_txt):\n", |
| 82 | + " messages = [\n", |
| 83 | + " {\"role\": \"system\", \"content\": self.system_prompt},\n", |
| 84 | + " {\"role\": \"user\", \"content\": self._get_user_prompt(chat_txt)}\n", |
| 85 | + " ]\n", |
| 86 | + "\n", |
| 87 | + " response = self.openai.chat.completions.create(model=self.model, messages=messages)\n", |
| 88 | + " return response.choices[0].message.content" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": 5, |
| 94 | + "id": "d243b582-66af-49f9-bcd1-e05a63e61c34", |
| 95 | + "metadata": {}, |
| 96 | + "outputs": [], |
| 97 | + "source": [ |
| 98 | + "chat_ai = ChatAI()" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": 8, |
| 104 | + "id": "c764ace6-5a0f-4dd0-9454-0b8a093b97fc", |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [ |
| 107 | + { |
| 108 | + "data": { |
| 109 | + "text/markdown": [ |
| 110 | + "# Chat1" |
| 111 | + ], |
| 112 | + "text/plain": [ |
| 113 | + "<IPython.core.display.Markdown object>" |
| 114 | + ] |
| 115 | + }, |
| 116 | + "metadata": {}, |
| 117 | + "output_type": "display_data" |
| 118 | + }, |
| 119 | + { |
| 120 | + "data": { |
| 121 | + "text/markdown": [ |
| 122 | + "- **Order:** 2 Medium Chicken BBQ Pizzas\n", |
| 123 | + "- **Cost:** 342 LE\n", |
| 124 | + "- **Experience:** Negative\n", |
| 125 | + " - **Summary:** The client expressed dissatisfaction with the pizza taste." |
| 126 | + ], |
| 127 | + "text/plain": [ |
| 128 | + "<IPython.core.display.Markdown object>" |
| 129 | + ] |
| 130 | + }, |
| 131 | + "metadata": {}, |
| 132 | + "output_type": "display_data" |
| 133 | + }, |
| 134 | + { |
| 135 | + "data": { |
| 136 | + "text/markdown": [ |
| 137 | + "# Chat2" |
| 138 | + ], |
| 139 | + "text/plain": [ |
| 140 | + "<IPython.core.display.Markdown object>" |
| 141 | + ] |
| 142 | + }, |
| 143 | + "metadata": {}, |
| 144 | + "output_type": "display_data" |
| 145 | + }, |
| 146 | + { |
| 147 | + "data": { |
| 148 | + "text/markdown": [ |
| 149 | + "- The client ordered: Nothing \n", |
| 150 | + "- Summary: The client did not place an order because the chicken ranch pizza was unavailable." |
| 151 | + ], |
| 152 | + "text/plain": [ |
| 153 | + "<IPython.core.display.Markdown object>" |
| 154 | + ] |
| 155 | + }, |
| 156 | + "metadata": {}, |
| 157 | + "output_type": "display_data" |
| 158 | + }, |
| 159 | + { |
| 160 | + "data": { |
| 161 | + "text/markdown": [ |
| 162 | + "# Chat3" |
| 163 | + ], |
| 164 | + "text/plain": [ |
| 165 | + "<IPython.core.display.Markdown object>" |
| 166 | + ] |
| 167 | + }, |
| 168 | + "metadata": {}, |
| 169 | + "output_type": "display_data" |
| 170 | + }, |
| 171 | + { |
| 172 | + "data": { |
| 173 | + "text/markdown": [ |
| 174 | + "- **Order**: Large pepperoni pizza and onion rings \n", |
| 175 | + "- **Total Cost**: 250 LE \n", |
| 176 | + "- **Experience**: Positive \n", |
| 177 | + " - The client enjoyed the pizza despite the delay in delivery." |
| 178 | + ], |
| 179 | + "text/plain": [ |
| 180 | + "<IPython.core.display.Markdown object>" |
| 181 | + ] |
| 182 | + }, |
| 183 | + "metadata": {}, |
| 184 | + "output_type": "display_data" |
| 185 | + } |
| 186 | + ], |
| 187 | + "source": [ |
| 188 | + "chats_txt = os.listdir(CHATS_PATH)\n", |
| 189 | + "for chat_file in chats_txt:\n", |
| 190 | + " markdown_heading = f\"# {chat_file[:-4]}\"\n", |
| 191 | + " display(Markdown(markdown_heading))\n", |
| 192 | + " display(Markdown(chat_ai.generate(CHATS_PATH+chat_file)))" |
| 193 | + ] |
| 194 | + } |
| 195 | + ], |
| 196 | + "metadata": { |
| 197 | + "kernelspec": { |
| 198 | + "display_name": "Python 3 (ipykernel)", |
| 199 | + "language": "python", |
| 200 | + "name": "python3" |
| 201 | + }, |
| 202 | + "language_info": { |
| 203 | + "codemirror_mode": { |
| 204 | + "name": "ipython", |
| 205 | + "version": 3 |
| 206 | + }, |
| 207 | + "file_extension": ".py", |
| 208 | + "mimetype": "text/x-python", |
| 209 | + "name": "python", |
| 210 | + "nbconvert_exporter": "python", |
| 211 | + "pygments_lexer": "ipython3", |
| 212 | + "version": "3.11.11" |
| 213 | + } |
| 214 | + }, |
| 215 | + "nbformat": 4, |
| 216 | + "nbformat_minor": 5 |
| 217 | +} |
0 commit comments