|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "5d799d2a-6e58-4a83-b17a-dbbc40efdc39", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## Project - Course Booking AI Asssistant\n", |
| 9 | + "AI Customer Support Bot that \n", |
| 10 | + "- Returns Prices\n", |
| 11 | + "- Books Tickets\n", |
| 12 | + "- Adds Information to Text File" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "code", |
| 17 | + "execution_count": 1, |
| 18 | + "id": "b1ad9acd-a702-48a3-8ff5-d536bcac8030", |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "# imports\n", |
| 23 | + "\n", |
| 24 | + "import os\n", |
| 25 | + "import json\n", |
| 26 | + "from dotenv import load_dotenv\n", |
| 27 | + "from openai import OpenAI\n", |
| 28 | + "import gradio as gr" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": 2, |
| 34 | + "id": "74adab0c-99b3-46cd-a79f-320a3e74138a", |
| 35 | + "metadata": {}, |
| 36 | + "outputs": [ |
| 37 | + { |
| 38 | + "name": "stdout", |
| 39 | + "output_type": "stream", |
| 40 | + "text": [ |
| 41 | + "OpenAI API Key exists and begins sk-proj-\n" |
| 42 | + ] |
| 43 | + } |
| 44 | + ], |
| 45 | + "source": [ |
| 46 | + "# Initialization\n", |
| 47 | + "\n", |
| 48 | + "load_dotenv(override=True)\n", |
| 49 | + "\n", |
| 50 | + "openai_api_key = os.getenv('OPENAI_API_KEY')\n", |
| 51 | + "if openai_api_key:\n", |
| 52 | + " print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n", |
| 53 | + "else:\n", |
| 54 | + " print(\"OpenAI API Key not set\")\n", |
| 55 | + " \n", |
| 56 | + "MODEL = \"gpt-4o-mini\"\n", |
| 57 | + "openai = OpenAI()" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "code", |
| 62 | + "execution_count": 15, |
| 63 | + "id": "8d3240a4-99c1-4c07-acaa-ecbb69ffd2e4", |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [], |
| 66 | + "source": [ |
| 67 | + "system_message = \"You are a helpful assistant for an Online Course Platform called StudyAI. \"\n", |
| 68 | + "system_message += \"Give short, courteous answers, no more than 1 sentence. \"\n", |
| 69 | + "system_message += \"Always be accurate. If you don't know the answer, say so.\"\n", |
| 70 | + "system_message += \"If you are given a partial name, for example 'discrete' instead of 'discrete structures' \\\n", |
| 71 | + "ask the user if they meant to say 'discrete structures', and then display the price. The user may also use \\\n", |
| 72 | + "acronyms like 'PF' instead of programming fundamentals or 'OOP' to mean 'Object oriented programming'. \\\n", |
| 73 | + "Clarify wh\"" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": 14, |
| 79 | + "id": "9a1b8d5f-f893-477b-8396-ff7d697eb0c3", |
| 80 | + "metadata": {}, |
| 81 | + "outputs": [], |
| 82 | + "source": [ |
| 83 | + "course_prices = {\"programming fundamentals\": \"$19\", \"discrete structures\": \"$39\", \"operating systems\": \"$24\", \"object oriented programming\": \"$39\"}\n", |
| 84 | + "\n", |
| 85 | + "def get_course_price(course):\n", |
| 86 | + " print(f\"Tool get_course_price called for {course}\")\n", |
| 87 | + " course = course.lower()\n", |
| 88 | + " return course_prices.get(course, \"Unknown\")\n", |
| 89 | + "\n", |
| 90 | + "def enroll_in_course(course):\n", |
| 91 | + " print(f'Tool enroll_in_course_ called for {course}')\n", |
| 92 | + " course_price = get_course_price(course)\n", |
| 93 | + " if course_price != 'Unknown':\n", |
| 94 | + " with open('enrolled_courses.txt', 'a') as file: \n", |
| 95 | + " file.write(course + \"\\n\")\n", |
| 96 | + " return 'Successfully enrolled in course'\n", |
| 97 | + " else:\n", |
| 98 | + " return 'Enrollment failed, no such course available'" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": 5, |
| 104 | + "id": "330d2b94-a8c5-4967-ace7-15d2cd52d7ae", |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [ |
| 107 | + { |
| 108 | + "name": "stdout", |
| 109 | + "output_type": "stream", |
| 110 | + "text": [ |
| 111 | + "Tool get_course_price called for graph theory\n", |
| 112 | + "Tool get_course_price called for discrete structures\n" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "data": { |
| 117 | + "text/plain": [ |
| 118 | + "'$39'" |
| 119 | + ] |
| 120 | + }, |
| 121 | + "execution_count": 5, |
| 122 | + "metadata": {}, |
| 123 | + "output_type": "execute_result" |
| 124 | + } |
| 125 | + ], |
| 126 | + "source": [ |
| 127 | + "get_course_price('graph theory')\n", |
| 128 | + "get_course_price('discrete structures')" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "code", |
| 133 | + "execution_count": 6, |
| 134 | + "id": "5bb65830-fab8-45a7-bf43-7e52186915a0", |
| 135 | + "metadata": {}, |
| 136 | + "outputs": [], |
| 137 | + "source": [ |
| 138 | + "price_function = {\n", |
| 139 | + " \"name\": \"get_course_price\",\n", |
| 140 | + " \"description\": \"Get the price of a course. Call this whenever you need to know the course price, for example when a customer asks 'How much is a ticket for this course?'\",\n", |
| 141 | + " \"parameters\": {\n", |
| 142 | + " \"type\": \"object\",\n", |
| 143 | + " \"properties\": {\n", |
| 144 | + " \"course\": {\n", |
| 145 | + " \"type\": \"string\",\n", |
| 146 | + " \"description\": \"The course that the customer wants to purchase\",\n", |
| 147 | + " },\n", |
| 148 | + " },\n", |
| 149 | + " \"required\": [\"course\"],\n", |
| 150 | + " \"additionalProperties\": False\n", |
| 151 | + " }\n", |
| 152 | + "}\n", |
| 153 | + "\n", |
| 154 | + "enroll_function = {\n", |
| 155 | + " \"name\": \"enroll_in_course\",\n", |
| 156 | + " \"description\":\"Get the success status of course enrollment. Call whenever a customer wants to enroll in a course\\\n", |
| 157 | + " for example, if they say 'I want to purchase this course' or 'I want to enroll in this course'\",\n", |
| 158 | + " \"parameters\":{\n", |
| 159 | + " \"type\":\"object\",\n", |
| 160 | + " \"properties\":{\n", |
| 161 | + " \"course\":{\n", |
| 162 | + " \"type\":\"string\",\n", |
| 163 | + " \"description\": \"The course that the customer wants to purchase\",\n", |
| 164 | + " },\n", |
| 165 | + " },\n", |
| 166 | + " \"required\": [\"course\"],\n", |
| 167 | + " \"additionalProperties\": False\n", |
| 168 | + " } \n", |
| 169 | + "}" |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + "cell_type": "code", |
| 174 | + "execution_count": 7, |
| 175 | + "id": "08af86b9-3aaa-4b6b-bf7c-ee668ba1cbfe", |
| 176 | + "metadata": {}, |
| 177 | + "outputs": [], |
| 178 | + "source": [ |
| 179 | + "tools = [\n", |
| 180 | + " {\"type\":\"function\",\"function\":price_function},\n", |
| 181 | + " {\"type\":\"function\",\"function\":enroll_function}\n", |
| 182 | + "]" |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "code", |
| 187 | + "execution_count": 8, |
| 188 | + "id": "482efc34-ff1f-4146-9570-58b4d59c3b2f", |
| 189 | + "metadata": {}, |
| 190 | + "outputs": [], |
| 191 | + "source": [ |
| 192 | + "def chat(message,history):\n", |
| 193 | + " messages = [{\"role\":\"system\",\"content\":system_message}] + history + [{\"role\":\"user\",\"content\":message}]\n", |
| 194 | + " response = openai.chat.completions.create(model=MODEL,messages=messages,tools=tools)\n", |
| 195 | + "\n", |
| 196 | + " if response.choices[0].finish_reason == \"tool_calls\":\n", |
| 197 | + " message = response.choices[0].message\n", |
| 198 | + " messages.append(message)\n", |
| 199 | + " for tool_call in message.tool_calls:\n", |
| 200 | + " messages.append(handle_tool_call(tool_call))\n", |
| 201 | + " response = openai.chat.completions.create(model=MODEL,messages=messages)\n", |
| 202 | + "\n", |
| 203 | + " return response.choices[0].message.content" |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "code", |
| 208 | + "execution_count": 9, |
| 209 | + "id": "f725b4fb-d477-4d7d-80b5-5d70e1b25a86", |
| 210 | + "metadata": {}, |
| 211 | + "outputs": [], |
| 212 | + "source": [ |
| 213 | + "# We have to write that function handle_tool_call:\n", |
| 214 | + "\n", |
| 215 | + "def handle_tool_call(tool_call):\n", |
| 216 | + " function = tool_call.function.name\n", |
| 217 | + " arguments = json.loads(tool_call.function.arguments)\n", |
| 218 | + " match function:\n", |
| 219 | + " case 'get_course_price':\n", |
| 220 | + " course = arguments.get('course')\n", |
| 221 | + " price = get_course_price(course)\n", |
| 222 | + " return {\n", |
| 223 | + " \"role\": \"tool\",\n", |
| 224 | + " \"content\": json.dumps({\"course\": course,\"price\": price}),\n", |
| 225 | + " \"tool_call_id\": tool_call.id\n", |
| 226 | + " }\n", |
| 227 | + " case 'enroll_in_course':\n", |
| 228 | + " course = arguments.get('course')\n", |
| 229 | + " status = enroll_in_course(course)\n", |
| 230 | + " return {\n", |
| 231 | + " \"role\": \"tool\",\n", |
| 232 | + " \"content\": json.dumps({\"course\": course, \"status\": status}),\n", |
| 233 | + " \"tool_call_id\": tool_call.id\n", |
| 234 | + " }\n", |
| 235 | + " " |
| 236 | + ] |
| 237 | + }, |
| 238 | + { |
| 239 | + "cell_type": "code", |
| 240 | + "execution_count": 13, |
| 241 | + "id": "c446272a-9ce1-4ffd-9bc8-483d782810b4", |
| 242 | + "metadata": {}, |
| 243 | + "outputs": [ |
| 244 | + { |
| 245 | + "name": "stdout", |
| 246 | + "output_type": "stream", |
| 247 | + "text": [ |
| 248 | + "* Running on local URL: http://127.0.0.1:7864\n", |
| 249 | + "\n", |
| 250 | + "To create a public link, set `share=True` in `launch()`.\n" |
| 251 | + ] |
| 252 | + }, |
| 253 | + { |
| 254 | + "data": { |
| 255 | + "text/html": [ |
| 256 | + "<div><iframe src=\"http://127.0.0.1:7864/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>" |
| 257 | + ], |
| 258 | + "text/plain": [ |
| 259 | + "<IPython.core.display.HTML object>" |
| 260 | + ] |
| 261 | + }, |
| 262 | + "metadata": {}, |
| 263 | + "output_type": "display_data" |
| 264 | + }, |
| 265 | + { |
| 266 | + "data": { |
| 267 | + "text/plain": [] |
| 268 | + }, |
| 269 | + "execution_count": 13, |
| 270 | + "metadata": {}, |
| 271 | + "output_type": "execute_result" |
| 272 | + }, |
| 273 | + { |
| 274 | + "name": "stdout", |
| 275 | + "output_type": "stream", |
| 276 | + "text": [ |
| 277 | + "Tool get_course_price called for programming fundamentals\n", |
| 278 | + "Tool enroll_in_course_ called for Programming Fundamentals\n", |
| 279 | + "Tool get_course_price called for Programming Fundamentals\n" |
| 280 | + ] |
| 281 | + }, |
| 282 | + { |
| 283 | + "name": "stderr", |
| 284 | + "output_type": "stream", |
| 285 | + "text": [ |
| 286 | + "Traceback (most recent call last):\n", |
| 287 | + " File \"C:\\Users\\92310\\anaconda3\\envs\\llms\\Lib\\site-packages\\gradio\\queueing.py\", line 625, in process_events\n", |
| 288 | + " response = await route_utils.call_process_api(\n", |
| 289 | + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", |
| 290 | + " File \"C:\\Users\\92310\\anaconda3\\envs\\llms\\Lib\\site-packages\\gradio\\route_utils.py\", line 322, in call_process_api\n", |
| 291 | + " output = await app.get_blocks().process_api(\n", |
| 292 | + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", |
| 293 | + " File \"C:\\Users\\92310\\anaconda3\\envs\\llms\\Lib\\site-packages\\gradio\\blocks.py\", line 2096, in process_api\n", |
| 294 | + " result = await self.call_function(\n", |
| 295 | + " ^^^^^^^^^^^^^^^^^^^^^^^^^\n", |
| 296 | + " File \"C:\\Users\\92310\\anaconda3\\envs\\llms\\Lib\\site-packages\\gradio\\blocks.py\", line 1641, in call_function\n", |
| 297 | + " prediction = await fn(*processed_input)\n", |
| 298 | + " ^^^^^^^^^^^^^^^^^^^^^^^^^^\n", |
| 299 | + " File \"C:\\Users\\92310\\anaconda3\\envs\\llms\\Lib\\site-packages\\gradio\\utils.py\", line 857, in async_wrapper\n", |
| 300 | + " response = await f(*args, **kwargs)\n", |
| 301 | + " ^^^^^^^^^^^^^^^^^^^^^^^^\n", |
| 302 | + " File \"C:\\Users\\92310\\anaconda3\\envs\\llms\\Lib\\site-packages\\gradio\\chat_interface.py\", line 862, in _submit_fn\n", |
| 303 | + " response = await anyio.to_thread.run_sync(\n", |
| 304 | + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", |
| 305 | + " File \"C:\\Users\\92310\\anaconda3\\envs\\llms\\Lib\\site-packages\\anyio\\to_thread.py\", line 56, in run_sync\n", |
| 306 | + " return await get_async_backend().run_sync_in_worker_thread(\n", |
| 307 | + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", |
| 308 | + " File \"C:\\Users\\92310\\anaconda3\\envs\\llms\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 2461, in run_sync_in_worker_thread\n", |
| 309 | + " return await future\n", |
| 310 | + " ^^^^^^^^^^^^\n", |
| 311 | + " File \"C:\\Users\\92310\\anaconda3\\envs\\llms\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 962, in run\n", |
| 312 | + " result = context.run(func, *args)\n", |
| 313 | + " ^^^^^^^^^^^^^^^^^^^^^^^^\n", |
| 314 | + " File \"C:\\Users\\92310\\AppData\\Local\\Temp\\ipykernel_3348\\1161680098.py\", line 9, in chat\n", |
| 315 | + " messages.append(handle_tool_call(tool_call))\n", |
| 316 | + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", |
| 317 | + " File \"C:\\Users\\92310\\AppData\\Local\\Temp\\ipykernel_3348\\1187326431.py\", line 17, in handle_tool_call\n", |
| 318 | + " status = enroll_in_course(course)\n", |
| 319 | + " ^^^^^^^^^^^^^^^^^^^^^^^^\n", |
| 320 | + " File \"C:\\Users\\92310\\AppData\\Local\\Temp\\ipykernel_3348\\2541918318.py\", line 13, in enroll_in_course\n", |
| 321 | + " file.write(course_name + \"\\n\")\n", |
| 322 | + " ^^^^^^^^^^^\n", |
| 323 | + "NameError: name 'course_name' is not defined\n" |
| 324 | + ] |
| 325 | + } |
| 326 | + ], |
| 327 | + "source": [ |
| 328 | + "gr.ChatInterface(fn=chat,type=\"messages\").launch(inbrowser=True)" |
| 329 | + ] |
| 330 | + }, |
| 331 | + { |
| 332 | + "cell_type": "code", |
| 333 | + "execution_count": null, |
| 334 | + "id": "1fe714a3-f793-4c3b-b5aa-6c81b82aea1b", |
| 335 | + "metadata": {}, |
| 336 | + "outputs": [], |
| 337 | + "source": [] |
| 338 | + } |
| 339 | + ], |
| 340 | + "metadata": { |
| 341 | + "kernelspec": { |
| 342 | + "display_name": "Python 3 (ipykernel)", |
| 343 | + "language": "python", |
| 344 | + "name": "python3" |
| 345 | + }, |
| 346 | + "language_info": { |
| 347 | + "codemirror_mode": { |
| 348 | + "name": "ipython", |
| 349 | + "version": 3 |
| 350 | + }, |
| 351 | + "file_extension": ".py", |
| 352 | + "mimetype": "text/x-python", |
| 353 | + "name": "python", |
| 354 | + "nbconvert_exporter": "python", |
| 355 | + "pygments_lexer": "ipython3", |
| 356 | + "version": "3.11.11" |
| 357 | + } |
| 358 | + }, |
| 359 | + "nbformat": 4, |
| 360 | + "nbformat_minor": 5 |
| 361 | +} |
0 commit comments