Skip to content
Empty file.
295 changes: 295 additions & 0 deletions week1/community-contributions/Andela_Nikhil/week1 EXERCISE.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
{
"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",
<<<<<<< HEAD
"execution_count": 37,
=======
<<<<<<< Updated upstream
"execution_count": 34,
=======
"execution_count": 37,
>>>>>>> Stashed changes
>>>>>>> week2_exercise_bootcamp_feb2026_nikhilsuresh
"id": "c1070317-3ed9-4659-abe3-828943230e03",
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'scraper'",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mModuleNotFoundError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[37]\u001b[39m\u001b[32m, line 5\u001b[39m\n\u001b[32m 3\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mjson\u001b[39;00m\n\u001b[32m 4\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mdotenv\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m load_dotenv\n\u001b[32m----> \u001b[39m\u001b[32m5\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mscraper\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m fetch_website_contents, fetch_website_links\n\u001b[32m 6\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mopenai\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m OpenAI\n\u001b[32m 7\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mIPython\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mdisplay\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Markdown, display, update_display\n",
"\u001b[31mModuleNotFoundError\u001b[39m: No module named 'scraper'"
]
}
],
"source": [
"# imports\n",
"import os \n",
"import json\n",
"from dotenv import load_dotenv\n",
"from scraper import fetch_website_contents, fetch_website_links\n",
"from openai import OpenAI\n",
"from IPython.display import Markdown, display, update_display\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "59db502b",
"metadata": {},
"source": [
"So let's look at what this week one exercise is.\n",
"\n",
"I want to see your familiarity with OpenAI and Olama to build a tool that takes a technical question,\n",
"\n",
"which you can just type right here and responds with an explanation of that question.\n",
"\n",
"So I've got a little couple of hints down below, but use this as a way to call a model so that you\n",
"\n",
"could retype a question in here, and the model will explain it."
]
},
{
"cell_type": "code",
<<<<<<< HEAD
"execution_count": null,
=======
"execution_count": 36,
>>>>>>> week2_exercise_bootcamp_feb2026_nikhilsuresh
"id": "4a456906-915a-4bfd-bb9d-57e505c5093f",
"metadata": {},
"outputs": [],
"source": [
"# constants\n",
"\n",
"MODEL_GPT = 'openai/gpt-4o-mini'\n",
"MODEL_LLAMA = 'meta-llama/llama-3.2-3b-instruct:free'\n",
"\n"
]
},
{
"cell_type": "code",
<<<<<<< HEAD
"execution_count": null,
=======
"execution_count": 37,
>>>>>>> week2_exercise_bootcamp_feb2026_nikhilsuresh
"id": "a8d7923c-5f28-4c30-8556-342d7c8497c1",
"metadata": {},
"outputs": [],
"source": [
"# set up environment\n",
"load_dotenv(override=True)\n",
"api_key = os.getenv('OPENROUTER_API_KEY')\n",
"\n",
"if api_key and api_key.startswith('sk-proj-') and len(api_key)>10:\n",
" print(\"API key looks good so far\")\n",
"elif api_key and api_key.startswith('sk-or-v1') and len(api_key)>10:\n",
" print(\"You are bang on...dude , because you're using OpenRouter!\\n\") \n",
"else:\n",
" print(\"There might be a problem with your API key? Please visit the troubleshooting notebook!\")"
]
},
{
"cell_type": "code",
<<<<<<< HEAD
"execution_count": null,
=======
"execution_count": 38,
>>>>>>> week2_exercise_bootcamp_feb2026_nikhilsuresh
"id": "63ed7c72",
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"openai = OpenAI(\n",
" base_url=\"https://openrouter.ai/api/v1\",\n",
" api_key=os.getenv('OPENROUTER_API_KEY'), \n",
")\n",
"\n"
]
},
{
"cell_type": "code",
<<<<<<< HEAD
"execution_count": null,
=======
"execution_count": 40,
>>>>>>> week2_exercise_bootcamp_feb2026_nikhilsuresh
"id": "3f0d0137-52b0-47a8-81a8-11a90a010798",
"metadata": {},
"outputs": [],
"source": [
"# here is the question; type over this to ask something new\n",
"\n",
"question = \"\"\"\n",
"You are a technical assistant, and you are extremely good in python. \n",
"You will take a technical question and respond with an explanation of that 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",
"\"\"\"\n",
"messages = [{\"role\": \"user\", \"content\": question}]\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "6f352e71",
"metadata": {},
"source": [
"# Streaming `without` using display and Markdown class from IPython.display "
]
},
{
"cell_type": "code",
<<<<<<< HEAD
"execution_count": null,
=======
"execution_count": 24,
>>>>>>> week2_exercise_bootcamp_feb2026_nikhilsuresh
"id": "60ce7000-a4a5-4cce-a261-e75ef45063b4",
"metadata": {},
"outputs": [],
"source": [
"# Get gpt-4o-mini to answer, with streaming\n",
"# Streaming with out using display and Markdown class from IPython.display \n",
"def call_gpt_4o():\n",
" response = []\n",
" stream = openai.chat.completions.create( model= MODEL_GPT,messages= messages , stream=True) \n",
" for chunk in stream:\n",
" delta = chunk.choices[0].delta\n",
" if delta.content:\n",
" print(delta.content, end=\"\", flush=True)\n",
" response.append(delta.content) \n",
" return response \n",
" \n",
"\n",
"print(call_gpt_4o())"
]
},
{
"cell_type": "markdown",
"id": "0d84c027",
"metadata": {},
"source": [
"# Streaming `WITH` using display and Markdown class from IPython.display "
]
},
{
"cell_type": "code",
<<<<<<< HEAD
"execution_count": null,
=======
"execution_count": 25,
>>>>>>> week2_exercise_bootcamp_feb2026_nikhilsuresh
"id": "93ac3f7e",
"metadata": {},
"outputs": [],
"source": [
"def call_gpt_4o():\n",
" response = ''\n",
" stream = openai.chat.completions.create( model= MODEL_GPT,messages= messages , stream=True) \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",
" \n",
" \n",
"\n",
"print(call_gpt_4o())"
]
},
{
"cell_type": "code",
<<<<<<< HEAD
"execution_count": null,
=======
"execution_count": 41,
>>>>>>> week2_exercise_bootcamp_feb2026_nikhilsuresh
"id": "008fe5dc",
"metadata": {},
"outputs": [],
"source": [
"def call_gpt_4o():\n",
" response = ''\n",
" stream = openai.chat.completions.create( model= MODEL_GPT,messages= messages , stream=True) \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",
" \n",
" \n",
"\n",
"print(call_gpt_4o())"
]
},
{
"cell_type": "code",
<<<<<<< HEAD
"execution_count": null,
=======
"execution_count": 42,
>>>>>>> week2_exercise_bootcamp_feb2026_nikhilsuresh
"id": "8f7c8ea8-4082-4ad0-8751-3301adcf6538",
"metadata": {},
"outputs": [],
"source": [
"# Get Llama 3.2 to answer\n",
"\n",
"def call_llama_3_3():\n",
" # \n",
" try:\n",
" response = ''\n",
" stream = openai.chat.completions.create(model= MODEL_LLAMA,messages= messages , stream=True)\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",
" except Exception as e:\n",
" print(f\"Error: {e}\") \n",
" \n",
" \n",
"print(call_llama_3_3()) "
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv (3.12.12)",
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading