diff --git a/community-contributions/week1exercise.ipynb b/community-contributions/week1exercise.ipynb new file mode 100644 index 000000000..e13182482 --- /dev/null +++ b/community-contributions/week1exercise.ipynb @@ -0,0 +1,145 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "ad99050c-a87b-42c1-bea1-47974cac8954", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install openai" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "751d62eb-afe2-493c-a9ae-34ec7f869505", + "metadata": {}, + "outputs": [], + "source": [ + "# imports\n", + "import os\n", + "import requests\n", + "import json\n", + "from typing import List\n", + "from dotenv import load_dotenv\n", + "from bs4 import BeautifulSoup\n", + "from IPython.display import Markdown, display, update_display\n", + "from openai import OpenAI" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "fbaec3e1-83e9-44d5-8f01-a1a2b9c51878", + "metadata": {}, + "outputs": [], + "source": [ + "MODEL_GPT = 'gpt-4o-mini'" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "3a64962e-fda3-4f9a-92f9-3bfe36b8c94d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Good\n" + ] + } + ], + "source": [ + "# set up environment\n", + "load_dotenv(override = True)\n", + "api_key = os.getenv('OPENAI_API_KEY')\n", + "if api_key:\n", + " print(\"Good\")\n", + "else:\n", + " print(\"Key not found\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "26bf790a-5102-4cf0-95f1-7c156851e9a4", + "metadata": {}, + "outputs": [], + "source": [ + "# here is the question; type over this to ask something new\n", + "client = OpenAI(api_key=api_key) \n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "b4ebb672-b798-4303-86a4-750afc857344", + "metadata": {}, + "outputs": [], + "source": [ + "# system prompt\n", + "system_prompt = \"\"\"\n", + "You are a technical tutor who explains programming and computer science concepts clearly and step by step. \n", + "When a user asks a technical question, break it down into simple parts, use examples where helpful, \n", + "and avoid giving just the final answer. Your goal is to help the user understand, not just memorize.\"\"\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e481f239-5b46-4da8-81ac-56a7d0c38333", + "metadata": {}, + "outputs": [], + "source": [ + "# Get gpt-4o-mini to answer\n", + "def answer_question(question):\n", + " response = client.chat.completions.create(\n", + " model = MODEL_GPT,\n", + " messages = [\n", + " {\"role\": \"system\", \"content\": system_prompt},\n", + " {\"role\": \"user\", \"content\": question}\n", + " ]\n", + " )\n", + " answer = response.choices[0].message.content\n", + " display(Markdown(answer))\n", + " \n", + "\n", + "question = input(\"Enter the technical question: \")\n", + "answer_question(question)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aca86800-3dbb-42c7-825c-d474100f77d9", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/week1/week1 EXERCISE.ipynb b/week1/week1 EXERCISE.ipynb deleted file mode 100644 index f3486fe32..000000000 --- a/week1/week1 EXERCISE.ipynb +++ /dev/null @@ -1,104 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "fe12c203-e6a6-452c-a655-afb8a03a4ff5", - "metadata": {}, - "source": [ - "# End of week 1 exercise\n", - "\n", - "To demonstrate your familiarity with OpenAI API, and also Ollama, build a tool that takes a technical question, \n", - "and responds with an explanation. This is a tool that you will be able to use yourself during the course!" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "c1070317-3ed9-4659-abe3-828943230e03", - "metadata": {}, - "outputs": [], - "source": [ - "# imports" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4a456906-915a-4bfd-bb9d-57e505c5093f", - "metadata": {}, - "outputs": [], - "source": [ - "# constants\n", - "\n", - "MODEL_GPT = 'gpt-4o-mini'\n", - "MODEL_LLAMA = 'llama3.2'" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a8d7923c-5f28-4c30-8556-342d7c8497c1", - "metadata": {}, - "outputs": [], - "source": [ - "# set up environment" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3f0d0137-52b0-47a8-81a8-11a90a010798", - "metadata": {}, - "outputs": [], - "source": [ - "# here is the question; type over this to ask something new\n", - "\n", - "question = \"\"\"\n", - "Please explain what this code does and why:\n", - "yield from {book.get(\"author\") for book in books if book.get(\"author\")}\n", - "\"\"\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "60ce7000-a4a5-4cce-a261-e75ef45063b4", - "metadata": {}, - "outputs": [], - "source": [ - "# Get gpt-4o-mini to answer, with streaming" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8f7c8ea8-4082-4ad0-8751-3301adcf6538", - "metadata": {}, - "outputs": [], - "source": [ - "# Get Llama 3.2 to answer" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.11" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/week2/community-contributions/recipeai.ipynb b/week2/community-contributions/recipeai.ipynb new file mode 100644 index 000000000..11f26f780 --- /dev/null +++ b/week2/community-contributions/recipeai.ipynb @@ -0,0 +1,174 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "edad8a5d-0393-4624-bc26-8153cb99ee57", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install openai\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "d5d8d93e-bd08-4509-9939-7255f308efdb", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import json\n", + "from dotenv import load_dotenv\n", + "from openai import OpenAI\n", + "from IPython.display import Markdown, display, update_display" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "e78e6211-9e3b-4fef-97e0-52807c8132f6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Key found\n" + ] + } + ], + "source": [ + "# check the environment variables\n", + "load_dotenv(override=True)\n", + "openai_api_key=os.getenv('OPENAI_API_KEY')\n", + "if openai_api_key:\n", + " print(\"Key found\")\n", + "else:\n", + " print(\"Invalid key\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "fa2fb7c4-ea50-4be4-a85a-1f5d36d24bd1", + "metadata": {}, + "outputs": [], + "source": [ + "openai = OpenAI()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "9dfd0448-b980-40b0-9b4c-8cc3086a97e4", + "metadata": {}, + "outputs": [], + "source": [ + "MODEL = \"gpt-4.1-mini\"" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "5bdc0533-bf17-4770-af38-c9055c88775f", + "metadata": {}, + "outputs": [], + "source": [ + "# Prompt to tell the system what it needs to do\n", + "system_prompt = \"\"\"You are RecipeAI, an expert chef assistant. \n", + "When a user tells you the food they want to eat or the ingredients they have, suggest a recipe in a clear, step-by-step format.\n", + "\n", + "Your response must include:\n", + "1. Recipe Name\n", + "2. Short Description\n", + "3. Ingredients List\n", + "4. Step-by-Step Instructions\n", + "5. Optional Tips (e.g., variations, healthier options)\n", + "\n", + "Keep the response concise but practical, and make sure the recipe is easy to follow.\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "6f937d3c-cc2c-4c40-9553-669f16e089da", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter food to get recipe: ugali mayai\n" + ] + } + ], + "source": [ + "food_question = input(\"Enter food to get recipe: \")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "967f3900-61d4-4e79-b427-adc86420e97f", + "metadata": {}, + "outputs": [], + "source": [ + "#getting the messages for the llm\n", + "prompts = [\n", + " {\"role\": \"system\", \"content\": system_prompt},\n", + " {\"role\": \"user\", \"content\": food_question}\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5506b796-6c90-4a03-8070-6d0ba9e83fc8", + "metadata": {}, + "outputs": [], + "source": [ + "#response of the ai\n", + "response = openai.chat.completions.create(\n", + " model = MODEL,\n", + " max_tokens = 200,\n", + " temperature = 0.6,\n", + " messages = prompts,\n", + " \n", + ")\n", + "recipe_output = response.choices[0].message.content\n", + "display(Markdown(recipe_output))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "719acf6e-b158-40f3-b515-e75eda458919", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}