Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions community-contributions/week1exercise.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
104 changes: 0 additions & 104 deletions week1/week1 EXERCISE.ipynb

This file was deleted.

Loading