From 0ab746d843f16a8a9a4227f26c808341a480de5d Mon Sep 17 00:00:00 2001 From: Mechantchulo Date: Sat, 6 Sep 2025 22:53:47 +0300 Subject: [PATCH 1/6] Add week1 exercise notebook --- week1/week1 EXERCISE.ipynb | 79 ++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 17 deletions(-) diff --git a/week1/week1 EXERCISE.ipynb b/week1/week1 EXERCISE.ipynb index f3486fe32..e73e1f0ba 100644 --- a/week1/week1 EXERCISE.ipynb +++ b/week1/week1 EXERCISE.ipynb @@ -13,17 +13,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "c1070317-3ed9-4659-abe3-828943230e03", "metadata": {}, "outputs": [], "source": [ - "# imports" + "# 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": null, + "execution_count": 2, "id": "4a456906-915a-4bfd-bb9d-57e505c5093f", "metadata": {}, "outputs": [], @@ -36,48 +44,85 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "a8d7923c-5f28-4c30-8556-342d7c8497c1", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Good\n" + ] + } + ], "source": [ - "# set up environment" + "# 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": null, + "execution_count": 4, "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", - "\"\"\"" + "client = OpenAI(api_key=api_key) \n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "60ce7000-a4a5-4cce-a261-e75ef45063b4", "metadata": {}, "outputs": [], "source": [ - "# Get gpt-4o-mini to answer, with streaming" + "# 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": "8f7c8ea8-4082-4ad0-8751-3301adcf6538", + "id": "d35420b1-5038-4db7-9d63-bc57c0722da5", "metadata": {}, "outputs": [], "source": [ - "# Get Llama 3.2 to answer" + "# 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": "8c220878-1b74-4690-95da-f26cac171256", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -96,7 +141,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.11" + "version": "3.11.13" } }, "nbformat": 4, From 91180244b19f65240af441e11010c8ba3ef467f7 Mon Sep 17 00:00:00 2001 From: Mechantchulo Date: Sun, 7 Sep 2025 23:36:58 +0300 Subject: [PATCH 2/6] Add a simple openai recipe creator using gpt-4.1-mini --- week2/day1recipeai.ipynb | 154 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 week2/day1recipeai.ipynb diff --git a/week2/day1recipeai.ipynb b/week2/day1recipeai.ipynb new file mode 100644 index 000000000..f94082da9 --- /dev/null +++ b/week2/day1recipeai.ipynb @@ -0,0 +1,154 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "5ecc1844-3dc9-4aa6-8626-09b315f5dacd", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import json\n", + "from dotenv import load_dotenv\n", + "from openai import OpenAI\n", + "import anthropic\n", + "from IPython.display import Markdown, display, update_display" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9d73ce60-a96d-4d43-b437-8e8925bb0853", + "metadata": { + "scrolled": true + }, + "outputs": [], + "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": null, + "id": "19dc2201-1212-451d-bf08-7dddc42bb7ed", + "metadata": {}, + "outputs": [], + "source": [ + "openai = OpenAI()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0c7fe962-6a10-4708-8d3a-a76254cc224b", + "metadata": {}, + "outputs": [], + "source": [ + "# Anthropic model to be used\n", + "MODEL = \"gpt-4.1-mini\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6ad4b4de-ffc6-4f36-a75a-057b5c153116", + "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": null, + "id": "b3694292-ab9d-4a01-8007-5f9532adf69b", + "metadata": {}, + "outputs": [], + "source": [ + "food_question = input(\"Enter food to get recipe: \")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3d248087-b728-4008-b986-6b011231a35c", + "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": "d3323c14-a247-4dc7-82e9-eeebb0ec6101", + "metadata": { + "scrolled": true + }, + "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": "4f901826-ed4a-4297-ae63-391826256caa", + "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.11.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From eafdb304ebfd4d9564dfbd44616d2ebe6e620b88 Mon Sep 17 00:00:00 2001 From: Mechantchulo Date: Tue, 9 Sep 2025 19:21:29 +0300 Subject: [PATCH 3/6] community contribution week2 day1 --- week2/community-contributions/recipeai.ipynb | 174 +++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 week2/community-contributions/recipeai.ipynb 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 +} From 805b92515db42ca4c4dc34df8c4114ba8d1450ba Mon Sep 17 00:00:00 2001 From: Mechantchulo Date: Tue, 9 Sep 2025 19:26:36 +0300 Subject: [PATCH 4/6] Remove week2/day1recipeai.ipynb from repo --- week2/day1recipeai.ipynb | 154 --------------------------------------- 1 file changed, 154 deletions(-) delete mode 100644 week2/day1recipeai.ipynb diff --git a/week2/day1recipeai.ipynb b/week2/day1recipeai.ipynb deleted file mode 100644 index f94082da9..000000000 --- a/week2/day1recipeai.ipynb +++ /dev/null @@ -1,154 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "id": "5ecc1844-3dc9-4aa6-8626-09b315f5dacd", - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "import json\n", - "from dotenv import load_dotenv\n", - "from openai import OpenAI\n", - "import anthropic\n", - "from IPython.display import Markdown, display, update_display" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9d73ce60-a96d-4d43-b437-8e8925bb0853", - "metadata": { - "scrolled": true - }, - "outputs": [], - "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": null, - "id": "19dc2201-1212-451d-bf08-7dddc42bb7ed", - "metadata": {}, - "outputs": [], - "source": [ - "openai = OpenAI()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0c7fe962-6a10-4708-8d3a-a76254cc224b", - "metadata": {}, - "outputs": [], - "source": [ - "# Anthropic model to be used\n", - "MODEL = \"gpt-4.1-mini\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6ad4b4de-ffc6-4f36-a75a-057b5c153116", - "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": null, - "id": "b3694292-ab9d-4a01-8007-5f9532adf69b", - "metadata": {}, - "outputs": [], - "source": [ - "food_question = input(\"Enter food to get recipe: \")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3d248087-b728-4008-b986-6b011231a35c", - "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": "d3323c14-a247-4dc7-82e9-eeebb0ec6101", - "metadata": { - "scrolled": true - }, - "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": "4f901826-ed4a-4297-ae63-391826256caa", - "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.11.13" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} From 0933c872f0e683a5a366713484a72980e8452885 Mon Sep 17 00:00:00 2001 From: Mechantchulo Date: Tue, 9 Sep 2025 19:33:34 +0300 Subject: [PATCH 5/6] Remove week1 EXERCISE.ipynb --- week1/week1 EXERCISE.ipynb | 149 ------------------------------------- 1 file changed, 149 deletions(-) delete mode 100644 week1/week1 EXERCISE.ipynb diff --git a/week1/week1 EXERCISE.ipynb b/week1/week1 EXERCISE.ipynb deleted file mode 100644 index e73e1f0ba..000000000 --- a/week1/week1 EXERCISE.ipynb +++ /dev/null @@ -1,149 +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": 1, - "id": "c1070317-3ed9-4659-abe3-828943230e03", - "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": 2, - "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": 3, - "id": "a8d7923c-5f28-4c30-8556-342d7c8497c1", - "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": 4, - "id": "3f0d0137-52b0-47a8-81a8-11a90a010798", - "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": 5, - "id": "60ce7000-a4a5-4cce-a261-e75ef45063b4", - "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": "d35420b1-5038-4db7-9d63-bc57c0722da5", - "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": "8c220878-1b74-4690-95da-f26cac171256", - "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.11.13" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} From 2a58af77584db1f28afc477d276619cc9792e3d4 Mon Sep 17 00:00:00 2001 From: Mechantchulo Date: Tue, 9 Sep 2025 19:34:12 +0300 Subject: [PATCH 6/6] Move week1 exercise into community contributions --- community-contributions/week1exercise.ipynb | 145 ++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 community-contributions/week1exercise.ipynb 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 +}