From b140c9d6d8294e9779707256fec043606cc4d759 Mon Sep 17 00:00:00 2001 From: Antonina Bagrin Date: Sat, 9 Aug 2025 11:33:38 +0300 Subject: [PATCH] Added my contribution week_1 to community-contribution --- .../week1_tennis.ipynb | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 week1/community-contributions/week1_tennis.ipynb diff --git a/week1/community-contributions/week1_tennis.ipynb b/week1/community-contributions/week1_tennis.ipynb new file mode 100644 index 0000000..9c36a80 --- /dev/null +++ b/week1/community-contributions/week1_tennis.ipynb @@ -0,0 +1,154 @@ +{ + "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": "markdown", + "id": "0ea775a9-12c7-4a63-a676-d7bd0cdb100c", + "metadata": {}, + "source": [ + "# imports\n", + "import os\n", + "from dotenv import load_dotenv\n", + "from IPython.display import Markdown, display, update_display\n", + "from openai import OpenAI\n", + "import ollama" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4a456906-915a-4bfd-bb9d-57e505c5093f", + "metadata": {}, + "outputs": [], + "source": [ + "# constants\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\n", + "load_dotenv(override=True)\n", + "api_key = os.getenv('OPENAI_API_KEY')\n", + "\n", + "if not api_key:\n", + " print(\"No API key was found!\")\n", + "else:\n", + " print(\"API key found and looks good so far!\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3f0d0137-52b0-47a8-81a8-11a90a010798", + "metadata": {}, + "outputs": [], + "source": [ + "# here is the question\n", + "question = \"\"\"\n", + "Please explain why do tennis players often use topspin on their forehand shots, and what advantages does it provide?\n", + "\"\"\" " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "967aac6b-9f9c-4def-8659-d9382b0c59e4", + "metadata": {}, + "outputs": [], + "source": [ + "system_prompt = \"You are a helpful tennis coach who answers questions about tennis rules, techniques, strategies, training, and equipment.\"\n", + "user_prompt = \"Please give a detailed explanation to the following question: \" + question" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7936b5af-e912-4e0e-b43e-87673c4857cf", + "metadata": {}, + "outputs": [], + "source": [ + "messages = [\n", + " {\"role\": \"system\", \"content\": system_prompt},\n", + " {\"role\": \"user\", \"content\": user_prompt}\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "60ce7000-a4a5-4cce-a261-e75ef45063b4", + "metadata": {}, + "outputs": [], + "source": [ + "# Get gpt-4o-mini to answer, with streaming\n", + "openai = OpenAI()\n", + "stream = openai.chat.completions.create(model=MODEL_GPT, messages=messages, stream=True)\n", + "response = \"\"\n", + "display_handle = display(Markdown(\"\"), display_id=True)\n", + "for chunk in stream:\n", + " response += chunk.choices[0].delta.content or ''\n", + " response = response.replace(\"```\",\"\").replace(\"markdown\", \"\")\n", + " update_display(Markdown(response), display_id=display_handle.display_id)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8f7c8ea8-4082-4ad0-8751-3301adcf6538", + "metadata": {}, + "outputs": [], + "source": [ + "# Get Llama 3.2 to answer\n", + "response = ollama.chat(model=MODEL_LLAMA, messages=messages)\n", + "result = response['message']['content']\n", + "display(Markdown(result))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "29e9cdd3-5adc-4428-9758-f761dc91783a", + "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 +}