{ "cells": [ { "cell_type": "markdown", "id": "75e2ef28-594f-4c18-9d22-c6b8cd40ead2", "metadata": {}, "source": [ "# Day 3 - Conversational AI - aka Chatbot!" ] }, { "cell_type": "code", "execution_count": 1, "id": "70e39cd8-ec79-4e3e-9c26-5659d42d0861", "metadata": {}, "outputs": [], "source": [ "# imports\n", "\n", "import os\n", "from dotenv import load_dotenv\n", "from openai import OpenAI\n", "import gradio as gr" ] }, { "cell_type": "code", "execution_count": 2, "id": "231605aa-fccb-447e-89cf-8b187444536a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OpenAI API Key exists and begins sk-proj-\n" ] } ], "source": [ "# Load environment variables in a file called .env\n", "# Print the key prefixes to help with any debugging\n", "\n", "load_dotenv(override=True)\n", "openai_api_key = os.getenv('OPENAI_API_KEY')\n", "\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\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "6541d58e-2297-4de1-b1f7-77da1b98b8bb", "metadata": {}, "outputs": [], "source": [ "# Initialize\n", "\n", "openai = OpenAI()\n", "MODEL = 'gpt-4.1-mini'" ] }, { "cell_type": "code", "execution_count": 4, "id": "e16839b5-c03b-4d9d-add6-87a0f6f37575", "metadata": {}, "outputs": [], "source": [ "# Again, I'll be in scientist-mode and change this global during the lab\n", "\n", "system_message = \"You are a helpful assistant\"" ] }, { "cell_type": "markdown", "id": "98e97227-f162-4d1a-a0b2-345ff248cbe7", "metadata": {}, "source": [ "## And now, writing a new callback\n", "\n", "We now need to write a function called:\n", "\n", "`chat(message, history)`\n", "\n", "Which will be a callback function we will give gradio.\n", "\n", "### The job of this function\n", "\n", "Take a message, take the prior conversation, and return the response.\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "354ce793", "metadata": {}, "outputs": [], "source": [ "def chat(message, history):\n", " return \"bananas\"" ] }, { "cell_type": "code", "execution_count": 6, "id": "e87f3417", "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",
" Business Applications\n", " Conversational Assistants are of course a hugely common use case for Gen AI, and the latest frontier models are remarkably good at nuanced conversation. And Gradio makes it easy to have a user interface. Another crucial skill we covered is how to use prompting to provide context, information and examples.\n", "\n", "Consider how you could apply an AI Assistant to your business, and make yourself a prototype. Use the system prompt to give context on your business, and set the tone for the LLM.\n", " | \n",
"