{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Welcome to the start of your adventure in Agentic AI" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", "

Are you ready for action??

\n", " Have you completed all the setup steps in the setup folder?
\n", " Have you read the README? Many common questions are answered here!
\n", " Have you checked out the guides in the guides folder?
\n", " Well in that case, you're ready!!\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", "

This code is a live resource - keep an eye out for my updates

\n", " I push updates regularly. As people ask questions or have problems, I add more examples and improve explanations. As a result, the code below might not be identical to the videos, as I've added more steps and better comments. Consider this like an interactive book that accompanies the lectures.

\n", " I try to send emails regularly with important updates related to the course. You can find this in the 'Announcements' section of Udemy in the left sidebar. You can also choose to receive my emails via your Notification Settings in Udemy. I'm respectful of your inbox and always try to add value with my emails!\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### And please do remember to contact me if I can help\n", "\n", "And I love to connect: https://www.linkedin.com/in/eddonner/\n", "\n", "\n", "### New to Notebooks like this one? Head over to the guides folder!\n", "\n", "Just to check you've already added the Python and Jupyter extensions to Cursor, if not already installed:\n", "- Open extensions (View >> extensions)\n", "- Search for python, and when the results show, click on the ms-python one, and Install it if not already installed\n", "- Search for jupyter, and when the results show, click on the Microsoft one, and Install it if not already installed \n", "Then View >> Explorer to bring back the File Explorer.\n", "\n", "And then:\n", "1. Click where it says \"Select Kernel\" near the top right, and select the option called `.venv (Python 3.12.9)` or similar, which should be the first choice or the most prominent choice. You may need to choose \"Python Environments\" first.\n", "2. Click in each \"cell\" below, starting with the cell immediately below this text, and press Shift+Enter to run\n", "3. Enjoy!\n", "\n", "After you click \"Select Kernel\", if there is no option like `.venv (Python 3.12.9)` then please do the following: \n", "1. On Mac: From the Cursor menu, choose Settings >> VS Code Settings (NOTE: be sure to select `VSCode Settings` not `Cursor Settings`); \n", "On Windows PC: From the File menu, choose Preferences >> VS Code Settings(NOTE: be sure to select `VSCode Settings` not `Cursor Settings`) \n", "2. In the Settings search bar, type \"venv\" \n", "3. In the field \"Path to folder with a list of Virtual Environments\" put the path to the project root, like C:\\Users\\username\\projects\\agents (on a Windows PC) or /Users/username/projects/agents (on Mac or Linux). \n", "And then try again.\n", "\n", "Having problems with missing Python versions in that list? Have you ever used Anaconda before? It might be interferring. Quit Cursor, bring up a new command line, and make sure that your Anaconda environment is deactivated: \n", "`conda deactivate` \n", "And if you still have any problems with conda and python versions, it's possible that you will need to run this too: \n", "`conda config --set auto_activate_base false` \n", "and then from within the Agents directory, you should be able to run `uv python list` and see the Python 3.12 version." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# First let's do an import. If you get an Import Error, double check that your Kernel is correct..\n", "\n", "from dotenv import load_dotenv\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "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", "\n", "load_dotenv(override=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Wait, did that just output `False`??\n", "\n", "If so, the most common reason is that you didn't save your `.env` file after adding the key! Be sure to have saved.\n", "\n", "Also, make sure the `.env` file is named precisely `.env` and is in the project root directory (`agents`)\n", "\n", "By the way, your `.env` file should have a stop symbol next to it in Cursor on the left, and that's actually a good thing: that's Cursor saying to you, \"hey, I realize this is a file filled with secret information, and I'm not going to send it to an external AI to suggest changes, because your keys should not be shown to anyone else.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", "

Final reminders

\n", " 1. If you're not confident about Environment Variables or Web Endpoints / APIs, please read Topics 3 and 5 in this technical foundations guide.
\n", " 2. If you want to use AIs other than OpenAI, like Gemini, DeepSeek or Ollama (free), please see the first section in this AI APIs guide.
\n", " 3. If you ever get a Name Error in Python, you can always fix it immediately; see the last section of this Python Foundations guide and follow both tutorials and exercises.
\n", "
\n", "
" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OpenAI API Key exists and begins sk-proj-\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", "\n", "if openai_api_key:\n", " print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n", "else:\n", " print(\"OpenAI API Key not set - please head to the troubleshooting guide in the setup folder\")\n", " \n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# And now - the all important import statement\n", "# If you get an import error - head over to troubleshooting in the Setup folder\n", "\n", "from openai import OpenAI" ] }, { "cell_type": "code", "execution_count": 5, "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", "\n", "openai = OpenAI()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Create a list of messages in the familiar OpenAI format\n", "\n", "messages = [{\"role\": \"user\", \"content\": \"What is 2+2?\"}]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 + 2 equals 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", " messages=messages\n", ")\n", "\n", "print(response.choices[0].message.content)\n" ] }, { "cell_type": "code", "execution_count": 8, "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" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "If two trains start from the same point at the same time, one heading north at 60 miles per hour and the other heading east at 80 miles per hour, how far apart will they be after 3 hours? Now, if a third train starts from the point 150 miles north of the starting point and travels west at 90 miles per hour, how long will it take for this third train to be exactly midway between the first two trains?\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", " messages=messages\n", ")\n", "\n", "question = response.choices[0].message.content\n", "\n", "print(question)\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# form a new messages list\n", "messages = [{\"role\": \"user\", \"content\": question}]\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Let's analyze the problem step-by-step.\n", "\n", "---\n", "\n", "### Part 1: Distance between the first two trains after 3 hours\n", "\n", "- Train 1 travels north at 60 mph.\n", "- Train 2 travels east at 80 mph.\n", "- Both start from the same point at the same time.\n", "\n", "After 3 hours:\n", "\n", "- Distance traveled by Train 1 = \\(60 \\text{ mph} \\times 3 \\text{ hr} = 180 \\text{ miles}\\)\n", "- Distance traveled by Train 2 = \\(80 \\text{ mph} \\times 3 \\text{ hr} = 240 \\text{ miles}\\)\n", "\n", "Since one goes north and the other east, their paths are perpendicular.\n", "\n", "Distance between them after 3 hours \\(d\\) is given by Pythagoras theorem:\n", "\n", "\\[\n", "d = \\sqrt{(180)^2 + (240)^2} = \\sqrt{32400 + 57600} = \\sqrt{90000} = 300 \\text{ miles}\n", "\\]\n", "\n", "---\n", "\n", "### Part 2: The third train problem\n", "\n", "- Third train starts from a point **150 miles north** of the starting point at time 0.\n", "- It travels **west** at 90 mph.\n", "- We want to find when the third train is exactly **midway** between the first two trains.\n", "\n", "---\n", "\n", "### Variables and geometry setup:\n", "\n", "- Let's set the starting point as the origin \\(O = (0,0)\\).\n", "- First train north after time \\(t\\) hours: \\(T_1 = (0, 60t)\\)\n", "- Second train east after time \\(t\\) hours: \\(T_2 = (80t, 0)\\)\n", "- Third train starts at \\(P_0 = (0, 150)\\)\n", "- Third train travels west at 90 mph, so after time \\(t\\), its position:\n", " \\[\n", " T_3 = ( -90 t, 150)\n", " \\]\n", "\n", "---\n", "\n", "### Midpoint of the first two trains at time \\(t\\):\n", "\n", "\\[\n", "M = \\left( \\frac{0 + 80t}{2}, \\frac{60t + 0}{2} \\right) = \\left( 40t, 30t \\right)\n", "\\]\n", "\n", "---\n", "\n", "### Condition:\n", "\n", "Third train's position equals this midpoint:\n", "\n", "\\[\n", "T_3 = M \\implies (-90 t, 150) = (40 t, 30 t)\n", "\\]\n", "\n", "So,\n", "\n", "1. \\( -90 t = 40 t \\implies -90 t - 40 t = 0 \\implies -130 t = 0 \\implies t=0 \\)\n", "2. \\(150 = 30 t \\implies t = \\frac{150}{30} = 5\\)\n", "\n", "There's a contradiction: \\(t = 0\\) from the x-coordinate, \\(t=5\\) from the y-coordinate.\n", "\n", "---\n", "\n", "The third train cannot be exactly at the midpoint (because it moves horizontally at y=150, midpoint moves along a path from (0,0) to (increasing x,y)).\n", "\n", "It seems like the point cannot be exactly the midpoint.\n", "\n", "---\n", "\n", "### Alternative interpretation: \"exactly midway\" means the third train's **distance** to the first two trains is equal (extends midway along the segment)?\n", "\n", "If the third train is \"exactly midway\" meaning:\n", "\n", "\\[\n", "\\text{Distance}(T_3, T_1) = \\text{Distance}(T_3, T_2)\n", "\\]\n", "\n", "Let's check this condition.\n", "\n", "---\n", "\n", "### Distance from third train to Train 1 and Train 2:\n", "\n", "At time \\(t\\):\n", "\n", "- \\(T_1 = (0, 60t)\\)\n", "- \\(T_2 = (80t, 0)\\)\n", "- \\(T_3 = (-90 t, 150)\\)\n", "\n", "Distances:\n", "\n", "\\[\n", "d_1 = |T_3 - T_1| = \\sqrt{(-90 t - 0)^2 + (150 - 60 t)^2} = \\sqrt{( -90 t)^2 + (150 - 60 t)^2 }\n", "\\]\n", "\n", "\\[\n", "d_2 = |T_3 - T_2| = \\sqrt{(-90 t - 80 t)^2 + (150 - 0)^2} = \\sqrt{(-170 t)^2 + 150^2}\n", "\\]\n", "\n", "We want:\n", "\n", "\\[\n", "d_1 = d_2\n", "\\]\n", "\n", "Square both sides:\n", "\n", "\\[\n", "(-90 t)^2 + (150 - 60 t)^2 = (-170 t)^2 + 150^2\n", "\\]\n", "\n", "Calculate:\n", "\n", "\\[\n", "8100 t^2 + (150 - 60 t)^2 = 28900 t^2 + 22500\n", "\\]\n", "\n", "Expand \\((150 - 60 t)^2\\):\n", "\n", "\\[\n", "(150)^2 - 2 \\times 150 \\times 60 t + (60 t)^2 = 22500 - 18000 t + 3600 t^2\n", "\\]\n", "\n", "Now:\n", "\n", "\\[\n", "8100 t^2 + 22500 - 18000 t + 3600 t^2 = 28900 t^2 + 22500\n", "\\]\n", "\n", "Combine terms on the left:\n", "\n", "\\[\n", "(8100 + 3600) t^2 - 18000 t + 22500 = 28900 t^2 + 22500\n", "\\]\n", "\n", "\\[\n", "11700 t^2 - 18000 t + 22500 = 28900 t^2 + 22500\n", "\\]\n", "\n", "Subtract 22500 from both sides:\n", "\n", "\\[\n", "11700 t^2 - 18000 t = 28900 t^2\n", "\\]\n", "\n", "Bring all terms to one side:\n", "\n", "\\[\n", "11700 t^2 - 18000 t - 28900 t^2 = 0\n", "\\]\n", "\n", "\\[\n", "(11700 - 28900) t^2 - 18000 t = 0\n", "\\]\n", "\n", "\\[\n", "-17200 t^2 - 18000 t = 0\n", "\\]\n", "\n", "Divide through by \\(-100\\):\n", "\n", "\\[\n", "172 t^2 + 180 t = 0\n", "\\]\n", "\n", "Factor:\n", "\n", "\\[\n", "t (172 t + 180) = 0\n", "\\]\n", "\n", "Solutions:\n", "\n", "- \\(t = 0\\) (at start)\n", "- \\(172 t + 180 = 0 \\Rightarrow t = -\\frac{180}{172} = -1.0465... \\) (negative time, discard)\n", "\n", "---\n", "\n", "### Conclusion for distances equality:\n", "\n", "No positive time solution other than \\(t=0\\) (start time).\n", "\n", "---\n", "\n", "### Reconsider the interpretation:\n", "\n", "Another approach: maybe \"midway between\" means the third train lies on the line segment joining the first two trains and is equidistant from both.\n", "\n", "Let's consider parametric representation of the segment \\(T_1 T_2\\):\n", "\n", "Point on segment:\n", "\n", "\\[\n", "Q = (1 - \\lambda) T_1 + \\lambda T_2 = ((1-\\lambda) \\times 0 + \\lambda \\times 80 t, (1-\\lambda) \\times 60 t + \\lambda \\times 0) = (80 t \\lambda, 60 t (1 - \\lambda))\n", "\\]\n", "\n", "We want \\(T_3 = Q\\):\n", "\n", "\\[\n", "(-90 t, 150) = (80 t \\lambda, 60 t (1 - \\lambda))\n", "\\]\n", "\n", "Separate components:\n", "\n", "1. \\( -90 t = 80 t \\lambda \\Rightarrow \\lambda = -\\frac{90 t}{80 t} = -\\frac{9}{8} \\)\n", "\n", "2. \\( 150 = 60 t (1 - \\lambda) \\)\n", "\n", "Using \\(\\lambda = -9/8\\), plug in:\n", "\n", "\\[\n", "150 = 60 t (1 - (-9/8)) = 60 t \\left(1 + \\frac{9}{8}\\right) = 60 t \\times \\frac{17}{8} = \\frac{1020}{8} t = 127.5 t\n", "\\]\n", "\n", "So:\n", "\n", "\\[\n", "127.5 t = 150 \\implies t = \\frac{150}{127.5} = 1.1765 \\text{ hours}\n", "\\]\n", "\n", "---\n", "\n", "But \\(\\lambda = -\\frac{9}{8} = -1.125\\), which is NOT between 0 and 1, so point \\(Q\\) is not on the segment between \\(T_1\\) and \\(T_2\\), but rather on the line extending beyond \\(T_1\\) opposite \\(T_2\\).\n", "\n", "So third train is on the *line through \\(T_1 T_2\\)* but not on the segment.\n", "\n", "---\n", "\n", "### Final note:\n", "\n", "The problem as posed: \"How long will it take for the third train to be exactly midway between the first two trains?\"\n", "\n", "- Part 1: the distance after 3 hours is 300 miles.\n", "- Part 2: The third train passes the point ( -90 t, 150 ), which never coincides with the midpoint of \\(T_1\\) and \\(T_2\\) explicitly.\n", "- The third train is closest to the midpoint at some point, but never exactly at it while on the segment.\n", "\n", "---\n", "\n", "**Summary:**\n", "\n", "- After 3 hours, the first two trains are 300 miles apart.\n", "- The third train never lies exactly at the midpoint of the first two trains with respect to position.\n", "- If the third train is considered to be \"midway\" as in having equal distances to the first two trains, this only happens at \\(t=0\\).\n", "- If \"midway\" means on the line extended through the first two trains' positions, it happens at approximately \\(t = 1.18\\) hours, but not on the segment between the first two trains.\n", "\n", "---\n", "\n", "Would you like me to explore a different interpretation or provide an approximate time when the third train is closest to the midpoint?\n" ] } ], "source": [ "# Ask it again\n", "\n", "response = openai.chat.completions.create(\n", " model=\"gpt-4.1-mini\",\n", " messages=messages\n", ")\n", "\n", "answer = response.choices[0].message.content\n", "print(answer)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import Markdown, display\n", "\n", "display(Markdown(answer))\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Congratulations!\n", "\n", "That was a small, simple step in the direction of Agentic AI, with your new environment!\n", "\n", "Next time things get more interesting..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", "

Exercise

\n", " Now try this commercial application:
\n", " First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.
\n", " Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.
\n", " Finally have 3 third LLM call propose the Agentic AI solution.
\n", " We will cover this at up-coming labs, so don't worry if you're unsure.. just give it a try!\n", "
\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# First create the messages:\n", "\n", "messages = [{\"role\": \"user\", \"content\": \"Something here\"}]\n", "\n", "# Then make the first call:\n", "\n", "response =\n", "\n", "# Then read the business idea:\n", "\n", "business_idea = response.\n", "\n", "# And repeat! In the next message, include the business idea within the message" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "agents", "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.11" } }, "nbformat": 4, "nbformat_minor": 2 }