From f14ab7d37bd541eb99ab4bdbf436956fc73ec0cf Mon Sep 17 00:00:00 2001 From: Chimwemwe Kachaje Date: Thu, 16 Oct 2025 11:41:05 +0200 Subject: [PATCH] Added solution for week1 exercise --- .gitignore | 3 + .../kachaje/week1-exercise.ipynb | 119 ++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 week1/community-contributions/kachaje/week1-exercise.ipynb diff --git a/.gitignore b/.gitignore index 334a768..6f98227 100644 --- a/.gitignore +++ b/.gitignore @@ -199,3 +199,6 @@ week4/main.cpp week4/main week4/main.exe week4/main.rs + +local/ + diff --git a/week1/community-contributions/kachaje/week1-exercise.ipynb b/week1/community-contributions/kachaje/week1-exercise.ipynb new file mode 100644 index 0000000..f215868 --- /dev/null +++ b/week1/community-contributions/kachaje/week1-exercise.ipynb @@ -0,0 +1,119 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "834bf7f1", + "metadata": {}, + "source": [ + "Task: build a tool that takes a technical question and responds with an explanation" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ac41ae00", + "metadata": {}, + "outputs": [], + "source": [ + "# imports \n", + "\n", + "from openai import OpenAI" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c9727896", + "metadata": {}, + "outputs": [], + "source": [ + "openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8e2ed70e", + "metadata": {}, + "outputs": [], + "source": [ + "MODEL_LLAMA = 'llama3.2'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ae31ec03", + "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": "918bc133", + "metadata": {}, + "outputs": [], + "source": [ + "system_prompt = \"\"\"\n", + "You are an expert software engineer.\n", + "You are given a technical question and you need to explain what the code does and why.\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c9bbdcb8", + "metadata": {}, + "outputs": [], + "source": [ + "# Get Llama 3.2 to answer\n", + "from IPython.display import Markdown, update_display\n", + "\n", + "\n", + "stream = openai.chat.completions.create(\n", + " model=MODEL_LLAMA,\n", + " messages=[\n", + " {\"role\": \"system\", \"content\": system_prompt},\n", + " {\"role\": \"user\", \"content\": question}\n", + " ],\n", + " stream=True\n", + ")\n", + "response = \"\"\n", + "display_handle = display(Markdown(\"\"), display_id=True)\n", + "for chunk in stream:\n", + " response += chunk.choices[0].delta.content or ''\n", + " update_display(Markdown(response), display_id=display_handle.display_id)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "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.12.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}