{ "cells": [ { "cell_type": "markdown", "id": "ddfa9ae6-69fe-444a-b994-8c4c5970a7ec", "metadata": {}, "source": [ "# Project - Airline AI Assistant\n", "\n", "We'll now bring together what we've learned to make an AI Customer Support assistant for an Airline" ] }, { "cell_type": "code", "execution_count": 1, "id": "8b50bbe2-c0b1-49c3-9a5c-1ba7efa2bcb4", "metadata": {}, "outputs": [], "source": [ "# imports\n", "\n", "import os\n", "import json\n", "from dotenv import load_dotenv\n", "from openai import OpenAI\n", "import gradio as gr\n", "import sqlite3" ] }, { "cell_type": "code", "execution_count": 2, "id": "747e8786-9da8-4342-b6c9-f5f69c2e22ae", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OpenAI API Key exists and begins sk-proj-\n" ] } ], "source": [ "# Initialization\n", "\n", "load_dotenv(override=True)\n", "\n", "openai_api_key = os.getenv('OPENAI_API_KEY')\n", "if openai_api_key:\n", " print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n", "else:\n", " print(\"OpenAI API Key not set\")\n", " \n", "MODEL = \"gpt-4.1-mini\"\n", "openai = OpenAI()\n", "\n", "DB = \"prices.db\"" ] }, { "cell_type": "code", "execution_count": 3, "id": "0a521d84-d07c-49ab-a0df-d6451499ed97", "metadata": {}, "outputs": [], "source": [ "system_message = \"\"\"\n", "You are a helpful assistant for an Airline called FlightAI.\n", "Give short, courteous answers, no more than 1 sentence.\n", "Always be accurate. If you don't know the answer, say so.\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 4, "id": "c3e8173c", "metadata": {}, "outputs": [], "source": [ "def get_ticket_price(city):\n", " print(f\"DATABASE TOOL CALLED: Getting price for {city}\", flush=True)\n", " with sqlite3.connect(DB) as conn:\n", " cursor = conn.cursor()\n", " cursor.execute('SELECT price FROM prices WHERE city = ?', (city.lower(),))\n", " result = cursor.fetchone()\n", " return f\"Ticket price to {city} is ${result[0]}\" if result else \"No price data available for this city\"" ] }, { "cell_type": "code", "execution_count": 5, "id": "03f19289", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "DATABASE TOOL CALLED: Getting price for Paris\n" ] }, { "data": { "text/plain": [ "'Ticket price to Paris is $899.0'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get_ticket_price(\"Paris\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "bcfb6523", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'type': 'function',\n", " 'function': {'name': 'get_ticket_price',\n", " 'description': 'Get the price of a return ticket to the destination city.',\n", " 'parameters': {'type': 'object',\n", " 'properties': {'destination_city': {'type': 'string',\n", " 'description': 'The city that the customer wants to travel to'}},\n", " 'required': ['destination_city'],\n", " 'additionalProperties': False}}}]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "price_function = {\n", " \"name\": \"get_ticket_price\",\n", " \"description\": \"Get the price of a return ticket to the destination city.\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"destination_city\": {\n", " \"type\": \"string\",\n", " \"description\": \"The city that the customer wants to travel to\",\n", " },\n", " },\n", " \"required\": [\"destination_city\"],\n", " \"additionalProperties\": False\n", " }\n", "}\n", "tools = [{\"type\": \"function\", \"function\": price_function}]\n", "tools" ] }, { "cell_type": "code", "execution_count": 7, "id": "61a2a15d-b559-4844-b377-6bd5cb4949f6", "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": [ "\n",
" \n",
" | \n",
" \n",
" I have a special request for you\n", " \n", " My editor tells me that it makes a HUGE difference when students rate this course on Udemy - it's one of the main ways that Udemy decides whether to show it to others. If you're able to take a minute to rate this, I'd be so very grateful! And regardless - always please reach out to me at ed@edwarddonner.com if I can help at any point.\n", " \n", " | \n",
"