{ "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": [], "source": [ "# Load environment variables in a file called .env\n", "\n", "load_dotenv()\n", "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n", "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n", "os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY', 'your-key-if-not-using-env')" ] }, { "cell_type": "code", "execution_count": 3, "id": "6541d58e-2297-4de1-b1f7-77da1b98b8bb", "metadata": {}, "outputs": [], "source": [ "# Initialize\n", "\n", "openai = OpenAI()\n", "MODEL = 'gpt-4o-mini'" ] }, { "cell_type": "code", "execution_count": 4, "id": "e16839b5-c03b-4d9d-add6-87a0f6f37575", "metadata": {}, "outputs": [], "source": [ "system_message = \"You are a helpful assistant\"" ] }, { "cell_type": "markdown", "id": "98e97227-f162-4d1a-a0b2-345ff248cbe7", "metadata": {}, "source": [ "## Reminder of the structure of prompt messages to OpenAI:\n", "\n", "```\n", "[\n", " {\"role\": \"system\", \"content\": \"system message here\"},\n", " {\"role\": \"user\", \"content\": \"first user prompt here\"},\n", " {\"role\": \"assistant\", \"content\": \"the assistant's response\"},\n", " {\"role\": \"user\", \"content\": \"the new user prompt\"},\n", "]\n", "```\n", "\n", "We will write a function `chat(message, history)` where:\n", "**message** is the prompt to use\n", "**history** is a list of pairs of user message with assistant's reply\n", "\n", "```\n", "[\n", " [\"user said this\", \"assistant replied\"],\n", " [\"then user said this\", \"and assistant replied again],\n", " ...\n", "]\n", "```\n", "We will convert this history into the prompt style for OpenAI, then call OpenAI. " ] }, { "cell_type": "code", "execution_count": 5, "id": "1eacc8a4-4b48-4358-9e06-ce0020041bc1", "metadata": {}, "outputs": [], "source": [ "def chat(message, history):\n", " messages = [{\"role\": \"system\", \"content\": system_message}]\n", " for user_message, assistant_message in history:\n", " messages.append({\"role\": \"user\", \"content\": user_message})\n", " messages.append({\"role\": \"assistant\", \"content\": assistant_message})\n", " messages.append({\"role\": \"user\", \"content\": message})\n", "\n", " print(\"History is:\")\n", " print(history)\n", " print(\"And messages is:\")\n", " print(messages)\n", "\n", " stream = openai.chat.completions.create(model=MODEL, messages=messages, stream=True)\n", "\n", " response = \"\"\n", " for chunk in stream:\n", " response += chunk.choices[0].delta.content or ''\n", " yield response" ] }, { "cell_type": "markdown", "id": "1334422a-808f-4147-9c4c-57d63d9780d0", "metadata": {}, "source": [ "## And then enter Gradio's magic!" ] }, { "cell_type": "code", "execution_count": 6, "id": "0866ca56-100a-44ab-8bd0-1568feaf6bf2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7870\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "