diff --git a/week2/community-contributions/kwabena/week2_solution_.ipynb b/week2/community-contributions/kwabena/week2_solution_.ipynb new file mode 100644 index 0000000..81af8bb --- /dev/null +++ b/week2/community-contributions/kwabena/week2_solution_.ipynb @@ -0,0 +1,210 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "fd1cdd6e", + "metadata": {}, + "source": [ + "## Week 2 - Full Prototype for Technical Questions Answerer" + ] + }, + { + "cell_type": "markdown", + "id": "70db9a0b", + "metadata": {}, + "source": [ + " This notebook will implement a Gradio UI, streaming, use of the system prompt to add expertise, and the ability to switch between models." + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "df46689d", + "metadata": {}, + "outputs": [], + "source": [ + "# imports\n", + "import os\n", + "import json\n", + "from dotenv import load_dotenv\n", + "from openai import OpenAI\n", + "import gradio as gr\n" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "c7416a2a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "OpenAI API Key exists and begins sk-proj-\n" + ] + } + ], + "source": [ + "# Initialization\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()" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "86966749", + "metadata": {}, + "outputs": [], + "source": [ + "system_message = \"\"\"\n", + "You are an expert technical question answerer specializing in data science, programming, \n", + "and software engineering. Your goal is to provide clear, accurate, and practical answers \n", + "to technical questions.\n", + "\n", + "When answering:\n", + "- Break down complex concepts into understandable explanations\n", + "- Provide code examples when relevant, with comments explaining key parts\n", + "- Mention common pitfalls or best practices\n", + "- If a question is ambiguous, state your assumptions or ask for clarification\n", + "- For debugging questions, explain both the fix and why the error occurred\n", + "- Cite specific documentation or resources when helpful\n", + "\n", + "Always prioritize accuracy and clarity over speed. If you're unsure about something, \n", + "acknowledge the uncertainty rather than guessing.\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "d34e5b81", + "metadata": {}, + "outputs": [], + "source": [ + "# Streaming chat funcion\n", + "def chat(model, history):\n", + " messages = [{\"role\": \"system\", \"content\": system_message}]\n", + " for h in history:\n", + " messages.append({\"role\": h[\"role\"], \"content\": h[\"content\"]})\n", + "\n", + " stream = openai.chat.completions.create(\n", + " model=model, \n", + " messages=messages,\n", + " stream=True\n", + " )\n", + "\n", + " response = \"\"\n", + " for chunk in stream:\n", + " if chunk.choices[0].delta.content is not None:\n", + " response += chunk.choices[0].delta.content\n", + " yield history + [{\"role\": \"assistant\", \"content\": response}]" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "32350869", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* Running on local URL: http://127.0.0.1:7876\n", + "* To create a public link, set `share=True` in `launch()`.\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "