Skip to content
Open
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
236 changes: 206 additions & 30 deletions 1_foundations/1_lab1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -96,9 +96,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Next it's time to load the API keys into environment variables\n",
"# If this returns false, see the next cell!\n",
Expand Down Expand Up @@ -141,14 +152,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OpenAI API Key exists and begins AIzaSyDN\n"
]
}
],
"source": [
"# Check the key - if you're not using OpenAI, check whichever key you're using! Ollama doesn't need a key.\n",
"\n",
"import os\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"openai_api_key = os.getenv('GEMINI_API_KEY')\n",
"\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
Expand All @@ -159,7 +178,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -172,21 +191,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# And now we'll create an instance of the OpenAI class\n",
"# If you're not sure what it means to create an instance of a class - head over to the guides folder (guide 6)!\n",
"# If you get a NameError - head over to the guides folder (guide 6)to learn about NameErrors - always instantly fixable\n",
"# If you're not using OpenAI, you just need to slightly modify this - precise instructions are in the AI APIs guide (guide 9)\n",
"GEMINI_BASE_URL = \"https://generativelanguage.googleapis.com/v1beta/openai/\"\n",
"\n",
"openai = OpenAI()"
"gemini = OpenAI(base_url=GEMINI_BASE_URL,api_key=openai_api_key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -197,17 +217,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 + 2 = 4\n"
]
}
],
"source": [
"# And now call it! Any problems, head to the troubleshooting guide\n",
"# This uses GPT 4.1 nano, the incredibly cheap model\n",
"# The APIs guide (guide 9) has exact instructions for using even cheaper or free alternatives to OpenAI\n",
"# If you get a NameError, head to the guides folder (guide 6) to learn about NameErrors - always instantly fixable\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-nano\",\n",
"model = \"gemini-2.5-flash\"\n",
"response = gemini.chat.completions.create(\n",
" model=model,\n",
" messages=messages\n",
")\n",
"\n",
Expand All @@ -216,26 +245,40 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# And now - let's ask for a question:\n",
"\n",
"question = \"Please propose a hard, challenging question to assess someone's IQ. Respond only with the question.\"\n",
"messages = [{\"role\": \"user\", \"content\": question}]\n"
"messages = [{\"role\": \"user\", \"content\": question}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Given the 3x3 matrix below, where each entry is a number:\n",
"\n",
"[ 3 ] [ 8 ] [ 15 ]\n",
"[ 35 ] [ 48 ] [ 63 ]\n",
"[ 99 ] [ 120 ] [ ? ]\n",
"\n",
"What number replaces the question mark?\n"
]
}
],
"source": [
"# ask it - this uses GPT 4.1 mini, still cheap but more powerful than nano\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
"\n",
"response = gemini.chat.completions.create(\n",
" model= model,\n",
" messages=messages\n",
")\n",
"\n",
Expand All @@ -246,7 +289,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -256,14 +299,77 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Let's analyze the patterns in the matrix:\n",
"\n",
"**Method 1: Row-wise differences**\n",
"\n",
"* **Row 1:**\n",
" * 8 - 3 = 5\n",
" * 15 - 8 = 7\n",
" * The differences are increasing by 2 (5, 7).\n",
"\n",
"* **Row 2:**\n",
" * 48 - 35 = 13\n",
" * 63 - 48 = 15\n",
" * The differences are increasing by 2 (13, 15).\n",
"\n",
"* **Row 3:**\n",
" * 120 - 99 = 21\n",
" * Following the pattern, the next difference should be 21 + 2 = 23.\n",
" * So, the missing number = 120 + 23 = **143**.\n",
"\n",
"**Method 2: Numbers as squares minus one**\n",
"\n",
"Let's look at each number and see if it can be represented as $n^2 - 1$:\n",
"\n",
"* **Row 1:**\n",
" * 3 = $2^2 - 1$\n",
" * 8 = $3^2 - 1$\n",
" * 15 = $4^2 - 1$\n",
" * (The base numbers are 2, 3, 4)\n",
"\n",
"* **Row 2:**\n",
" * 35 = $6^2 - 1$\n",
" * 48 = $7^2 - 1$\n",
" * 63 = $8^2 - 1$\n",
" * (The base numbers are 6, 7, 8)\n",
"\n",
"* **Row 3:**\n",
" * 99 = $10^2 - 1$\n",
" * 120 = $11^2 - 1$\n",
"\n",
"Now, let's observe the sequence of the base numbers (n) from $n^2 - 1$:\n",
"\n",
"* Row 1 bases: 2, 3, 4\n",
"* Row 2 bases: 6, 7, 8\n",
"* Row 3 bases: 10, 11, ?\n",
"\n",
"Notice the relationship between the base numbers in the same column across rows:\n",
"* Column 1: 2, 6, 10 (Increases by 4 each time: 2+4=6, 6+4=10)\n",
"* Column 2: 3, 7, 11 (Increases by 4 each time: 3+4=7, 7+4=11)\n",
"* Column 3: 4, 8, ? (Following the pattern, it should increase by 4: 4+4=8, 8+4=12)\n",
"\n",
"So, the missing base number for the third position in Row 3 is 12.\n",
"Therefore, the missing number is $12^2 - 1 = 144 - 1 = **143**.\n",
"\n",
"Both methods yield the same answer, confirming the pattern.\n",
"\n",
"The number that replaces the question mark is **143**.\n"
]
}
],
"source": [
"# Ask it again\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
"response = gemini.chat.completions.create(\n",
" model=model,\n",
" messages=messages\n",
")\n",
"\n",
Expand All @@ -273,9 +379,77 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/markdown": [
"Let's analyze the patterns in the matrix:\n",
"\n",
"**Method 1: Row-wise differences**\n",
"\n",
"* **Row 1:**\n",
" * 8 - 3 = 5\n",
" * 15 - 8 = 7\n",
" * The differences are increasing by 2 (5, 7).\n",
"\n",
"* **Row 2:**\n",
" * 48 - 35 = 13\n",
" * 63 - 48 = 15\n",
" * The differences are increasing by 2 (13, 15).\n",
"\n",
"* **Row 3:**\n",
" * 120 - 99 = 21\n",
" * Following the pattern, the next difference should be 21 + 2 = 23.\n",
" * So, the missing number = 120 + 23 = **143**.\n",
"\n",
"**Method 2: Numbers as squares minus one**\n",
"\n",
"Let's look at each number and see if it can be represented as $n^2 - 1$:\n",
"\n",
"* **Row 1:**\n",
" * 3 = $2^2 - 1$\n",
" * 8 = $3^2 - 1$\n",
" * 15 = $4^2 - 1$\n",
" * (The base numbers are 2, 3, 4)\n",
"\n",
"* **Row 2:**\n",
" * 35 = $6^2 - 1$\n",
" * 48 = $7^2 - 1$\n",
" * 63 = $8^2 - 1$\n",
" * (The base numbers are 6, 7, 8)\n",
"\n",
"* **Row 3:**\n",
" * 99 = $10^2 - 1$\n",
" * 120 = $11^2 - 1$\n",
"\n",
"Now, let's observe the sequence of the base numbers (n) from $n^2 - 1$:\n",
"\n",
"* Row 1 bases: 2, 3, 4\n",
"* Row 2 bases: 6, 7, 8\n",
"* Row 3 bases: 10, 11, ?\n",
"\n",
"Notice the relationship between the base numbers in the same column across rows:\n",
"* Column 1: 2, 6, 10 (Increases by 4 each time: 2+4=6, 6+4=10)\n",
"* Column 2: 3, 7, 11 (Increases by 4 each time: 3+4=7, 7+4=11)\n",
"* Column 3: 4, 8, ? (Following the pattern, it should increase by 4: 4+4=8, 8+4=12)\n",
"\n",
"So, the missing base number for the third position in Row 3 is 12.\n",
"Therefore, the missing number is $12^2 - 1 = 144 - 1 = **143**.\n",
"\n",
"Both methods yield the same answer, confirming the pattern.\n",
"\n",
"The number that replaces the question mark is **143**."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import Markdown, display\n",
"\n",
Expand Down Expand Up @@ -328,7 +502,9 @@
"\n",
"# Then make the first call:\n",
"\n",
"response =\n",
"response = {\n",
" \n",
"}\n",
"\n",
"# Then read the business idea:\n",
"\n",
Expand Down Expand Up @@ -359,7 +535,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.9"
"version": "3.12.12"
}
},
"nbformat": 4,
Expand Down