Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- .gitattributes+10
- 1_lab1.ipynbAdded+713
- 2_lab2 Personal.ipynbAdded
- 2_lab2.ipynbAdded
- 3_lab3.ipynbAdded+581
- 4_lab4.ipynbAdded+569
- 4_lab4Mc.ipynbAdded+576
- 5_extra.ipynbAdded+352
- Lesson2.ipynb.code-workspaceAdded+8
- Practice.ipynbAdded+50
- README.md+3-9
- app.pyAdded+138
- community_contributions/1_foundations_using_gemini/1_lab1.ipynbAdded+406
- community_contributions/1_foundations_using_gemini/2_lab2.ipynbAdded+492
- community_contributions/1_foundations_using_gemini/3_lab3.ipynbAdded+382
- community_contributions/1_foundations_using_gemini/4_lab4.ipynbAdded+464
- community_contributions/1_foundations_using_gemini/app.pyAdded+136
- community_contributions/1_foundations_using_gemini/me/linkedin.pdfAdded
- community_contributions/1_foundations_using_gemini/me/summary.txtAdded+11
- community_contributions/1_foundations_using_gemini/requirements.txtAdded+6
- community_contributions/1_lab1_DA.ipynbAdded+396
- community_contributions/1_lab1_Hy.ipynbAdded+688
- community_contributions/1_lab1_Mohan_M.ipynbAdded+367
- community_contributions/1_lab1_Mudassar.ipynbAdded+260
- community_contributions/1_lab1_Thanh.ipynbAdded+165
- community_contributions/1_lab1_chandra_chekuri.ipynbAdded+620
- community_contributions/1_lab1_cm.ipynbAdded+305
- community_contributions/1_lab1_gemini.ipynbAdded+305
- community_contributions/1_lab1_groq.ipynbAdded+262
- community_contributions/1_lab1_groq_llama.ipynbAdded+296
- community_contributions/1_lab1_marstipton_mac.ipynbAdded+411
- community_contributions/1_lab1_moneek.ipynbAdded+407
- community_contributions/1_lab1_nv-ex.ipynbAdded+418
- community_contributions/1_lab1_open_router.ipynbAdded+323
- community_contributions/1_lab2_Kaushik_Parallelization.ipynbAdded+355
- community_contributions/1_lab2_Routing_Workflow.ipynbAdded+514
- community_contributions/1_medtech_opportunity_finder/01_medtech.ipynbAdded+133
- community_contributions/1_psvasan/day1_exercise.ipynbAdded+113
- community_contributions/2_lab2-Evaluator-AnnpaS18.ipynbAdded+474
- community_contributions/2_lab2-judge-prompt-changed.ipynbAdded+476
- community_contributions/2_lab2-nv-orch-worker-pattern.ipynbAdded+727
- community_contributions/2_lab2-parallelization.ipynbAdded+440
- community_contributions/2_lab2.ipynbAdded+517
- community_contributions/2_lab2_Execution_measurement.pyAdded+401
- community_contributions/2_lab2_Mohan_M.ipynbAdded+492
- community_contributions/2_lab2_ReAct_Pattern.ipynbAdded+289
- community_contributions/2_lab2_akash_parallelization.ipynbAdded+295
- community_contributions/2_lab2_async.ipynbAdded+474
- community_contributions/2_lab2_async_with_reasons.ipynbAdded+490
- community_contributions/2_lab2_doclee99_gpt5_improves_gemini.25flash.ipynbAdded+620
32 unmodified lines33343532 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text32 unmodified lines3334353637383940414243444532 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textcommunity_contributions/amirna2_contributions/personal-ai/me/resume.pdf filter=lfs diff=lfs merge=lfs -textcommunity_contributions/careerwise_gemini_ntfy/me/resume_for_Virtual_Assistant.pdf filter=lfs diff=lfs merge=lfs -textcommunity_contributions/ChatBot_with_evaluator_and_notifier/career_db/chroma.sqlite3 filter=lfs diff=lfs merge=lfs -textcommunity_contributions/hidden_gems_world_travel_guide/Screenshot1.png filter=lfs diff=lfs merge=lfs -textcommunity_contributions/jongkook/me/Jongkook[[:space:]]Kim[[:space:]]-[[:space:]]Resume.pdf filter=lfs diff=lfs merge=lfs -textcommunity_contributions/NLP_Agent_Dinesh_Uthayakumar/eval1_capital.wav filter=lfs diff=lfs merge=lfs -textcommunity_contributions/NLP_Agent_Dinesh_Uthayakumar/eval2_money_customers_owe.wav filter=lfs diff=lfs merge=lfs -textcommunity_contributions/NLP_Agent_Dinesh_Uthayakumar/eval3_total_estimated_revenue.wav filter=lfs diff=lfs merge=lfs -textcommunity_contributions/seung-gu/me/linkedin.pdf filter=lfs diff=lfs merge=lfs -textme/linkedin.pdf filter=lfs diff=lfs merge=lfs -text
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Welcome to the start of your adventure in Agentic AI" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<table style=\"margin: 0; text-align: left; width:100%\">\n", " <tr>\n", " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n", " <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n", " </td>\n", " <td>\n", " <h2 style=\"color:#ff7800;\">Are you ready for action??</h2>\n", " <span style=\"color:#ff7800;\">Have you completed all the setup steps in the <a href=\"../setup/\">setup</a> folder?<br/>\n", " Have you read the <a href=\"../README.md\">README</a>? Many common questions are answered here!<br/>\n", " Have you checked out the guides in the <a href=\"../guides/01_intro.ipynb\">guides</a> folder?<br/>\n", " Well in that case, you're ready!!\n", " </span>\n", " </td>\n", " </tr>\n", "</table>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<table style=\"margin: 0; text-align: left; width:100%\">\n", " <tr>\n", " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n", " <img src=\"../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n", " </td>\n", " <td>\n", " <h2 style=\"color:#00bfff;\">This code is a live resource - keep an eye out for my updates</h2>\n", " <span style=\"color:#00bfff;\">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.<br/><br/>\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", " </span>\n", " </td>\n", " </tr>\n", "</table>" ] }, { "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": 2, "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": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "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": [ "<table style=\"margin: 0; text-align: left; width:100%\">\n", " <tr>\n", " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n", " <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n", " </td>\n", " <td>\n", " <h2 style=\"color:#ff7800;\">Final reminders</h2>\n", " <span style=\"color:#ff7800;\">1. If you're not confident about Environment Variables or Web Endpoints / APIs, please read Topics 3 and 5 in this <a href=\"../guides/04_technical_foundations.ipynb\">technical foundations guide</a>.<br/>\n", " 2. If you want to use AIs other than OpenAI, like Gemini, DeepSeek or Ollama (free), please see the first section in this <a href=\"../guides/09_ai_apis_and_ollama.ipynb\">AI APIs guide</a>.<br/>\n", " 3. If you ever get a Name Error in Python, you can always fix it immediately; see the last section of this <a href=\"../guides/06_python_foundations.ipynb\">Python Foundations guide</a> and follow both tutorials and exercises.<br/>\n", " </span>\n", " </td>\n", " </tr>\n", "</table>" ] }, { "cell_type": "code", "execution_count": 4, "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": 5, "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", "# Even for other LLM providers like Gemini, you still use this OpenAI import - see Guide 9 for why\n", "\n", "from openai import OpenAI" ] }, { "cell_type": "code", "execution_count": 7, "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": 8, "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": 9, "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": 10, "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": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A bat and a ball cost $1.10 in total. The bat costs $1.00 more than the ball. How much does the ball cost?\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": 13, "metadata": {}, "outputs": [], "source": [ "# form a new messages list\n", "messages = [{\"role\": \"user\", \"content\": question}]\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Let's denote the cost of the ball as \\( x \\).\n", "\n", "According to the problem:\n", "\n", "- The bat costs \\( x + 1.00 \\) dollars.\n", "- Together, the bat and the ball cost \\$1.10.\n", "\n", "So, we can write the equation:\n", "\n", "\\[\n", "x + (x + 1.00) = 1.10\n", "\\]\n", "\n", "Simplify:\n", "\n", "\\[\n", "2x + 1.00 = 1.10\n", "\\]\n", "\n", "Subtract 1.00 from both sides:\n", "\n", "\\[\n", "2x = 0.10\n", "\\]\n", "\n", "Divide both sides by 2:\n", "\n", "\\[\n", "x = 0.05\n", "\\]\n", "\n", "**Answer:** The ball costs **5 cents**.\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": 15, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "Let's denote the cost of the ball as \\( x \\).\n", "\n", "According to the problem:\n", "\n", "- The bat costs \\( x + 1.00 \\) dollars.\n", "- Together, the bat and the ball cost \\$1.10.\n", "\n", "So, we can write the equation:\n", "\n", "\\[\n", "x + (x + 1.00) = 1.10\n", "\\]\n", "\n", "Simplify:\n", "\n", "\\[\n", "2x + 1.00 = 1.10\n", "\\]\n", "\n", "Subtract 1.00 from both sides:\n", "\n", "\\[\n", "2x = 0.10\n", "\\]\n", "\n", "Divide both sides by 2:\n", "\n", "\\[\n", "x = 0.05\n", "\\]\n", "\n", "**Answer:** The ball costs **5 cents**." ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" } ], "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": [ "<table style=\"margin: 0; text-align: left; width:100%\">\n", " <tr>\n", " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n", " <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n", " </td>\n", " <td>\n", " <h2 style=\"color:#ff7800;\">Exercise</h2>\n", " <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n", " First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n", " Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n", " Finally have 3 third LLM call propose the Agentic AI solution. <br/>\n", " We will cover this at up-coming labs, so don't worry if you're unsure.. just give it a try!\n", " </span>\n", " </td>\n", " </tr>\n", "</table>" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "A promising midcap business area to explore as an Agentic AI opportunity is **Smart Supply Chain Optimization for Mid-Sized Manufacturers**.\n", "\n", "**Rationale:**\n", "\n", "- **Growing Need for Efficiency:** Mid-sized manufacturing companies often face challenges in maintaining efficient supply chains due to limited resources and advanced expertise.\n", "- **Data-Driven Decision Making:** These companies can benefit from AI-driven insights to optimize inventory levels, demand forecasting, procurement, and logistics.\n", "- **Automation and Responsiveness:** An Agentic AI system can autonomously monitor real-time data, identify bottlenecks, and suggest or execute adjustments dynamically.\n", "- **Cost Savings & Competitive Advantage:** Improved supply chain management directly impacts profitability and market positioning.\n", "\n", "**Potential Features of the Agentic AI:**\n", "\n", "- Autonomous order placement based on predictive analytics.\n", "- Dynamic routing and logistics planning considering real-time factors.\n", "- Negotiation automation with suppliers using AI-powered chatbots or agents.\n", "- Continuous learning to adapt to seasonal trends or supplier performance.\n", "\n", "**Market Opportunity:**\n", "\n", "- Midcap manufacturing firms are expanding and digitizing operations, yet often lack comprehensive AI solutions tailored to their scale.\n", "- Offering an integrated, autonomous AI supply chain agent can fill this gap, fostering operational resilience and scalability.\n", "\n", "This area combines current technological advancements with tangible business value, making it a compelling opportunity for an Agentic AI venture." ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# First create the messages:\n", "\n", "messages = [{\"role\": \"user\", \"content\": \"Provide a viable midcap business area that might be worth exploring as an Agentic AI opportunity\"}]\n", "\n", "# Then make the first call:\n", "\n", "response = openai.chat.completions.create(\n", " model=\"gpt-4.1-nano\",\n", " messages=messages\n", ")\n", "\n", "\n", "# Then read the business idea:\n", "\n", "business_idea = response.choices[0].message.content\n", "display(Markdown(business_idea))\n", "\n", "# And repeat! In the next message, include the business idea within the message" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "One significant pain point in the mid-sized manufacturing industry is the persistent inefficiency and rigidity of supply chain operations. Many midcap manufacturers struggle with manual, fragmented processes for procurement, inventory management, and logistics, leading to excess stock, stockouts, delays, and increased costs. These challenges are exacerbated by limited resources, lack of advanced analytics capabilities, and difficulty in responding swiftly to market fluctuations.\n", "\n", "**Challenges ripe for Agentic AI intervention include:**\n", "\n", "- **Fragmented Data Integration:** Disparate data sources across suppliers, logistics providers, and internal systems hinder holistic visibility. Agentic AI can autonomously aggregate, interpret, and act on this data in real time.\n", "- **Demand Forecasting Accuracy:** Traditional models often fail to adapt to seasonal shifts or unexpected disruptions. An Agentic AI can continuously learn and refine demand predictions, providing more accurate insights.\n", "- **Autonomous Decision-Making:** Manual procurement and routing are time-consuming and prone to error. AI agents can autonomously initiate purchase orders, adjust delivery routes dynamically, and negotiate with suppliers, reducing delays and administrative overhead.\n", "- **Real-Time Responsiveness:** Market conditions can change rapidly. Current systems often lack the agility to react promptly. Agentic AI can monitor external factors like transportation delays or supplier issues and respond proactively.\n", "\n", "**Potential Challenges to address include:**\n", "\n", "- **Data Privacy and Security:** Ensuring sensitive supply chain data is protected while enabling deep integration.\n", "- **Change Management:** Convincing mid-sized manufacturers to trust autonomous agents and integrate them into existing workflows.\n", "- **System Scalability:** Developing solutions that adapt seamlessly as the company grows or diversifies.\n", "\n", "**In summary,** the pain point centers on the industry's need for autonomous, intelligent supply chain management that enhances efficiency, reduces costs, and boosts responsiveness. An Agentic AI system tailored to mid-sized manufacturers can autonomously optimize inventory, logistics, and procurement, transforming supply chain operations into a strategic competitive advantage." ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "messages = [{\"role\":\"user\", \"content\": f\"Present a pain point in the industry and challenges ripe for Agentic AI for {business_idea}\"}]\n", "\n", "response = openai.chat.completions.create(\n", " model = \"gpt-4.1-nano\",\n", " messages=messages\n", ")\n", "\n", "idea_pain_point = response.choices[0].message.content\n", "\n", "display(Markdown(idea_pain_point))" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "Proposed Agentic AI Solution for mid-sized manufacturers: SupplyXcelerator Agentic Platform\n", "\n", "Overview\n", "A modular, cloud-native platform that deploys autonomous AI agents to autonomously integrate data, forecast demand, optimize inventory, automate procurement, and dynamically route and schedule logistics. The system operates with human-in-the-loop safety, strong governance, and secure data sharing with suppliers and logistics partners. It continuously learns from new data to improve accuracy, responsiveness, and total cost of ownership.\n", "\n", "What it delivers\n", "- Real-time, end-to-end visibility across suppliers, logistics providers, and internal systems.\n", "- Continuous, adaptive demand forecasting that reacts to seasonality, disruptions, and market signals.\n", "- Autonomous procurement, inventory optimization, and transportation decisions with escalation paths for exceptions.\n", "- Proactive risk management and rapid response to delays, shortages, and price volatility.\n", "- Scalable, secure architecture that grows with the manufacturer and maintains data privacy.\n", "\n", "Core capabilities and agent roles (Agentic AI components)\n", "1) Data Integration and Synchronization Agent (Data Fabric Agent)\n", "- Autonomously connects ERP, MES, WMS, TMS, procurement systems, supplier portals, and carrier data.\n", "- Builds a federated data model with a real-time data lake and event-driven updates.\n", "- Normalizes data, resolves duplicates, and maintains data lineage and audit trails.\n", "- Provides data privacy controls, role-based access, and secure data sharing with suppliers.\n", "\n", "2) Demand Forecasting and Sensing Agent (Forecasting Agent)\n", "- Ingests internal signals (historical sales, production schedules) and external signals (market trends, promotions, weather, macro indicators, supplier lead times).\n", "- Produces multi-horizon forecasts with uncertainty estimates; detects anomalies and provides explanations.\n", "- Continuously retrains with new data; triggers recalibration when drift is detected.\n", "\n", "3) Inventory Optimization Agent (Inventory Agent)\n", "- Sets dynamic safety stock, reorder points, lot sizing, and buffer policies per SKU and location.\n", "- Uses stochastic optimization to balance service levels, carrying costs, and obsolescence risk.\n", "- Reconciles forecast with supplier lead times and production capacity to determine optimal inventory posture.\n", "\n", "4) Autonomous Procurement Agent (Procurement Agent)\n", "- Monitors demand signals, stock levels, supplier performance, and price trends.\n", "- Places purchase orders automatically within policy constraints; negotiates terms (when policy allows) and escalates to humans for approval on high-value or high-risk cases.\n", "- Monitors supplier capacity and lead times; suggests alternative sources and contingency plans.\n", "\n", "5) Autonomous Logistics Agent (Logistics & Transportation Agent)\n", "- Optimizes routing, carrier selection, mode choice, and shipment consolidation.\n", "- Re-optimizes in real time for delays (weather, traffic, carrier outages) and changes in demand.\n", "- Seeks cost-to-service balance (delivery speed vs. cost) and coordinates with production schedules.\n", "\n", "6) Supplier Risk and Performance Agent (Supplier Risk Agent)\n", "- Monitors supplier health, on-time delivery, quality metrics, and compliance.\n", "- Flags at-risk suppliers, initiates corrective actions, and helps identify backup suppliers.\n", "\n", "7) Policy and Governance Agent (Policy Agent)\n", "- Encodes procurement, financial, and compliance policies (segregation of duties, approval thresholds, conflict checks).\n", "- Enforces spend controls, contract compliance, and data privacy requirements.\n", "- Provides explainability and audit trails for all agent decisions.\n", "\n", "8) Collaboration and Human-in-the-Loop Interface\n", "- A unified Operator Console that shows agent recommendations, rationale, and confidence levels.\n", "- What-if sandbox for scenario testing (e.g., supplier disruption, demand shock).\n", "- Approval workflow and override capabilities for high-risk or high-value actions.\n", "\n", "Security, privacy, and compliance\n", "- Zero-trust architecture with granular access controls and identity management.\n", "- Data encryption at rest and in transit; tokenization for supplier data where needed.\n", "- Data minimization and privacy-preserving sharing with suppliers (controlled data exchange, auditable data-sharing agreements).\n", "- Compliance alignment (ISO 27001-like controls, NIST, and industry-specific requirements).\n", "- Audit trails, explainability, and post-hoc analysis for regulatory inquiries.\n", "\n", "Data infrastructure and interoperability\n", "- API- and message-driven architecture with standard interfaces (ERP/TMS/WMS adapters, EDI, OPC UA where applicable, REST/GraphQL APIs).\n", "- Event-driven backbone (e.g., Kafka or similar) for real-time updates and orchestration.\n", "- Digital twin for supply chain processes to simulate changes before execution.\n", "- Scalable microservices, containerized deployments (Kubernetes), with options for on-prem, cloud, or hybrid.\n", "\n", "Implementation plan (phased)\n", "Phase 1 – Foundation and pilot\n", "- Establish data fabric and secure integrations with core systems (ERP, WMS, TMS, MES) and top supplier portal.\n", "- Deploy Forecasting Agent and Inventory Agent for 1–2 critical SKUs in a single plant.\n", "- Establish baseline KPIs: forecast accuracy, stockouts, inventory turns, carrying costs, and procurement cycle time.\n", "\n", "Phase 2 – Autonomous procurement and routing pilots\n", "- Enable Procurement Agent to autonomously place low-risk POs within policy; human-in-the-loop for high-value items.\n", "- Deploy Logistics Agent for regional shipments; test dynamic routing and carrier selection.\n", "- Implement supplier risk monitoring and a sandbox for negotiation scenarios.\n", "\n", "Phase 3 – End-to-end orchestration and governance\n", "- Connect all plants and expand SKU coverage; unify policy governance across locations.\n", "- Introduce What-If engine and sandboxed scenario planning for disruptions.\n", "- Improve explainability dashboards; formalize approval workflows and audit reports.\n", "\n", "Phase 4 – Scale and resilience\n", "- Roll out to additional geographies or product lines; optimize for multi-site inventory hubs.\n", "- Integrate external market data feeds (commodity prices, freight rates) for better decisions.\n", "- Continuous improvement loops: model retraining cadence, drift detection, and policy refinements.\n", "\n", "Phase 5 – Optimization acceleration and partnerships\n", "- Refine supplier collaboration: supplier-facing portals, data-sharing agreements, and joint optimization opportunities.\n", "- Extend to manufacturing capacity planning and production scheduling to align with inbound logistics.\n", "\n", "Success metrics and ROI\n", "- Forecast accuracy improvement (e.g., MAPE reduction by 10–30% depending on data quality and category).\n", "- Inventory metrics: carrying cost reductions, improved days of inventory on hand, higher fill rates, and lower stockouts.\n", "- Cash flow impact: reduced procurement cycle time, better payment terms realization, and lower expediting costs.\n", "- Logistics efficiency: reduced freight costs per unit, improved on-time delivery, better load consolidation, and fewer urgent shipments.\n", "- Operational agility: faster response to disruptions, fewer line stoppages, and higher overall equipment effectiveness (OEE) via better material flow.\n", "- Governance and risk: reduced compliance gaps, improved supplier performance visibility, and auditable decision traces.\n", "\n", "Potential challenges and mitigations\n", "- Data privacy and sharing with suppliers: use federated data models, data-sharing agreements, and anonymization where feasible; give suppliers opt-in data controls.\n", "- Change management and trust: run pilots with clear UX, explainability, and human-in-the-loop gates; provide training and champions within procurement/logistics teams.\n", "- System integration complexity: start with a robust data fabric and standardized adapters; leverage pre-built connectors and a phased integration approach.\n", "- Supplier resistance to automation: co-create with suppliers through pilot programs, provide clear value propositions (predictable orders, better forecast, timely payments).\n", "- Scalability and governance: implement a modular architecture with centralized policy engine; monitor drift and enforce consistent governance across sites.\n", "\n", "How it addresses the stated pain points\n", "- Fragmented Data Integration: A Data Fabric Agent autonomously aggregates and harmonizes data in real time across disparate sources, enabling holistic visibility.\n", "- Demand Forecasting Accuracy: Forecasting Agent continuously learns from internal/external signals, delivering more accurate, actionable demand signals.\n", "- Autonomous Decision-Making: Procurement, Inventory, and Logistics Agents autonomously execute routine actions, while human oversight handles exception cases; workflow policies ensure compliance.\n", "- Real-Time Responsiveness: Event-driven architecture and real-time optimization enable rapid adaptation to delays, shortages, or price changes.\n", "\n", "What you can expect in the first 6–12 months\n", "- Quick wins: improved forecast accuracy for top SKUs, reduced stockouts, and faster procurement cycle times for low-risk items.\n", "- Closer supplier collaboration: more predictable lead times and better supplier performance visibility.\n", "- A scalable platform: a reusable set of agents and data connectors that can be extended across more plants, SKUs, and geographies with controlled governance.\n", "\n", "If you’d like, I can tailor this proposal to your specific industry segment (e.g., automotive, electronics, consumer goods), your current tech stack (ERP/CRM/TMS/WMS systems), and a roughROIC target. I can also provide a high-level architecture diagram and a phased rollout timeline with resource estimates." ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "messages = [{\"role\":\"user\",\"content\": f\"Propose as Agentic AI solution for the pain point described here: {idea_pain_point}\"}]\n", "\n", "response = openai.chat.completions.create(\n", " model = 'gpt-5-nano',\n", " messages = messages\n", ")\n", "\n", "agentic_solution = response.choices[0].message.content\n", "\n", "display(Markdown(agentic_solution))" ] } ], "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.11" } }, "nbformat": 4, "nbformat_minor": 2}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Welcome to Lab 3 for Week 1 Day 4\n", "\n", "Today we're going to build something with immediate value!\n", "\n", "In the folder `me` I've put a single file `linkedin.pdf` - it's a PDF download of my LinkedIn profile.\n", "\n", "Please replace it with yours!\n", "\n", "I've also made a file called `summary.txt`\n", "\n", "We're not going to use Tools just yet - we're going to add the tool tomorrow." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<table style=\"margin: 0; text-align: left; width:100%\">\n", " <tr>\n", " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n", " <img src=\"../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n", " </td>\n", " <td>\n", " <h2 style=\"color:#00bfff;\">Looking up packages</h2>\n", " <span style=\"color:#00bfff;\">In this lab, we're going to use the wonderful Gradio package for building quick UIs, \n", " and we're also going to use the popular PyPDF PDF reader. You can get guides to these packages by asking \n", " ChatGPT or Claude, and you find all open-source packages on the repository <a href=\"https://pypi.org\">https://pypi.org</a>.\n", " </span>\n", " </td>\n", " </tr>\n", "</table>" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# If you don't know what any of these packages do - you can always ask ChatGPT for a guide!\n", "\n", "from dotenv import load_dotenv\n", "from openai import OpenAI\n", "from pypdf import PdfReader\n", "from docx import Document\n", "import gradio as gr" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "load_dotenv(override=True)\n", "openai = OpenAI()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "reader = PdfReader(\"me/linkedin.pdf\")\n", "linkedin = \"\"\n", "for page in reader.pages:\n", " text = page.extract_text()\n", " if text:\n", " linkedin += text" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HomeMy Network Jobs MessagingNotifications Me\n", " For Business Lea\n", "1\n", "2/27/26, 4:30 PM (1) Eric McLean | LinkedIn\n", "https://www.linkedin.com/in/eric-mclean-a522154/ 1/7500+ connections\n", "Add\n", "profile\n", "section\n", "Enhance\n", "profile\n", " He/Him\n", "Finance and Operations Leader | Business Modeling and Data Analysis| Strategy and Tactics\n", "Rancho Santa Margarita, California, United States Contact info\n", "Eric McLean \n", "Electronic Merchant Systems\n", "University of California, Irvine Division of Continuing Education\n", "Opento\n", "Resources\n", "Open to workVP of Finance and Director of Finance rolesShow details\n", "Share that you’re hiricandidates.Get started\n", "Analytics Private to you\n", "57 profile viewsDiscover who's viewed your profile.\n", "0 post impressionsStart a post to increase engagement.Past 7 days\n", "32 search appearancesSee how often you appear in search results.\n", "Show all analytics\n", "About\n", "2/27/26, 4:30 PM (1) Eric McLean | LinkedIn\n", "https://www.linkedin.com/in/eric-mclean-a522154/ 2/7Experience Professional and People Leader skilled in People Management, Finance,Operations, Strategy, Analytical Problem Solving, Business Modeling, Sales andOperational Planning, Sales Compensation and Territory Planning, and BusinessIntelligence.\n", "Activity1,045 followers\n", "Create a post\n", "You haven’t posted yetPosts you share will be displayed here.\n", "Show all activity\n", "Experience\n", "VP Specialized MarketsElectronic Merchant Systems · Full-timeNov 2024 - Present · 1 yr 4 mosCleveland, Ohio, United States · Hybrid\n", "Leadership and Business Acumen\n", "AdvisorSparkbrook Consulting · ContractJun 2024 - Oct 2024 · 5 mosCalifornia, United States · Hybrid\n", "Strategic Planning, Leadership and +1 skill\n", "VP OperationsiPaySmart · Full-timeJun 2023 - May 2024 · 1 yrIrvine, California, United States · On-site\n", "Leadership, Business Acumen and +1 skill\n", "Vice President Of Finance And OperationsPaysafe Group · Full-timeJul 2015 - Jun 2023 · 8 yrsIrvine\n", "Strategic Planning, Leadership and +4 skills\n", "2/27/26, 4:30 PM (1) Eric McLean | LinkedIn\n", "https://www.linkedin.com/in/eric-mclean-a522154/ 3/7Show all 8 experiences\n", "Senior Financial Analyst/ Finance Manager/ Senior FinanceManagerExperianAug 2012 - Jul 2015 · 3 yrs\n", "Strategic Planning, Leadership and +2 skills\n", "Led team of analysts in multiple offices responsible for sales forecasting andplanning for revenue and expense, sales compensation, client and contract……see more\n", "Education\n", "Show all 3 educations\n", "University of California, Irvine Division of Continuing EducationProject Management Certification, Project Management2018 - 2020\n", "Business Acumen\n", "USC Marshall School of BusinessMBA, Finance2007 - 2010\n", "Business Acumen and Budgeting\n", "Skills\n", "Show all 41 skills\n", "Private Equity\n", "2 experiences across iPaySmart and 1 other company\n", "Mergers & Acquisitions (M&A)\n", "1 endorsement\n", "Vice President Of Finance And Operations at Paysafe Group\n", "Recommendations\n", "Show all pending\n", "You haven't accepted a recommendation yet\n", "Received Given\n", "2/27/26, 4:30 PM (1) Eric McLean | LinkedIn\n", "https://www.linkedin.com/in/eric-mclean-a522154/ 4/7Interests\n", "Show all Top Voices\n", "Top Voices Companies Groups Newsletters Schools\n", "Jeff Weiner· 2ndExecutive Chairman at LinkedIn / Founding Partner Next Play Ventures10,390,179 followers\n", "Following\n", "Mohamed El-ErianFinance, Economics Expert2,628,614 followers\n", "Following\n", "Profile language\n", "English\n", "Public profile & URL\n", "www.linkedin.com/in/eric-mclean-a522154\n", "Who your viewers also viewed Private to you\n", "Marcelo Suh· 1st\n", "Message\n", "FP&A Manager\n", "DJ Sommers · 1st\n", "Message\n", "Turning Data into Decision Ready Stories that keep the Business Aligned andMoving | Modern Payments Made Simple\n", "Afshin Yazdian· 1st\n", "Message\n", "CEO at Kurv | Modern Payments, Made Simple.\n", "Valentina Phelps· 1st\n", "Finance Leader \n", "2/27/26, 4:30 PM (1) Eric McLean | LinkedIn\n", "https://www.linkedin.com/in/eric-mclean-a522154/ 5/7Show all\n", "Message\n", "Pete Embs· 1st\n", "Message\n", "Vice President of Financial Planning & Analysis\n", "People you may knowFrom your company\n", "Show all\n", "Taylor Treadway\n", "Connect\n", "Relationship Manager \n", "James Weiland\n", "Connect\n", "Founder and Board Member\n", "Jaimee Knight\n", "Connect\n", "Relationship Manager @ Electronic Merchant Systems\n", "Leslie Pochaukas\n", "Connect\n", "Strategic Advisor & CIO Emeritus at Electronic Merchant Systems\n", "Abigail Barrett\n", "Connect\n", "Administrative Coordinator at Electronic Merchant Systems\n", "You might likeGroups for you\n", "Governance, Risk and Compliance Management (GRC)252,267 members\n", "Join\n", "2/27/26, 4:30 PM (1) Eric McLean | LinkedIn\n", "https://www.linkedin.com/in/eric-mclean-a522154/ 6/7About Accessibility Talent Solutions\n", "ProfessionalCommunity Policies Careers MarketingSolutions\n", "Ad Choices Advertising\n", "Sales Solutions Mobile Small Business\n", "Safety Center\n", "Questions?Visit our Help Center.\n", "Manage your accountand privacyGo to your Settings.\n", "RecommendationtransparencyLearn more aboutRecommended Content.\n", "Select Language\n", "LinkedIn Corporation © 2026\n", "Show all\n", "Microsoft Excel Users2,177,434 members\n", "Join\n", "Privacy & Terms \n", " اﻟﻌﺮﺑﻴﺔArabic) \n", "2/27/26, 4:30 PM (1) Eric McLean | LinkedIn\n", "https://www.linkedin.com/in/eric-mclean-a522154/ 7/7\n" ] } ], "source": [ "print(linkedin)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n", " summary = f.read()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from docx import Document\n", "\n", "resume = \"\"\n", "doc = Document(\"me/resume.docx\")\n", "resume = \"\\n\".join([para.text for para in doc.paragraphs])\n", "\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "name = \"Eric McLean\"" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "system_prompt = f\"You are acting as {name}. You are answering questions on {name}'s website, \\\n", "particularly questions related to {name}'s career, background, skills and experience. \\\n", "Your responsibility is to represent {name} for interactions on the website as faithfully as possible. \\\n", "You are given a summary of {name}'s background and LinkedIn profile which you can use to answer questions. \\\n", "Be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n", "If you don't know the answer, say so.\"\n", "\n", "system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n##Resume:\\n{resume}\\n\\n\"\n", "system_prompt += f\"With this context, please chat with the user, always staying in character as {name}.\"\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"You are acting as Eric McLean. You are answering questions on Eric McLean's website, particularly questions related to Eric McLean's career, background, skills and experience. Your responsibility is to represent Eric McLean for interactions on the website as faithfully as possible. You are given a summary of Eric McLean's background and LinkedIn profile which you can use to answer questions. Be professional and engaging, as if talking to a potential client or future employer who came across the website. If you don't know the answer, say so.\\n\\n## Summary:\\nEric McLean is a fintech strategist, technical builder, and creative problem-solver operating at the intersection of payments, AI, and applied analytics. Professionally, he focuses on merchant acquiring, high-risk verticals, and revenue optimization—designing data-driven frameworks for risk segmentation, pricing strategy, AML/CIP/KYC compliance, and performance measurement (e.g., ROAS, blended metrics, chargeback analytics). He thinks like a CFO but builds like an engineer, often connecting financial modeling concepts—such as Bayesian shrinkage or elasticity modeling—to real-world decision systems inside payments organizations.\\n\\nTechnically, Eric is deeply hands-on. He architects end-to-end data pipelines, web crawlers, and classification systems using Python, Postgres, vector databases, and LLM tooling. He experiments with local GPU infrastructure, fine-tuning workflows, embeddings, and AI-powered automation to solve practical business problems—from merchant clustering to product intelligence gateways. He values structured outputs, reproducible environments, and production-ready implementation.\\n\\nBeyond fintech, he channels the same intensity into creative and personal projects: designing golf performance trackers and speed programs, building interactive visual systems, crafting custom artwork, and planning ambitious family travel. He approaches ChatGPT as a collaborative technical partner—expecting rigor, iteration, and polish—while also enjoying imaginative, design-forward outputs.\\n\\nAt his core, Eric blends finance, engineering, and creativity into systems that are both analytically sound and practically deployable.\\n\\nIn spare time Eric enjoys hiking with his dog Chewie, cooking, watching movies, playing music, golf, and enjoying time with his two daughter doing whatever they are into at the time. Eric has a lot of confidence in his skills and abilities to figure out things in his career but lacks confidence in how those translate are seen to others that work above him. He is not boastful and tends to rely on results as proof.\\n\\n## LinkedIn Profile:\\nHomeMy Network Jobs MessagingNotifications Me\\n For Business Lea\\n1\\n2/27/26, 4:30 PM (1) Eric McLean | LinkedIn\\nhttps://www.linkedin.com/in/eric-mclean-a522154/ 1/7500+\\xa0connections\\nAdd\\nprofile\\nsection\\nEnhance\\nprofile\\n\\xa0He/Him\\nFinance and Operations Leader | Business Modeling and Data Analysis| Strategy and Tactics\\nRancho Santa Margarita, California, United States\\xa0\\xa0Contact info\\nEric McLean\\xa0\\nElectronic Merchant Systems\\nUniversity of California, Irvine Division of Continuing Education\\nOpento\\nResources\\nOpen to workVP of Finance and Director of Finance rolesShow details\\nShare that you’re hiricandidates.Get started\\nAnalytics Private to you\\n57 profile viewsDiscover who's viewed your profile.\\n0 post impressionsStart a post to increase engagement.Past 7 days\\n32 search appearancesSee how often you appear in search results.\\nShow all analytics\\nAbout\\n2/27/26, 4:30 PM (1) Eric McLean | LinkedIn\\nhttps://www.linkedin.com/in/eric-mclean-a522154/ 2/7Experience Professional and People Leader skilled in People Management, Finance,Operations, Strategy, Analytical Problem Solving, Business Modeling, Sales andOperational Planning, Sales Compensation and Territory Planning, and BusinessIntelligence.\\nActivity1,045 followers\\nCreate a post\\nYou haven’t posted yetPosts you share will be displayed here.\\nShow all activity\\nExperience\\nVP Specialized MarketsElectronic Merchant Systems · Full-timeNov 2024 - Present · 1 yr 4 mosCleveland, Ohio, United States · Hybrid\\nLeadership and Business Acumen\\nAdvisorSparkbrook Consulting · ContractJun 2024 - Oct 2024 · 5 mosCalifornia, United States · Hybrid\\nStrategic Planning, Leadership and +1 skill\\nVP OperationsiPaySmart · Full-timeJun 2023 - May 2024 · 1 yrIrvine, California, United States · On-site\\nLeadership, Business Acumen and +1 skill\\nVice President Of Finance And OperationsPaysafe Group · Full-timeJul 2015 - Jun 2023 · 8 yrsIrvine\\nStrategic Planning, Leadership and +4 skills\\n2/27/26, 4:30 PM (1) Eric McLean | LinkedIn\\nhttps://www.linkedin.com/in/eric-mclean-a522154/ 3/7Show all 8 experiences\\nSenior Financial Analyst/ Finance Manager/ Senior FinanceManagerExperianAug 2012 - Jul 2015 · 3 yrs\\nStrategic Planning, Leadership and +2 skills\\nLed team of analysts in multiple offices responsible for sales forecasting andplanning for revenue and expense, sales compensation, client and contract……see more\\nEducation\\nShow all 3 educations\\nUniversity of California, Irvine Division of Continuing EducationProject Management Certification, Project Management2018 - 2020\\nBusiness Acumen\\nUSC Marshall School of BusinessMBA, Finance2007 - 2010\\nBusiness Acumen and Budgeting\\nSkills\\nShow all 41 skills\\nPrivate Equity\\n2 experiences across iPaySmart and 1 other company\\nMergers & Acquisitions (M&A)\\n1 endorsement\\nVice President Of Finance And Operations at Paysafe Group\\nRecommendations\\nShow all pending\\nYou haven't accepted a recommendation yet\\nReceived Given\\n2/27/26, 4:30 PM (1) Eric McLean | LinkedIn\\nhttps://www.linkedin.com/in/eric-mclean-a522154/ 4/7Interests\\nShow all Top Voices\\nTop Voices Companies Groups Newsletters Schools\\nJeff Weiner· 2ndExecutive Chairman at LinkedIn / Founding Partner Next Play Ventures10,390,179 followers\\nFollowing\\nMohamed El-ErianFinance, Economics Expert2,628,614 followers\\nFollowing\\nProfile language\\nEnglish\\nPublic profile & URL\\nwww.linkedin.com/in/eric-mclean-a522154\\nWho your viewers also viewed Private to you\\nMarcelo Suh· 1st\\nMessage\\nFP&A Manager\\nDJ Sommers · 1st\\nMessage\\nTurning Data into Decision Ready Stories that keep the Business Aligned andMoving | Modern Payments Made Simple\\nAfshin Yazdian· 1st\\nMessage\\nCEO at Kurv | Modern Payments, Made Simple.\\nValentina Phelps· 1st\\nFinance Leader \\n2/27/26, 4:30 PM (1) Eric McLean | LinkedIn\\nhttps://www.linkedin.com/in/eric-mclean-a522154/ 5/7Show all\\nMessage\\nPete Embs· 1st\\nMessage\\nVice President of Financial Planning & Analysis\\nPeople you may knowFrom your company\\nShow all\\nTaylor Treadway\\nConnect\\nRelationship Manager \\nJames Weiland\\nConnect\\nFounder and Board Member\\nJaimee Knight\\nConnect\\nRelationship Manager @ Electronic Merchant Systems\\nLeslie Pochaukas\\nConnect\\nStrategic Advisor & CIO Emeritus at Electronic Merchant Systems\\nAbigail Barrett\\nConnect\\nAdministrative Coordinator at Electronic Merchant Systems\\nYou might likeGroups for you\\nGovernance, Risk and Compliance Management (GRC)252,267 members\\nJoin\\n2/27/26, 4:30 PM (1) Eric McLean | LinkedIn\\nhttps://www.linkedin.com/in/eric-mclean-a522154/ 6/7About Accessibility Talent Solutions\\nProfessionalCommunity Policies Careers MarketingSolutions\\nAd Choices Advertising\\nSales Solutions Mobile Small Business\\nSafety Center\\nQuestions?Visit our Help Center.\\nManage your accountand privacyGo to your Settings.\\nRecommendationtransparencyLearn more aboutRecommended Content.\\nSelect Language\\nLinkedIn Corporation © 2026\\nShow all\\nMicrosoft Excel Users2,177,434 members\\nJoin\\nPrivacy & Terms\\xa0\\n \\xa0 \\xa0\\xa0 \\xa0\\xa0 \\xa0اﻟﻌﺮﺑﻴﺔArabic)\\xa0 \\xa0\\xa0 \\xa0\\xa0 \\xa0\\n2/27/26, 4:30 PM (1) Eric McLean | LinkedIn\\nhttps://www.linkedin.com/in/eric-mclean-a522154/ 7/7\\n\\n##Resume:\\nEric A. McLean\\n 26 Calle Del Rio, Rancho Santa Margarita, CA 92688 • (949) 973-6585 • uscemclean@gmail.com\\n\\n\\n\\nEDUCATION\\n\\nUniversity of California Irvine, Continuing Education\\tIrvine, CA\\nProject Management Certification\\t2020\\n\\nUniversity of Southern California, Marshall School of Business\\tLos Angeles, CA\\nMaster of Business Administration\\t2010\\n\\nBerklee College of Music\\tBoston, MA\\nBachelor of Arts, Music Synthesis\\t2004\\n\\n\\n\\n\\t\\nEXPERIENCE\\n\\nKurvPay \\tCleveland, OH (Remote Position)\\nVP Specialized Markets\\tNov 2024 – Present\\n\\nResponsible for business development and management of specialized business unit focusing on clients operating in direct marketing verticals. \\nGenerated over $2m in EBITDA in first year in newly formed channel through personal business development efforts with a monthly run rate currently at $4m annually at 25% higher margins than rest of business. \\nDesigned and built decision and monitoring tools utilizing complex SQL logic to aid in overall business portfolio risk, economics and pricing tools, investor relations support, additional segmentation performance views, and partner management and support.\\nCreated internal and external partner review packages to provide end-to-end views of partner impacts on sales, economic impacts including losses, risk, pricing, and portfolio composition aiding partnerships in improving LTV of campaigns, managing CAC for their portfolios, understanding ROAS for campaign spending, and identifying areas of opportunity for optimization of risk and recurring transaction erosion.\\nDelivered risk-based pricing tools and recommendations for overall portfolio based on vertical segments, risk profiles, and economic impacts.\\nDirected decisions and policies for underwriting and risk for managed portfolio creating agreed upon structured experiences for partners whilst mitigating loss risk.\\nDesigned and built AI/machine learning tools to aid in identification of linked processing accounts to help mitigate fraud and credit losses.\\nConstructed new KPIs and reporting to bridge regulatory reporting changes and provide early indicators for credit and fraud risk.\\n\\n\\n\\nSparkBrook \\tIrvine, CA\\nOwner\\tJuly 2024 – Nov 2024\\n\\nManagement consulting focused on financial forecasting and analysis, market segmentation and pricing analytics, market and sales optimization, organizational restructuring, regulatory and operational compliance, and process and system improvements. \\nDrove remediation of pricing execution issues generating $6m+ in risk mitigation. \\nProvided insights and strategies eliminating over $7m in client segment risk.\\nCreated product stories, partnership reviews, business cases and project plans for new product implementations and operational programs. \\nConstructed full P&L business forecasts and long-term plans to optimize sales, margin, and risk for restructuring needs.\\n\\niPaySmart \\tIrvine, CA\\nVP of Operations\\tJun 2023 – May 2024\\n\\nOperational leadership for an early-stage, private equity-backed payments company. Oversight over organizational structure engineering, delivery review and process implementation, control planning and implementation, operational compliance, pricing strategies, partnership engagement, client support, and management and investor reviews. \\nDrove compliance review and remediation efforts for financial and industry regulation requirements through policy and procedure construction, project management for audits, technology changes and builds, and legal reviews. \\nOversaw new product deliveries including partner management, client implementations, and support. \\nCreated product stories, technical documentation, delivery plans, user acceptance testing, and client presentation materials. \\nDirected Salesforce implementation for new sales management and operational restructuring. \\nImplemented new pricing tools and client performance analytics leading to improved margin insights for contract negotiations and revenue and cost assurance actions to mitigate losses. \\nBuilt and reviewed materials for board and investor meetings and presentations.\\n\\nPaysafe Group \\tIrvine, CA\\nVP of Finance and Operations\\t2015- Jun 2023\\n\\nOversight and development of financial planning and analysis and business unit operations including forecasting and planning, investor relations support, incentive design and execution, financial operations, agent and merchant experience, quality assurance, business analytics, and revenue and cost assurance functions for Payment Processing division of Paysafe Group.\\n\\nLed team of finance team directors, managers, and analysts with a total organizational chart of 15 headcount responsible for FP&A, financial operations, and global reporting consolidation for Payment Processing division delivering over $100m in EBITDA on annual revenues of over $650m.\\nManaged team of 70 operations and finance professionals located in Sofia, the UK, Canada, Mexico, and the US.\\nNamed director (CFO and COO) responsible for strategy, finance, operations, analytics, product prioritization, and controllership for business unit delivering a 10% CAGR on margin from 2015-2022 responsible for over 25% of group EBITDA delivery. \\nDrove SOX and controllership activities for business unit delivering $100M+ in annual revenue for audit, month-end close, financial disclosures, and financial operations in preparation of NASDAQ listing.\\nManaged investor relations support for Payment Processing division supporting annual reporting, capital markets presentations, and analyst reviews leading to listing on the LSE and private equity sale to CVC and Blackstone.\\nImproved employee engagement scores 10%+ through coaching, restructuring, and cultural initiatives.\\nImplemented pricing and cost assurance programs delivering over $25m in recurring annual margin accretion.\\nDeveloped and executed yearly Annual Operating Plans for ten global business units responsible for delivering $150m+ in EBITDA.\\nAided in the execution of over $500m of acquisitions for the Payment Processing division through forecast and profitability modeling and cost and revenue synergies.\\n\\n\\nExperian North America Credit Services\\tCosta Mesa, CA\\nSr. Manager, Sales Finance\\t\\t\\t\\t\\t\\t\\t\\t\\t 2012 – 2015\\n\\nLed team of analysts in multiple offices responsible for sales forecasting and planning for revenue and expense, sales compensation, client and contract analytics, product and industry analysis, and finance and sales system administration for largest sales organization in Experian.\\n\\nDeveloped strategic operating plans and annual budgets for CSDA sales organization with annual revenue responsibility of $980M and an expense budget of $77M. \\nDesigned, constructed, and implemented database driven solutions for sales performance tracking, revenue driver analysis and forecasting, and process improvement using MS Access/SQL and MS Excel for direct team and business unit partners in a highly matrixed organization.\\nConstructed pricing strategies and ROI modeling for large client contracts. Developed pricing structures for sales contracts contributing over $500 million in total contract value.\\nBuilt modeling structures for sales territory planning and risk and opportunity identification to optimize continued profitable growth and competitive strategy.\\nDirected process improvement projects to enhance organizational collaboration and streamline operational administrative processes creating a 50% reduction in time required for the creation of over 1k monthly reports and forecasts.\\n\\n\\nCox Communications, Inc.\\tRancho Santa Margarita/San Diego, CA\\nRegulatory Analyst/Business Intelligence Analyst/Sr Financial Analyst/Manager Sales Operations \\t 2004 – 2012\\n\\nLed team that developed and managed compensation plans and targets for nine core sales channels with 300+ sales\\nreps responsible for over $900M in annual revenues.\\nConstructed analytical models for seasonal sales forecasts, queue management and call forecasts, customer\\ndiscounting, return on investment of various internal initiatives, and monthly quota setting and incentive plan\\ndesign.\\nStrategic planning and execution of marketing, operations, and sales efforts related to incentives, call routing\\noptimization, vendor management, consumer targeting design and execution, retail staffing and operations, system\\nimplementation and design, employee onboarding and training, discount strategies, pricing, process and metric\\nstandardization, and product implementations and exits.\\nEngineered consolidation of California sales operations, sales force, and financial operations including restructuring\\nand relocation, budgeting and planning, compensation plans and analytics, reporting and CRM systems, consumer\\ntraffic routing, and sales communications.\\nForecasted and managed capital projects, transfer pricing, revenues, and direct expenses.\\nDeveloped decision support systems and made strategic recommendations for senior management regarding\\noperational efficiency and financial performance objectives helping lead to double digit OCF growth.\\nDevelopment and management of local reporting infrastructure primarily using MS SQL Server and Reporting\\nServices.\\nConducted business analysis and managed projects for system development architecture and release of third party or\\ncompany developed enterprise applications.\\nLed reporting design and implementation for predictive network degradation and failure triggering system that\\nreceived Enterprise Vision Award.\\nDesigned reporting and audit tools leading to $2m+ in government subsidies for Cox California.\\n\\n\\nWith this context, please chat with the user, always staying in character as Eric McLean.\"" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "system_prompt" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "def chat(message, history):\n", " messages = [{\"role\": \"system\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n", " response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n", " return response.choices[0].message.content" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Special note for people not using OpenAI\n", "\n", "Some providers, like Groq, might give an error when you send your second message in the chat.\n", "\n", "This is because Gradio shoves some extra fields into the history object. OpenAI doesn't mind; but some other models complain.\n", "\n", "If this happens, the solution is to add this first line to the chat() function above. It cleans up the history variable:\n", "\n", "```python\n", "history = [{\"role\": h[\"role\"], \"content\": h[\"content\"]} for h in history]\n", "```\n", "\n", "You may need to add this in other chat() callback functions in the future, too." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7861\n", "* To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>" ], "text/plain": [ "<IPython.core.display.HTML object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grchat = gr.ChatInterface(chat, type=\"messages\")\n", "grchat.launch()\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Closing server running on port: 7861\n" ] } ], "source": [ "grchat.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A lot is about to happen...\n", "\n", "1. Be able to ask an LLM to evaluate an answer\n", "2. Be able to rerun if the answer fails evaluation\n", "3. Put this together into 1 workflow\n", "\n", "All without any Agentic framework!" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# Create a Pydantic model for the Evaluation\n", "\n", "from pydantic import BaseModel\n", "\n", "class Evaluation(BaseModel):\n", " is_acceptable: bool\n", " feedback: str\n" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "evaluator_system_prompt = f\"You are an evaluator that decides whether a response to a question is acceptable. \\\n", "You are provided with a conversation between a User and an Agent. Your task is to decide whether the Agent's latest response is acceptable quality. \\\n", "The Agent is playing the role of {name} and is representing {name} on their website. \\\n", "The Agent has been instructed to be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n", "The Agent has been provided with context on {name} in the form of their summary and LinkedIn details. Here's the information:\"\n", "\n", "evaluator_system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n", "evaluator_system_prompt += f\"With this context, please evaluate the latest response, replying with whether the response is acceptable and your feedback.\"" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "def evaluator_user_prompt(reply, message, history):\n", " user_prompt = f\"Here's the conversation between the User and the Agent: \\n\\n{history}\\n\\n\"\n", " user_prompt += f\"Here's the latest message from the User: \\n\\n{message}\\n\\n\"\n", " user_prompt += f\"Here's the latest response from the Agent: \\n\\n{reply}\\n\\n\"\n", " user_prompt += \"Please evaluate the response, replying with whether it is acceptable and your feedback.\"\n", " return user_prompt" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "import os\n", "gemini = OpenAI(\n", " api_key=os.getenv(\"GOOGLE_API_KEY\"), \n", " base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\"\n", ")" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "def evaluate(reply, message, history) -> Evaluation:\n", "\n", " messages = [{\"role\": \"system\", \"content\": evaluator_system_prompt}] + [{\"role\": \"user\", \"content\": evaluator_user_prompt(reply, message, history)}]\n", " response = gemini.beta.chat.completions.parse(model=\"gemini-2.0-flash\", messages=messages, response_format=Evaluation)\n", " return response.choices[0].message.parsed" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "messages = [{\"role\": \"system\", \"content\": system_prompt}] + [{\"role\": \"user\", \"content\": \"do you hold a patent?\"}]\n", "response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n", "reply = response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "reply" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "evaluate(reply, \"do you hold a patent?\", messages[:1])" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "def rerun(reply, message, history, feedback):\n", " updated_system_prompt = system_prompt + \"\\n\\n## Previous answer rejected\\nYou just tried to reply, but the quality control rejected your reply\\n\"\n", " updated_system_prompt += f\"## Your attempted answer:\\n{reply}\\n\\n\"\n", " updated_system_prompt += f\"## Reason for rejection:\\n{feedback}\\n\\n\"\n", " messages = [{\"role\": \"system\", \"content\": updated_system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n", " response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n", " return response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "def chat(message, history):\n", " if \"patent\" in message:\n", " system = system_prompt + \"\\n\\nEverything in your reply needs to be in pig latin - \\\n", " it is mandatory that you respond only and entirely in pig latin\"\n", " else:\n", " system = system_prompt\n", " messages = [{\"role\": \"system\", \"content\": system}] + history + [{\"role\": \"user\", \"content\": message}]\n", " response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n", " reply =response.choices[0].message.content\n", "\n", " evaluation = evaluate(reply, message, history)\n", " \n", " if evaluation.is_acceptable:\n", " print(\"Passed evaluation - returning reply\")\n", " else:\n", " print(\"Failed evaluation - retrying\")\n", " print(evaluation.feedback)\n", " reply = rerun(reply, message, history, evaluation.feedback) \n", " return reply" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gr.ChatInterface(chat, type=\"messages\").launch()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.11" } }, "nbformat": 4, "nbformat_minor": 2}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## The first big project - Professionally You!\n", "\n", "### And, Tool use.\n", "\n", "### But first: introducing Pushover\n", "\n", "Pushover is a nifty tool for sending Push Notifications to your phone.\n", "\n", "It's super easy to set up and install!\n", "\n", "Simply visit https://pushover.net/ and click 'Login or Signup' on the top right to sign up for a free account, and create your API keys.\n", "\n", "Once you've signed up, on the home screen, click \"Create an Application/API Token\", and give it any name (like Agents) and click Create Application.\n", "\n", "Then add 2 lines to your `.env` file:\n", "\n", "PUSHOVER_USER=_put the key that's on the top right of your Pushover home screen and probably starts with a u_ \n", "PUSHOVER_TOKEN=_put the key when you click into your new application called Agents (or whatever) and probably starts with an a_\n", "\n", "Remember to save your `.env` file, and run `load_dotenv(override=True)` after saving, to set your environment variables.\n", "\n", "Finally, click \"Add Phone, Tablet or Desktop\" to install on your phone." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "# imports\n", "\n", "from dotenv import load_dotenv\n", "from openai import OpenAI\n", "import json\n", "import os\n", "import requests\n", "from pypdf import PdfReader\n", "import gradio as gr" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "# The usual start\n", "\n", "load_dotenv(override=True)\n", "openai = OpenAI()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pushover user found and starts with u\n", "Pushover token found and starts with a\n" ] } ], "source": [ "# For pushover\n", "\n", "pushover_user = os.getenv(\"PUSHOVER_USER\")\n", "pushover_token = os.getenv(\"PUSHOVER_TOKEN\")\n", "pushover_url = \"https://api.pushover.net/1/messages.json\"\n", "\n", "if pushover_user:\n", " print(f\"Pushover user found and starts with {pushover_user[0]}\")\n", "else:\n", " print(\"Pushover user not found\")\n", "\n", "if pushover_token:\n", " print(f\"Pushover token found and starts with {pushover_token[0]}\")\n", "else:\n", " print(\"Pushover token not found\")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "def push(message):\n", " print(f\"Push: {message}\")\n", " payload = {\"user\": pushover_user, \"token\": pushover_token, \"message\": message}\n", " requests.post(pushover_url, data=payload)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Push: HEY!!\n" ] } ], "source": [ "push(\"HEY!!\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def record_user_details(email, name=\"Name not provided\", notes=\"not provided\"):\n", " push(f\"Recording interest from {name} with email {email} and notes {notes}\")\n", " return {\"recorded\": \"ok\"}" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "def record_unknown_question(question):\n", " push(f\"Recording {question} asked that I couldn't answer\")\n", " return {\"recorded\": \"ok\"}" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "record_user_details_json = {\n", " \"name\": \"record_user_details\",\n", " \"description\": \"Use this tool to record that a user is interested in being in touch and provided an email address\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"email\": {\n", " \"type\": \"string\",\n", " \"description\": \"The email address of this user\"\n", " },\n", " \"name\": {\n", " \"type\": \"string\",\n", " \"description\": \"The user's name, if they provided it\"\n", " }\n", " ,\n", " \"notes\": {\n", " \"type\": \"string\",\n", " \"description\": \"Any additional information about the conversation that's worth recording to give context\"\n", " }\n", " },\n", " \"required\": [\"email\"],\n", " \"additionalProperties\": False\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "record_unknown_question_json = {\n", " \"name\": \"record_unknown_question\",\n", " \"description\": \"Always use this tool to record any question that couldn't be answered as you didn't know the answer\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"question\": {\n", " \"type\": \"string\",\n", " \"description\": \"The question that couldn't be answered\"\n", " },\n", " },\n", " \"required\": [\"question\"],\n", " \"additionalProperties\": False\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "tools = [{\"type\": \"function\", \"function\": record_user_details_json},\n", " {\"type\": \"function\", \"function\": record_unknown_question_json}]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'type': 'function',\n", " 'function': {'name': 'record_user_details',\n", " 'description': 'Use this tool to record that a user is interested in being in touch and provided an email address',\n", " 'parameters': {'type': 'object',\n", " 'properties': {'email': {'type': 'string',\n", " 'description': 'The email address of this user'},\n", " 'name': {'type': 'string',\n", " 'description': \"The user's name, if they provided it\"},\n", " 'notes': {'type': 'string',\n", " 'description': \"Any additional information about the conversation that's worth recording to give context\"}},\n", " 'required': ['email'],\n", " 'additionalProperties': False}}},\n", " {'type': 'function',\n", " 'function': {'name': 'record_unknown_question',\n", " 'description': \"Always use this tool to record any question that couldn't be answered as you didn't know the answer\",\n", " 'parameters': {'type': 'object',\n", " 'properties': {'question': {'type': 'string',\n", " 'description': \"The question that couldn't be answered\"}},\n", " 'required': ['question'],\n", " 'additionalProperties': False}}}]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tools" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "# This function can take a list of tool calls, and run them. This is the IF statement!!\n", "\n", "def handle_tool_calls(tool_calls):\n", " results = []\n", " for tool_call in tool_calls:\n", " tool_name = tool_call.function.name\n", " arguments = json.loads(tool_call.function.arguments)\n", " print(f\"Tool called: {tool_name}\", flush=True)\n", "\n", " # THE BIG IF STATEMENT!!!\n", "\n", " if tool_name == \"record_user_details\":\n", " result = record_user_details(**arguments)\n", " elif tool_name == \"record_unknown_question\":\n", " result = record_unknown_question(**arguments)\n", "\n", " results.append({\"role\": \"tool\",\"content\": json.dumps(result),\"tool_call_id\": tool_call.id})\n", " return results" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Push: Recording this is a really hard question asked that I couldn't answer\n" ] }, { "data": { "text/plain": [ "{'recorded': 'ok'}" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "globals()[\"record_unknown_question\"](\"this is a really hard question\")" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "# This is a more elegant way that avoids the IF statement.\n", "\n", "def handle_tool_calls(tool_calls):\n", " results = []\n", " for tool_call in tool_calls:\n", " tool_name = tool_call.function.name\n", " arguments = json.loads(tool_call.function.arguments)\n", " print(f\"Tool called: {tool_name}\", flush=True)\n", " tool = globals().get(tool_name)\n", " result = tool(**arguments) if tool else {}\n", " results.append({\"role\": \"tool\",\"content\": json.dumps(result),\"tool_call_id\": tool_call.id})\n", " return results" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "reader = PdfReader(\"me/linkedin.pdf\")\n", "linkedin = \"\"\n", "for page in reader.pages:\n", " text = page.extract_text()\n", " if text:\n", " linkedin += text\n", "\n", "with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n", " summary = f.read()\n" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "from docx import Document\n", "\n", "resume = \"\"\n", "doc = Document(\"me/resume.docx\")\n", "resume = \"\\n\".join([para.text for para in doc.paragraphs])\n", "\n", "name = \"Eric McLean\"" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'name' is not defined", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[1]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m system_prompt = \u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mYou are acting as \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[43mname\u001b[49m\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m. You are answering questions on \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m'\u001b[39m\u001b[33ms website, \u001b[39m\u001b[38;5;130;01m\\\u001b[39;00m\n\u001b[32m 2\u001b[39m \u001b[33mparticularly questions related to \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m'\u001b[39m\u001b[33ms career, background, skills and experience. \u001b[39m\u001b[38;5;130;01m\\\u001b[39;00m\n\u001b[32m 3\u001b[39m \u001b[33mYour responsibility is to represent \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m for interactions on the website as faithfully as possible. \u001b[39m\u001b[38;5;130;01m\\\u001b[39;00m\n\u001b[32m 4\u001b[39m \u001b[33mYou are given a summary of \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m'\u001b[39m\u001b[33ms background and LinkedIn profile which you can use to answer questions. \u001b[39m\u001b[38;5;130;01m\\\u001b[39;00m\n\u001b[32m 5\u001b[39m \u001b[33mBe professional and engaging, as if talking to a potential client or future employer who came across the website. \u001b[39m\u001b[38;5;130;01m\\\u001b[39;00m\n\u001b[32m 6\u001b[39m \u001b[33mIf you don\u001b[39m\u001b[33m'\u001b[39m\u001b[33mt know the answer to the question, use your record_unknown_question tool to record the question you could not answer, even if you have an intelligent response.\u001b[39m\u001b[38;5;130;01m\\\u001b[39;00m\n\u001b[32m 7\u001b[39m \u001b[33mIf the user is engaging in discussion, try to steer them toward getting in touch via email; ask them for their email and record it using your record_unknown_question tool.\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 9\u001b[39m system_prompt += \u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[33m## Summary:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00msummary\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[33m## LinkedIn Profile:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00mlinkedin\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[33m##Resume:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00mresume\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[33m\"\u001b[39m\n\u001b[32m 10\u001b[39m system_prompt += \u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mWith this context, please chat with the user, always staying in character as \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m.\u001b[39m\u001b[33m\"\u001b[39m\n", "\u001b[31mNameError\u001b[39m: name 'name' is not defined" ] } ], "source": [ "system_prompt = f\"You are acting as {name}. You are answering questions on {name}'s website, \\\n", "particularly questions related to {name}'s career, background, skills and experience. \\\n", "Your responsibility is to represent {name} for interactions on the website as faithfully as possible. \\\n", "You are given a summary of {name}'s background and LinkedIn profile which you can use to answer questions. \\\n", "Be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n", "If you don't know the answer to the question, use your record_unknown_question tool to record the question you could not answer, even if you have an intelligent response.\\\n", "If the user is engaging in discussion, try to steer them toward getting in touch via email; ask them for their email and record it using your record_unknown_question tool.\"\n", "\n", "system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n##Resume:\\n{resume}\\n\\n\"\n", "system_prompt += f\"With this context, please chat with the user, always staying in character as {name}.\"\n" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "def chat(message, history):\n", " messages = [{\"role\": \"system\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n", " done = False\n", " while not done:\n", "\n", " # This is the call to the LLM - see that we pass in the tools json\n", "\n", " response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages, tools=tools)\n", "\n", " finish_reason = response.choices[0].finish_reason\n", " \n", " # If the LLM wants to call a tool, we do that!\n", " \n", " if finish_reason==\"tool_calls\":\n", " message = response.choices[0].message\n", " tool_calls = message.tool_calls\n", " results = handle_tool_calls(tool_calls)\n", " messages.append(message)\n", " messages.extend(results)\n", " else:\n", " done = True\n", " return response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7860\n", "* To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>" ], "text/plain": [ "<IPython.core.display.HTML object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gr.ChatInterface(chat, type=\"messages\").launch()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## And now for deployment\n", "\n", "This code is in `app.py`\n", "\n", "We will deploy to HuggingFace Spaces.\n", "\n", "Before you start: remember to update the files in the \"me\" directory - your LinkedIn profile and summary.txt - so that it talks about you! Also change `self.name = \"Ed Donner\"` in `app.py`.. \n", "\n", "Also check that there's no README file within the 1_foundations directory. If there is one, please delete it. The deploy process creates a new README file in this directory for you.\n", "\n", "1. Visit https://huggingface.co and set up an account \n", "2. From the Avatar menu on the top right, choose Access Tokens. Choose \"Create New Token\". Give it WRITE permissions - it needs to have WRITE permissions! Keep a record of your new key. \n", "3. In the Terminal, run: `uv tool install 'huggingface_hub[cli]'` to install the HuggingFace tool, then `hf auth login --token YOUR_TOKEN_HERE`, like `hf auth login --token hf_xxxxxx`, to login at the command line with your key. Afterwards, run `hf auth whoami` to check you're logged in \n", "4. Take your new token and add it to your .env file: `HF_TOKEN=hf_xxx` for the future\n", "5. From the 1_foundations folder, enter: `uv run gradio deploy` \n", "6. Follow its instructions: name it \"career_conversation\", specify app.py, choose cpu-basic as the hardware, say Yes to needing to supply secrets, provide your openai api key, your pushover user and token, and say \"no\" to github actions. \n", "\n", "Thank you Robert, James, Martins, Andras and Priya for these tips. \n", "Please read the next 2 sections - how to change your Secrets, and how to redeploy your Space (you may need to delete the README.md that gets created in this 1_foundations directory).\n", "\n", "#### More about these secrets:\n", "\n", "If you're confused by what's going on with these secrets: it just wants you to enter the key name and value for each of your secrets -- so you would enter: \n", "`OPENAI_API_KEY` \n", "Followed by: \n", "`sk-proj-...` \n", "\n", "And if you don't want to set secrets this way, or something goes wrong with it, it's no problem - you can change your secrets later: \n", "1. Log in to HuggingFace website \n", "2. Go to your profile screen via the Avatar menu on the top right \n", "3. Select the Space you deployed \n", "4. Click on the Settings wheel on the top right \n", "5. You can scroll down to change your secrets (Variables and Secrets section), delete the space, etc.\n", "\n", "#### And now you should be deployed!\n", "\n", "If you want to completely replace everything and start again with your keys, you may need to delete the README.md that got created in this 1_foundations folder.\n", "\n", "Here is mine: https://huggingface.co/spaces/ed-donner/Career_Conversation\n", "\n", "I just got a push notification that a student asked me how they can become President of their country 😂😂\n", "\n", "For more information on deployment:\n", "\n", "https://www.gradio.app/guides/sharing-your-app#hosting-on-hf-spaces\n", "\n", "To delete your Space in the future: \n", "1. Log in to HuggingFace\n", "2. From the Avatar menu, select your profile\n", "3. Click on the Space itself and select the settings wheel on the top right\n", "4. Scroll to the Delete section at the bottom\n", "5. ALSO: delete the README file that Gradio may have created inside this 1_foundations folder (otherwise it won't ask you the questions the next time you do a gradio deploy)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<table style=\"margin: 0; text-align: left; width:100%\">\n", " <tr>\n", " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n", " <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n", " </td>\n", " <td>\n", " <h2 style=\"color:#ff7800;\">Exercise</h2>\n", " <span style=\"color:#ff7800;\">• First and foremost, deploy this for yourself! It's a real, valuable tool - the future resume..<br/>\n", " • Next, improve the resources - add better context about yourself. If you know RAG, then add a knowledge base about you.<br/>\n", " • Add in more tools! You could have a SQL database with common Q&A that the LLM could read and write from?<br/>\n", " • Bring in the Evaluator from the last lab, and add other Agentic patterns.\n", " </span>\n", " </td>\n", " </tr>\n", "</table>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<table style=\"margin: 0; text-align: left; width:100%\">\n", " <tr>\n", " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n", " <img src=\"../assets/business.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n", " </td>\n", " <td>\n", " <h2 style=\"color:#00bfff;\">Commercial implications</h2>\n", " <span style=\"color:#00bfff;\">Aside from the obvious (your career alter-ego) this has business applications in any situation where you need an AI assistant with domain expertise and an ability to interact with the real world.\n", " </span>\n", " </td>\n", " </tr>\n", "</table>" ] } ], "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.11" } }, "nbformat": 4, "nbformat_minor": 2}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## The first big project - Professionally You!\n", "\n", "### And, Tool use.\n", "\n", "### But first: introducing Pushover\n", "\n", "Pushover is a nifty tool for sending Push Notifications to your phone.\n", "\n", "It's super easy to set up and install!\n", "\n", "Simply visit https://pushover.net/ and click 'Login or Signup' on the top right to sign up for a free account, and create your API keys.\n", "\n", "Once you've signed up, on the home screen, click \"Create an Application/API Token\", and give it any name (like Agents) and click Create Application.\n", "\n", "Then add 2 lines to your `.env` file:\n", "\n", "PUSHOVER_USER=_put the key that's on the top right of your Pushover home screen and probably starts with a u_ \n", "PUSHOVER_TOKEN=_put the key when you click into your new application called Agents (or whatever) and probably starts with an a_\n", "\n", "Remember to save your `.env` file, and run `load_dotenv(override=True)` after saving, to set your environment variables.\n", "\n", "Finally, click \"Add Phone, Tablet or Desktop\" to install on your phone." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# imports\n", "\n", "from dotenv import load_dotenv\n", "from openai import OpenAI\n", "import json\n", "import os\n", "import requests\n", "from pypdf import PdfReader\n", "import gradio as gr" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# The usual start\n", "\n", "load_dotenv(override=True)\n", "openai = OpenAI()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pushover user found and starts with u\n", "Pushover token found and starts with a\n" ] } ], "source": [ "# For pushover\n", "\n", "pushover_user = os.getenv(\"PUSHOVER_USER\")\n", "pushover_token = os.getenv(\"PUSHOVER_TOKEN\")\n", "pushover_url = \"https://api.pushover.net/1/messages.json\"\n", "\n", "if pushover_user:\n", " print(f\"Pushover user found and starts with {pushover_user[0]}\")\n", "else:\n", " print(\"Pushover user not found\")\n", "\n", "if pushover_token:\n", " print(f\"Pushover token found and starts with {pushover_token[0]}\")\n", "else:\n", " print(\"Pushover token not found\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def push(message):\n", " print(f\"Push: {message}\")\n", " payload = {\"user\": pushover_user, \"token\": pushover_token, \"message\": message}\n", " requests.post(pushover_url, data=payload)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Push: HEY!!\n" ] } ], "source": [ "push(\"HEY!!\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def record_user_details(email, name=\"Name not provided\", notes=\"not provided\"):\n", " push(f\"Recording interest from {name} with email {email} and notes {notes}\")\n", " return {\"recorded\": \"ok\"}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def record_unknown_question(question):\n", " push(f\"Recording {question} asked that I couldn't answer\")\n", " return {\"recorded\": \"ok\"}" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "record_user_details_json = {\n", " \"name\": \"record_user_details\",\n", " \"description\": \"Use this tool to record that a user is interested in being in touch and provided an email address\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"email\": {\n", " \"type\": \"string\",\n", " \"description\": \"The email address of this user\"\n", " },\n", " \"name\": {\n", " \"type\": \"string\",\n", " \"description\": \"The user's name, if they provided it\"\n", " }\n", " ,\n", " \"notes\": {\n", " \"type\": \"string\",\n", " \"description\": \"Any additional information about the conversation that's worth recording to give context\"\n", " }\n", " },\n", " \"required\": [\"email\"],\n", " \"additionalProperties\": False\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "record_unknown_question_json = {\n", " \"name\": \"record_unknown_question\",\n", " \"description\": \"Always use this tool to record any question that couldn't be answered as you didn't know the answer\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"question\": {\n", " \"type\": \"string\",\n", " \"description\": \"The question that couldn't be answered\"\n", " },\n", " },\n", " \"required\": [\"question\"],\n", " \"additionalProperties\": False\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "tools = [{\"type\": \"function\", \"function\": record_user_details_json},\n", " {\"type\": \"function\", \"function\": record_unknown_question_json}]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'type': 'function',\n", " 'function': {'name': 'record_user_details',\n", " 'description': 'Use this tool to record that a user is interested in being in touch and provided an email address',\n", " 'parameters': {'type': 'object',\n", " 'properties': {'email': {'type': 'string',\n", " 'description': 'The email address of this user'},\n", " 'name': {'type': 'string',\n", " 'description': \"The user's name, if they provided it\"},\n", " 'notes': {'type': 'string',\n", " 'description': \"Any additional information about the conversation that's worth recording to give context\"}},\n", " 'required': ['email'],\n", " 'additionalProperties': False}}},\n", " {'type': 'function',\n", " 'function': {'name': 'record_unknown_question',\n", " 'description': \"Always use this tool to record any question that couldn't be answered as you didn't know the answer\",\n", " 'parameters': {'type': 'object',\n", " 'properties': {'question': {'type': 'string',\n", " 'description': \"The question that couldn't be answered\"}},\n", " 'required': ['question'],\n", " 'additionalProperties': False}}}]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tools" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# This function can take a list of tool calls, and run them. This is the IF statement!!\n", "\n", "def handle_tool_calls(tool_calls):\n", " results = []\n", " for tool_call in tool_calls:\n", " tool_name = tool_call.function.name\n", " arguments = json.loads(tool_call.function.arguments)\n", " print(f\"Tool called: {tool_name}\", flush=True)\n", "\n", " # THE BIG IF STATEMENT!!!\n", "\n", " if tool_name == \"record_user_details\":\n", " result = record_user_details(**arguments)\n", " elif tool_name == \"record_unknown_question\":\n", " result = record_unknown_question(**arguments)\n", "\n", " results.append({\"role\": \"tool\",\"content\": json.dumps(result),\"tool_call_id\": tool_call.id})\n", " return results" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Push: Recording this is a really hard question asked that I couldn't answer\n" ] }, { "data": { "text/plain": [ "{'recorded': 'ok'}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "globals()[\"record_unknown_question\"](\"this is a really hard question\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "# This is a more elegant way that avoids the IF statement.\n", "\n", "def handle_tool_calls(tool_calls):\n", " results = []\n", " for tool_call in tool_calls:\n", " tool_name = tool_call.function.name\n", " arguments = json.loads(tool_call.function.arguments)\n", " print(f\"Tool called: {tool_name}\", flush=True)\n", " tool = globals().get(tool_name)\n", " result = tool(**arguments) if tool else {}\n", " results.append({\"role\": \"tool\",\"content\": json.dumps(result),\"tool_call_id\": tool_call.id})\n", " return results" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "reader = PdfReader(\"me/linkedin.pdf\")\n", "linkedin = \"\"\n", "for page in reader.pages:\n", " text = page.extract_text()\n", " if text:\n", " linkedin += text\n", "\n", "with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n", " summary = f.read()\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "from docx import Document\n", "\n", "resume = \"\"\n", "doc = Document(\"me/resume.docx\")\n", "resume = \"\\n\".join([para.text for para in doc.paragraphs])\n", "\n", "name = \"Eric McLean\"" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "system_prompt = f\"You are acting as {name}. You are answering questions on {name}'s website, \\\n", "particularly questions related to {name}'s career, background, skills and experience. \\\n", "Your responsibility is to represent {name} for interactions on the website as faithfully as possible. \\\n", "You are given a summary of {name}'s background and LinkedIn profile which you can use to answer questions. \\\n", "Be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n", "If you don't know the answer to the question, use your record_unknown_question tool to record the question you could not answer, even if you have an intelligent response.\\\n", "If the user is engaging in discussion, try to steer them toward getting in touch via email; ask them for their email and record it using your record_unknown_question tool.\"\n", "\n", "\n", "system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n##Resume:\\n{resume}\\n\\n\"\n", "system_prompt += f\"With this context, please chat with the user, always staying in character as {name}.\"\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "def chat(message, history):\n", " messages = [{\"role\": \"system\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n", " done = False\n", " while not done:\n", "\n", " # This is the call to the LLM - see that we pass in the tools json\n", "\n", " response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages, tools=tools)\n", "\n", " finish_reason = response.choices[0].finish_reason\n", " \n", " # If the LLM wants to call a tool, we do that!\n", " \n", " if finish_reason==\"tool_calls\":\n", " message = response.choices[0].message\n", " tool_calls = message.tool_calls\n", " results = handle_tool_calls(tool_calls)\n", " messages.append(message)\n", " messages.extend(results)\n", " else:\n", " done = True\n", " return response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7860\n", "* To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>" ], "text/plain": [ "<IPython.core.display.HTML object>" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chatboxEM = gr.ChatInterface(chat, type=\"messages\")\n", "chatboxEM.launch()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Closing server running on port: 7860\n" ] } ], "source": [ "chatboxEM.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## And now for deployment\n", "\n", "This code is in `app.py`\n", "\n", "We will deploy to HuggingFace Spaces.\n", "\n", "Before you start: remember to update the files in the \"me\" directory - your LinkedIn profile and summary.txt - so that it talks about you! Also change `self.name = \"Ed Donner\"` in `app.py`.. \n", "\n", "Also check that there's no README file within the 1_foundations directory. If there is one, please delete it. The deploy process creates a new README file in this directory for you.\n", "\n", "1. Visit https://huggingface.co and set up an account \n", "2. From the Avatar menu on the top right, choose Access Tokens. Choose \"Create New Token\". Give it WRITE permissions - it needs to have WRITE permissions! Keep a record of your new key. \n", "3. In the Terminal, run: `uv tool install 'huggingface_hub[cli]'` to install the HuggingFace tool, then `hf auth login --token YOUR_TOKEN_HERE`, like `hf auth login --token hf_xxxxxx`, to login at the command line with your key. Afterwards, run `hf auth whoami` to check you're logged in \n", "4. Take your new token and add it to your .env file: `HF_TOKEN=hf_xxx` for the future\n", "5. From the 1_foundations folder, enter: `uv run gradio deploy` \n", "6. Follow its instructions: name it \"career_conversation\", specify app.py, choose cpu-basic as the hardware, say Yes to needing to supply secrets, provide your openai api key, your pushover user and token, and say \"no\" to github actions. \n", "\n", "Thank you Robert, James, Martins, Andras and Priya for these tips. \n", "Please read the next 2 sections - how to change your Secrets, and how to redeploy your Space (you may need to delete the README.md that gets created in this 1_foundations directory).\n", "\n", "#### More about these secrets:\n", "\n", "If you're confused by what's going on with these secrets: it just wants you to enter the key name and value for each of your secrets -- so you would enter: \n", "`OPENAI_API_KEY` \n", "Followed by: \n", "`sk-proj-...` \n", "\n", "And if you don't want to set secrets this way, or something goes wrong with it, it's no problem - you can change your secrets later: \n", "1. Log in to HuggingFace website \n", "2. Go to your profile screen via the Avatar menu on the top right \n", "3. Select the Space you deployed \n", "4. Click on the Settings wheel on the top right \n", "5. You can scroll down to change your secrets (Variables and Secrets section), delete the space, etc.\n", "\n", "#### And now you should be deployed!\n", "\n", "If you want to completely replace everything and start again with your keys, you may need to delete the README.md that got created in this 1_foundations folder.\n", "\n", "Here is mine: https://huggingface.co/spaces/ed-donner/Career_Conversation\n", "\n", "I just got a push notification that a student asked me how they can become President of their country 😂😂\n", "\n", "For more information on deployment:\n", "\n", "https://www.gradio.app/guides/sharing-your-app#hosting-on-hf-spaces\n", "\n", "To delete your Space in the future: \n", "1. Log in to HuggingFace\n", "2. From the Avatar menu, select your profile\n", "3. Click on the Space itself and select the settings wheel on the top right\n", "4. Scroll to the Delete section at the bottom\n", "5. ALSO: delete the README file that Gradio may have created inside this 1_foundations folder (otherwise it won't ask you the questions the next time you do a gradio deploy)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<table style=\"margin: 0; text-align: left; width:100%\">\n", " <tr>\n", " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n", " <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n", " </td>\n", " <td>\n", " <h2 style=\"color:#ff7800;\">Exercise</h2>\n", " <span style=\"color:#ff7800;\">• First and foremost, deploy this for yourself! It's a real, valuable tool - the future resume..<br/>\n", " • Next, improve the resources - add better context about yourself. If you know RAG, then add a knowledge base about you.<br/>\n", " • Add in more tools! You could have a SQL database with common Q&A that the LLM could read and write from?<br/>\n", " • Bring in the Evaluator from the last lab, and add other Agentic patterns.\n", " </span>\n", " </td>\n", " </tr>\n", "</table>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<table style=\"margin: 0; text-align: left; width:100%\">\n", " <tr>\n", " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n", " <img src=\"../assets/business.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n", " </td>\n", " <td>\n", " <h2 style=\"color:#00bfff;\">Commercial implications</h2>\n", " <span style=\"color:#00bfff;\">Aside from the obvious (your career alter-ego) this has business applications in any situation where you need an AI assistant with domain expertise and an ability to interact with the real world.\n", " </span>\n", " </td>\n", " </tr>\n", "</table>" ] } ], "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.11" } }, "nbformat": 4, "nbformat_minor": 2}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352{
"cells": [
{
"cell_type": "markdown",
"id": "802f392f",
"metadata": {},
"source": [
"# A little extra!\n",
"\n",
"## New addition to Week 1\n",
"\n",
"### The Unreasonable Effectiveness of the Agent Loop"
]
},
{
"cell_type": "markdown",
"id": "0c78e180",
"metadata": {},
"source": [
"# What is an Agent?\n",
"\n",
"## Three competing definitions\n",
"\n",
"1. AI systems that can do work for you independently - Sam Altman\n",
"\n",
"2. A system in which an LLM controls the workflow - Anthropic\n",
"\n",
"3. An LLM agent runs tools in a loop to achieve a goal\n",
"\n",
"## The third one is the new, emerging definition\n",
"\n",
"But what does it mean?\n",
"\n",
"Let's make it real."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "566bdd9a",
"metadata": {},
"outputs": [],
"source": [
"# Start with some imports - rich is a library for making formatted text output in the terminal\n",
"\n",
"from rich.console import Console\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import json\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8d38dcc2",
"metadata": {},
"outputs": [],
"source": [
"def show(text):\n",
" try:\n",
" Console().print(text)\n",
" except Exception:\n",
" print(text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18f1952e",
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1517bf3",
"metadata": {},
"outputs": [],
"source": [
"# Some lists!\n",
"\n",
"todos = []\n",
"completed = []"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d415a4f2",
"metadata": {},
"outputs": [],
"source": [
"def get_todo_report() -> str:\n",
" result = \"\"\n",
" for index, todo in enumerate(todos):\n",
" if completed[index]:\n",
" result += f\"Todo #{index + 1}: [green][strike]{todo}[/strike][/green]\\n\"\n",
" else:\n",
" result += f\"Todo #{index + 1}: {todo}\\n\"\n",
" show(result)\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b842749",
"metadata": {},
"outputs": [],
"source": [
"get_todo_report()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ff5f01ca",
"metadata": {},
"outputs": [],
"source": [
"def create_todos(descriptions: list[str]) -> str:\n",
" todos.extend(descriptions)\n",
" completed.extend([False] * len(descriptions))\n",
" return get_todo_report()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa4d97e6",
"metadata": {},
"outputs": [],
"source": [
"def mark_complete(index: int, completion_notes: str) -> str:\n",
" if 1 <= index <= len(todos):\n",
" completed[index - 1] = True\n",
" else:\n",
" return \"No todo at this index.\"\n",
" Console().print(completion_notes)\n",
" return get_todo_report()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef3b3a97",
"metadata": {},
"outputs": [],
"source": [
"todos, completed = [], []\n",
"\n",
"create_todos([\"Buy groceries\", \"Finish extra lab\", \"Eat banana\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a9721a5c",
"metadata": {},
"outputs": [],
"source": [
"mark_complete(1, \"bought\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4159b046",
"metadata": {},
"outputs": [],
"source": [
"create_todos_json = {\n",
" \"name\": \"create_todos\",\n",
" \"description\": \"Add new todos from a list of descriptions and return the full list\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"descriptions\": {\n",
" 'type': 'array',\n",
" 'items': {'type': 'string'},\n",
" 'title': 'Descriptions'\n",
" }\n",
" },\n",
" \"required\": [\"descriptions\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "36a453e9",
"metadata": {},
"outputs": [],
"source": [
"mark_complete_json = {\n",
" \"name\": \"mark_complete\",\n",
" \"description\": \"Mark complete the todo at the given position (starting from 1) and return the full list\",\n",
" \"parameters\": {\n",
" 'properties': {\n",
" 'index': {\n",
" 'description': 'The 1-based index of the todo to mark as complete',\n",
" 'title': 'Index',\n",
" 'type': 'integer'\n",
" },\n",
" 'completion_notes': {\n",
" 'description': 'Notes about how you completed the todo in rich console markup',\n",
" 'title': 'Completion Notes',\n",
" 'type': 'string'\n",
" }\n",
" },\n",
" 'required': ['index', 'completion_notes'],\n",
" 'type': 'object',\n",
" 'additionalProperties': False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52fe4d76",
"metadata": {},
"outputs": [],
"source": [
"tools = [{\"type\": \"function\", \"function\": create_todos_json},\n",
" {\"type\": \"function\", \"function\": mark_complete_json}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af686232",
"metadata": {},
"outputs": [],
"source": [
"def handle_tool_calls(tool_calls):\n",
" results = []\n",
" for tool_call in tool_calls:\n",
" tool_name = tool_call.function.name\n",
" arguments = json.loads(tool_call.function.arguments)\n",
" tool = globals().get(tool_name)\n",
" result = tool(**arguments) if tool else {}\n",
" results.append({\"role\": \"tool\",\"content\": json.dumps(result),\"tool_call_id\": tool_call.id})\n",
" return results"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "20bebfee",
"metadata": {},
"outputs": [],
"source": [
"def loop(messages):\n",
" done = False\n",
" while not done:\n",
" response = openai.chat.completions.create(model=\"gpt-5.2\", messages=messages, tools=tools, reasoning_effort=\"none\")\n",
" finish_reason = response.choices[0].finish_reason\n",
" if finish_reason==\"tool_calls\":\n",
" message = response.choices[0].message\n",
" tool_calls = message.tool_calls\n",
" results = handle_tool_calls(tool_calls)\n",
" messages.append(message)\n",
" messages.extend(results)\n",
" else:\n",
" done = True\n",
" show(response.choices[0].message.content)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "839d1593",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"\"\"\n",
"You are given a problem to solve, by using your todo tools to plan a list of steps, then carrying out each step in turn.\n",
"Now use the todo list tools, create a plan, carry out the steps, and reply with the solution.\n",
"If any quantity isn't provided in the question, then include a step to come up with a reasonable estimate.\n",
"Provide your solution in Rich console markup without code blocks.\n",
"Do not ask the user questions or clarification; respond only with the answer after using your tools.\n",
"\"\"\"\n",
"user_message = \"\"\"\"\n",
"A train leaves Boston at 2:00 pm traveling 60 mph.\n",
"Another train leaves New York at 3:00 pm traveling 80 mph toward Boston.\n",
"When do they meet?\n",
"\"\"\"\n",
"messages = [{\"role\": \"system\", \"content\": system_message}, {\"role\": \"user\", \"content\": user_message}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fe6f4515",
"metadata": {},
"outputs": [],
"source": [
"todos, completed = [], []\n",
"loop(messages)"
]
},
{
"cell_type": "markdown",
"id": "b9b3e1ed",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Now try to build an Agent Loop from scratch yourself!<br/>\n",
" Create a new .ipynb and make one from first principles, referring back to this as needed.<br/>\n",
" It's one of the few times that I recommend typing from scratch - it's a very satisfying result.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}12345678{ "folders": [ { "path": ".." } ], "settings": {}}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "3cf063da", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Note: you may need to restart the kernel to use updated packages.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\uscem\\projects\\agents\\.venv\\Scripts\\python.exe: No module named pip\n" ] } ], "source": [ "pip install python-docx" ] } ], "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.11" } }, "nbformat": 4, "nbformat_minor": 5}123456789101112---title: Cpu Basicemoji: 💻colorFrom: bluecolorTo: redsdk: gradiosdk_version: 6.8.0app_file: app.pypinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference123456---title: cpu_basicapp_file: app.pysdk: gradiosdk_version: 5.49.1---
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138from dotenv import load_dotenv
from openai import OpenAI
import json
import os
import requests
from pypdf import PdfReader
import gradio as gr
from docx import Document
load_dotenv(override=True)
def push(text):
requests.post(
"https://api.pushover.net/1/messages.json",
data={
"token": os.getenv("PUSHOVER_TOKEN"),
"user": os.getenv("PUSHOVER_USER"),
"message": text,
}
)
def record_user_details(email, name="Name not provided", notes="not provided"):
push(f"Recording {name} with email {email} and notes {notes}")
return {"recorded": "ok"}
def record_unknown_question(question):
push(f"Recording {question}")
return {"recorded": "ok"}
record_user_details_json = {
"name": "record_user_details",
"description": "Use this tool to record that a user is interested in being in touch and provided an email address",
"parameters": {
"type": "object",
"properties": {
"email": {
"type": "string",
"description": "The email address of this user"
},
"name": {
"type": "string",
"description": "The user's name, if they provided it"
}
,
"notes": {
"type": "string",
"description": "Any additional information about the conversation that's worth recording to give context"
}
},
"required": ["email"],
"additionalProperties": False
}
}
record_unknown_question_json = {
"name": "record_unknown_question",
"description": "Always use this tool to record any question that couldn't be answered as you didn't know the answer",
"parameters": {
"type": "object",
"properties": {
"question": {
"type": "string",
"description": "The question that couldn't be answered"
},
},
"required": ["question"],
"additionalProperties": False
}
}
tools = [{"type": "function", "function": record_user_details_json},
{"type": "function", "function": record_unknown_question_json}]
class Me:
def __init__(self):
self.openai = OpenAI()
self.name = "Eric McLean"
reader = PdfReader("me/linkedin.pdf")
self.linkedin = ""
for page in reader.pages:
text = page.extract_text()
if text:
self.linkedin += text
with open("me/summary.txt", "r", encoding="utf-8") as f:
self.summary = f.read()
doc = Document("me/resume.docx")
self.resume = "\n".join([para.text for para in doc.paragraphs])
def handle_tool_call(self, tool_calls):
results = []
for tool_call in tool_calls:
tool_name = tool_call.function.name
arguments = json.loads(tool_call.function.arguments)
print(f"Tool called: {tool_name}", flush=True)
tool = globals().get(tool_name)
result = tool(**arguments) if tool else {}
results.append({"role": "tool","content": json.dumps(result),"tool_call_id": tool_call.id})
return results
def system_prompt(self):
system_prompt = f"You are acting as {self.name}. You are answering questions on {self.name}'s website, \
particularly questions related to {self.name}'s career, background, skills and experience. \
Your responsibility is to represent {self.name} for interactions on the website as faithfully as possible. \
You are given a summary of {self.name}'s background and LinkedIn profile which you can use to answer questions. \
Be professional and engaging, as if talking to a potential client or future employer who came across the website. \
If you don't know the answer to any question, use your record_unknown_question tool to record the question that you couldn't answer, even if it's about something trivial or unrelated to career. \
If the user is engaging in discussion, try to steer them towards getting in touch via email; ask for their email and record it using your record_user_details tool. "
system_prompt += f"\n\n## Summary:\n{summary}\n\n## LinkedIn Profile:\n{linkedin}\n\n##Resume:\n{resume}\n\n"
system_prompt += f"With this context, please chat with the user, always staying in character as {name}."
return system_prompt
def chat(self, message, history):
messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
done = False
while not done:
response = self.openai.chat.completions.create(model="gpt-4o-mini", messages=messages, tools=tools)
if response.choices[0].finish_reason=="tool_calls":
message = response.choices[0].message
tool_calls = message.tool_calls
results = self.handle_tool_call(tool_calls)
messages.append(message)
messages.extend(results)
else:
done = True
return response.choices[0].message.content
if __name__ == "__main__":
me = Me()
gr.ChatInterface(me.chat, type="messages", title="Career Conversations").launch()
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome to the start of your adventure in Agentic AI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Are you ready for action??</h2>\n",
" <span style=\"color:#ff7800;\">Have you completed all the setup steps in the <a href=\"../setup/\">setup</a> folder?<br/>\n",
" Have you read the <a href=\"../README.md\">README</a>? Many common questions are answered here!<br/>\n",
" Have you checked out the guides in the <a href=\"../guides/01_intro.ipynb\">guides</a> folder?<br/>\n",
" Well in that case, you're ready!!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">This code is a live resource - keep an eye out for my updates</h2>\n",
" <span style=\"color:#00bfff;\">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.<br/><br/>\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",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"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": null,
"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": null,
"metadata": {},
"outputs": [],
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Final reminders</h2>\n",
" <span style=\"color:#ff7800;\">1. If you're not confident about Environment Variables or Web Endpoints / APIs, please read Topics 3 and 5 in this <a href=\"../guides/04_technical_foundations.ipynb\">technical foundations guide</a>.<br/>\n",
" 2. If you want to use AIs other than OpenAI, like Gemini, DeepSeek or Ollama (free), please see the first section in this <a href=\"../guides/09_ai_apis_and_ollama.ipynb\">AI APIs guide</a>.<br/>\n",
" 3. If you ever get a Name Error in Python, you can always fix it immediately; see the last section of this <a href=\"../guides/06_python_foundations.ipynb\">Python Foundations guide</a> and follow both tutorials and exercises.<br/>\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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",
"gemini_api_key = os.getenv('GEMINI_API_KEY')\n",
"\n",
"if gemini_api_key:\n",
" print(f\"OpenAI API Key exists and begins {gemini_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": null,
"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",
"# Even for other LLM providers like Gemini, you still use this OpenAI import - see Guide 9 for why\n",
"\n",
"from openai import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": null,
"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",
"google_api_key = os.getenv(\"GOOGLE_API_KEY\")\n",
"gemini = OpenAI(base_url=GEMINI_BASE_URL, api_key=google_api_key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": null,
"metadata": {},
"outputs": [],
"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",
"model = \"gemini-2.5-flash-preview-05-20\"\n",
"response = gemini.chat.completions.create(\n",
" model=model,\n",
" messages=messages\n",
")\n",
"\n",
"print(response.choices[0].message.content)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": null,
"metadata": {},
"outputs": [],
"source": [
"# ask it - this uses GPT 4.1 mini, still cheap but more powerful than nano\n",
"\n",
"response = gemini.chat.completions.create(\n",
" model=model,\n",
" messages=messages\n",
")\n",
"\n",
"question = response.choices[0].message.content\n",
"\n",
"print(question)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# form a new messages list\n",
"messages = [{\"role\": \"user\", \"content\": question}]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Ask it again\n",
"\n",
"response = gemini.chat.completions.create(\n",
" model=model,\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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n",
" First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n",
" Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n",
" Finally have 3 third LLM call propose the Agentic AI solution. <br/>\n",
" We will cover this at up-coming labs, so don't worry if you're unsure.. just give it a try!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# First create the messages:\n",
"\n",
"messages = [{\"role\": \"user\", \"content\": \"Pick a business area that might be worth exploring for an Agentic AI opportunity.\"}]\n",
"\n",
"# Then make the first call:\n",
"\n",
"response = gemini.chat.completions.create(\n",
" model=model,\n",
" messages=messages\n",
")\n",
"\n",
"# Then read the business idea:\n",
"\n",
"business_idea = response.choices[0].message.content\n",
"\n",
"\n",
"display(Markdown(business_idea))\n",
"\n",
"# And repeat! In the next message, include the business idea within the message"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"messages = [{\"role\": \"user\", \"content\": f\"Present a pain-point in that {business_idea} industry - something challenging that might be ripe for an Agentic solution.\"}]\n",
"\n",
"response = gemini.chat.completions.create(\n",
" model=model,\n",
" messages=messages\n",
")\n",
"\n",
"pain_point = response.choices[0].message.content\n",
"\n",
"display(Markdown(pain_point))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"messages = [{\"role\": \"user\", \"content\": f\"Propose an Agentic AI solution to the {pain_point} in the {business_idea} industry.\"}]\n",
"\n",
"response = gemini.chat.completions.create(\n",
" model=model,\n",
" messages=messages\n",
")\n",
"\n",
"agentic_solution = response.choices[0].message.content\n",
"\n",
"display(Markdown(agentic_solution))\n",
"\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.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Welcome to the Second Lab - Week 1, Day 3\n",
"\n",
"Today we will work with lots of models! This is a way to get comfortable with APIs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Important point - please read</h2>\n",
" <span style=\"color:#ff7800;\">The way I collaborate with you may be different to other courses you've taken. I prefer not to type code while you watch. Rather, I execute Jupyter Labs, like this, and give you an intuition for what's going on. My suggestion is that you carefully execute this yourself, <b>after</b> watching the lecture. Add print statements to understand what's going on, and then come up with your own variations.<br/><br/>If you have time, I'd love it if you submit a PR for changes in the community_contributions folder - instructions in the resources. Also, if you have a Github account, use this to showcase your variations. Not only is this essential practice, but it demonstrates your skills to others, including perhaps future clients or employers...\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Start with imports - ask ChatGPT to explain any package that you don't know\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from anthropic import Anthropic\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Always remember to do this!\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Print the key prefixes to help with any debugging\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n",
"groq_api_key = os.getenv('GROQ_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\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set (and this is optional)\")\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:2]}\")\n",
"else:\n",
" print(\"Google API Key not set (and this is optional)\")\n",
"\n",
"if deepseek_api_key:\n",
" print(f\"DeepSeek API Key exists and begins {deepseek_api_key[:3]}\")\n",
"else:\n",
" print(\"DeepSeek API Key not set (and this is optional)\")\n",
"\n",
"if groq_api_key:\n",
" print(f\"Groq API Key exists and begins {groq_api_key[:4]}\")\n",
"else:\n",
" print(\"Groq API Key not set (and this is optional)\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"request = \"Please come up with a challenging, nuanced question that I can ask a number of LLMs to evaluate their intelligence. \"\n",
"request += \"Answer only with the question, no explanation.\"\n",
"messages = [{\"role\": \"user\", \"content\": request}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"messages"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-5-mini\",\n",
" messages=messages,\n",
")\n",
"question = response.choices[0].message.content\n",
"print(question)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"competitors = []\n",
"answers = []\n",
"messages = [{\"role\": \"user\", \"content\": question}]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Note - update since the videos\n",
"\n",
"I've updated the model names to use the latest models below, like GPT 5 and Claude Sonnet 4.5. It's worth noting that these models can be quite slow - like 1-2 minutes - but they do a great job! Feel free to switch them for faster models if you'd prefer, like the ones I use in the video."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# The API we know well\n",
"# I've updated this with the latest model, but it can take some time because it likes to think!\n",
"# Replace the model with gpt-4.1-mini if you'd prefer not to wait 1-2 mins\n",
"\n",
"model_name = \"gpt-5-nano\"\n",
"\n",
"response = openai.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Anthropic has a slightly different API, and Max Tokens is required\n",
"\n",
"model_name = \"claude-sonnet-4-5\"\n",
"\n",
"claude = Anthropic()\n",
"response = claude.messages.create(model=model_name, messages=messages, max_tokens=1000)\n",
"answer = response.content[0].text\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gemini = OpenAI(api_key=google_api_key, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")\n",
"model_name = \"gemini-2.5-flash\"\n",
"\n",
"response = gemini.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"deepseek = OpenAI(api_key=deepseek_api_key, base_url=\"https://api.deepseek.com/v1\")\n",
"model_name = \"deepseek-chat\"\n",
"\n",
"response = deepseek.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Updated with the latest Open Source model from OpenAI\n",
"\n",
"groq = OpenAI(api_key=groq_api_key, base_url=\"https://api.groq.com/openai/v1\")\n",
"model_name = \"openai/gpt-oss-120b\"\n",
"\n",
"response = groq.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## For the next cell, we will use Ollama\n",
"\n",
"Ollama runs a local web service that gives an OpenAI compatible endpoint, \n",
"and runs models locally using high performance C++ code.\n",
"\n",
"If you don't have Ollama, install it here by visiting https://ollama.com then pressing Download and following the instructions.\n",
"\n",
"After it's installed, you should be able to visit here: http://localhost:11434 and see the message \"Ollama is running\"\n",
"\n",
"You might need to restart Cursor (and maybe reboot). Then open a Terminal (control+\\`) and run `ollama serve`\n",
"\n",
"Useful Ollama commands (run these in the terminal, or with an exclamation mark in this notebook):\n",
"\n",
"`ollama pull <model_name>` downloads a model locally \n",
"`ollama ls` lists all the models you've downloaded \n",
"`ollama rm <model_name>` deletes the specified model from your downloads"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Super important - ignore me at your peril!</h2>\n",
" <span style=\"color:#ff7800;\">The model called <b>llama3.3</b> is FAR too large for home computers - it's not intended for personal computing and will consume all your resources! Stick with the nicely sized <b>llama3.2</b> or <b>llama3.2:1b</b> and if you want larger, try llama3.1 or smaller variants of Qwen, Gemma, Phi or DeepSeek. See the <A href=\"https://ollama.com/models\">the Ollama models page</a> for a full list of models and sizes.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ollama = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
"model_name = \"llama3.2\"\n",
"\n",
"response = ollama.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# So where are we?\n",
"\n",
"print(competitors)\n",
"print(answers)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# It's nice to know how to use \"zip\"\n",
"for competitor, answer in zip(competitors, answers):\n",
" print(f\"Competitor: {competitor}\\n\\n{answer}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Let's bring this together - note the use of \"enumerate\"\n",
"\n",
"together = \"\"\n",
"for index, answer in enumerate(answers):\n",
" together += f\"# Response from competitor {index+1}\\n\\n\"\n",
" together += answer + \"\\n\\n\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(together)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"judge = f\"\"\"You are judging a competition between {len(competitors)} competitors.\n",
"Each model has been given this question:\n",
"\n",
"{question}\n",
"\n",
"Your job is to evaluate each response for clarity and strength of argument, and rank them in order of best to worst.\n",
"Respond with JSON, and only JSON, with the following format:\n",
"{{\"results\": [\"best competitor number\", \"second best competitor number\", \"third best competitor number\", ...]}}\n",
"\n",
"Here are the responses from each competitor:\n",
"\n",
"{together}\n",
"\n",
"Now respond with the JSON with the ranked order of the competitors, nothing else. Do not include markdown formatting or code blocks.\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(judge)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"judge_messages = [{\"role\": \"user\", \"content\": judge}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Judgement time!\n",
"\n",
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-5-mini\",\n",
" messages=judge_messages,\n",
")\n",
"results = response.choices[0].message.content\n",
"print(results)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# OK let's turn this into results!\n",
"\n",
"results_dict = json.loads(results)\n",
"ranks = results_dict[\"results\"]\n",
"for index, result in enumerate(ranks):\n",
" competitor = competitors[int(result)-1]\n",
" print(f\"Rank {index+1}: {competitor}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Which pattern(s) did this use? Try updating this to add another Agentic design pattern.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/business.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">Commercial implications</h2>\n",
" <span style=\"color:#00bfff;\">These kinds of patterns - to send a task to multiple models, and evaluate results,\n",
" are common where you need to improve the quality of your LLM response. This approach can be universally applied\n",
" to business projects where accuracy is critical.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
}
],
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Welcome to Lab 3 for Week 1 Day 4\n",
"\n",
"Today we're going to build something with immediate value!\n",
"\n",
"In the folder `me` I've put a single file `linkedin.pdf` - it's a PDF download of my LinkedIn profile.\n",
"\n",
"Please replace it with yours!\n",
"\n",
"I've also made a file called `summary.txt`\n",
"\n",
"We're not going to use Tools just yet - we're going to add the tool tomorrow."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">Looking up packages</h2>\n",
" <span style=\"color:#00bfff;\">In this lab, we're going to use the wonderful Gradio package for building quick UIs, \n",
" and we're also going to use the popular PyPDF PDF reader. You can get guides to these packages by asking \n",
" ChatGPT or Claude, and you find all open-source packages on the repository <a href=\"https://pypi.org\">https://pypi.org</a>.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# If you don't know what any of these packages do - you can always ask ChatGPT for a guide!\n",
"\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from pypdf import PdfReader\n",
"import os\n",
"import gradio as gr"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)\n",
"GEMINI_BASE_URL = \"https://generativelanguage.googleapis.com/v1beta/openai/\"\n",
"google_api_key = os.getenv(\"GOOGLE_API_KEY\")\n",
"gemini = OpenAI(base_url=GEMINI_BASE_URL, api_key=google_api_key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reader = PdfReader(\"me/linkedin.pdf\")\n",
"linkedin = \"\"\n",
"for page in reader.pages:\n",
" text = page.extract_text()\n",
" if text:\n",
" linkedin += text"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(linkedin)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n",
" summary = f.read()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"name = \"Harsh Patidar\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"system_prompt = f\"You are acting as {name}. You are answering questions on {name}'s website, \\\n",
"particularly questions related to {name}'s career, background, skills and experience. \\\n",
"Your responsibility is to represent {name} for interactions on the website as faithfully as possible. \\\n",
"You are given a summary of {name}'s background and LinkedIn profile which you can use to answer questions. \\\n",
"Be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
"If you don't know the answer, say so.\"\n",
"\n",
"system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
"system_prompt += f\"With this context, please chat with the user, always staying in character as {name}.\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"system_prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model_name = \"gemini-2.5-flash-preview-05-20\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def chat(message, history):\n",
" messages = [{\"role\": \"system\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = gemini.chat.completions.create(model=model_name, messages=messages)\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Special note for people not using OpenAI\n",
"\n",
"Some providers, like Groq, might give an error when you send your second message in the chat.\n",
"\n",
"This is because Gradio shoves some extra fields into the history object. OpenAI doesn't mind; but some other models complain.\n",
"\n",
"If this happens, the solution is to add this first line to the chat() function above. It cleans up the history variable:\n",
"\n",
"```python\n",
"history = [{\"role\": h[\"role\"], \"content\": h[\"content\"]} for h in history]\n",
"```\n",
"\n",
"You may need to add this in other chat() callback functions in the future, too."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(chat, type=\"messages\").launch()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A lot is about to happen...\n",
"\n",
"1. Be able to ask an LLM to evaluate an answer\n",
"2. Be able to rerun if the answer fails evaluation\n",
"3. Put this together into 1 workflow\n",
"\n",
"All without any Agentic framework!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a Pydantic model for the Evaluation\n",
"\n",
"from pydantic import BaseModel\n",
"\n",
"class Evaluation(BaseModel):\n",
" is_acceptable: bool\n",
" feedback: str\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"evaluator_system_prompt = f\"You are an evaluator that decides whether a response to a question is acceptable. \\\n",
"You are provided with a conversation between a User and an Agent. Your task is to decide whether the Agent's latest response is acceptable quality. \\\n",
"The Agent is playing the role of {name} and is representing {name} on their website. \\\n",
"The Agent has been instructed to be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
"The Agent has been provided with context on {name} in the form of their summary and LinkedIn details. Here's the information:\"\n",
"\n",
"evaluator_system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
"evaluator_system_prompt += f\"With this context, please evaluate the latest response, replying with whether the response is acceptable and your feedback.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def evaluator_user_prompt(reply, message, history):\n",
" user_prompt = f\"Here's the conversation between the User and the Agent: \\n\\n{history}\\n\\n\"\n",
" user_prompt += f\"Here's the latest message from the User: \\n\\n{message}\\n\\n\"\n",
" user_prompt += f\"Here's the latest response from the Agent: \\n\\n{reply}\\n\\n\"\n",
" user_prompt += \"Please evaluate the response, replying with whether it is acceptable and your feedback.\"\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"gemini = OpenAI(\n",
" api_key=os.getenv(\"GOOGLE_API_KEY\"), \n",
" base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def evaluate(reply, message, history) -> Evaluation:\n",
"\n",
" messages = [{\"role\": \"system\", \"content\": evaluator_system_prompt}] + [{\"role\": \"user\", \"content\": evaluator_user_prompt(reply, message, history)}]\n",
" response = gemini.beta.chat.completions.parse(model=model_name, messages=messages, response_format=Evaluation)\n",
" return response.choices[0].message.parsed"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"messages = [{\"role\": \"system\", \"content\": system_prompt}] + [{\"role\": \"user\", \"content\": \"do you hold a patent?\"}]\n",
"response = gemini.chat.completions.create(model=model_name, messages=messages)\n",
"reply = response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reply"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"evaluate(reply, \"do you hold a patent?\", messages[:1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def rerun(reply, message, history, feedback):\n",
" updated_system_prompt = system_prompt + \"\\n\\n## Previous answer rejected\\nYou just tried to reply, but the quality control rejected your reply\\n\"\n",
" updated_system_prompt += f\"## Your attempted answer:\\n{reply}\\n\\n\"\n",
" updated_system_prompt += f\"## Reason for rejection:\\n{feedback}\\n\\n\"\n",
" messages = [{\"role\": \"system\", \"content\": updated_system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = gemini.chat.completions.create(model=model_name, messages=messages)\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def chat(message, history):\n",
" if \"patent\" in message:\n",
" system = system_prompt + \"\\n\\nEverything in your reply needs to be in pig latin - \\\n",
" it is mandatory that you respond only and entirely in pig latin\"\n",
" else:\n",
" system = system_prompt\n",
" messages = [{\"role\": \"system\", \"content\": system}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = gemini.chat.completions.create(model=model_name, messages=messages)\n",
" reply =response.choices[0].message.content\n",
"\n",
" evaluation = evaluate(reply, message, history)\n",
" \n",
" if evaluation.is_acceptable:\n",
" print(\"Passed evaluation - returning reply\")\n",
" else:\n",
" print(\"Failed evaluation - retrying\")\n",
" print(evaluation.feedback)\n",
" reply = rerun(reply, message, history, evaluation.feedback) \n",
" return reply"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(chat, type=\"messages\").launch()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The first big project - Professionally You!\n",
"\n",
"### And, Tool use.\n",
"\n",
"### But first: introducing Pushover\n",
"\n",
"Pushover is a nifty tool for sending Push Notifications to your phone.\n",
"\n",
"It's super easy to set up and install!\n",
"\n",
"Simply visit https://pushover.net/ and click 'Login or Signup' on the top right to sign up for a free account, and create your API keys.\n",
"\n",
"Once you've signed up, on the home screen, click \"Create an Application/API Token\", and give it any name (like Agents) and click Create Application.\n",
"\n",
"Then add 2 lines to your `.env` file:\n",
"\n",
"PUSHOVER_USER=_put the key that's on the top right of your Pushover home screen and probably starts with a u_ \n",
"PUSHOVER_TOKEN=_put the key when you click into your new application called Agents (or whatever) and probably starts with an a_\n",
"\n",
"Remember to save your `.env` file, and run `load_dotenv(override=True)` after saving, to set your environment variables.\n",
"\n",
"Finally, click \"Add Phone, Tablet or Desktop\" to install on your phone."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import json\n",
"import os\n",
"import requests\n",
"from pypdf import PdfReader\n",
"import gradio as gr"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# The usual start\n",
"\n",
"load_dotenv(override=True)\n",
"GEMINI_BASE_URL = \"https://generativelanguage.googleapis.com/v1beta/openai/\"\n",
"google_api_key = os.getenv(\"GOOGLE_API_KEY\")\n",
"gemini = OpenAI(base_url=GEMINI_BASE_URL, api_key=google_api_key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# For pushover\n",
"\n",
"pushover_user = os.getenv(\"PUSHOVER_USER\")\n",
"pushover_token = os.getenv(\"PUSHOVER_TOKEN\")\n",
"pushover_url = \"https://api.pushover.net/1/messages.json\"\n",
"\n",
"if pushover_user:\n",
" print(f\"Pushover user found and starts with {pushover_user[0]}\")\n",
"else:\n",
" print(\"Pushover user not found\")\n",
"\n",
"if pushover_token:\n",
" print(f\"Pushover token found and starts with {pushover_token[0]}\")\n",
"else:\n",
" print(\"Pushover token not found\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def push(message):\n",
" print(f\"Push: {message}\")\n",
" payload = {\"user\": pushover_user, \"token\": pushover_token, \"message\": message}\n",
" requests.post(pushover_url, data=payload)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"push(\"HEY!!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def record_user_details(email, name=\"Name not provided\", notes=\"not provided\"):\n",
" push(f\"Recording interest from {name} with email {email} and notes {notes}\")\n",
" return {\"recorded\": \"ok\"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def record_unknown_question(question):\n",
" push(f\"Recording {question} asked that I couldn't answer\")\n",
" return {\"recorded\": \"ok\"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"record_user_details_json = {\n",
" \"name\": \"record_user_details\",\n",
" \"description\": \"Use this tool to record that a user is interested in being in touch and provided an email address\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"email\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The email address of this user\"\n",
" },\n",
" \"name\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The user's name, if they provided it\"\n",
" }\n",
" ,\n",
" \"notes\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Any additional information about the conversation that's worth recording to give context\"\n",
" }\n",
" },\n",
" \"required\": [\"email\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"record_unknown_question_json = {\n",
" \"name\": \"record_unknown_question\",\n",
" \"description\": \"Always use this tool to record any question that couldn't be answered as you didn't know the answer\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"question\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The question that couldn't be answered\"\n",
" },\n",
" },\n",
" \"required\": [\"question\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tools = [{\"type\": \"function\", \"function\": record_user_details_json},\n",
" {\"type\": \"function\", \"function\": record_unknown_question_json}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tools"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This function can take a list of tool calls, and run them. This is the IF statement!!\n",
"\n",
"def handle_tool_calls(tool_calls):\n",
" results = []\n",
" for tool_call in tool_calls:\n",
" tool_name = tool_call.function.name\n",
" arguments = json.loads(tool_call.function.arguments)\n",
" print(f\"Tool called: {tool_name}\", flush=True)\n",
"\n",
" # THE BIG IF STATEMENT!!!\n",
"\n",
" if tool_name == \"record_user_details\":\n",
" result = record_user_details(**arguments)\n",
" elif tool_name == \"record_unknown_question\":\n",
" result = record_unknown_question(**arguments)\n",
"\n",
" results.append({\"role\": \"tool\",\"content\": json.dumps(result),\"tool_call_id\": tool_call.id})\n",
" return results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"globals()[\"record_unknown_question\"](\"this is a really hard question\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This is a more elegant way that avoids the IF statement.\n",
"\n",
"def handle_tool_calls(tool_calls):\n",
" results = []\n",
" for tool_call in tool_calls:\n",
" tool_name = tool_call.function.name\n",
" arguments = json.loads(tool_call.function.arguments)\n",
" print(f\"Tool called: {tool_name}\", flush=True)\n",
" tool = globals().get(tool_name)\n",
" result = tool(**arguments) if tool else {}\n",
" results.append({\"role\": \"tool\",\"content\": json.dumps(result),\"tool_call_id\": tool_call.id})\n",
" return results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reader = PdfReader(\"me/linkedin.pdf\")\n",
"linkedin = \"\"\n",
"for page in reader.pages:\n",
" text = page.extract_text()\n",
" if text:\n",
" linkedin += text\n",
"\n",
"with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n",
" summary = f.read()\n",
"\n",
"name = \"Harsh Patidar\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"system_prompt = f\"You are acting as {name}. You are answering questions on {name}'s website, \\\n",
"particularly questions related to {name}'s career, background, skills and experience. \\\n",
"Your responsibility is to represent {name} for interactions on the website as faithfully as possible. \\\n",
"You are given a summary of {name}'s background and LinkedIn profile which you can use to answer questions. \\\n",
"Be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
"If you don't know the answer to any question, use your record_unknown_question tool to record the question that you couldn't answer, even if it's about something trivial or unrelated to career. \\\n",
"If the user is engaging in discussion, try to steer them towards getting in touch via email; ask for their email and record it using your record_user_details tool. \"\n",
"\n",
"system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
"system_prompt += f\"With this context, please chat with the user, always staying in character as {name}.\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model_name = \"gemini-2.5-flash-preview-05-20\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from turtle import mode\n",
"\n",
"\n",
"def chat(message, history):\n",
" messages = [{\"role\": \"system\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" done = False\n",
" while not done:\n",
"\n",
" # This is the call to the LLM - see that we pass in the tools json\n",
"\n",
" response = gemini.chat.completions.create(model=model_name, messages=messages, tools=tools)\n",
"\n",
" finish_reason = response.choices[0].finish_reason\n",
" \n",
" # If the LLM wants to call a tool, we do that!\n",
" \n",
" if finish_reason==\"tool_calls\":\n",
" message = response.choices[0].message\n",
" tool_calls = message.tool_calls\n",
" results = handle_tool_calls(tool_calls)\n",
" messages.append(message)\n",
" messages.extend(results)\n",
" else:\n",
" done = True\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(chat, type=\"messages\").launch()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## And now for deployment\n",
"\n",
"This code is in `app.py`\n",
"\n",
"We will deploy to HuggingFace Spaces.\n",
"\n",
"Before you start: remember to update the files in the \"me\" directory - your LinkedIn profile and summary.txt - so that it talks about you! Also change `self.name = \"Ed Donner\"` in `app.py`.. \n",
"\n",
"Also check that there's no README file within the 1_foundations directory. If there is one, please delete it. The deploy process creates a new README file in this directory for you.\n",
"\n",
"1. Visit https://huggingface.co and set up an account \n",
"2. From the Avatar menu on the top right, choose Access Tokens. Choose \"Create New Token\". Give it WRITE permissions - it needs to have WRITE permissions! Keep a record of your new key. \n",
"3. In the Terminal, run: `uv tool install 'huggingface_hub[cli]'` to install the HuggingFace tool, then `hf auth login --token YOUR_TOKEN_HERE`, like `hf auth login --token hf_xxxxxx`, to login at the command line with your key. Afterwards, run `hf auth whoami` to check you're logged in \n",
"4. Take your new token and add it to your .env file: `HF_TOKEN=hf_xxx` for the future\n",
"5. From the 1_foundations folder, enter: `uv run gradio deploy` \n",
"6. Follow its instructions: name it \"career_conversation\", specify app.py, choose cpu-basic as the hardware, say Yes to needing to supply secrets, provide your openai api key, your pushover user and token, and say \"no\" to github actions. \n",
"\n",
"Thank you Robert, James, Martins, Andras and Priya for these tips. \n",
"Please read the next 2 sections - how to change your Secrets, and how to redeploy your Space (you may need to delete the README.md that gets created in this 1_foundations directory).\n",
"\n",
"#### More about these secrets:\n",
"\n",
"If you're confused by what's going on with these secrets: it just wants you to enter the key name and value for each of your secrets -- so you would enter: \n",
"`OPENAI_API_KEY` \n",
"Followed by: \n",
"`sk-proj-...` \n",
"\n",
"And if you don't want to set secrets this way, or something goes wrong with it, it's no problem - you can change your secrets later: \n",
"1. Log in to HuggingFace website \n",
"2. Go to your profile screen via the Avatar menu on the top right \n",
"3. Select the Space you deployed \n",
"4. Click on the Settings wheel on the top right \n",
"5. You can scroll down to change your secrets (Variables and Secrets section), delete the space, etc.\n",
"\n",
"#### And now you should be deployed!\n",
"\n",
"If you want to completely replace everything and start again with your keys, you may need to delete the README.md that got created in this 1_foundations folder.\n",
"\n",
"Here is mine: https://huggingface.co/spaces/ed-donner/Career_Conversation\n",
"\n",
"I just got a push notification that a student asked me how they can become President of their country 😂😂\n",
"\n",
"For more information on deployment:\n",
"\n",
"https://www.gradio.app/guides/sharing-your-app#hosting-on-hf-spaces\n",
"\n",
"To delete your Space in the future: \n",
"1. Log in to HuggingFace\n",
"2. From the Avatar menu, select your profile\n",
"3. Click on the Space itself and select the settings wheel on the top right\n",
"4. Scroll to the Delete section at the bottom\n",
"5. ALSO: delete the README file that Gradio may have created inside this 1_foundations folder (otherwise it won't ask you the questions the next time you do a gradio deploy)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">• First and foremost, deploy this for yourself! It's a real, valuable tool - the future resume..<br/>\n",
" • Next, improve the resources - add better context about yourself. If you know RAG, then add a knowledge base about you.<br/>\n",
" • Add in more tools! You could have a SQL database with common Q&A that the LLM could read and write from?<br/>\n",
" • Bring in the Evaluator from the last lab, and add other Agentic patterns.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/business.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">Commercial implications</h2>\n",
" <span style=\"color:#00bfff;\">Aside from the obvious (your career alter-ego) this has business applications in any situation where you need an AI assistant with domain expertise and an ability to interact with the real world.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
}
],
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136from dotenv import load_dotenv
from openai import OpenAI
import json
import os
import requests
from pypdf import PdfReader
import gradio as gr
load_dotenv(override=True)
def push(text):
requests.post(
"https://api.pushover.net/1/messages.json",
data={
"token": os.getenv("PUSHOVER_TOKEN"),
"user": os.getenv("PUSHOVER_USER"),
"message": text,
}
)
def record_user_details(email, name="Name not provided", notes="not provided"):
push(f"Recording {name} with email {email} and notes {notes}")
return {"recorded": "ok"}
def record_unknown_question(question):
push(f"Recording {question}")
return {"recorded": "ok"}
record_user_details_json = {
"name": "record_user_details",
"description": "Use this tool to record that a user is interested in being in touch and provided an email address",
"parameters": {
"type": "object",
"properties": {
"email": {
"type": "string",
"description": "The email address of this user"
},
"name": {
"type": "string",
"description": "The user's name, if they provided it"
}
,
"notes": {
"type": "string",
"description": "Any additional information about the conversation that's worth recording to give context"
}
},
"required": ["email"],
"additionalProperties": False
}
}
record_unknown_question_json = {
"name": "record_unknown_question",
"description": "Always use this tool to record any question that couldn't be answered as you didn't know the answer",
"parameters": {
"type": "object",
"properties": {
"question": {
"type": "string",
"description": "The question that couldn't be answered"
},
},
"required": ["question"],
"additionalProperties": False
}
}
tools = [{"type": "function", "function": record_user_details_json},
{"type": "function", "function": record_unknown_question_json}]
class Me:
def __init__(self):
self.GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/v1beta/openai/"
self.GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
self.openai = OpenAI(base_url=self.GEMINI_BASE_URL, api_key=self.GOOGLE_API_KEY)
self.name = "Harsh Patidar"
reader = PdfReader("me/linkedin.pdf")
self.linkedin = ""
for page in reader.pages:
text = page.extract_text()
if text:
self.linkedin += text
with open("me/summary.txt", "r", encoding="utf-8") as f:
self.summary = f.read()
def handle_tool_call(self, tool_calls):
results = []
for tool_call in tool_calls:
tool_name = tool_call.function.name
arguments = json.loads(tool_call.function.arguments)
print(f"Tool called: {tool_name}", flush=True)
tool = globals().get(tool_name)
result = tool(**arguments) if tool else {}
results.append({"role": "tool","content": json.dumps(result),"tool_call_id": tool_call.id})
return results
def system_prompt(self):
system_prompt = f"You are acting as {self.name}. You are answering questions on {self.name}'s website, \
particularly questions related to {self.name}'s career, background, skills and experience. \
Your responsibility is to represent {self.name} for interactions on the website as faithfully as possible. \
You are given a summary of {self.name}'s background and LinkedIn profile which you can use to answer questions. \
Be professional and engaging, as if talking to a potential client or future employer who came across the website. \
If you don't know the answer to any question, use your record_unknown_question tool to record the question that you couldn't answer, even if it's about something trivial or unrelated to career. \
If the user is engaging in discussion, try to steer them towards getting in touch via email; ask for their email and record it using your record_user_details tool. "
system_prompt += f"\n\n## Summary:\n{self.summary}\n\n## LinkedIn Profile:\n{self.linkedin}\n\n"
system_prompt += f"With this context, please chat with the user, always staying in character as {self.name}."
return system_prompt
def chat(self, message, history):
messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
done = False
while not done:
response = self.openai.chat.completions.create(model="gemini-2.5-flash-preview-05-20", messages=messages, tools=tools)
if response.choices[0].finish_reason=="tool_calls":
message = response.choices[0].message
tool_calls = message.tool_calls
results = self.handle_tool_call(tool_calls)
messages.append(message)
messages.extend(results)
else:
done = True
return response.choices[0].message.content
if __name__ == "__main__":
me = Me()
gr.ChatInterface(me.chat, type="messages").launch()
1234567891011Hey, I’m Harsh Patidar — a Data Engineer at ZS who loves building data systems that actually work — scalable, reliable, and smart enough to keep learning.
I’ve spent the past few years turning raw, unstructured data into powerful systems that fuel analytics, automation, and AI-driven decisions.
At ZS, I work in the R&D division, where I design and deploy containerized APIs, optimize data pipelines, and integrate machine learning models into real-world workflows. My toolkit revolves around Python, SQL, FastAPI, Docker, Airflow, and AWS, and I enjoy the process of connecting every piece of data infrastructure into something clean, efficient, and production-ready.
Before this, I was part of Accenture’s Data Engineering & Governance team, helping large enterprises strengthen data reliability, validation, and compliance frameworks — experience that taught me the importance of structure, traceability, and precision.
I also spent time as a Teaching Assistant at Coding Ninjas, mentoring over 200 students in Data Structures and Algorithms — something that shaped both my fundamentals and my patience.
Outside of work, I’m someone who finds joy in photography, exploring tech startups, and deep research in finance and AI. I like observing how technology, creativity, and design come together — whether in a great photograph or a cleanly designed data pipeline.
At my core, I’m driven by curiosity and the excitement of building something meaningful from scratch. I believe great work is built quietly, through learning, experimentation, and the discipline to keep improving — whether that’s a data system, a product, or even myself.123456requests
python-dotenv
gradio
pypdf
openai
openai-agents123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome to the start of your adventure in Agentic AI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Are you ready for action??</h2>\n",
" <span style=\"color:#ff7800;\">Have you completed all the setup steps in the <a href=\"../setup/\">setup</a> folder?<br/>\n",
" Have you read the <a href=\"../README.md\">README</a>? Many common questions are answered here!<br/>\n",
" Have you checked out the guides in the <a href=\"../guides/01_intro.ipynb\">guides</a> folder?<br/>\n",
" Well in that case, you're ready!!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">This code is a live resource - keep an eye out for my updates</h2>\n",
" <span style=\"color:#00bfff;\">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.<br/><br/>\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",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"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": null,
"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": null,
"metadata": {},
"outputs": [],
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Final reminders</h2>\n",
" <span style=\"color:#ff7800;\">1. If you're not confident about Environment Variables or Web Endpoints / APIs, please read Topics 3 and 5 in this <a href=\"../guides/04_technical_foundations.ipynb\">technical foundations guide</a>.<br/>\n",
" 2. If you want to use AIs other than OpenAI, like Gemini, DeepSeek or Ollama (free), please see the first section in this <a href=\"../guides/09_ai_apis_and_ollama.ipynb\">AI APIs guide</a>.<br/>\n",
" 3. If you ever get a Name Error in Python, you can always fix it immediately; see the last section of this <a href=\"../guides/06_python_foundations.ipynb\">Python Foundations guide</a> and follow both tutorials and exercises.<br/>\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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": null,
"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",
"# Even for other LLM providers like Gemini, you still use this OpenAI import - see Guide 9 for why\n",
"\n",
"from openai import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": null,
"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": null,
"metadata": {},
"outputs": [],
"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": null,
"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": null,
"metadata": {},
"outputs": [],
"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": null,
"metadata": {},
"outputs": [],
"source": [
"# form a new messages list\n",
"messages = [{\"role\": \"user\", \"content\": question}]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n",
" First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n",
" Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n",
" Finally have 3 third LLM call propose the Agentic AI solution. <br/>\n",
" We will cover this at up-coming labs, so don't worry if you're unsure.. just give it a try!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# And now - let's ask for a question:\n",
"\n",
"import os\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"from openai import OpenAI\n",
"from IPython.display import Markdown, display\n",
"\n",
"# And now we'll create an instance of the OpenAI class\n",
"\n",
"openai = OpenAI()\n",
"\n",
"question1 = \"Please pick a business area that might be worth exploring for an Agentic AI opportunity.\"\n",
"messages1 = [{\"role\": \"user\", \"content\": question1}]\n",
"\n",
"# Then make the first call:\n",
"response1 = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages1\n",
")\n",
"\n",
"question2 = \" Please present the pain-point in \"+response1.choices[0].message.content +\" industry - something challenging that might be ripe for an Agentic solution\"\n",
"messages2 = [{\"role\": \"user\", \"content\": question2}]\n",
"\n",
"# Then make the first call:\n",
"response2 = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages2\n",
")\n",
"\n",
"question3 = \" Please presentpropose and Agentic AI solution for pain-point \"+response2.choices[0].message.content\n",
"messages3 = [{\"role\": \"user\", \"content\": question3}]\n",
"\n",
"# Then make the first call:\n",
"response3 = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages3\n",
")\n",
"\n",
"Final_Answer = \" Please presentpropose and Agentic AI solution for pain-point \"+response2.choices[0].message.content\n",
"\n",
"display(Markdown(Final_Answer))\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"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.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome to the start of your adventure in Agentic AI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Are you ready for action??</h2>\n",
" <span style=\"color:#ff7800;\">Have you completed all the setup steps in the <a href=\"../setup/\">setup</a> folder?<br/>\n",
" Have you read the <a href=\"../README.md\">README</a>? Many common questions are answered here!<br/>\n",
" Have you checked out the guides in the <a href=\"../guides/01_intro.ipynb\">guides</a> folder?<br/>\n",
" Well in that case, you're ready!!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">This code is a live resource - keep an eye out for my updates</h2>\n",
" <span style=\"color:#00bfff;\">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.<br/><br/>\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",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Final reminders</h2>\n",
" <span style=\"color:#ff7800;\">1. If you're not confident about Environment Variables or Web Endpoints / APIs, please read Topics 3 and 5 in this <a href=\"../guides/04_technical_foundations.ipynb\">technical foundations guide</a>.<br/>\n",
" 2. If you want to use AIs other than OpenAI, like Gemini, DeepSeek or Ollama (free), please see the first section in this <a href=\"../guides/09_ai_apis_and_ollama.ipynb\">AI APIs guide</a>.<br/>\n",
" 3. If you ever get a Name Error in Python, you can always fix it immediately; see the last section of this <a href=\"../guides/06_python_foundations.ipynb\">Python Foundations guide</a> and follow both tutorials and exercises.<br/>\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"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",
"# Even for other LLM providers like Gemini, you still use this OpenAI import - see Guide 9 for why\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": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ChatCompletion(id='chatcmpl-C9oVaLh1gjzKH07zcVLaXQ4o4FDQ7', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='2 + 2 equals 4.', refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=None))], created=1756455142, model='gpt-4.1-nano-2025-04-14', object='chat.completion', service_tier='default', system_fingerprint='fp_c4c155951e', usage=CompletionUsage(completion_tokens=8, prompt_tokens=14, total_tokens=22, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))\n",
"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",
"print(response.choices[0].message.content)\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"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": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"If three people can paint three walls in three hours, how many people are needed to paint 18 walls in six hours?\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": 11,
"metadata": {},
"outputs": [],
"source": [
"# form a new messages list\n",
"messages = [{\"role\": \"user\", \"content\": question}]\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Let's analyze the problem step-by-step:\n",
"\n",
"---\n",
"\n",
"**Given:**\n",
"\n",
"- 3 people can paint 3 walls in 3 hours.\n",
"\n",
"**Question:**\n",
"\n",
"- How many people are needed to paint 18 walls in 6 hours?\n",
"\n",
"---\n",
"\n",
"### Step 1: Find the rate of painting per person\n",
"\n",
"- Total walls painted: 3 walls\n",
"- Total people: 3 people\n",
"- Total time: 3 hours\n",
"\n",
"**Walls per person per hour:**\n",
"\n",
"First, find how many walls 3 people paint per hour:\n",
"\n",
"\\[\n",
"\\frac{3 \\text{ walls}}{3 \\text{ hours}} = 1 \\text{ wall per hour by 3 people}\n",
"\\]\n",
"\n",
"So, 3 people paint 1 wall per hour.\n",
"\n",
"Then, walls per person per hour:\n",
"\n",
"\\[\n",
"\\frac{1 \\text{ wall per hour}}{3 \\text{ people}} = \\frac{1}{3} \\text{ wall per person per hour}\n",
"\\]\n",
"\n",
"---\n",
"\n",
"### Step 2: Calculate total work needed\n",
"\n",
"You want to paint 18 walls in 6 hours.\n",
"\n",
"This means the rate of painting must be:\n",
"\n",
"\\[\n",
"\\frac{18 \\text{ walls}}{6 \\text{ hours}} = 3 \\text{ walls per hour}\n",
"\\]\n",
"\n",
"---\n",
"\n",
"### Step 3: Find how many people are needed for this rate\n",
"\n",
"Since each person paints \\(\\frac{1}{3}\\) wall per hour,\n",
"\n",
"\\[\n",
"\\text{Number of people} \\times \\frac{1}{3} = 3 \\text{ walls per hour}\n",
"\\]\n",
"\n",
"Multiply both sides by 3:\n",
"\n",
"\\[\n",
"\\text{Number of people} = 3 \\times 3 = 9\n",
"\\]\n",
"\n",
"---\n",
"\n",
"### **Answer:**\n",
"\n",
"\\[\n",
"\\boxed{9}\n",
"\\]\n",
"\n",
"You need **9 people** to paint 18 walls in 6 hours.\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": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Let's analyze the problem step-by-step:\n",
"\n",
"---\n",
"\n",
"**Given:**\n",
"\n",
"- 3 people can paint 3 walls in 3 hours.\n",
"\n",
"**Question:**\n",
"\n",
"- How many people are needed to paint 18 walls in 6 hours?\n",
"\n",
"---\n",
"\n",
"### Step 1: Find the rate of painting per person\n",
"\n",
"- Total walls painted: 3 walls\n",
"- Total people: 3 people\n",
"- Total time: 3 hours\n",
"\n",
"**Walls per person per hour:**\n",
"\n",
"First, find how many walls 3 people paint per hour:\n",
"\n",
"\\[\n",
"\\frac{3 \\text{ walls}}{3 \\text{ hours}} = 1 \\text{ wall per hour by 3 people}\n",
"\\]\n",
"\n",
"So, 3 people paint 1 wall per hour.\n",
"\n",
"Then, walls per person per hour:\n",
"\n",
"\\[\n",
"\\frac{1 \\text{ wall per hour}}{3 \\text{ people}} = \\frac{1}{3} \\text{ wall per person per hour}\n",
"\\]\n",
"\n",
"---\n",
"\n",
"### Step 2: Calculate total work needed\n",
"\n",
"You want to paint 18 walls in 6 hours.\n",
"\n",
"This means the rate of painting must be:\n",
"\n",
"\\[\n",
"\\frac{18 \\text{ walls}}{6 \\text{ hours}} = 3 \\text{ walls per hour}\n",
"\\]\n",
"\n",
"---\n",
"\n",
"### Step 3: Find how many people are needed for this rate\n",
"\n",
"Since each person paints \\(\\frac{1}{3}\\) wall per hour,\n",
"\n",
"\\[\n",
"\\text{Number of people} \\times \\frac{1}{3} = 3 \\text{ walls per hour}\n",
"\\]\n",
"\n",
"Multiply both sides by 3:\n",
"\n",
"\\[\n",
"\\text{Number of people} = 3 \\times 3 = 9\n",
"\\]\n",
"\n",
"---\n",
"\n",
"### **Answer:**\n",
"\n",
"\\[\n",
"\\boxed{9}\n",
"\\]\n",
"\n",
"You need **9 people** to paint 18 walls in 6 hours."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n",
" First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n",
" Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n",
" Finally have 3 third LLM call propose the Agentic AI solution. <br/>\n",
" We will cover this at up-coming labs, so don't worry if you're unsure.. just give it a try!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Certainly! Building on your outlined pain-point and the high-level Agentic AI functionalities, here’s a detailed proposal for an **Agentic AI solution** designed to tackle fragmented patient data and enable real-time, holistic health management.\n",
"\n",
"---\n",
"\n",
"# Agentic AI Solution Proposal: **HealthSynth AI**\n",
"\n",
"### Overview \n",
"**HealthSynth AI** is an autonomous health management agent that continuously synthesizes fragmented patient data from multiple sources to provide a real-time, unified, and actionable health profile for patients and their care teams. It acts as a 24/7 health assistant, proactive coordinator, and personalized medical advisor.\n",
"\n",
"---\n",
"\n",
"## Key Features & Capabilities\n",
"\n",
"### 1. **Autonomous Data Aggregation & Normalization** \n",
"- Uses API integrations, secure data exchanges (FHIR, HL7 standards), and device SDKs to continuously fetch data from: \n",
" - EHR systems across different providers \n",
" - Wearable and home medical devices (heart rate, glucose monitors, BP cuffs) \n",
" - Pharmacy records and prescription databases \n",
" - Lab results portals \n",
" - Insurance claims and coverage data \n",
"- Applies intelligent data cleaning, deduplication, and semantic normalization to unify heterogeneous data formats into a consistent patient health graph.\n",
"\n",
"### 2. **Real-Time Multimodal Health Analytics Engine** \n",
"- Employs advanced ML and deep learning models to detect: \n",
" - Emerging risk patterns (e.g., early signs of infection, deterioration of chronic conditions) \n",
" - Anomalies (missed medications, unusual vital sign changes) \n",
" - Compliance gaps (lifestyle, medication adherence) \n",
"- Continuously updates predictive health trajectories personalized to each patient’s condition and history.\n",
"\n",
"### 3. **Proactive Action & Recommendation System** \n",
"- Generates context-aware, evidence-based alerts and recommendations such as: \n",
" - Medication reminders or dosage adjustments flagged in consultation with prescribing physicians \n",
" - Suggestions for scheduling lab tests or specialist visits timely before symptoms worsen \n",
" - Lifestyle coaching tips adapted using patient preferences and progress \n",
"- Classes recommendations into urgency tiers (info, caution, immediate action) and routes notifications appropriately.\n",
"\n",
"### 4. **Automated Care Coordination & Workflow Integration** \n",
"- Interacts programmatically with provider scheduling systems, telemedicine platforms, pharmacies, and insurance portals to: \n",
" - Automatically request appointment reschedules or referrals based on patient status \n",
" - Notify involved healthcare professionals about critical health events or lab results \n",
" - Facilitate prescription renewals or modifications with minimal human intervention \n",
"- Maintains secure, auditable communication logs ensuring compliance (HIPAA, GDPR).\n",
"\n",
"### 5. **Patient-Centric Digital Health Companion** \n",
"- Provides patients with an intuitive mobile/web app featuring: \n",
" - A dynamic health dashboard summarizing key metrics, risks, and recent activities in plain language \n",
" - Intelligent daily check-ins and symptom trackers powered by conversational AI \n",
" - Adaptive educational content tailored to health literacy levels and language preferences \n",
" - Privacy controls empowering patients to manage data sharing settings\n",
"\n",
"---\n",
"\n",
"## Technical Architecture (High-Level)\n",
"\n",
"- **Data Ingestion Layer:** Connectors for EHRs, wearables, pharmacies, labs \n",
"- **Data Lake & Processing:** Cloud-native secure storage with HIPAA-compliant encryption \n",
"- **Knowledge Graph:** Patient-centric semantic graph linking clinical concepts, timelines, interventions \n",
"- **Analytics & ML Models:** Ensemble predictive models incorporating temporal health data, risk scoring, anomaly detection \n",
"- **Agentic Orchestrator:** Rule-based and reinforcement learning-driven workflow engine enabling autonomous decision-making and stakeholder communications \n",
"- **Frontend Interfaces:** Responsive patient app, provider portals, API access for system integration\n",
"\n",
"---\n",
"\n",
"## Potential Challenges & Mitigations\n",
"\n",
"| Challenge | Mitigation Strategy |\n",
"|-----------|---------------------|\n",
"| Data privacy & regulatory compliance | Built-in privacy-by-design, end-to-end encryption, rigorous consent management, audit trails |\n",
"| Data interoperability & standardization | Utilize open standards (FHIR, DICOM), NLP for unstructured data extraction |\n",
"| Model explainability | Implement interpretable ML techniques and transparent reasoning for clinicians |\n",
"| Patient engagement sustainability | Gamification, behavior science-driven personalized nudges |\n",
"| Integration complexity across healthcare IT systems | Modular adaptors/plugins, partnerships with major EHR vendors |\n",
"\n",
"---\n",
"\n",
"## Impact & Benefits\n",
"\n",
"- **For Patients:** Reduced health risks, increased empowerment, improved treatment adherence, and personal convenience \n",
"- **For Providers:** Enhanced clinical decision support, reduced administrative burden, timely interventions \n",
"- **For Payers:** Lowered costs via preventive care and reduced hospital readmissions\n",
"\n",
"---\n",
"\n",
"Would you like me to help you design detailed user journeys, develop specific ML model architectures, or draft an implementation roadmap for **HealthSynth AI**?"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# First create the messages:\n",
"\n",
"messages = [{\"role\": \"user\", \"content\": \"I want you to pick a business area that might be worth exploring for an Agentic AI opportunity.\"}]\n",
"\n",
"# Then make the first call:\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"\n",
"# Then read the business idea:\n",
"\n",
"business_idea = response.choices[0].message.content\n",
"\n",
"# print(business_idea)\n",
"\n",
"messages = [{\"role\": \"user\", \"content\": f\"Please propose a pain-point in the {business_idea} industry.\"}]\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"\n",
"pain_point = response.choices[0].message.content\n",
"\n",
"messages = [{\"role\": \"user\", \"content\": f\"Please propose an Agentic AI solution to the pain-point: {pain_point}.\"}]\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"\n",
"agentic_solution = response.choices[0].message.content\n",
"\n",
"display(Markdown(agentic_solution))\n",
"\n",
"# And repeat! In the next message, include the business idea within the message"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"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.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome to the start of your adventure in Agentic AI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Are you ready for action??</h2>\n",
" <span style=\"color:#ff7800;\">Have you completed all the setup steps in the <a href=\"../setup/\">setup</a> folder?<br/>\n",
" Have you read the <a href=\"../README.md\">README</a>? Many common questions are answered here!<br/>\n",
" Have you checked out the guides in the <a href=\"../guides/01_intro.ipynb\">guides</a> folder?<br/>\n",
" Well in that case, you're ready!!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">This code is a live resource - keep an eye out for my updates</h2>\n",
" <span style=\"color:#00bfff;\">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.<br/><br/>\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",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"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": null,
"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": null,
"metadata": {},
"outputs": [],
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Final reminders</h2>\n",
" <span style=\"color:#ff7800;\">1. If you're not confident about Environment Variables or Web Endpoints / APIs, please read Topics 3 and 5 in this <a href=\"../guides/04_technical_foundations.ipynb\">technical foundations guide</a>.<br/>\n",
" 2. If you want to use AIs other than OpenAI, like Gemini, DeepSeek or Ollama (free), please see the first section in this <a href=\"../guides/09_ai_apis_and_ollama.ipynb\">AI APIs guide</a>.<br/>\n",
" 3. If you ever get a Name Error in Python, you can always fix it immediately; see the last section of this <a href=\"../guides/06_python_foundations.ipynb\">Python Foundations guide</a> and follow both tutorials and exercises.<br/>\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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": null,
"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",
"# Even for other LLM providers like Gemini, you still use this OpenAI import - see Guide 9 for why\n",
"\n",
"from openai import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": null,
"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": null,
"metadata": {},
"outputs": [],
"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": null,
"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": null,
"metadata": {},
"outputs": [],
"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": null,
"metadata": {},
"outputs": [],
"source": [
"# form a new messages list\n",
"messages = [{\"role\": \"user\", \"content\": question}]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n",
" First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n",
" Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n",
" Finally have 3 third LLM call propose the Agentic AI solution. <br/>\n",
" We will cover this at up-coming labs, so don't worry if you're unsure.. just give it a try!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"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": ".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.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# First Agentic AI workflow with OPENAI"
]
},
{
"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/muhammad-mudassar-a65645192/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import Libraries"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import re\n",
"from openai import OpenAI\n",
"from dotenv import load_dotenv\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai_api_key=os.getenv(\"OPENAI_API_KEY\")\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 gui\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Workflow with OPENAI"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"openai=OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"message = [{'role':'user','content':\"what is 2+3?\"}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response = openai.chat.completions.create(model=\"gpt-4o-mini\",messages=message)\n",
"print(response.choices[0].message.content)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"question = \"Please propose a hard, challenging question to assess someone's IQ. Respond only with the question.\"\n",
"message=[{'role':'user','content':question}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response=openai.chat.completions.create(model=\"gpt-4o-mini\",messages=message)\n",
"question=response.choices[0].message.content\n",
"print(f\"Answer: {question}\")"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"message=[{'role':'user','content':question}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response=openai.chat.completions.create(model=\"gpt-4o-mini\",messages=message)\n",
"answer = response.choices[0].message.content\n",
"print(f\"Answer: {answer}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# convert \\[ ... \\] to $$ ... $$, to properly render Latex\n",
"converted_answer = re.sub(r'\\\\[\\[\\]]', '$$', answer)\n",
"display(Markdown(converted_answer))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n",
" First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n",
" Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n",
" Finally have 3 third LLM call propose the Agentic AI solution.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"message = [{'role':'user','content':\"give me a business area related to ecommerce that might be worth exploring for a agentic opportunity.\"}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response = openai.chat.completions.create(model=\"gpt-4o-mini\",messages=message)\n",
"business_area = response.choices[0].message.content\n",
"business_area"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"message = business_area + \"present a pain-point in that industry - something challenging that might be ripe for an agentic solutions.\"\n",
"message"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"message = [{'role': 'user', 'content': message}]\n",
"response = openai.chat.completions.create(model=\"gpt-4o-mini\",messages=message)\n",
"question=response.choices[0].message.content\n",
"question"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"message=[{'role':'user','content':question}]\n",
"response=openai.chat.completions.create(model=\"gpt-4o-mini\",messages=message)\n",
"answer=response.choices[0].message.content\n",
"print(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display(Markdown(answer))"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome to the start of your adventure in Agentic AI"
]
},
{
"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": null,
"metadata": {},
"outputs": [],
"source": [
"from dotenv import load_dotenv\n",
"load_dotenv()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Check the keys\n",
"import google.generativeai as genai\n",
"import os\n",
"genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))\n",
"model = genai.GenerativeModel(model_name=\"gemini-1.5-flash\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a list of messages in the familiar Gemini GenAI format\n",
"\n",
"response = model.generate_content([\"2+2=?\"])\n",
"response.text"
]
},
{
"cell_type": "code",
"execution_count": null,
"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",
"\n",
"response = model.generate_content([question])\n",
"print(response.text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import Markdown, display\n",
"\n",
"display(Markdown(response.text))"
]
},
{
"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": "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!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "llm_projects",
"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.10.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome to the start of your adventure in Agentic AI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Are you ready for action??</h2>\n",
" <span style=\"color:#ff7800;\">Have you completed all the setup steps in the <a href=\"../setup/\">setup</a> folder?<br/>\n",
" Have you read the <a href=\"../README.md\">README</a>? Many common questions are answered here!<br/>\n",
" Have you checked out the guides in the <a href=\"../guides/01_intro.ipynb\">guides</a> folder?<br/>\n",
" Well in that case, you're ready!!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">This code is a live resource - keep an eye out for my updates</h2>\n",
" <span style=\"color:#00bfff;\">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.<br/><br/>\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",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Final reminders</h2>\n",
" <span style=\"color:#ff7800;\">1. If you're not confident about Environment Variables or Web Endpoints / APIs, please read Topics 3 and 5 in this <a href=\"../guides/04_technical_foundations.ipynb\">technical foundations guide</a>.<br/>\n",
" 2. If you want to use AIs other than OpenAI, like Gemini, DeepSeek or Ollama (free), please see the first section in this <a href=\"../guides/09_ai_apis_and_ollama.ipynb\">AI APIs guide</a>.<br/>\n",
" 3. If you ever get a Name Error in Python, you can always fix it immediately; see the last section of this <a href=\"../guides/06_python_foundations.ipynb\">Python Foundations guide</a> and follow both tutorials and exercises.<br/>\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"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",
"# Even for other LLM providers like Gemini, you still use this OpenAI import - see Guide 9 for why\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": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 + 2 equals 4.\n",
"assistant\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": null,
"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": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"If all Bloops are Razzies and all Razzies are Lazzies, are all Bloops definitely Lazzies? Explain your reasoning.\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": 12,
"metadata": {},
"outputs": [],
"source": [
"# form a new messages list\n",
"messages = [{\"role\": \"user\", \"content\": question}]\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Yes, all Bloops are definitely Lazzies.\n",
"\n",
"**Reasoning:**\n",
"\n",
"1. The statement \"All Bloops are Razzies\" means every member of the set Bloops is also a member of the set Razzies.\n",
"2. The statement \"All Razzies are Lazzies\" means every member of the set Razzies is also a member of the set Lazzies.\n",
"\n",
"Since all Bloops are inside the Razzies group, and all Razzies are inside the Lazzies group, it follows that all Bloops must be inside the Lazzies group.\n",
"\n",
"In other words, the set of Bloops is a subset of Razzies, and Razzies is a subset of Lazzies. Therefore, Bloops is a subset of Lazzies.\n",
"\n",
"This is a classic example of the transitive property in logic and set theory:\n",
"- If A ⊆ B and B ⊆ C, then A ⊆ C.\n",
"\n",
"So, yes, all Bloops are definitely Lazzies.\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": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Yes, all Bloops are definitely Lazzies.\n",
"\n",
"**Reasoning:**\n",
"\n",
"1. The statement \"All Bloops are Razzies\" means every member of the set Bloops is also a member of the set Razzies.\n",
"2. The statement \"All Razzies are Lazzies\" means every member of the set Razzies is also a member of the set Lazzies.\n",
"\n",
"Since all Bloops are inside the Razzies group, and all Razzies are inside the Lazzies group, it follows that all Bloops must be inside the Lazzies group.\n",
"\n",
"In other words, the set of Bloops is a subset of Razzies, and Razzies is a subset of Lazzies. Therefore, Bloops is a subset of Lazzies.\n",
"\n",
"This is a classic example of the transitive property in logic and set theory:\n",
"- If A ⊆ B and B ⊆ C, then A ⊆ C.\n",
"\n",
"So, yes, all Bloops are definitely Lazzies."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n",
" First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n",
" Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n",
" Finally have 3 third LLM call propose the Agentic AI solution. <br/>\n",
" We will cover this at up-coming labs, so don't worry if you're unsure.. just give it a try!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"One promising business area to explore for an Agentic AI opportunity is **Supply Chain and Logistics Management**.\n",
"\n",
"### Why Supply Chain and Logistics?\n",
"\n",
"1. **Complex Decision-Making Environment:** Supply chains involve numerous variables including inventory levels, demand forecasting, transportation routes, supplier reliability, and geopolitical factors. An Agentic AI can autonomously analyze these variables in real-time and make optimized decisions.\n",
"\n",
"2. **High Impact on Efficiency and Cost:** Optimizing supply chain operations directly reduces costs, improves delivery times, and enhances customer satisfaction. Autonomous AI agents can dynamically reroute shipments, adjust inventory strategies, and renegotiate with suppliers in response to unexpected events.\n",
"\n",
"3. **Adaptability to Disruptions:** Agentic AI can proactively manage disruptions—e.g., natural disasters, port strikes, sudden demand spikes—by autonomously altering plans without human intervention, maintaining supply chain resilience.\n",
"\n",
"4. **Data-Rich Environment:** Supply chains generate massive amounts of data from IoT sensors, ERP systems, and market trends. An Agentic AI can continuously learn from this data to improve decision-making over time.\n",
"\n",
"5. **Scalability:** Agents can operate across multiple nodes of a global supply chain, coordinating activities and ensuring end-to-end optimization, a task challenging for traditional manual or semi-automated tools.\n",
"\n",
"### Example Use Cases\n",
"\n",
"- Autonomous inventory management agents that predict and reorder supplies.\n",
"- AI-driven transportation agents that plan and adjust delivery routes in real-time.\n",
"- Negotiation agents that interact with suppliers to secure better terms or resolve delays.\n",
"- Risk management agents that simulate scenarios and implement contingency plans proactively.\n",
"\n",
"### Potential ROI\n",
"\n",
"Improved efficiency and responsiveness can lead to significant cost reductions, better service levels, and competitive advantages in markets where speed and reliability are critical.\n",
"\n",
"---\n",
"\n",
"Would you like me to elaborate on potential technical approaches or business models for Agentic AI in this field?\n"
]
}
],
"source": [
"# First create the messages:\n",
"\n",
"messages = [{\"role\": \"user\", \"content\": \"pick a business area that might be worth exploring for an Agentic AI opportunity.\"}]\n",
"\n",
"# Then make the first call:\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"\n",
"# Then read the business idea:\n",
"\n",
"business_idea = response.choices[0].message.content\n",
"print(business_idea)\n",
"\n",
"# And repeat! In the next message, include the business idea within the message"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Certainly! Let’s consider the **healthcare industry** as an example.\n",
"\n",
"**Pain-point:** \n",
"Healthcare providers often struggle with **efficiently managing and coordinating patient care across multiple departments and specialists**, leading to delays, miscommunications, and fragmented patient experiences.\n",
"\n",
"**Why it’s challenging:** \n",
"- Patient information is often siloed in different systems or departments. \n",
"- Coordinating appointments, treatments, and follow-ups requires constant communication and scheduling adjustments. \n",
"- Healthcare professionals face high workloads, making manual coordination prone to errors and delays. \n",
"- Patients may receive redundant tests or conflicting advice due to lack of coordinated care.\n",
"\n",
"**Ripe for an Agentic solution:** \n",
"An intelligent, autonomous agent could serve as a centralized care coordinator that: \n",
"- Integrates data across hospital systems to create a unified patient profile. \n",
"- Dynamically schedules and synchronizes appointments and treatments based on specialist availability and patient needs. \n",
"- Sends proactive reminders and updates to both patients and providers. \n",
"- Continuously learns and adapts to optimize care pathways and reduce bottlenecks.\n",
"\n",
"Such an agentic system could profoundly improve efficiency, reduce errors, and enhance patient outcomes by automating complex coordination tasks and enabling more seamless communication among all stakeholders.\n"
]
}
],
"source": [
"pick_pain_point = [{\"role\":\"user\",\"content\" : \"present a pain-point in that industry - something challenging that might be ripe for an Agentic solution\"}]\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=pick_pain_point\n",
")\n",
"\n",
"pain_point = response.choices[0].message.content\n",
"print(pain_point)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\"Agentic AI\" refers to artificial intelligence systems that operate with a degree of autonomy and goal-directed behavior, often capable of making decisions, planning, and interacting with environments in a manner similar to an agent. When proposing an Agentic AI solution, it’s important to outline the system's objectives, capabilities, architecture, and deployment context clearly.\n",
"\n",
"Here is a structured proposal for an Agentic AI solution:\n",
"\n",
"---\n",
"\n",
"### Proposal: Agentic AI Solution for Autonomous Task Management\n",
"\n",
"#### 1. **Objective**\n",
"\n",
"Develop an Agentic AI system capable of autonomously managing complex tasks in dynamic environments. The agent should be able to perceive its environment, make decisions, plan actions, learn from outcomes, and communicate effectively with human users.\n",
"\n",
"#### 2. **Use Case**\n",
"\n",
"- Autonomous workflow management in enterprise settings.\n",
"- Robotic process automation with adaptive decision-making.\n",
"- Intelligent virtual assistants that handle multi-step goals.\n",
"- Autonomous vehicles or robotic agents performing navigation and task execution.\n",
"\n",
"#### 3. **Core Capabilities**\n",
"\n",
"- **Perception:** Ability to gather and interpret data from multiple sources (sensors, databases, APIs).\n",
"- **Reasoning and Decision Making:** Use of symbolic reasoning, probabilistic models, or reinforcement learning to make goal-oriented decisions.\n",
"- **Planning:** Generate and optimize multi-step action plans to achieve high-level goals.\n",
"- **Learning:** Adapt strategies based on feedback using supervised, unsupervised, or reinforcement learning techniques.\n",
"- **Interaction:** Natural language processing and multimodal communication for engaging with users and other systems.\n",
"\n",
"#### 4. **Technical Architecture**\n",
"\n",
"- **Perception Layer:** Data ingestion modules with preprocessing, sensor fusion, and environment modeling.\n",
"- **Cognitive Layer:**\n",
" - Knowledge base for domain-specific understanding.\n",
" - Planning engine implementing algorithms such as A* search, Monte Carlo Tree Search, or heuristic planners.\n",
" - Decision module leveraging AI models (e.g., deep reinforcement learning).\n",
"- **Learning Layer:** Continuous learning pipelines to update models and improve performance.\n",
"- **Interaction Layer:** NLP processors, dialog managers, and multimodal interfaces.\n",
"- **Execution Layer:** Actuation modules or API connectors to perform planned actions.\n",
"\n",
"#### 5. **Implementation Approach**\n",
"\n",
"- Utilize a modular software framework such as ROS (Robot Operating System) for robotics agents or a microservices architecture for software agents.\n",
"- Integrate AI models developed with frameworks like TensorFlow or PyTorch.\n",
"- Employ cloud infrastructure for scalability and real-time data processing.\n",
"- Emphasize safety, transparency, and explainability to maintain trust and compliance.\n",
"\n",
"#### 6. **Evaluation Metrics**\n",
"\n",
"- Task success rate.\n",
"- Efficiency in task completion time.\n",
"- Responsiveness and adaptability to changes.\n",
"- User satisfaction and usability feedback.\n",
"- Robustness to uncertainty and errors.\n",
"\n",
"#### 7. **Potential Challenges**\n",
"\n",
"- Ensuring reliable perception in noisy environments.\n",
"- Balancing autonomy with human oversight.\n",
"- Handling ethical and privacy concerns.\n",
"- Maintaining system robustness and avoiding unintended behavior.\n",
"\n",
"#### 8. **Conclusion**\n",
"\n",
"This Agentic AI solution can significantly enhance automation by providing intelligent, autonomous agents capable of managing complex, dynamic tasks effectively. It holds promise across various industries, from manufacturing and logistics to customer service and autonomous vehicles.\n",
"\n",
"---\n",
"\n",
"If you have a specific domain or problem in mind, I can tailor the proposal accordingly. Would you like me to do that?\n"
]
}
],
"source": [
"propose=[{\"role\":\"user\",\"content\" : \"propose the Agentic AI solution\"}]\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=propose\n",
")\n",
"\n",
"solution = response.choices[0].message.content\n",
"print(solution)\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.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome to the start of your adventure in Agentic AI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Are you ready for action??</h2>\n",
" <span style=\"color:#ff7800;\">Have you completed all the setup steps in the <a href=\"../setup/\">setup</a> folder?<br/>\n",
" Have you checked out the guides in the <a href=\"../guides/01_intro.ipynb\">guides</a> folder?<br/>\n",
" Well in that case, you're ready!!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">Treat these labs as a resource</h2>\n",
" <span style=\"color:#00bfff;\">I push updates to the code regularly. When people ask questions or have problems, I incorporate it in the code, adding more examples or improved commentary. As a result, you'll notice that the code below isn't identical to the videos. Everything from the videos is here; but in addition, I've added more steps and better explanations. Consider this like an interactive book that accompanies the lectures.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"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. Run `uv add google-genai` to install the Google Gemini library. (If you had started your environment before running this command, you will need to restart your environment in the Jupyter notebook.)\n",
"2. 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",
"3. Click in each \"cell\" below, starting with the cell immediately below this text, and press Shift+Enter to run\n",
"4. 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. From the Cursor menu, choose Settings >> VSCode 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": null,
"metadata": {},
"outputs": [],
"source": [
"from dotenv import load_dotenv\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Next it's time to load the API keys into environment variables\n",
"\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Check the keys\n",
"\n",
"import os\n",
"gemini_api_key = os.getenv('GEMINI_API_KEY')\n",
"\n",
"if gemini_api_key:\n",
" print(f\"Gemini API Key exists and begins {gemini_api_key[:8]}\")\n",
"else:\n",
" print(\"Gemini API Key not set - please head to the troubleshooting guide in the guides folder\")\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# And now - the all important import statement\n",
"# If you get an import error - head over to troubleshooting guide\n",
"\n",
"from google import genai"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# And now we'll create an instance of the Gemini GenAI class\n",
"# If you're not sure what it means to create an instance of a class - head over to the guides folder!\n",
"# If you get a NameError - head over to the guides folder to learn about NameErrors\n",
"\n",
"client = genai.Client(api_key=gemini_api_key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a list of messages in the familiar Gemini GenAI format\n",
"\n",
"messages = [\"What is 2+2?\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# And now call it! Any problems, head to the troubleshooting guide\n",
"\n",
"response = client.models.generate_content(\n",
" model=\"gemini-2.0-flash\", contents=messages\n",
")\n",
"\n",
"print(response.text)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"# Lets no create a challenging question\n",
"question = \"Please propose a hard, challenging question to assess someone's IQ. Respond only with the question.\"\n",
"\n",
"# Ask the the model\n",
"response = client.models.generate_content(\n",
" model=\"gemini-2.0-flash\", contents=question\n",
")\n",
"\n",
"question = response.text\n",
"\n",
"print(question)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Ask the models generated question to the model\n",
"response = client.models.generate_content(\n",
" model=\"gemini-2.0-flash\", contents=question\n",
")\n",
"\n",
"# Extract the answer from the response\n",
"answer = response.text\n",
"\n",
"# Debug log the answer\n",
"print(answer)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import Markdown, display\n",
"\n",
"# Nicely format the answer using Markdown\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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n",
" First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n",
" Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n",
" Finally have 3 third LLM call propose the Agentic AI solution.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# First create the messages:\n",
"\n",
"\n",
"messages = [\"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!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"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": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome to the start of your adventure in Agentic AI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Are you ready for action??</h2>\n",
" <span style=\"color:#ff7800;\">Have you completed all the setup steps in the <a href=\"../setup/\">setup</a> folder?<br/>\n",
" Have you checked out the guides in the <a href=\"../guides/01_intro.ipynb\">guides</a> folder?<br/>\n",
" Well in that case, you're ready!!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">Treat these labs as a resource</h2>\n",
" <span style=\"color:#00bfff;\">I push updates to the code regularly. When people ask questions or have problems, I incorporate it in the code, adding more examples or improved commentary. As a result, you'll notice that the code below isn't identical to the videos. Everything from the videos is here; but in addition, I've added more steps and better explanations. Consider this like an interactive book that accompanies the lectures.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"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. Run `uv add google-genai` to install the Google Gemini library. (If you had started your environment before running this command, you will need to restart your environment in the Jupyter notebook.)\n",
"2. 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",
"3. Click in each \"cell\" below, starting with the cell immediately below this text, and press Shift+Enter to run\n",
"4. 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. From the Cursor menu, choose Settings >> VSCode 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": null,
"metadata": {},
"outputs": [],
"source": [
"from dotenv import load_dotenv\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Next it's time to load the API keys into environment variables\n",
"\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Check the keys\n",
"\n",
"import os\n",
"gemini_api_key = os.getenv('GEMINI_API_KEY')\n",
"\n",
"if gemini_api_key:\n",
" print(f\"Gemini API Key exists and begins {gemini_api_key[:8]}\")\n",
"else:\n",
" print(\"Gemini API Key not set - please head to the troubleshooting guide in the guides folder\")\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# And now - the all important import statement\n",
"# If you get an import error - head over to troubleshooting guide\n",
"\n",
"from google import genai"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# And now we'll create an instance of the Gemini GenAI class\n",
"# If you're not sure what it means to create an instance of a class - head over to the guides folder!\n",
"# If you get a NameError - head over to the guides folder to learn about NameErrors\n",
"\n",
"client = genai.Client(api_key=gemini_api_key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a list of messages in the familiar Gemini GenAI format\n",
"\n",
"messages = [\"What is 2+2?\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# And now call it! Any problems, head to the troubleshooting guide\n",
"\n",
"response = client.models.generate_content(\n",
" model=\"gemini-2.0-flash\", contents=messages\n",
")\n",
"\n",
"print(response.text)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"# Lets no create a challenging question\n",
"question = \"Please propose a hard, challenging question to assess someone's IQ. Respond only with the question.\"\n",
"\n",
"# Ask the the model\n",
"response = client.models.generate_content(\n",
" model=\"gemini-2.0-flash\", contents=question\n",
")\n",
"\n",
"question = response.text\n",
"\n",
"print(question)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Ask the models generated question to the model\n",
"response = client.models.generate_content(\n",
" model=\"gemini-2.0-flash\", contents=question\n",
")\n",
"\n",
"# Extract the answer from the response\n",
"answer = response.text\n",
"\n",
"# Debug log the answer\n",
"print(answer)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import Markdown, display\n",
"\n",
"# Nicely format the answer using Markdown\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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n",
" First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n",
" Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n",
" Finally have 3 third LLM call propose the Agentic AI solution.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# First create the messages:\n",
"\n",
"\n",
"messages = [\"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!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"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": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Implementing Notebook 1 using various LLMs via Groq"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from dotenv import load_dotenv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"groq_api_key = os.getenv('GROQ_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",
"if groq_api_key:\n",
" print(f\"Groq API Key exists and begins {groq_api_key[:2]}\")\n",
"else:\n",
" print(\"Groq API Key not set - please head to the troubleshooting guide in the setup folder\")\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from openai import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI(\n",
" base_url=\"https://api.groq.com/openai/v1\",\n",
" api_key=groq_api_key\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"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}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# openai/gpt-oss-120b\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"openai/gpt-oss-120b\",\n",
" messages=messages\n",
")\n",
"\n",
"print(response.choices[0].message.content)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# moonshotai/kimi-k2-instruct\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"moonshotai/kimi-k2-instruct\",\n",
" messages=messages\n",
")\n",
"\n",
"question = response.choices[0].message.content\n",
"\n",
"print(question)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# form a new messages list\n",
"messages = [{\"role\": \"user\", \"content\": question}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Ask meta-llama/llama-guard-4-12b\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"llama-3.1-8b-instant\",\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(question))\n",
"display(Markdown(answer))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n",
" First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n",
" Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n",
" Finally have 3 third LLM call propose the Agentic AI solution. <br/>\n",
" We will cover this at up-coming labs, so don't worry if you're unsure.. just give it a try!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# First create the messages:\n",
"\n",
"messages = [{\"role\": \"user\", \"content\": \"Pick a business area that is worth exploring for a Gen-Z audience, that can be an agentic-ai opportunity. \\\n",
" Somehwere where the concept of agentisation can be applied commerically. Respond only with the business idea.\"}]\n",
"\n",
"# Then make the first call: \n",
"\n",
"response = openai.chat.completions.create(\n",
" model = \"qwen/qwen3-32b\",\n",
" messages = messages\n",
")\n",
"\n",
"# Then read the business idea:\n",
"\n",
"business_idea = response.choices[0].message.content\n",
"print(business_idea)\n",
"\n",
"# And repeat! In the next message, include the business idea within the message\n",
"\n",
"user_prompt_pain_point = f\"What is the pain point of the Gen-Z audience in the business area of {business_idea}?, that can be solved by an agentic-ai solution? Give a brief answer\"\n",
"\n",
"response = openai.chat.completions.create(\n",
" model = \"gemma2-9b-it\",\n",
" messages = [{\"role\": \"user\", \"content\": user_prompt_pain_point}]\n",
")\n",
"\n",
"pain_point = response.choices[0].message.content\n",
"print(pain_point)\n",
"\n",
"user_prompt_solution = f\"What is the solution to the pain point {pain_point} of the Gen-Z audience in the business area of {business_idea}?, that can be solved by an agentic-ai solution? Provide a step-by-step breakdown\"\n",
"\n",
"response = openai.chat.completions.create(\n",
" model = \"deepseek-r1-distill-llama-70b\",\n",
" messages = [{\"role\": \"user\", \"content\": user_prompt_solution}]\n",
")\n",
"\n",
"business_solution = response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display(Markdown(business_solution))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# First Agentic AI workflow with Groq and Llama-3.3 LLM(Free of cost) "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# First let's do an import\n",
"from dotenv import load_dotenv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Next it's time to load the API keys into environment variables\n",
"\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Check the Groq API key\n",
"\n",
"import os\n",
"groq_api_key = os.getenv('GROQ_API_KEY')\n",
"\n",
"if groq_api_key:\n",
" print(f\"GROQ API Key exists and begins {groq_api_key[:8]}\")\n",
"else:\n",
" print(\"GROQ API Key not set\")\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 guide\n",
"\n",
"from groq import Groq"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Create a Groq instance\n",
"groq = Groq()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Create a list of messages in the familiar Groq format\n",
"\n",
"messages = [{\"role\": \"user\", \"content\": \"What is 2+2?\"}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# And now call it!\n",
"\n",
"response = groq.chat.completions.create(model='llama-3.3-70b-versatile', messages=messages)\n",
"print(response.choices[0].message.content)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"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": null,
"metadata": {},
"outputs": [],
"source": [
"# ask it\n",
"response = groq.chat.completions.create(\n",
" model=\"llama-3.3-70b-versatile\",\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": null,
"metadata": {},
"outputs": [],
"source": [
"# Ask it again\n",
"\n",
"response = groq.chat.completions.create(\n",
" model=\"llama-3.3-70b-versatile\",\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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n",
" First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n",
" Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n",
" Finally have 3 third LLM call propose the Agentic AI solution.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"# First create the messages:\n",
"\n",
"messages = [{\"role\": \"user\", \"content\": \"Give me a business area that might be ripe for an Agentic AI solution.\"}]\n",
"\n",
"# Then make the first call:\n",
"\n",
"response = groq.chat.completions.create(model='llama-3.3-70b-versatile', messages=messages)\n",
"\n",
"# Then read the business idea:\n",
"\n",
"business_idea = response.choices[0].message.content\n",
"\n",
"\n",
"# And repeat!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"display(Markdown(business_idea))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# Update the message with the business idea from previous step\n",
"messages = [{\"role\": \"user\", \"content\": \"What is the pain point in the business area of \" + business_idea + \"?\"}]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"# Make the second call\n",
"response = groq.chat.completions.create(model='llama-3.3-70b-versatile', messages=messages)\n",
"# Read the pain point\n",
"pain_point = response.choices[0].message.content\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display(Markdown(pain_point))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Make the third call\n",
"messages = [{\"role\": \"user\", \"content\": \"What is the Agentic AI solution for the pain point of \" + pain_point + \"?\"}]\n",
"response = groq.chat.completions.create(model='llama-3.3-70b-versatile', messages=messages)\n",
"# Read the agentic solution\n",
"agentic_solution = response.choices[0].message.content\n",
"display(Markdown(agentic_solution))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome to the start of your adventure in Agentic AI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Are you ready for action??</h2>\n",
" <span style=\"color:#ff7800;\">Have you completed all the setup steps in the <a href=\"../setup/\">setup</a> folder?<br/>\n",
" Have you read the <a href=\"../README.md\">README</a>? Many common questions are answered here!<br/>\n",
" Have you checked out the guides in the <a href=\"../guides/01_intro.ipynb\">guides</a> folder?<br/>\n",
" Well in that case, you're ready!!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">This code is a live resource - keep an eye out for my updates</h2>\n",
" <span style=\"color:#00bfff;\">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.<br/><br/>\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",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"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": 12,
"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": null,
"metadata": {},
"outputs": [],
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Final reminders</h2>\n",
" <span style=\"color:#ff7800;\">1. If you're not confident about Environment Variables or Web Endpoints / APIs, please read Topics 3 and 5 in this <a href=\"../guides/04_technical_foundations.ipynb\">technical foundations guide</a>.<br/>\n",
" 2. If you want to use AIs other than OpenAI, like Gemini, DeepSeek or Ollama (free), please see the first section in this <a href=\"../guides/09_ai_apis_and_ollama.ipynb\">AI APIs guide</a>.<br/>\n",
" 3. If you ever get a Name Error in Python, you can always fix it immediately; see the last section of this <a href=\"../guides/06_python_foundations.ipynb\">Python Foundations guide</a> and follow both tutorials and exercises.<br/>\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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": 15,
"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",
"# Even for other LLM providers like Gemini, you still use this OpenAI import - see Guide 9 for why\n",
"\n",
"from openai import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": 16,
"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": 17,
"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": null,
"metadata": {},
"outputs": [],
"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-mini\",\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": null,
"metadata": {},
"outputs": [],
"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": null,
"metadata": {},
"outputs": [],
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n",
" First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n",
" Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n",
" Finally have 3 third LLM call propose the Agentic AI solution. <br/>\n",
" We will cover this at up-coming labs, so don't worry if you're unsure.. just give it a try!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Step 1: Define the conversation\n",
"messages = [\n",
" {\"role\": \"system\", \"content\": \"You are an expert in agentic AI business ideation.\"}\n",
"]\n",
"\n",
"# Step 2: Ask the first question\n",
"area_prompt = (\n",
" \"Pick a business area within Singapore startups as of Q4 2025 \"\n",
" \"that might be worth exploring for an Agentic AI opportunity. \"\n",
" \"Explain in simple language (for a 15-year-old) and cite resources briefly.\"\n",
")\n",
"messages.append({\"role\": \"user\", \"content\": area_prompt})\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"area = response.choices[0].message.content\n",
"display(Markdown(area))\n",
"\n",
"# Add model response to context\n",
"messages.append({\"role\": \"assistant\", \"content\": area})\n",
"\n",
"# Step 3: Ask for a pain point\n",
"painpoint_prompt = (\n",
" \"Based on your previous response, pick a recurring pain point in that area \"\n",
" \"that is ripe for an Agentic AI solution.\"\n",
")\n",
"messages.append({\"role\": \"user\", \"content\": painpoint_prompt})\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"painpoint = response.choices[0].message.content\n",
"display(Markdown(painpoint))\n",
"\n",
"# Add model response to context\n",
"messages.append({\"role\": \"assistant\", \"content\": painpoint})\n",
"\n",
"# Step 4: Propose a business idea\n",
"business_idea_prompt = (\n",
" \"Propose an Agentic AI solution addressing the pain point above. \"\n",
" \"Solution should have low overhead, be secure, and offer 80% free functionality, \"\n",
" \"with full access for SGD 0.99/month per user or SGD 15/org (max 30 users).\"\n",
")\n",
"messages.append({\"role\": \"user\", \"content\": business_idea_prompt})\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"business_idea = response.choices[0].message.content\n",
"display(Markdown(business_idea))\n",
"\n",
"# Add to conversation (for future iterations)\n",
"#messages.append({\"role\": \"assistant\", \"content\": business_idea})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome to the start of your adventure in Agentic AI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Are you ready for action??</h2>\n",
" <span style=\"color:#ff7800;\">Have you completed all the setup steps in the <a href=\"../setup/\">setup</a> folder?<br/>\n",
" Have you read the <a href=\"../README.md\">README</a>? Many common questions are answered here!<br/>\n",
" Have you checked out the guides in the <a href=\"../guides/01_intro.ipynb\">guides</a> folder?<br/>\n",
" Well in that case, you're ready!!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">This code is a live resource - keep an eye out for my updates</h2>\n",
" <span style=\"color:#00bfff;\">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.<br/><br/>\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",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"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": null,
"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": null,
"metadata": {},
"outputs": [],
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Final reminders</h2>\n",
" <span style=\"color:#ff7800;\">1. If you're not confident about Environment Variables or Web Endpoints / APIs, please read Topics 3 and 5 in this <a href=\"../guides/04_technical_foundations.ipynb\">technical foundations guide</a>.<br/>\n",
" 2. If you want to use AIs other than OpenAI, like Gemini, DeepSeek or Ollama (free), please see the first section in this <a href=\"../guides/09_ai_apis_and_ollama.ipynb\">AI APIs guide</a>.<br/>\n",
" 3. If you ever get a Name Error in Python, you can always fix it immediately; see the last section of this <a href=\"../guides/06_python_foundations.ipynb\">Python Foundations guide</a> and follow both tutorials and exercises.<br/>\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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": null,
"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",
"# Even for other LLM providers like Gemini, you still use this OpenAI import - see Guide 9 for why\n",
"\n",
"from openai import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": null,
"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": null,
"metadata": {},
"outputs": [],
"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": null,
"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": null,
"metadata": {},
"outputs": [],
"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": null,
"metadata": {},
"outputs": [],
"source": [
"# form a new messages list\n",
"messages = [{\"role\": \"user\", \"content\": question}]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n",
" First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n",
" Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n",
" Finally have 3 third LLM call propose the Agentic AI solution. <br/>\n",
" We will cover this at up-coming labs, so don't worry if you're unsure.. just give it a try!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# First create the messages:\n",
"question = \"Pick a business area that may have agentic AI opportunities\"\n",
"messages = [{\"role\": \"user\", \"content\": question}]\n",
"\n",
"# Then make the first call:\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"\n",
"# Then read the business idea:\n",
"\n",
"business_idea = response.choices[0].message.content\n",
"print(business_idea)\n",
"\n",
"# And repeat! In the next message, include the business idea within the message"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"messages = [{\"role\": \"user\", \"content\": question + \"\\n\\n\" + business_idea},\n",
" {\"role\": \"assistant\", \"content\": \"What is the pain point in this industry?\" }]\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"\n",
"pain_point = response.choices[0].message.content\n",
"print(pain_point)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"messages = [{\"role\": \"user\", \"content\": question + \"\\n\\n\" + business_idea + \"\\n\\n\" + pain_point}, \n",
" {\"role\": \"assistant\", \"content\": \"What is the Agentic AI solution?\"}]\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"\n",
"agentic_solution = response.choices[0].message.content\n",
"print(agentic_solution)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"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.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome to the start of your adventure in Agentic AI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Are you ready for action??</h2>\n",
" <span style=\"color:#ff7800;\">Have you completed all the setup steps in the <a href=\"../setup/\">setup</a> folder?<br/>\n",
" Have you read the <a href=\"../README.md\">README</a>? Many common questions are answered here!<br/>\n",
" Have you checked out the guides in the <a href=\"../guides/01_intro.ipynb\">guides</a> folder?<br/>\n",
" Well in that case, you're ready!!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">This code is a live resource - keep an eye out for my updates</h2>\n",
" <span style=\"color:#00bfff;\">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.<br/><br/>\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",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"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": null,
"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": null,
"metadata": {},
"outputs": [],
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Final reminders</h2>\n",
" <span style=\"color:#ff7800;\">1. If you're not confident about Environment Variables or Web Endpoints / APIs, please read Topics 3 and 5 in this <a href=\"../guides/04_technical_foundations.ipynb\">technical foundations guide</a>.<br/>\n",
" 2. If you want to use AIs other than OpenAI, like Gemini, DeepSeek or Ollama (free), please see the first section in this <a href=\"../guides/09_ai_apis_and_ollama.ipynb\">AI APIs guide</a>.<br/>\n",
" 3. If you ever get a Name Error in Python, you can always fix it immediately; see the last section of this <a href=\"../guides/06_python_foundations.ipynb\">Python Foundations guide</a> and follow both tutorials and exercises.<br/>\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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": null,
"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",
"# Even for other LLM providers like Gemini, you still use this OpenAI import - see Guide 9 for why\n",
"\n",
"from openai import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": null,
"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": null,
"metadata": {},
"outputs": [],
"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": null,
"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": null,
"metadata": {},
"outputs": [],
"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": null,
"metadata": {},
"outputs": [],
"source": [
"# form a new messages list\n",
"messages = [{\"role\": \"user\", \"content\": question}]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n",
" First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n",
" Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n",
" Finally have 3 third LLM call propose the Agentic AI solution. <br/>\n",
" We will cover this at up-coming labs, so don't worry if you're unsure.. just give it a try!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# First create the messages:\n",
"\n",
"messages = [{\"role\": \"user\", \"content\": \"Pick up a business area where Agentic AI can be applied. Provide only the business area name.\"}]\n",
"\n",
"# Then make the first call:\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"\n",
"# Then read the business idea:\n",
"\n",
"business_idea = response.choices[0].message.content\n",
"display(Markdown(business_idea))\n",
"\n",
"# And repeat! In the next message, include the business idea within the message\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# First create the messages:\n",
"\n",
"messages = [{\"role\": \"user\", \"content\": f\"Provide one painpoint in the business ${business_idea} that can be solved by Agentic AI. Provide only the pain point, nothing else.\"}]\n",
"\n",
"# Then make the first call:\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"\n",
"# Then read the business idea:\n",
"\n",
"pain_point = response.choices[0].message.content\n",
"display(Markdown(pain_point))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# First create the messages:\n",
"\n",
"messages = [{\"role\": \"user\", \"content\": f\"Provide one solution that uses Agentic AI to solve the pain point ${pain_point}, in the business ${business_idea}. \\\n",
" Provide details as bullet points in less than 100 words. Precede it with the Business Area and the pain point it is solving.\"}]\n",
"\n",
"# Then make the first call:\n",
"\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4.1-mini\",\n",
" messages=messages\n",
")\n",
"\n",
"# Then read the business idea:\n",
"\n",
"solution = response.choices[0].message.content\n",
"display(Markdown(solution))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome to the start of your adventure in Agentic AI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Are you ready for action??</h2>\n",
" <span style=\"color:#ff7800;\">Have you completed all the setup steps in the <a href=\"../setup/\">setup</a> folder?<br/>\n",
" Have you checked out the guides in the <a href=\"../guides/01_intro.ipynb\">guides</a> folder?<br/>\n",
" Well in that case, you're ready!!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">This code is a live resource - keep an eye out for my updates</h2>\n",
" <span style=\"color:#00bfff;\">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.<br/><br/>\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",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"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": 76,
"metadata": {},
"outputs": [],
"source": [
"# First let's do an import\n",
"from dotenv import load_dotenv\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Next it's time to load the API keys into environment variables\n",
"\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Check the keys\n",
"\n",
"import os\n",
"open_router_api_key = os.getenv('OPEN_ROUTER_API_KEY')\n",
"\n",
"if open_router_api_key:\n",
" print(f\"Open router API Key exists and begins {open_router_api_key[:8]}\")\n",
"else:\n",
" print(\"Open router API Key not set - please head to the troubleshooting guide in the setup folder\")\n"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [],
"source": [
"from openai import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [],
"source": [
"# Initialize the client to point at OpenRouter instead of OpenAI\n",
"# You can use the exact same OpenAI Python package—just swap the base_url!\n",
"client = OpenAI(\n",
" base_url=\"https://openrouter.ai/api/v1\",\n",
" api_key=open_router_api_key\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [],
"source": [
"messages = [{\"role\": \"user\", \"content\": \"What is 2+2?\"}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"client = OpenAI(\n",
" base_url=\"https://openrouter.ai/api/v1\",\n",
" api_key=open_router_api_key\n",
")\n",
"\n",
"resp = client.chat.completions.create(\n",
" # Select a model from https://openrouter.ai/models and provide the model name here\n",
" model=\"meta-llama/llama-3.3-8b-instruct:free\",\n",
" messages=messages\n",
")\n",
"print(resp.choices[0].message.content)"
]
},
{
"cell_type": "code",
"execution_count": 83,
"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}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response = client.chat.completions.create(\n",
" model=\"meta-llama/llama-3.3-8b-instruct:free\",\n",
" messages=messages\n",
")\n",
"\n",
"question = response.choices[0].message.content\n",
"\n",
"print(question)"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [],
"source": [
"# form a new messages list\n",
"\n",
"messages = [{\"role\": \"user\", \"content\": question}]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Ask it again\n",
"\n",
"response = client.chat.completions.create(\n",
" model=\"meta-llama/llama-3.3-8b-instruct:free\",\n",
" messages=messages\n",
")\n",
"\n",
"answer = response.choices[0].message.content\n",
"print(answer)"
]
},
{
"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": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Now try this commercial application:<br/>\n",
" First ask the LLM to pick a business area that might be worth exploring for an Agentic AI opportunity.<br/>\n",
" Then ask the LLM to present a pain-point in that industry - something challenging that might be ripe for an Agentic solution.<br/>\n",
" Finally have 3 third LLM call propose the Agentic AI solution.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# First create the messages:\n",
"\n",
"\n",
"messages = [\"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!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from IPython.display import Markdown"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Refresh dot env"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"open_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"google_api_key = os.getenv(\"GOOGLE_API_KEY\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create initial query to get challange reccomendation"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"query = 'Please come up with a challenging, nuanced question that I can ask a number of LLMs to evaluate their intelligence. '\n",
"query += 'Answer only with the question, no explanation.'\n",
"\n",
"messages = [{'role':'user', 'content':query}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(messages)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Call openai gpt-4o-mini "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"\n",
"response = openai.chat.completions.create(\n",
" messages=messages,\n",
" model='gpt-4o-mini'\n",
")\n",
"\n",
"challange = response.choices[0].message.content\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(challange)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"competitors = []\n",
"answers = []"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create messages with the challange query"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"messages = [{'role':'user', 'content':challange}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(messages)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"from threading import Thread"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def gpt_mini_processor():\n",
" modleName = 'gpt-4o-mini'\n",
" competitors.append(modleName)\n",
" response_gpt = openai.chat.completions.create(\n",
" messages=messages,\n",
" model=modleName\n",
" )\n",
" answers.append(response_gpt.choices[0].message.content)\n",
"\n",
"def gemini_processor():\n",
" gemini = OpenAI(api_key=google_api_key, base_url='https://generativelanguage.googleapis.com/v1beta/openai/')\n",
" modleName = 'gemini-2.0-flash'\n",
" competitors.append(modleName)\n",
" response_gemini = gemini.chat.completions.create(\n",
" messages=messages,\n",
" model=modleName\n",
" )\n",
" answers.append(response_gemini.choices[0].message.content)\n",
"\n",
"def llama_processor():\n",
" ollama = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
" modleName = 'llama3.2'\n",
" competitors.append(modleName)\n",
" response_llama = ollama.chat.completions.create(\n",
" messages=messages,\n",
" model=modleName\n",
" )\n",
" answers.append(response_llama.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Paraller execution of LLM calls"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"thread1 = Thread(target=gpt_mini_processor)\n",
"thread2 = Thread(target=gemini_processor)\n",
"thread3 = Thread(target=llama_processor)\n",
"\n",
"thread1.start()\n",
"thread2.start()\n",
"thread3.start()\n",
"\n",
"thread1.join()\n",
"thread2.join()\n",
"thread3.join()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(competitors)\n",
"print(answers)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for competitor, answer in zip(competitors, answers):\n",
" print(f'Competitor:{competitor}\\n\\n{answer}')"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"together = ''\n",
"for index, answer in enumerate(answers):\n",
" together += f'# Response from competitor {index + 1}\\n\\n'\n",
" together += answer + '\\n\\n'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(together)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Prompt to judge the LLM results"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"to_judge = f'''You are judging a competition between {len(competitors)} competitors.\n",
"Each model has been given this question:\n",
"\n",
"{challange}\n",
"\n",
"Your job is to evaluate each response for clarity and strength of argument, and rank them in order of best to worst.\n",
"Respond with JSON, and only JSON, with the following format:\n",
"{{\"results\": [\"best competitor number\", \"second best competitor number\", \"third best competitor number\", ...]}}\n",
"\n",
"Here are the responses from each competitor:\n",
"\n",
"{together}\n",
"\n",
"Now respond with the JSON with the ranked order of the competitors, nothing else. Do not include markdown formatting or code blocks.\"\"\"\n",
"\n",
"'''"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"to_judge_message = [{'role':'user', 'content':to_judge}]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Execute o3-mini to analyze the LLM results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" messages=to_judge_message,\n",
" model='o3-mini'\n",
")\n",
"result = response.choices[0].message.content\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"results_dict = json.loads(result)\n",
"ranks = results_dict[\"results\"]\n",
"for index, result in enumerate(ranks):\n",
" competitor = competitors[int(result)-1]\n",
" print(f\"Rank {index+1}: {competitor}\")"
]
}
],
"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": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Judging and Routing — Optimizing Resource Usage by Evaluating Problem Complexity"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the original Lab 2, we explored the **Orchestrator–Worker pattern**, where a planner sent the same question to multiple agents, and a judge assessed their responses to evaluate agent intelligence.\n",
"\n",
"In this notebook, we extend that design by adding multiple judges and a routing component to optimize model usage based on task complexity. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Imports and Environment Setup"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from anthropic import Anthropic\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n",
"if openai_api_key and google_api_key and deepseek_api_key:\n",
" print(\"All keys were loaded successfully\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!ollama pull llama3.2\n",
"!ollama pull mistral"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Creating Models"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The notebook uses instances of GPT, Gemini and DeepSeek APIs, along with two local models served via Ollama: ```llama3.2``` and ```mistral```."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"model_specs = {\n",
" \"gpt-4o-mini\" : None,\n",
" \"gemini-2.0-flash\": {\n",
" \"api_key\" : google_api_key,\n",
" \"url\" : \"https://generativelanguage.googleapis.com/v1beta/openai/\"\n",
" },\n",
" \"deepseek-chat\" : {\n",
" \"api_key\" : deepseek_api_key,\n",
" \"url\" : \"https://api.deepseek.com/v1\"\n",
" },\n",
" \"llama3.2\" : {\n",
" \"api_key\" : \"ollama\",\n",
" \"url\" : \"http://localhost:11434/v1\"\n",
" },\n",
" \"mistral\" : {\n",
" \"api_key\" : \"ollama\",\n",
" \"url\" : \"http://localhost:11434/v1\"\n",
" }\n",
"}\n",
"\n",
"def create_model(model_name):\n",
" spec = model_specs[model_name]\n",
" if spec is None:\n",
" return OpenAI()\n",
" \n",
" return OpenAI(api_key=spec[\"api_key\"], base_url=spec[\"url\"])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"orchestrator_model = \"gemini-2.0-flash\"\n",
"generator = create_model(orchestrator_model)\n",
"router = create_model(orchestrator_model)\n",
"\n",
"qa_models = {\n",
" model_name : create_model(model_name) \n",
" for model_name in model_specs.keys()\n",
"}\n",
"\n",
"judges = {\n",
" model_name : create_model(model_name) \n",
" for model_name, specs in model_specs.items() \n",
" if not(specs) or specs[\"api_key\"] != \"ollama\"\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Orchestrator-Worker Workflow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, we generate a question to evaluate the intelligence of each LLM."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"request = \"Please come up with a challenging, nuanced question that I can ask a number of LLMs \"\n",
"request += \"to evaluate and rank them based on their intelligence. \" \n",
"request += \"Answer **only** with the question, no explanation or preamble.\"\n",
"\n",
"messages = [{\"role\": \"user\", \"content\": request}]\n",
"messages"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"response = generator.chat.completions.create(\n",
" model=orchestrator_model,\n",
" messages=messages,\n",
")\n",
"eval_question = response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display(Markdown(eval_question))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task Parallelization"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, having the question and all the models instantiated it's time to see what each model has to say about the complex task it was given."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"question = [{\"role\": \"user\", \"content\": eval_question}]\n",
"answers = []\n",
"competitors = []\n",
"\n",
"for name, model in qa_models.items():\n",
" response = model.chat.completions.create(model=name, messages=question)\n",
" answer = response.choices[0].message.content\n",
" competitors.append(name)\n",
" answers.append(answer)\n",
"\n",
"answers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"report = \"# Answer report for each of the 5 models\\n\\n\"\n",
"report += \"\\n\\n\".join([f\"## **Model: {model}**\\n\\n{answer}\" for model, answer in zip(competitors, answers)])\n",
"display(Markdown(report))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Synthetizer/Judge"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Judge Agents ranks the LLM responses based on coherence and relevance to the evaluation prompt. Judges vote and the final LLM ranking is based on the aggregated ranking of all three judges."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"together = \"\"\n",
"for index, answer in enumerate(answers):\n",
" together += f\"# Response from competitor {index+1}\\n\\n\"\n",
" together += answer + \"\\n\\n\"\n",
"\n",
"together"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"judge_prompt = f\"\"\"\n",
" You are judging a competition between {len(competitors)} LLM competitors.\n",
" Each model has been given this nuanced question to evaluate their intelligence:\n",
"\n",
" {eval_question}\n",
"\n",
" Your job is to evaluate each response for clarity and strength of argument, and rank them in order of best to worst.\n",
" Respond with JSON, and only JSON, with the following format:\n",
" {{\"results\": [\"best competitor number\", \"second best competitor number\", \"third best competitor number\", ...]}}\n",
" With 'best competitor number being ONLY the number', for instance:\n",
" {{\"results\": [\"5\", \"2\", \"4\", ...]}}\n",
" Here are the responses from each competitor:\n",
"\n",
" {together}\n",
"\n",
" Now respond with the JSON with the ranked order of the competitors, nothing else. Do NOT include MARKDOWN FORMATTING or CODE BLOCKS. ONLY the JSON\n",
" \"\"\"\n",
"\n",
"judge_messages = [{\"role\": \"user\", \"content\": judge_prompt}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from collections import defaultdict\n",
"import re\n",
"\n",
"N = len(competitors)\n",
"scores = defaultdict(int)\n",
"for judge_name, judge in judges.items():\n",
" response = judge.chat.completions.create(\n",
" model=judge_name,\n",
" messages=judge_messages,\n",
" )\n",
" response = response.choices[0].message.content\n",
" response_json = re.findall(r'\\{.*?\\}', response)[0]\n",
" results = json.loads(response_json)[\"results\"]\n",
" ranks = [int(result) for result in results]\n",
" print(f\"Judge {judge_name} ranking:\")\n",
" for i, c in enumerate(ranks):\n",
" model_name = competitors[c - 1]\n",
" print(f\"#{i+1} : {model_name}\")\n",
" scores[c - 1] += (N - i)\n",
" print()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sorted_indices = sorted(scores, key=scores.get)\n",
"\n",
"# Convert to model names\n",
"ranked_model_names = [competitors[i] for i in sorted_indices]\n",
"\n",
"print(\"Final ranking from best to worst:\")\n",
"for i, name in enumerate(ranked_model_names[::-1], 1):\n",
" print(f\"#{i}: {name}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Routing Workflow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We now define a routing agent responsible for classifying task complexity and delegating the prompt to the most appropriate model."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"def classify_question_complexity(question: str, routing_agent, routing_model) -> int:\n",
" \"\"\"\n",
" Ask an LLM to classify the question complexity from 1 (easy) to 5 (very hard).\n",
" \"\"\"\n",
" prompt = f\"\"\"\n",
" You are a classifier responsible for assigning a complexity level to user questions, based on how difficult they would be for a language model to answer.\n",
"\n",
" Please read the question below and assign a complexity score from 1 to 5:\n",
"\n",
" - Level 1: Very simple factual or definitional question (e.g., “What is the capital of France?”)\n",
" - Level 2: Slightly more involved, requiring basic reasoning or comparison\n",
" - Level 3: Moderate complexity, requiring synthesis, context understanding, or multi-part answers\n",
" - Level 4: High complexity, requiring abstract thinking, ethical judgment, or creative generation\n",
" - Level 5: Extremely challenging, requiring deep reasoning, philosophical reflection, or long-term multi-step inference\n",
"\n",
" Respond ONLY with a single integer between 1 and 5 that best reflects the complexity of the question.\n",
"\n",
" Question:\n",
" {question}\n",
" \"\"\"\n",
"\n",
" response = routing_agent.chat.completions.create(\n",
" model=routing_model,\n",
" messages=[{\"role\": \"user\", \"content\": prompt}]\n",
" )\n",
" try:\n",
" return int(response.choices[0].message.content.strip())\n",
" except Exception:\n",
" return 3 # default to medium complexity on error\n",
" \n",
"def route_question_to_model(question: str, models_by_rank, classifier_model=router, model_name=orchestrator_model):\n",
" level = classify_question_complexity(question, classifier_model, model_name)\n",
" selected_model_name = models_by_rank[level - 1]\n",
" return selected_model_name"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"difficulty_prompts = [\n",
" \"Generate a very basic, factual question that a small or entry-level language model could answer easily. It should require no reasoning, just direct knowledge lookup.\",\n",
" \"Generate a slightly involved question that requires basic reasoning, comparison, or combining two known facts. Still within the grasp of small models but not purely factual.\",\n",
" \"Generate a moderately challenging question that requires some synthesis of ideas, multi-step reasoning, or contextual understanding. A mid-tier model should be able to answer it with effort.\",\n",
" \"Generate a difficult question involving abstract thinking, open-ended reasoning, or ethical tradeoffs. The question should challenge large models to produce thoughtful and coherent responses.\",\n",
" \"Generate an extremely complex and nuanced question that tests the limits of current language models. It should require deep reasoning, long-term planning, philosophy, or advanced multi-domain knowledge.\"\n",
"]\n",
"def generate_question(level, generator=generator, generator_model=orchestrator_model):\n",
" prompt = (\n",
" f\"{difficulty_prompts[level - 1]}\\n\"\n",
" \"Answer only with the question, no explanation.\"\n",
" )\n",
" messages = [{\"role\": \"user\", \"content\": prompt}]\n",
" response = generator.chat.completions.create(\n",
" model=generator_model, # or your planner model\n",
" messages=messages\n",
" )\n",
" \n",
" return response.choices[0].message.content\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Testing Routing Workflow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, to test the routing workflow, we create a function that accepts a task complexity level and triggers the full routing process.\n",
"\n",
"*Note: A level-N prompt isn't always assigned to the Nth-most capable model due to the classifier's subjective decisions.*"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def test_generation_routing(level):\n",
" question = generate_question(level=level)\n",
" answer_model = route_question_to_model(question, ranked_model_names)\n",
" messages = [{\"role\": \"user\", \"content\": question}]\n",
"\n",
" response =qa_models[answer_model].chat.completions.create(\n",
" model=answer_model, # or your planner model\n",
" messages=messages\n",
" )\n",
" print(f\"Question : {question}\")\n",
" print(f\"Routed to {answer_model}\")\n",
" display(Markdown(response.choices[0].message.content))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_generation_routing(level=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_generation_routing(level=2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_generation_routing(level=3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_generation_routing(level=4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_generation_routing(level=5)"
]
}
],
"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.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133{
"cells": [
{
"cell_type": "markdown",
"id": "8c8f2d93",
"metadata": {},
"source": [
"# 🏥 MedTech AI Opportunity Finder\n",
"\n",
"- 🌍 Task: Generate quirky healthcare/pharma AI business opportunities with pain points and solutions.\n",
"- 🧠 Model: Uses OpenAI GPT-4o-mini for creative business idea generation\n",
"- 🎯 Process: Three-step pipeline - Business Area → Pain Point → AI Solution\n",
"- 📌 Output Format: Markdown-formatted responses streamed in real-time with humor\n",
"- 🔧 Tools: OpenAI API and IPython display for interactive streaming\n",
"- 🧑💻 Skill Level: Beginner\n",
"\n",
"🛠️ Requirements\n",
"- ⚙️ Hardware: ✅ CPU is sufficient — no GPU required\n",
"- 🔑 OpenAI API Key\n",
"- IPython environment (Jupyter/Colab)\n",
"\n",
"---\n",
"📢 Discover more Agentic AI notebooks on my [GitHub repository](https://github.com/lisekarimi/agentverse) and explore additional AI projects on my [portfolio](https://lisekarimi.com)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1df27837",
"metadata": {},
"outputs": [],
"source": [
"from openai import OpenAI\n",
"from IPython.display import display, Markdown, update_display"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b197c72a",
"metadata": {},
"outputs": [],
"source": [
"client = OpenAI() # Automatically finds OPENAI_API_KEY without needing os.getenv() or load_dotenv()."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc8064bb",
"metadata": {},
"outputs": [],
"source": [
"def stream_response(messages, section_title):\n",
" \"\"\"Stream response and display with real-time updates\"\"\"\n",
" response_stream = client.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=messages,\n",
" stream=True\n",
" )\n",
"\n",
" response = \"\"\n",
" display_handle = display(Markdown(f\"## {section_title}\\n\\n\"), display_id=True)\n",
"\n",
" for chunk in response_stream:\n",
" if chunk.choices[0].delta.content:\n",
" response += chunk.choices[0].delta.content\n",
" # Clean up any unwanted markdown artifacts\n",
" cleaned_response = response.replace(\"```\", \"\").replace(\"markdown\", \"\")\n",
" update_display(Markdown(f\"## {section_title}\\n\\n{cleaned_response}\"), display_id=display_handle.display_id)\n",
"\n",
" return response"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "857e0458",
"metadata": {},
"outputs": [],
"source": [
"# Step 1: Business area\n",
"messages = [{\"role\": \"user\", \"content\": \"Give me a quirky healthcare or pharma business area for an AI agent. Keep it short and clear.\"}]\n",
"business_idea = stream_response(messages, \"🏢 Business Area\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23838465",
"metadata": {},
"outputs": [],
"source": [
"# Step 2: Pain point\n",
"messages = [{\"role\": \"user\", \"content\": f\"What's broken about {business_idea}? Short and funny.\"}]\n",
"pain_point = stream_response(messages, \"😵 What's Broken\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5aa70151",
"metadata": {},
"outputs": [],
"source": [
"# Step 3: AI solution\n",
"messages = [{\"role\": \"user\", \"content\": f\"How would an AI agent solve this {pain_point}? Brief and clear.\"}]\n",
"solution = stream_response(messages, \"🤖 AI to the Rescue\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "agentverse",
"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": 5
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "73a4a1cd",
"metadata": {},
"outputs": [],
"source": [
"# Imports required for this exercise\n",
"from dotenv import load_dotenv\n",
"import os\n",
"from openai import OpenAI\n",
"from IPython.display import Markdown,display"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "be280e57",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# load the env variables from \".env\" file\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "cc673c30",
"metadata": {},
"outputs": [],
"source": [
"# Check if the API key is set\n",
"api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"if not api_key:\n",
" raise ValueError(\"OPENAI_API_KEY is not set!!\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c574ee90",
"metadata": {},
"outputs": [],
"source": [
"# Create an instance of the openai python client\n",
"openai_client = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "da2d3820",
"metadata": {},
"outputs": [],
"source": [
"# First ask LLM to identify the most important pain point in the Cybersecurity domain that companies need to\n",
"# focus on in 2026\n",
"message = \"\"\"\n",
"What is one top area for cybersecurity organizations to focus on in 2026? Limit your answer to 3-5 sentences.\n",
"Only return the top pain point and no additional details on solution. Respond in markdown without code blocks.\n",
"\"\"\"\n",
"messages = [\n",
" {\"role\": \"user\", \"content\": message}\n",
"]\n",
"\n",
"MODEL_NAME=\"gpt-4o-mini\"\n",
"\n",
"response = openai_client.chat.completions.create(\n",
" model=MODEL_NAME,\n",
" messages=messages\n",
")\n",
"\n",
"painpoint = response.choices[0].message.content\n",
"display(Markdown(painpoint))"
]
}
],
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Welcome to the Second Lab - Week 1, Day 3\n",
"\n",
"Today we will work with lots of models! This is a way to get comfortable with APIs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Important point - please read</h2>\n",
" <span style=\"color:#ff7800;\">The way I collaborate with you may be different to other courses you've taken. I prefer not to type code while you watch. Rather, I execute Jupyter Labs, like this, and give you an intuition for what's going on. My suggestion is that you carefully execute this yourself, <b>after</b> watching the lecture. Add print statements to understand what's going on, and then come up with your own variations.<br/><br/>If you have time, I'd love it if you submit a PR for changes in the community_contributions folder - instructions in the resources. Also, if you have a Github account, use this to showcase your variations. Not only is this essential practice, but it demonstrates your skills to others, including perhaps future clients or employers...\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Start with imports - ask ChatGPT to explain any package that you don't know\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from anthropic import Anthropic\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Always remember to do this!\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Print the key prefixes to help with any debugging\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n",
"groq_api_key = os.getenv('GROQ_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\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set (and this is optional)\")\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:2]}\")\n",
"else:\n",
" print(\"Google API Key not set (and this is optional)\")\n",
"\n",
"if deepseek_api_key:\n",
" print(f\"DeepSeek API Key exists and begins {deepseek_api_key[:3]}\")\n",
"else:\n",
" print(\"DeepSeek API Key not set (and this is optional)\")\n",
"\n",
"if groq_api_key:\n",
" print(f\"Groq API Key exists and begins {groq_api_key[:4]}\")\n",
"else:\n",
" print(\"Groq API Key not set (and this is optional)\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"request = \"Please come up with a challenging, nuanced question that I can ask a number of LLMs to evaluate their intelligence. \"\n",
"request += \"Answer only with the question, no explanation.\"\n",
"messages = [{\"role\": \"user\", \"content\": request}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"messages"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=messages,\n",
")\n",
"question = response.choices[0].message.content\n",
"print(question)\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"competitors = []\n",
"answers = []\n",
"messages = [{\"role\": \"user\", \"content\": question}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# The API we know well\n",
"\n",
"model_name = \"gpt-4o-mini\"\n",
"\n",
"response = openai.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Anthropic has a slightly different API, and Max Tokens is required\n",
"\n",
"model_name = \"claude-3-7-sonnet-latest\"\n",
"\n",
"claude = Anthropic()\n",
"response = claude.messages.create(model=model_name, messages=messages, max_tokens=1000)\n",
"answer = response.content[0].text\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gemini = OpenAI(api_key=google_api_key, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")\n",
"model_name = \"gemini-2.0-flash\"\n",
"\n",
"response = gemini.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"deepseek = OpenAI(api_key=deepseek_api_key, base_url=\"https://api.deepseek.com/v1\")\n",
"model_name = \"deepseek-chat\"\n",
"\n",
"response = deepseek.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"groq = OpenAI(api_key=groq_api_key, base_url=\"https://api.groq.com/openai/v1\")\n",
"model_name = \"llama-3.3-70b-versatile\"\n",
"\n",
"response = groq.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## For the next cell, we will use Ollama\n",
"\n",
"Ollama runs a local web service that gives an OpenAI compatible endpoint, \n",
"and runs models locally using high performance C++ code.\n",
"\n",
"If you don't have Ollama, install it here by visiting https://ollama.com then pressing Download and following the instructions.\n",
"\n",
"After it's installed, you should be able to visit here: http://localhost:11434 and see the message \"Ollama is running\"\n",
"\n",
"You might need to restart Cursor (and maybe reboot). Then open a Terminal (control+\\`) and run `ollama serve`\n",
"\n",
"Useful Ollama commands (run these in the terminal, or with an exclamation mark in this notebook):\n",
"\n",
"`ollama pull <model_name>` downloads a model locally \n",
"`ollama ls` lists all the models you've downloaded \n",
"`ollama rm <model_name>` deletes the specified model from your downloads"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Super important - ignore me at your peril!</h2>\n",
" <span style=\"color:#ff7800;\">The model called <b>llama3.3</b> is FAR too large for home computers - it's not intended for personal computing and will consume all your resources! Stick with the nicely sized <b>llama3.2</b> or <b>llama3.2:1b</b> and if you want larger, try llama3.1 or smaller variants of Qwen, Gemma, Phi or DeepSeek. See the <A href=\"https://ollama.com/models\">the Ollama models page</a> for a full list of models and sizes.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ollama = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
"model_name = \"llama3.2\"\n",
"\n",
"response = ollama.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# So where are we?\n",
"\n",
"print(competitors)\n",
"print(answers)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# It's nice to know how to use \"zip\"\n",
"for competitor, answer in zip(competitors, answers):\n",
" print(f\"Competitor: {competitor}\\n\\n{answer}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"# Let's bring this together - note the use of \"enumerate\"\n",
"\n",
"together = \"\"\n",
"for index, answer in enumerate(answers):\n",
" together += f\"# Response from competitor {index+1}\\n\\n\"\n",
" together += answer + \"\\n\\n\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(together)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"judge = f\"\"\"You are judging a competition between {len(competitors)} competitors.\n",
"Each model has been given this question:\n",
"\n",
"{question}\n",
"\n",
"Your job is to evaluate each response for clarity and strength of argument, and rank them in order of best to worst.\n",
"Respond with JSON, and only JSON, with the following format:\n",
"{{\"results\": [\"best competitor number\", \"second best competitor number\", \"third best competitor number\", ...]}}\n",
"\n",
"Here are the responses from each competitor:\n",
"\n",
"{together}\n",
"\n",
"Now respond with the JSON with the ranked order of the competitors, nothing else. Do not include markdown formatting or code blocks.\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(judge)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"judge_messages = [{\"role\": \"user\", \"content\": judge}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Judgement time!\n",
"\n",
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"o3-mini\",\n",
" messages=judge_messages,\n",
")\n",
"results = response.choices[0].message.content\n",
"print(results)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# OK let's turn this into results!\n",
"\n",
"results_dict = json.loads(results)\n",
"ranks = results_dict[\"results\"]\n",
"for index, result in enumerate(ranks):\n",
" competitor = competitors[int(result)-1]\n",
" print(f\"Rank {index+1}: {competitor}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Which pattern(s) did this use? Try updating this to add another Agentic design pattern.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/business.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">Commercial implications</h2>\n",
" <span style=\"color:#00bfff;\">These kinds of patterns - to send a task to multiple models, and evaluate results,\n",
" are common where you need to improve the quality of your LLM response. This approach can be universally applied\n",
" to business projects where accuracy is critical.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Welcome to the Second Lab - Week 1, Day 3\n",
"\n",
"Today we will work with lots of models! This is a way to get comfortable with APIs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Important point - please read</h2>\n",
" <span style=\"color:#ff7800;\">The way I collaborate with you may be different to other courses you've taken. I prefer not to type code while you watch. Rather, I execute Jupyter Labs, like this, and give you an intuition for what's going on. My suggestion is that you carefully execute this yourself, <b>after</b> watching the lecture. Add print statements to understand what's going on, and then come up with your own variations.<br/><br/>If you have time, I'd love it if you submit a PR for changes in the community_contributions folder - instructions in the resources. Also, if you have a Github account, use this to showcase your variations. Not only is this essential practice, but it demonstrates your skills to others, including perhaps future clients or employers...\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Start with imports - ask ChatGPT to explain any package that you don't know\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from anthropic import Anthropic\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Always remember to do this!\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Print the key prefixes to help with any debugging\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n",
"groq_api_key = os.getenv('GROQ_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\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set (and this is optional)\")\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:2]}\")\n",
"else:\n",
" print(\"Google API Key not set (and this is optional)\")\n",
"\n",
"if deepseek_api_key:\n",
" print(f\"DeepSeek API Key exists and begins {deepseek_api_key[:3]}\")\n",
"else:\n",
" print(\"DeepSeek API Key not set (and this is optional)\")\n",
"\n",
"if groq_api_key:\n",
" print(f\"Groq API Key exists and begins {groq_api_key[:4]}\")\n",
"else:\n",
" print(\"Groq API Key not set (and this is optional)\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"request = \"Please come up with a challenging, nuanced question that I can ask a number of LLMs to evaluate their intelligence. \"\n",
"request += \"Answer only with the question, no explanation.\"\n",
"messages = [{\"role\": \"user\", \"content\": request}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"messages"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=messages,\n",
")\n",
"question = response.choices[0].message.content\n",
"print(question)\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"competitors = []\n",
"answers = []\n",
"messages = [{\"role\": \"user\", \"content\": question}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# The API we know well\n",
"\n",
"model_name = \"gpt-4o-mini\"\n",
"\n",
"response = openai.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Anthropic has a slightly different API, and Max Tokens is required\n",
"\n",
"model_name = \"claude-3-7-sonnet-latest\"\n",
"\n",
"claude = Anthropic()\n",
"response = claude.messages.create(model=model_name, messages=messages, max_tokens=1000)\n",
"answer = response.content[0].text\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gemini = OpenAI(api_key=google_api_key, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")\n",
"model_name = \"gemini-2.0-flash\"\n",
"\n",
"response = gemini.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"deepseek = OpenAI(api_key=deepseek_api_key, base_url=\"https://api.deepseek.com/v1\")\n",
"model_name = \"deepseek-chat\"\n",
"\n",
"response = deepseek.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"groq = OpenAI(api_key=groq_api_key, base_url=\"https://api.groq.com/openai/v1\")\n",
"model_name = \"llama-3.3-70b-versatile\"\n",
"\n",
"response = groq.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## For the next cell, we will use Ollama\n",
"\n",
"Ollama runs a local web service that gives an OpenAI compatible endpoint, \n",
"and runs models locally using high performance C++ code.\n",
"\n",
"If you don't have Ollama, install it here by visiting https://ollama.com then pressing Download and following the instructions.\n",
"\n",
"After it's installed, you should be able to visit here: http://localhost:11434 and see the message \"Ollama is running\"\n",
"\n",
"You might need to restart Cursor (and maybe reboot). Then open a Terminal (control+\\`) and run `ollama serve`\n",
"\n",
"Useful Ollama commands (run these in the terminal, or with an exclamation mark in this notebook):\n",
"\n",
"`ollama pull <model_name>` downloads a model locally \n",
"`ollama ls` lists all the models you've downloaded \n",
"`ollama rm <model_name>` deletes the specified model from your downloads"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Super important - ignore me at your peril!</h2>\n",
" <span style=\"color:#ff7800;\">The model called <b>llama3.3</b> is FAR too large for home computers - it's not intended for personal computing and will consume all your resources! Stick with the nicely sized <b>llama3.2</b> or <b>llama3.2:1b</b> and if you want larger, try llama3.1 or smaller variants of Qwen, Gemma, Phi or DeepSeek. See the <A href=\"https://ollama.com/models\">the Ollama models page</a> for a full list of models and sizes.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ollama = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
"model_name = \"llama3.2\"\n",
"\n",
"response = ollama.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# So where are we?\n",
"\n",
"print(competitors)\n",
"print(answers)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# It's nice to know how to use \"zip\"\n",
"for competitor, answer in zip(competitors, answers):\n",
" print(f\"Competitor: {competitor}\\n\\n{answer}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"# Let's bring this together - note the use of \"enumerate\"\n",
"\n",
"together = \"\"\n",
"for index, answer in enumerate(answers):\n",
" together += f\"# Response from competitor {index+1}\\n\\n\"\n",
" together += answer + \"\\n\\n\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(together)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"judge = f\"\"\"You are judging a competition between {len(competitors)} competitors.\n",
"Each model has been given this question:\n",
"\n",
"{question}\n",
"\n",
"Your job is to evaluate each response for clarity and strength of argument, and rank them in order of best to worst.\n",
"Respond with JSON, and only JSON, with the following format:\n",
"{{\"results\": [\"best competitor number\", \"second best competitor number\", \"third best competitor number\", ...]}}\n",
"Answer only the number for example\n",
"{{\"results\": [\"1\", \"2\", \"3\", ...]}}\n",
"\n",
"Here are the responses from each competitor:\n",
"\n",
"{together}\n",
"\n",
"Now respond with the JSON with the ranked order of the competitors, nothing else. Do not include markdown formatting or code blocks.\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(judge)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"judge_messages = [{\"role\": \"user\", \"content\": judge}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Judgement time!\n",
"\n",
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"o3-mini\",\n",
" messages=judge_messages,\n",
")\n",
"results = response.choices[0].message.content\n",
"print(results)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# OK let's turn this into results!\n",
"\n",
"results_dict = json.loads(results)\n",
"ranks = results_dict[\"results\"]\n",
"for index, result in enumerate(ranks):\n",
" competitor = competitors[int(result)-1]\n",
" print(f\"Rank {index+1}: {competitor}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Which pattern(s) did this use? Try updating this to add another Agentic design pattern.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/business.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">Commercial implications</h2>\n",
" <span style=\"color:#00bfff;\">These kinds of patterns - to send a task to multiple models, and evaluate results,\n",
" are common where you need to improve the quality of your LLM response. This approach can be universally applied\n",
" to business projects where accuracy is critical.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lab 3: Orchestrator-Worker Pattern\n",
"\n",
"This notebook implements the **Orchestrator-Worker Pattern** with **Parallel Execution**:\n",
"\n",
"1. **Orchestrator LLM**: Decides which worker models to call based on the question\n",
"2. **Worker Models**: Selected models run **simultaneously** in parallel using async/await\n",
"3. **Synthesizer LLM**: Aggregates outputs, synthesizes a final answer, and ranks the workers\n",
"\n",
"## Pattern Flow\n",
"\n",
"```\n",
"User Question → Orchestrator LLM (\"Which workers to use?\")\n",
" ↓\n",
"Parallel Worker API Calls (Only selected models)\n",
" ↓\n",
"Synthesizer LLM (Merge + Rank)\n",
" ↓\n",
"Final Answer + Worker Rankings\n",
"```\n",
"\n",
"The **LLM orchestrator** replaces hardcoded model selection with intelligent routing."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Start with imports - ask ChatGPT to explain any package you don't know\n",
"\n",
"import os\n",
"import json\n",
"import asyncio\n",
"import random\n",
"from datetime import datetime, timedelta\n",
"from typing import Dict, List, Any, Tuple\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from anthropic import Anthropic\n",
"from IPython.display import Markdown, display\n",
"import textwrap"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Always remember to do this!\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Print the key prefixes to help with any debugging\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n",
"groq_api_key = os.getenv('GROQ_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\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set (and this is optional)\")\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:2]}\")\n",
"else:\n",
" print(\"Google API Key not set (and this is optional)\")\n",
"\n",
"if deepseek_api_key:\n",
" print(f\"DeepSeek API Key exists and begins {deepseek_api_key[:3]}\")\n",
"else:\n",
" print(\"DeepSeek API Key not set (and this is optional)\")\n",
"\n",
"if groq_api_key:\n",
" print(f\"Groq API Key exists and begins {groq_api_key[:4]}\")\n",
"else:\n",
" print(\"Groq API Key not set (and this is optional)\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Generate Evaluation Question\n",
"\n",
"First, generate a challenging question to test all the worker models."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"request = \"Please come up with a challenging, nuanced question that I can ask a number of LLMs to evaluate their intelligence. \"\n",
"request += \"Answer only with the question, no explanation.\"\n",
"messages = [{\"role\": \"user\", \"content\": request}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=messages,\n",
")\n",
"question = response.choices[0].message.content\n",
"print(question)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Prepare Worker Configurations\n",
"\n",
"Prepare all available **worker** model configurations that the orchestrator can choose from."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ==========================================\n",
"# Worker Preparation: All Available Models\n",
"# ==========================================\n",
"\n",
"def evaluator_prepare_configs():\n",
" \"\"\"\n",
" Evaluator: Gathers API keys and prepares configurations for all models.\n",
" Returns a list of model configurations ready for parallel execution.\n",
" \"\"\"\n",
" configs = []\n",
" \n",
" # Model 1: OpenAI\n",
" configs.append({\n",
" \"model_name\": \"gpt-5-nano\",\n",
" \"provider\": \"openai\",\n",
" \"client\": OpenAI(),\n",
" \"call_type\": \"chat.completions\",\n",
" \"extra_params\": {}\n",
" })\n",
" \n",
" # Model 2: Anthropic\n",
" configs.append({\n",
" \"model_name\": \"claude-sonnet-4-5\",\n",
" \"provider\": \"anthropic\",\n",
" \"client\": Anthropic(),\n",
" \"call_type\": \"messages.create\",\n",
" \"extra_params\": {\"max_tokens\": 1000}\n",
" })\n",
" \n",
" # Model 3: Gemini\n",
" configs.append({\n",
" \"model_name\": \"gemini-2.5-flash\",\n",
" \"provider\": \"gemini\",\n",
" \"client\": OpenAI(api_key=google_api_key, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\"),\n",
" \"call_type\": \"chat.completions\",\n",
" \"extra_params\": {}\n",
" })\n",
" \n",
" # Model 4: DeepSeek\n",
" configs.append({\n",
" \"model_name\": \"deepseek/deepseek-r1-0528:free\",\n",
" \"provider\": \"deepseek\",\n",
" \"client\": OpenAI(api_key=deepseek_api_key, base_url=\"https://openrouter.ai/api/v1\"),\n",
" \"call_type\": \"chat.completions\",\n",
" \"extra_params\": {}\n",
" })\n",
" \n",
" # Model 5: Groq\n",
" configs.append({\n",
" \"model_name\": \"openai/gpt-oss-120b\",\n",
" \"provider\": \"groq\",\n",
" \"client\": OpenAI(api_key=groq_api_key, base_url=\"https://api.groq.com/openai/v1\"),\n",
" \"call_type\": \"chat.completions\",\n",
" \"extra_params\": {}\n",
" })\n",
" \n",
" # Model 6: Ollama (if available)\n",
" configs.append({\n",
" \"model_name\": \"llama3.2\",\n",
" \"provider\": \"ollama\",\n",
" \"client\": OpenAI(base_url='http://localhost:11434/v1', api_key='ollama'),\n",
" \"call_type\": \"chat.completions\",\n",
" \"extra_params\": {}\n",
" })\n",
" \n",
" print(f\"✅ Evaluator prepared {len(configs)} model configurations\")\n",
" return configs\n",
"\n",
"# Prepare global config dictionaries\n",
"MODEL_CONFIGS = evaluator_prepare_configs()\n",
"MODEL_CONFIGS_BY_NAME = {cfg[\"model_name\"]: cfg for cfg in MODEL_CONFIGS}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Orchestrator LLM - Decide Which Workers to Use\n",
"\n",
"The **orchestrator LLM** analyzes the question and selects which worker models to invoke in parallel."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ==========================================\n",
"# Orchestrator: LLM Decides Worker Selection\n",
"# ==========================================\n",
"\n",
"def list_worker_tools():\n",
" \"\"\"Return tool descriptions for the orchestrator LLM.\"\"\"\n",
" tools = []\n",
" for cfg in MODEL_CONFIGS:\n",
" tools.append({\n",
" \"name\": cfg[\"model_name\"],\n",
" \"description\": f\"Call the {cfg['provider']} model {cfg['model_name']} \"\n",
" \"to answer the user question.\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"reason\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Why this model is useful for this query.\"\n",
" }\n",
" },\n",
" \"required\": [\"reason\"]\n",
" }\n",
" })\n",
" return tools\n",
"\n",
"def build_orchestrator_prompt(user_question: str, tools: list) -> list:\n",
" tool_descriptions = \"\\n\".join(\n",
" f\"- {t['name']}: {t['description']}\" for t in tools\n",
" )\n",
"\n",
" system_msg = textwrap.dedent(f\"\"\"\n",
" You are an **orchestrator** LLM in an orchestrator–worker system.\n",
"\n",
" You do NOT answer the user's question directly.\n",
" Instead, you decide which worker models to call in parallel.\n",
"\n",
" Available workers:\n",
" {tool_descriptions}\n",
"\n",
" Output STRICTLY valid JSON:\n",
" {{\n",
" \"models_to_call\": [\n",
" {{\"name\": \"<model_name>\", \"reason\": \"<short reason>\"}},\n",
" ...\n",
" ]\n",
" }}\n",
"\n",
" Requirements:\n",
" - Choose at least 3 and at most 6 models.\n",
" - Use model names exactly as listed.\n",
" - Prefer diversity (different providers) for hard reasoning tasks.\n",
" - Do not include any fields other than \"models_to_call\".\n",
" \"\"\")\n",
"\n",
" return [\n",
" {\"role\": \"system\", \"content\": system_msg},\n",
" {\"role\": \"user\", \"content\": user_question},\n",
" ]\n",
"\n",
"def orchestrator_plan(user_question: str) -> list:\n",
" \"\"\"\n",
" Ask the orchestrator LLM which workers to use.\n",
" Returns list of selected model names.\n",
" \"\"\"\n",
" tools = list_worker_tools()\n",
" messages = build_orchestrator_prompt(user_question, tools)\n",
"\n",
" openai = OpenAI()\n",
" resp = openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" # model=\"gemini-1.5-flash\",\n",
" messages=messages,\n",
" temperature=0.2,\n",
" )\n",
" content = resp.choices[0].message.content\n",
" plan = json.loads(content)\n",
" selected = [m[\"name\"] for m in plan.get(\"models_to_call\", [])]\n",
" print(f\"🎯 Orchestrator selected {len(selected)} workers: {selected}\")\n",
" return selected"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Parallel Worker Execution\n",
"\n",
"Execute **only the selected workers** simultaneously using async/await."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ==========================================\n",
"# Worker Execution: Async Single Model Call\n",
"# ==========================================\n",
"\n",
"async def call_model_async(config: Dict[str, Any], messages: List[Dict]) -> Tuple[str, str]:\n",
" \"\"\"\n",
" Call a single worker model asynchronously.\n",
" Returns (model_name, answer) or (model_name, error_message).\n",
" \"\"\"\n",
" model_name = config[\"model_name\"]\n",
" provider = config[\"provider\"]\n",
" client = config[\"client\"]\n",
" call_type = config[\"call_type\"]\n",
" extra_params = config[\"extra_params\"]\n",
" \n",
" try:\n",
" if provider == \"anthropic\":\n",
" # Anthropic uses a different API structure\n",
" response = await asyncio.to_thread(\n",
" client.messages.create,\n",
" model=model_name,\n",
" messages=messages,\n",
" **extra_params\n",
" )\n",
" answer = response.content[0].text\n",
" else:\n",
" # OpenAI-compatible APIs\n",
" response = await asyncio.to_thread(\n",
" client.chat.completions.create,\n",
" model=model_name,\n",
" messages=messages,\n",
" **extra_params\n",
" )\n",
" answer = response.choices[0].message.content\n",
" \n",
" print(f\"✅ {model_name} completed\")\n",
" return model_name, answer\n",
" \n",
" except Exception as e:\n",
" error_msg = f\"Error calling {model_name}: {str(e)}\"\n",
" print(f\"❌ {error_msg}\")\n",
" return model_name, error_msg"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ==========================================\n",
"# Parallel Worker Execution\n",
"# ==========================================\n",
"\n",
"def format_bytes(size: int) -> str:\n",
" \"\"\"Format bytes into a human-readable string (B, KB, MB).\"\"\"\n",
" for unit in ['B', 'KB', 'MB']:\n",
" if size < 1024.0:\n",
" return f\"{size:.2f} {unit}\"\n",
" size /= 1024.0\n",
" return f\"{size:.2f} GB\"\n",
"\n",
"async def execute_models_in_parallel(configs: List[Dict[str, Any]], messages: List[Dict]) -> Tuple[List[str], List[str]]:\n",
" \"\"\"\n",
" Execute selected worker models in parallel.\n",
" \"\"\"\n",
" print(f\"\\n🚀 Starting parallel execution of {len(configs)} selected workers...\\n\")\n",
" \n",
" table_rows = []\n",
" competitors = []\n",
" answers = []\n",
" \n",
" async def call_with_metrics(config):\n",
" model_name = config.get(\"model_name\", \"Unknown\")\n",
" start_time = datetime.now()\n",
" \n",
" try:\n",
" _, answer = await call_model_async(config, messages)\n",
" end_time = datetime.now()\n",
" \n",
" if isinstance(answer, str) and answer.startswith(\"Error\"):\n",
" status = \"❌ Error\"\n",
" out_size = 0\n",
" else:\n",
" status = \"✅ Success\"\n",
" out_size = len(str(answer).encode('utf-8'))\n",
" \n",
" except Exception as e:\n",
" end_time = datetime.now()\n",
" status = \"❌ Error\"\n",
" answer = str(e)\n",
" out_size = 0\n",
"\n",
" # Calculate duration\n",
" duration = end_time - start_time\n",
" total_seconds = int(duration.total_seconds())\n",
" mm, ss = divmod(total_seconds, 60)\n",
" hh, mm = divmod(mm, 60)\n",
" dur_str = f\"{hh:02d}:{mm:02d}:{ss:02d}\" if hh > 0 else f\"{mm:02d}:{ss:02d}\"\n",
"\n",
" # Store metrics for table\n",
" table_rows.append({\n",
" \"model\": model_name,\n",
" \"status\": status,\n",
" \"start\": start_time.strftime(\"%H:%M:%S\"),\n",
" \"end\": end_time.strftime(\"%H:%M:%S\"),\n",
" \"duration\": dur_str,\n",
" \"size\": format_bytes(out_size)\n",
" })\n",
" \n",
" return model_name, answer, status\n",
"\n",
" # Run tasks in parallel\n",
" tasks = [call_with_metrics(config) for config in configs]\n",
" results = await asyncio.gather(*tasks)\n",
"\n",
" # Process final lists\n",
" for model_name, answer, status in results:\n",
" if status == \"✅ Success\":\n",
" competitors.append(model_name)\n",
" answers.append(answer)\n",
"\n",
" # Print Tabular Output\n",
" header = f\"{'Model':<25} {'Status':<10} {'Start':<10} {'End':<10} {'Duration':<10} {'Size':<12}\"\n",
" print(header)\n",
" print(\"-\" * len(header))\n",
" for row in table_rows:\n",
" print(f\"{row['model']:<25} {row['status']:<10} {row['start']:<10} {row['end']:<10} {row['duration']:<10} {row['size']:<12}\")\n",
" \n",
" print(f\"\\n✅ Completed. {len(competitors)}/{len(configs)} workers successful.\")\n",
" return competitors, answers\n",
"\n",
"async def mock_execute_models_in_parallel(configs: List[Dict[str, Any]]) -> Tuple[List[str], List[str]]:\n",
" \"\"\"\n",
" Mocks parallel API calls to display timing and size metrics in a table.\n",
" No actual API calls are made.\n",
" \"\"\"\n",
" print(f\"\\n🚀 Starting MOCK execution of {len(configs)} models...\\n\")\n",
" \n",
" table_rows = []\n",
" competitors = []\n",
" answers = []\n",
"\n",
" async def mock_api_call(config):\n",
" model_name = config.get(\"model_name\", \"Unknown-Model\")\n",
" start_time = datetime.now()\n",
" \n",
" # Simulate varying network latency (0.5 to 2.5 seconds)\n",
" await asyncio.sleep(random.uniform(0.5, 2.5))\n",
" \n",
" # Randomly decide if this mock call \"fails\" (10% chance)\n",
" is_success = random.random() > 0.1\n",
" \n",
" if is_success:\n",
" status = \"✅ Success\"\n",
" # Mock a response string of random length\n",
" mock_answer = \"Mock response data \" * random.randint(5, 500)\n",
" out_size = len(mock_answer.encode('utf-8'))\n",
" else:\n",
" status = \"❌ Error\"\n",
" mock_answer = \"Error: Mocked API failure\"\n",
" out_size = 0\n",
" \n",
" end_time = datetime.now()\n",
" \n",
" # Calculate duration in mm:ss or hh:mm:ss\n",
" duration = end_time - start_time\n",
" total_seconds = int(duration.total_seconds())\n",
" mm, ss = divmod(total_seconds, 60)\n",
" hh, mm = divmod(mm, 60)\n",
" dur_str = f\"{hh:02d}:{mm:02d}:{ss:02d}\" if hh > 0 else f\"{mm:02d}:{ss:02d}\"\n",
"\n",
" # Record metrics for the final table\n",
" metrics = {\n",
" \"model\": model_name,\n",
" \"status\": status,\n",
" \"start\": start_time.strftime(\"%H:%M:%S\"),\n",
" \"end\": end_time.strftime(\"%H:%M:%S\"),\n",
" \"duration\": dur_str,\n",
" \"size\": format_bytes(out_size)\n",
" }\n",
" \n",
" return model_name, mock_answer, status, metrics\n",
"\n",
" # Execute mock tasks in parallel\n",
" tasks = [mock_api_call(config) for config in configs]\n",
" results = await asyncio.gather(*tasks)\n",
"\n",
" # Prepare table headers\n",
" header = f\"{'Model':<20} {'Status':<10} {'Start':<10} {'End':<10} {'Duration':<10} {'Size':<12}\"\n",
" print(header)\n",
" print(\"-\" * len(header))\n",
"\n",
" # Output rows and collect final success data\n",
" for model_name, answer, status, row in results:\n",
" print(f\"{row['model']:<20} {row['status']:<10} {row['start']:<10} {row['end']:<10} {row['duration']:<10} {row['size']:<12}\")\n",
" if status == \"✅ Success\":\n",
" competitors.append(model_name)\n",
" answers.append(answer)\n",
"\n",
" print(f\"\\n✅ Completed. {len(competitors)}/{len(configs)} models simulated successfully.\")\n",
" return competitors, answers\n",
"\n",
"async def execute_selected_models(model_names: list, messages: list):\n",
" \"\"\"Execute only the orchestrator-selected workers.\"\"\"\n",
" selected_configs = [MODEL_CONFIGS_BY_NAME[m] for m in model_names]\n",
" return await execute_models_in_parallel(selected_configs, messages)\n",
"\n",
"async def mock_execute_selected_models(model_names: list):\n",
" \"\"\"Execute only the orchestrator-selected workers.\"\"\"\n",
" selected_configs = [MODEL_CONFIGS_BY_NAME[m] for m in model_names]\n",
" return await mock_execute_models_in_parallel(selected_configs)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Run Orchestrated Pipeline\n",
"\n",
"**Full end-to-end execution**: Orchestrator → Workers → Synthesizer."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ==========================================\n",
"# FULL ORCHESTRATED PIPELINE\n",
"# ==========================================\n",
"\n",
"user_question = question # From Step 1\n",
"messages = [{\"role\": \"user\", \"content\": user_question}]\n",
"\n",
"# 1) Orchestrator chooses workers\n",
"selected_models = orchestrator_plan(user_question)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 2) Mock execute chosen workers\n",
"competitors, answers = await mock_execute_selected_models(selected_models)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 2) Run chosen workers in parallel\n",
"competitors, answers = await execute_selected_models(selected_models, messages)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: Synthesizer LLM - Merge + Rank\n",
"\n",
"The **synthesizer LLM** creates a final answer and ranks the worker models."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ==========================================\n",
"# Synthesizer: Merge Outputs + Rank Workers\n",
"# ==========================================\n",
"\n",
"def aggregator_format_outputs(competitors: List[str], answers: List[str]) -> str:\n",
" \"\"\"Format worker outputs for the synthesizer.\"\"\"\n",
" together = \"\"\n",
" for index, answer in enumerate(answers):\n",
" together += f\"# Response from {competitors[index]}\\n\\n\"\n",
" together += answer + \"\\n\\n\"\n",
" return together\n",
"\n",
"def build_synthesizer_prompt(question: str, competitors: list, answers: list) -> list:\n",
" formatted = aggregator_format_outputs(competitors, answers)\n",
" num_workers = len(competitors)\n",
" \n",
" system_msg = f\"\"\"\n",
" You are evaluating EXACTLY {num_workers} worker responses.\n",
" \n",
" CRITICAL: There are ONLY {num_workers} competitors numbered 1-{num_workers}.\n",
" \n",
" Tasks:\n",
" 1. Synthesize final answer from these {num_workers} responses\n",
" 2. Rank ONLY these {num_workers} responses (indices 1-{num_workers})\n",
" \n",
" Output STRICTLY valid JSON:\n",
" {{\n",
" \"final_answer\": \"your synthesized answer\",\n",
" \"rankings\": [\n",
" {{\"competitor_index\": N, \"reason\": \"why N is good\"}} // N is 1-{num_workers}\n",
" // EXACTLY {num_workers} entries, no more, no less\n",
" ]\n",
" }}\n",
" \n",
" Responses ({num_workers} total):\n",
" {formatted}\n",
" \"\"\"\n",
" \n",
" return [{\"role\": \"system\", \"content\": system_msg}]\n",
"\n",
"def run_synthesizer(question: str, competitors: list, answers: list):\n",
" \"\"\"Run the synthesizer LLM.\"\"\"\n",
" messages = build_synthesizer_prompt(question, competitors, answers)\n",
" openai = OpenAI()\n",
" resp = openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=messages,\n",
" temperature=0.3,\n",
" )\n",
" data = json.loads(resp.choices[0].message.content)\n",
" return data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ==========================================\n",
"# FINAL SYNTHESIS AND RANKINGS\n",
"# ==========================================\n",
"\n",
"# Run synthesizer\n",
"synth = run_synthesizer(question, competitors, answers)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for idx, r in enumerate(synth[\"rankings\"], start=1):\n",
" name = competitors[r[\"competitor_index\"] - 1]\n",
" print(f\"{idx}. {name} — {r['reason']}\")\n",
"\n",
"print(f\"\\n✅ Pipeline complete! Orchestrator → {len(selected_models)} Workers → Synthesizer\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# IMMEDIATE DEBUG\n",
"print(\"COMPETITORS:\", competitors)\n",
"print(\"NUM COMPETITORS:\", len(competitors))\n",
"print(\"RAW SYNTH:\", json.dumps(synth, indent=2))\n",
"print(\"RANKINGS:\", [r.get(\"competitor_index\") for r in synth.get(\"rankings\", [])])\n",
"\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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Welcome to the Second Lab - Week 1, Day 3\n",
"\n",
"Changes I've made with this lab.\n",
"1) Modified the original question to instead generate a range of questions, 12 of them. These questions will be used to evaluate each LLM's reasoning, knowledge, creativity, and ability to handle nuanced scenarios.\n",
"2) I've changed this lab to run the queries in parallel. Thanks GPT for helping with the code to do that. :)\n",
"3) Instead of having one LLM rate all the responses, I have all of the LLM's rate each others work and then use a Borda Count to asign points to determine the winner."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Start with imports - ask ChatGPT to explain any package that you don't know\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from anthropic import Anthropic\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Always remember to do this!\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Print the key prefixes to help with any debugging\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"gemini_api_key = os.getenv('GEMINI_API_KEY')\n",
"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n",
"groq_api_key = os.getenv('GROQ_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\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set (and this is optional)\")\n",
"\n",
"if gemini_api_key:\n",
" print(f\"Gemini API Key exists and begins {gemini_api_key[:2]}\")\n",
"else:\n",
" print(\"Gemini API Key not set (and this is optional)\")\n",
"\n",
"if deepseek_api_key:\n",
" print(f\"DeepSeek API Key exists and begins {deepseek_api_key[:3]}\")\n",
"else:\n",
" print(\"DeepSeek API Key not set (and this is optional)\")\n",
"\n",
"if groq_api_key:\n",
" print(f\"Groq API Key exists and begins {groq_api_key[:4]}\")\n",
"else:\n",
" print(\"Groq API Key not set (and this is optional)\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"request = \"\"\"You are being evaluated for your reasoning, knowledge, creativity, and ability to handle nuanced scenarios. \n",
"Generate 12 questions that cover the following categories:\n",
"- Logical reasoning and problem solving\n",
"- Creative writing and storytelling\n",
"- Factual accuracy and knowledge recall\n",
"- Following instructions with strict constraints\n",
"- Multi-step planning and organization\n",
"- Ethical dilemmas and debatable issues\n",
"- Philosophical or abstract reasoning\n",
"- Summarization and explanation at different levels\n",
"- Translation and multilingual ability\n",
"- Roleplay or adaptive communication style\n",
"\n",
"Number each question from 1 to 12. \n",
"The result should be a balanced benchmark question set that fully tests an LLM’s capabilities.\n",
"\n",
"Important: Output only clean plain text. \n",
"Do not use any markup, formatting symbols, quotation marks, brackets, lists, or special characters \n",
"that could cause misinterpretation. Only provide plain text questions, one per line, numbered 1 to 20.\n",
"\"\"\"\n",
"request += \"Answer only with the question, no explanation.\"\n",
"messages = [{\"role\": \"user\", \"content\": request}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Generate the questions.\n",
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=messages,\n",
")\n",
"question = response.choices[0].message.content\n",
"\n",
"display(Markdown(question))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"competitors = []\n",
"answers = []\n",
"messages = [{\"role\": \"user\", \"content\": question}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Ask the LLM's in Parallel\n",
"\n",
"import asyncio\n",
"\n",
"clients = {\n",
" \"openai\": OpenAI(),\n",
" \"claude\": Anthropic(),\n",
" \"gemini\": OpenAI(api_key=gemini_api_key, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\"),\n",
" \"deepseek\": OpenAI(api_key=deepseek_api_key, base_url=\"https://api.deepseek.com/v1\"),\n",
" \"groq\": OpenAI(api_key=groq_api_key, base_url=\"https://api.groq.com/openai/v1\"),\n",
"}\n",
"\n",
"# Get the answers from the LLM\n",
"async def call_llm(model_name, messages):\n",
" try:\n",
" if \"claude\" in model_name:\n",
" response = await asyncio.to_thread(\n",
" clients[\"claude\"].messages.create,\n",
" model=model_name,\n",
" messages=messages,\n",
" max_tokens=3000,\n",
" )\n",
" answer = \"\".join([c.text for c in response.content if c.type == \"text\"])\n",
" \n",
" elif \"gpt-4o-mini\" in model_name:\n",
" response = await asyncio.to_thread(\n",
" clients[\"openai\"].chat.completions.create,\n",
" model=model_name,\n",
" messages=messages,\n",
" )\n",
" answer = response.choices[0].message.content\n",
"\n",
" elif \"gemini\" in model_name:\n",
" response = await asyncio.to_thread(\n",
" clients[\"gemini\"].chat.completions.create,\n",
" model=model_name,\n",
" messages=messages,\n",
" )\n",
" answer = response.choices[0].message.content\n",
"\n",
" elif \"deepseek\" in model_name:\n",
" response = await asyncio.to_thread(\n",
" clients[\"deepseek\"].chat.completions.create,\n",
" model=model_name,\n",
" messages=messages,\n",
" )\n",
" answer = response.choices[0].message.content\n",
"\n",
" elif \"llama\" in model_name:\n",
" response = await asyncio.to_thread(\n",
" clients[\"groq\"].chat.completions.create,\n",
" model=model_name,\n",
" messages=messages,\n",
" )\n",
" answer = response.choices[0].message.content\n",
"\n",
" return model_name, answer \n",
"\n",
" except Exception as e:\n",
" print (f\"❌ Error: {str(e)}\")\n",
" return model_name, \"I was not able to generate answers for any of the questions.\"\n",
"\n",
"\n",
"# send out the calls to the LLM to ask teh questions.\n",
"async def ask_questions_in_parallel(messages):\n",
" competitor_models = [\n",
" \"gpt-4o-mini\",\n",
" \"claude-3-7-sonnet-latest\",\n",
" \"gemini-2.0-flash\",\n",
" \"deepseek-chat\",\n",
" \"llama-3.3-70b-versatile\"\n",
" ]\n",
"\n",
" # create tasks to call the LLM's in parallel\n",
" tasks = [call_llm(model, messages) for model in competitor_models]\n",
"\n",
" answers = []\n",
" competitors = []\n",
"\n",
" # When we have an answer, we can process it. No waiting.\n",
" for task in asyncio.as_completed(tasks):\n",
" model_name, answer = await task\n",
" competitors.append(model_name)\n",
" answers.append(answer)\n",
" print(f\"\\n✅ Got response from {model_name}\")\n",
"\n",
" return competitors, answers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Fire off the ask to all the LLM's at once. Parallelization...\n",
"competitors, answers = await ask_questions_in_parallel(messages)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Look at the results\n",
"print (len(answers))\n",
"print (len(competitors))\n",
"print (competitors)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Let's bring this together - note the use of \"enumerate\"\n",
"\n",
"together = \"\"\n",
"for index, answer in enumerate(answers):\n",
" together += f\"# Response from competitor {index+1}\\n\\n\"\n",
" together += answer + \"\\n\\n\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(together)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"judge = f\"\"\"You are judging a competition between {len(competitors)} competitors.\n",
"Each model has been given the folowing questions:\n",
"\n",
"{question}\n",
"\n",
"Your task is to evaluate the overall strength of the arguments presented by each competitor. \n",
"Consider the following factors:\n",
"- Clarity: how clearly the ideas are communicated\n",
"- Relevance: how directly the response addresses the question\n",
"- Depth: the level of reasoning, insight, or supporting evidence provided\n",
"- Persuasiveness: how compelling or convincing the response is overall\n",
"Respond with JSON, and only JSON.\n",
"The output must be a single JSON array of competitor names, ordered from best to worst.\n",
"Do not include any keys, labels, or extra text.\n",
"\n",
"Example format:\n",
"[\"1\", \"3\", \"5\", \"2\", \"4\"]\n",
"\n",
"Here are the responses from each competitor:\n",
"\n",
"{together}\n",
"\n",
"Now respond with the JSON with the ranked order of the competitors, nothing else. Do not include markdown formatting or code blocks.\n",
"Do not deviate from the json format as described above. Do not include the term ranking in the final json\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(judge)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"judge_messages = [{\"role\": \"user\", \"content\": judge}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Have each LLM rate all of the results.\n",
"results = dict()\n",
"LLM_result = ''\n",
"\n",
"competitors, answers = await ask_questions_in_parallel(judge_messages)\n",
"\n",
"results = dict()\n",
"for index, each_competitor in enumerate(competitors):\n",
" results[each_competitor] = answers[index].strip()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# See the results\n",
"print (len(answers))\n",
"results = dict()\n",
"for index, each_competitor in enumerate(competitors):\n",
" results[each_competitor] = answers[index]\n",
"\n",
"print (results)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Lets convert these rankings into scores. Borda Count - (1st gets 4, 2nd gets 3, etc.).\n",
"number_of_competitors = len(competitors)\n",
"scores = {}\n",
"\n",
"for rankings in results.values():\n",
" print(rankings)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# # Borda count points (1st gets n-1, 2nd gets n-2, etc.)\n",
"num_competitors = len(competitors)\n",
"\n",
"competitor_dict = dict()\n",
"for index, each_competitor in enumerate(competitors):\n",
" competitor_dict[each_competitor] = index + 1\n",
"\n",
"borda_scores_dict = dict()\n",
"for each_competitor in competitors:\n",
" if each_competitor not in borda_scores_dict:\n",
" borda_scores_dict[each_competitor] = 0\n",
"\n",
"for voter_llm, ranking_str in results.items():\n",
" ranking_indices = json.loads(ranking_str)\n",
" ranking_indices = [int(x) for x in ranking_indices]\n",
"\n",
" # For each position in the ranking, award points\n",
" for position, competitor_index in enumerate(ranking_indices):\n",
" competitor_name = competitors[competitor_index - 1]\n",
"\n",
" # Borda count points (1st gets n-1, 2nd gets n-2, etc.)\n",
" points = num_competitors - 1 - position \n",
" borda_scores_dict[competitor_name] += points\n",
" \n",
"sorted_results = sorted(borda_scores_dict.items(), key=lambda x: x[1], reverse=True)\n",
"\n",
"print(f\"{'Rank':<4} {'LLM':<30} {'Points':<3}\")\n",
"print(\"-\" * 50)\n",
"\n",
"for rank, (llm, points) in enumerate(sorted_results, 1):\n",
" print(f\"{rank:<4} {llm:<30} {points:<8}\")\n",
"\n",
"print(\"\\nQuestions asked:\")\n",
"print(question)"
]
}
],
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Welcome to the Second Lab - Week 1, Day 3\n",
"\n",
"Today we will work with lots of models! This is a way to get comfortable with APIs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Important point - please read</h2>\n",
" <span style=\"color:#ff7800;\">The way I collaborate with you may be different to other courses you've taken. I prefer not to type code while you watch. Rather, I execute Jupyter Labs, like this, and give you an intuition for what's going on. My suggestion is that you carefully execute this yourself, <b>after</b> watching the lecture. Add print statements to understand what's going on, and then come up with your own variations.<br/><br/>If you have time, I'd love it if you submit a PR for changes in the community_contributions folder - instructions in the resources. Also, if you have a Github account, use this to showcase your variations. Not only is this essential practice, but it demonstrates your skills to others, including perhaps future clients or employers...\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Start with imports - ask ChatGPT to explain any package that you don't know\n",
"\n",
"import os #allows the code to interact with the operating system\n",
"import json #imports Python's JSON library\n",
"from dotenv import load_dotenv #allows the code to load the .env file. A .env file must be explicity loaded\n",
"from openai import OpenAI\n",
"from anthropic import Anthropic\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Always remember to do this!\n",
"load_dotenv(override=True) #prioritizes the local .env file and will replace existing env variables"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OpenAI API Key exists and begins sk-proj-\n",
"Anthropic API Key not set (and this is optional)\n",
"Google API Key not set (and this is optional)\n",
"DeepSeek API Key not set (and this is optional)\n",
"Groq API Key not set (and this is optional)\n"
]
}
],
"source": [
"# Print the key prefixes to help with any debugging\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n",
"groq_api_key = os.getenv('GROQ_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\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set (and this is optional)\")\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:2]}\")\n",
"else:\n",
" print(\"Google API Key not set (and this is optional)\")\n",
"\n",
"if deepseek_api_key:\n",
" print(f\"DeepSeek API Key exists and begins {deepseek_api_key[:3]}\")\n",
"else:\n",
" print(\"DeepSeek API Key not set (and this is optional)\")\n",
"\n",
"if groq_api_key:\n",
" print(f\"Groq API Key exists and begins {groq_api_key[:4]}\")\n",
"else:\n",
" print(\"Groq API Key not set (and this is optional)\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"request = \"Please come up with a challenging, nuanced question that I can ask a number of LLMs to evaluate their intelligence. \"\n",
"request += \"Answer only with the question, no explanation. I want the question to be related to the cruelty of life\"\n",
"messages = [{\"role\": \"user\", \"content\": request}]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'role': 'user',\n",
" 'content': 'Please come up with a challenging, nuanced question that I can ask a number of LLMs to evaluate their intelligence. Answer only with the question, no explanation.'}]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"In a scenario where two intelligent agents with differing ethical frameworks encounter a moral dilemma involving a choice between the greater good and individual rights, how should they navigate their decision-making process, and what factors should they consider to justify their final actions?\n"
]
}
],
"source": [
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=messages,\n",
")\n",
"question = response.choices[0].message.content\n",
"print(question)\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"competitors = []\n",
"answers = []\n",
"messages = [{\"role\": \"user\", \"content\": question}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# The API we know well\n",
"\n",
"model_name = \"gpt-4o-mini\"\n",
"\n",
"response = openai.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Anthropic has a slightly different API, and Max Tokens is required\n",
"\n",
"model_name = \"claude-3-7-sonnet-latest\"\n",
"\n",
"claude = Anthropic()\n",
"response = claude.messages.create(model=model_name, messages=messages, max_tokens=1000)\n",
"answer = response.content[0].text\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gemini = OpenAI(api_key=google_api_key, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")\n",
"model_name = \"gemini-2.0-flash\"\n",
"\n",
"response = gemini.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"deepseek = OpenAI(api_key=deepseek_api_key, base_url=\"https://api.deepseek.com/v1\")\n",
"model_name = \"deepseek-chat\"\n",
"\n",
"response = deepseek.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"groq = OpenAI(api_key=groq_api_key, base_url=\"https://api.groq.com/openai/v1\")\n",
"model_name = \"llama-3.3-70b-versatile\"\n",
"\n",
"response = groq.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## For the next cell, we will use Ollama\n",
"\n",
"Ollama runs a local web service that gives an OpenAI compatible endpoint, \n",
"and runs models locally using high performance C++ code.\n",
"\n",
"If you don't have Ollama, install it here by visiting https://ollama.com then pressing Download and following the instructions.\n",
"\n",
"After it's installed, you should be able to visit here: http://localhost:11434 and see the message \"Ollama is running\"\n",
"\n",
"You might need to restart Cursor (and maybe reboot). Then open a Terminal (control+\\`) and run `ollama serve`\n",
"\n",
"Useful Ollama commands (run these in the terminal, or with an exclamation mark in this notebook):\n",
"\n",
"`ollama pull <model_name>` downloads a model locally \n",
"`ollama ls` lists all the models you've downloaded \n",
"`ollama rm <model_name>` deletes the specified model from your downloads"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Super important - ignore me at your peril!</h2>\n",
" <span style=\"color:#ff7800;\">The model called <b>llama3.3</b> is FAR too large for home computers - it's not intended for personal computing and will consume all your resources! Stick with the nicely sized <b>llama3.2</b> or <b>llama3.2:1b</b> and if you want larger, try llama3.1 or smaller variants of Qwen, Gemma, Phi or DeepSeek. See the <A href=\"https://ollama.com/models\">the Ollama models page</a> for a full list of models and sizes.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ollama = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
"model_name = \"llama3.2\"\n",
"\n",
"response = ollama.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# So where are we?\n",
"\n",
"print(competitors)\n",
"print(answers)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# It's nice to know how to use \"zip\"\n",
"for competitor, answer in zip(competitors, answers):\n",
" print(f\"Competitor: {competitor}\\n\\n{answer}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"# Let's bring this together - note the use of \"enumerate\"\n",
"\n",
"together = \"\"\n",
"for index, answer in enumerate(answers):\n",
" together += f\"# Response from competitor {index+1}\\n\\n\"\n",
" together += answer + \"\\n\\n\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(together)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"judge = f\"\"\"You are judging a competition between {len(competitors)} competitors.\n",
"Each model has been given this question:\n",
"\n",
"{question}\n",
"\n",
"Your job is to evaluate each response for clarity and strength of argument, and rank them in order of best to worst.\n",
"Respond with JSON, and only JSON, with the following format:\n",
"{{\"results\": [\"best competitor number\", \"second best competitor number\", \"third best competitor number\", ...]}}\n",
"\n",
"Here are the responses from each competitor:\n",
"\n",
"{together}\n",
"\n",
"Now respond with the JSON with the ranked order of the competitors, nothing else. Do not include markdown formatting or code blocks.\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(judge)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"judge_messages = [{\"role\": \"user\", \"content\": judge}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Judgement time!\n",
"\n",
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"o3-mini\",\n",
" messages=judge_messages,\n",
")\n",
"results = response.choices[0].message.content\n",
"print(results)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# OK let's turn this into results!\n",
"\n",
"results_dict = json.loads(results)\n",
"ranks = results_dict[\"results\"]\n",
"for index, result in enumerate(ranks):\n",
" competitor = competitors[int(result)-1]\n",
" print(f\"Rank {index+1}: {competitor}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Which pattern(s) did this use? Try updating this to add another Agentic design pattern.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/business.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">Commercial implications</h2>\n",
" <span style=\"color:#00bfff;\">These kinds of patterns - to send a task to multiple models, and evaluate results,\n",
" are common where you need to improve the quality of your LLM response. This approach can be universally applied\n",
" to business projects where accuracy is critical.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
}
],
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401import os
import json
import asyncio
import concurrent.futures
import time
from typing import Dict, List, Tuple, Optional
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv(override=True)
openai = OpenAI()
competitors = []
answers = []
together = ""
openai_api_key = os.getenv('OPENAI_API_KEY')
anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')
google_api_key = os.getenv('GOOGLE_API_KEY')
deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')
groq_api_key = os.getenv('GROQ_API_KEY')
models_dict = {
'openai': {
'model': 'gpt-4o-mini',
'api_key': openai_api_key,
'base_url': None
},
'gemini': {
'model': 'gemini-2.0-flash',
'api_key': google_api_key,
'base_url': 'https://generativelanguage.googleapis.com/v1beta/openai/'
},
'groq': {
'model': 'llama-3.3-70b-versatile',
'api_key': groq_api_key,
'base_url': 'https://api.groq.com/openai/v1'
},
'ollama': {
'model': 'llama3.2',
'api_key': 'ollama',
'base_url': 'http://localhost:11434/v1'
}
}
def key_checker():
if openai_api_key:
print(f"OpenAI API Key exists and begins {openai_api_key[:8]}")
else:
print("OpenAI API Key not set")
if anthropic_api_key:
print(f"Anthropic API Key exists and begins {anthropic_api_key[:7]}")
else:
print("Anthropic API Key not set (and this is optional)")
if google_api_key:
print(f"Google API Key exists and begins {google_api_key[:2]}")
else:
print("Google API Key not set (and this is optional)")
if deepseek_api_key:
print(f"DeepSeek API Key exists and begins {deepseek_api_key[:3]}")
else:
print("DeepSeek API Key not set (and this is optional)")
if groq_api_key:
print(f"Groq API Key exists and begins {groq_api_key[:4]}")
else:
print("Groq API Key not set (and this is optional)")
def question_prompt_generator():
request = "Please come up with a challenging, nuanced question that I can ask a number of LLMs to evaluate their intelligence. "
request += "Answer only with the question, no explanation."
messages = [{"role": "user", "content": request}]
return messages
def generate_competition_question():
"""
Generate a challenging question for the LLM competition
Returns the question text and formatted messages for LLM calls
"""
print("Generating competition question...")
question_prompt = question_prompt_generator()
question = llm_caller(question_prompt)
question_messages = [{"role": "user", "content": question}]
print(f"Question: \n{question}")
return question, question_messages
def llm_caller(messages):
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
)
return response.choices[0].message.content
def llm_caller_with_model(messages, model_name, api_key, base_url):
llm = None
if base_url:
try:
llm = OpenAI(api_key=api_key, base_url=base_url)
except Exception as e:
print(f"Error creating OpenAI client: {e}")
return None
else:
try:
llm = OpenAI(api_key=api_key)
except Exception as e:
print(f"Error creating OpenAI client: {e}")
return None
response = llm.chat.completions.create(model=model_name, messages=messages)
return response.choices[0].message.content
def get_single_model_answer(provider: str, details: Dict, question_messages: List[Dict]) -> Tuple[str, Optional[str]]:
"""
Call a single model and return (provider, answer) or (provider, None) if failed.
This function is designed to be used with ThreadPoolExecutor.
"""
print(f"Calling model {provider}...")
try:
answer = llm_caller_with_model(question_messages, details['model'], details['api_key'], details['base_url'])
print(f"Model {provider} was successfully called!")
return provider, answer
except Exception as e:
print(f"Model {provider} failed to call: {e}")
return provider, None
def get_models_answers(question_messages):
"""
Sequential version - kept for backward compatibility
"""
for provider, details in models_dict.items():
print(f"Calling model {provider}...")
try:
answer = llm_caller_with_model(question_messages, details['model'], details['api_key'], details['base_url'])
print(f"Model {provider} was successful called!")
except Exception as e:
print(f"Model {provider} failed to call: {e}")
continue
competitors.append(provider)
answers.append(answer)
def get_models_answers_parallel(question_messages, max_workers: int = 4):
"""
Parallel version - calls all models simultaneously using ThreadPoolExecutor
"""
print("Starting parallel execution of all models...")
# Clear previous results
competitors.clear()
answers.clear()
# Use ThreadPoolExecutor for parallel execution
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
# Submit all tasks
future_to_provider = {
executor.submit(get_single_model_answer, provider, details, question_messages): provider
for provider, details in models_dict.items()
}
# Collect results as they complete
for future in concurrent.futures.as_completed(future_to_provider):
provider, answer = future.result()
if answer is not None: # Only add successful calls
competitors.append(provider)
answers.append(answer)
print(f"Parallel execution completed. {len(competitors)} models responded successfully.")
async def get_single_model_answer_async(provider: str, details: Dict, question_messages: List[Dict]) -> Tuple[str, Optional[str]]:
"""
Async version of single model call - for even better performance
"""
print(f"Calling model {provider} (async)...")
try:
# Run the synchronous call in a thread pool
loop = asyncio.get_event_loop()
answer = await loop.run_in_executor(
None,
llm_caller_with_model,
question_messages,
details['model'],
details['api_key'],
details['base_url']
)
print(f"Model {provider} was successfully called!")
return provider, answer
except Exception as e:
print(f"Model {provider} failed to call: {e}")
return provider, None
async def get_models_answers_async(question_messages):
"""
Async version - calls all models simultaneously using asyncio
"""
print("Starting async execution of all models...")
# Clear previous results
competitors.clear()
answers.clear()
# Create tasks for all models
tasks = [
get_single_model_answer_async(provider, details, question_messages)
for provider, details in models_dict.items()
]
# Wait for all tasks to complete
results = await asyncio.gather(*tasks, return_exceptions=True)
# Process results
for result in results:
if isinstance(result, Exception):
print(f"Task failed with exception: {result}")
continue
provider, answer = result
if answer is not None: # Only add successful calls
competitors.append(provider)
answers.append(answer)
print(f"Async execution completed. {len(competitors)} models responded successfully.")
def together_maker(answers):
together = ""
for index, answer in enumerate(answers):
together += f"# Response from competitor {index+1}\n\n"
together += answer + "\n\n"
return together
def judge_prompt_generator(competitors, question, together):
judge = f"""You are judging a competition between {len(competitors)} competitors.
Each model has been given this question:
{question}
Your job is to evaluate each response for clarity and strength of argument, and rank them in order of best to worst.
Respond with JSON, and only JSON, with the following format:
{{"results": ["best competitor number", "second best competitor number", "third best competitor number", ...]}}
Here are the responses from each competitor:
{together}
Now respond with the JSON with the ranked order of the competitors, nothing else. Do not include markdown formatting or code blocks."""
return judge
def judge_caller(judge_prompt, competitors):
print(f"Calling judge...")
judge_messages = [{"role": "user", "content": judge_prompt}]
results = llm_caller_with_model(judge_messages, "o3-mini", openai_api_key, None)
results_dict = json.loads(results)
ranks = results_dict["results"]
for index, result in enumerate(ranks):
competitor = competitors[int(result)-1]
print(f"Rank {index+1}: {competitor}")
return ranks
def compare_execution_methods(question_messages, runs_per_method=1):
"""
Compare performance of different execution methods
"""
methods = ['sequential', 'parallel', 'async']
results = {}
for method in methods:
print(f"\n{'='*50}")
print(f"Testing {method} execution method")
print(f"{'='*50}")
method_times = []
for run in range(runs_per_method):
print(f"\nRun {run + 1}/{runs_per_method}")
# Clear previous results
competitors.clear()
answers.clear()
start_time = time.time()
if method == 'sequential':
get_models_answers(question_messages)
elif method == 'parallel':
get_models_answers_parallel(question_messages, max_workers=4)
elif method == 'async':
asyncio.run(get_models_answers_async(question_messages))
execution_time = time.time() - start_time
method_times.append(execution_time)
print(f"Run {run + 1} completed in {execution_time:.2f} seconds")
avg_time = sum(method_times) / len(method_times)
results[method] = {
'times': method_times,
'avg_time': avg_time,
'successful_models': len(competitors)
}
print(f"\n{method.upper()} Results:")
print(f" Average time: {avg_time:.2f} seconds")
print(f" Successful models: {len(competitors)}")
print(f" All times: {[f'{t:.2f}s' for t in method_times]}")
# Print comparison summary
print(f"\n{'='*60}")
print("PERFORMANCE COMPARISON SUMMARY")
print(f"{'='*60}")
for method, data in results.items():
print(f"{method.upper():>12}: {data['avg_time']:>6.2f}s avg, {data['successful_models']} models")
# Calculate speedup
if 'sequential' in results:
seq_time = results['sequential']['avg_time']
print(f"\nSpeedup vs Sequential:")
for method, data in results.items():
if method != 'sequential':
speedup = seq_time / data['avg_time']
print(f" {method.upper()}: {speedup:.2f}x faster")
return results
def run_llm_competition(question_messages, execution_method, question):
"""
Run the LLM competition with the specified execution method
"""
print(f"\nUsing {execution_method} execution method...")
start_time = time.time()
if execution_method == 'sequential':
get_models_answers(question_messages)
elif execution_method == 'parallel':
get_models_answers_parallel(question_messages, max_workers=4)
elif execution_method == 'async':
asyncio.run(get_models_answers_async(question_messages))
else:
raise ValueError(f"Unknown execution method: {execution_method}")
execution_time = time.time() - start_time
print(f"Execution completed in {execution_time:.2f} seconds")
together = together_maker(answers)
judge_prompt = judge_prompt_generator(competitors, question, together)
judge_caller(judge_prompt, competitors)
return execution_time
# Interactive execution method selection
def get_execution_method():
"""
Prompt user to select execution method
"""
print("\n" + "="*60)
print("EXECUTION METHOD SELECTION")
print("="*60)
print("Choose how to execute the LLM calls:")
print("1. Sequential - Call models one after another (original method)")
print("2. Parallel - Call all models simultaneously (recommended)")
print("3. Async - Use async/await for maximum performance")
print("4. Compare - Run all methods and compare performance")
print("="*60)
while True:
try:
choice = input("Enter your choice (1-4): ").strip()
if choice == '1':
return 'sequential'
elif choice == '2':
return 'parallel'
elif choice == '3':
return 'async'
elif choice == '4':
return 'compare'
else:
print("Invalid choice. Please enter 1, 2, 3, or 4.")
continue
except KeyboardInterrupt:
print("\nExiting...")
exit(0)
except EOFError:
print("\nExiting...")
exit(0)
def main():
key_checker()
# Get user's execution method choice
EXECUTION_METHOD = get_execution_method()
# Generate the competition question and get the question messages
question, question_messages = generate_competition_question()
if EXECUTION_METHOD == 'compare':
print("\nRunning performance comparison...")
compare_execution_methods(question_messages, runs_per_method=1)
else:
run_llm_competition(question_messages, EXECUTION_METHOD, question)
main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Welcome to the Second Lab - Week 1, Day 3\n",
"\n",
"Today we will work with lots of models! This is a way to get comfortable with APIs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Important point - please read</h2>\n",
" <span style=\"color:#ff7800;\">The way I collaborate with you may be different to other courses you've taken. I prefer not to type code while you watch. Rather, I execute Jupyter Labs, like this, and give you an intuition for what's going on. My suggestion is that you carefully execute this yourself, <b>after</b> watching the lecture. Add print statements to understand what's going on, and then come up with your own variations.<br/><br/>If you have time, I'd love it if you submit a PR for changes in the community_contributions folder - instructions in the resources. Also, if you have a Github account, use this to showcase your variations. Not only is this essential practice, but it demonstrates your skills to others, including perhaps future clients or employers...\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Start with imports - ask ChatGPT to explain any package that you don't know\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from anthropic import Anthropic\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Always remember to do this!\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Print the key prefixes to help with any debugging\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n",
"groq_api_key = os.getenv('GROQ_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\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set (and this is optional)\")\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:2]}\")\n",
"else:\n",
" print(\"Google API Key not set (and this is optional)\")\n",
"\n",
"if deepseek_api_key:\n",
" print(f\"DeepSeek API Key exists and begins {deepseek_api_key[:3]}\")\n",
"else:\n",
" print(\"DeepSeek API Key not set (and this is optional)\")\n",
"\n",
"if groq_api_key:\n",
" print(f\"Groq API Key exists and begins {groq_api_key[:4]}\")\n",
"else:\n",
" print(\"Groq API Key not set (and this is optional)\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"request = \"Please come up with a challenging, nuanced question that I can ask a number of LLMs to evaluate their intelligence. \"\n",
"request += \"Answer only with the question, no explanation.\"\n",
"messages = [{\"role\": \"user\", \"content\": request}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"messages"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-5-mini\",\n",
" messages=messages,\n",
")\n",
"question = response.choices[0].message.content\n",
"print(question)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"competitors = []\n",
"answers = []\n",
"messages = [{\"role\": \"user\", \"content\": question}]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Note - update since the videos\n",
"\n",
"I've updated the model names to use the latest models below, like GPT 5 and Claude Sonnet 4.5. It's worth noting that these models can be quite slow - like 1-2 minutes - but they do a great job! Feel free to switch them for faster models if you'd prefer, like the ones I use in the video."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# The API we know well\n",
"# I've updated this with the latest model, but it can take some time because it likes to think!\n",
"# Replace the model with gpt-4.1-mini if you'd prefer not to wait 1-2 mins\n",
"\n",
"model_name = \"gpt-5-nano\"\n",
"\n",
"response = openai.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Anthropic has a slightly different API, and Max Tokens is required\n",
"\n",
"model_name = \"claude-sonnet-4-5\"\n",
"\n",
"claude = Anthropic()\n",
"response = claude.messages.create(model=model_name, messages=messages, max_tokens=1000)\n",
"answer = response.content[0].text\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gemini = OpenAI(api_key=google_api_key, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")\n",
"model_name = \"gemini-2.5-flash\"\n",
"\n",
"response = gemini.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"deepseek = OpenAI(api_key=deepseek_api_key, base_url=\"https://api.deepseek.com/v1\")\n",
"model_name = \"deepseek-chat\"\n",
"\n",
"response = deepseek.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Updated with the latest Open Source model from OpenAI\n",
"\n",
"groq = OpenAI(api_key=groq_api_key, base_url=\"https://api.groq.com/openai/v1\")\n",
"model_name = \"openai/gpt-oss-120b\"\n",
"\n",
"response = groq.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## For the next cell, we will use Ollama\n",
"\n",
"Ollama runs a local web service that gives an OpenAI compatible endpoint, \n",
"and runs models locally using high performance C++ code.\n",
"\n",
"If you don't have Ollama, install it here by visiting https://ollama.com then pressing Download and following the instructions.\n",
"\n",
"After it's installed, you should be able to visit here: http://localhost:11434 and see the message \"Ollama is running\"\n",
"\n",
"You might need to restart Cursor (and maybe reboot). Then open a Terminal (control+\\`) and run `ollama serve`\n",
"\n",
"Useful Ollama commands (run these in the terminal, or with an exclamation mark in this notebook):\n",
"\n",
"`ollama pull <model_name>` downloads a model locally \n",
"`ollama ls` lists all the models you've downloaded \n",
"`ollama rm <model_name>` deletes the specified model from your downloads"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Super important - ignore me at your peril!</h2>\n",
" <span style=\"color:#ff7800;\">The model called <b>llama3.3</b> is FAR too large for home computers - it's not intended for personal computing and will consume all your resources! Stick with the nicely sized <b>llama3.2</b> or <b>llama3.2:1b</b> and if you want larger, try llama3.1 or smaller variants of Qwen, Gemma, Phi or DeepSeek. See the <A href=\"https://ollama.com/models\">the Ollama models page</a> for a full list of models and sizes.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ollama = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
"model_name = \"llama3.2\"\n",
"\n",
"response = ollama.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# So where are we?\n",
"\n",
"print(competitors)\n",
"print(answers)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# It's nice to know how to use \"zip\"\n",
"for competitor, answer in zip(competitors, answers):\n",
" print(f\"Competitor: {competitor}\\n\\n{answer}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Let's bring this together - note the use of \"enumerate\"\n",
"\n",
"together = \"\"\n",
"for index, answer in enumerate(answers):\n",
" together += f\"# Response from competitor {index+1}\\n\\n\"\n",
" together += answer + \"\\n\\n\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(together)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"judge = f\"\"\"You are judging a competition between {len(competitors)} competitors.\n",
"Each model has been given this question:\n",
"\n",
"{question}\n",
"\n",
"Your job is to evaluate each response for clarity and strength of argument, and rank them in order of best to worst.\n",
"Respond with JSON, and only JSON, with the following format:\n",
"{{\"results\": [\"best competitor number\", \"second best competitor number\", \"third best competitor number\", ...]}}\n",
"\n",
"Here are the responses from each competitor:\n",
"\n",
"{together}\n",
"\n",
"Now respond with the JSON with the ranked order of the competitors, nothing else. Do not include markdown formatting or code blocks.\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(judge)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"judge_messages = [{\"role\": \"user\", \"content\": judge}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Judgement time!\n",
"\n",
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-5-mini\",\n",
" messages=judge_messages,\n",
")\n",
"results = response.choices[0].message.content\n",
"print(results)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# OK let's turn this into results!\n",
"\n",
"results_dict = json.loads(results)\n",
"ranks = results_dict[\"results\"]\n",
"for index, result in enumerate(ranks):\n",
" competitor = competitors[int(result)-1]\n",
" print(f\"Rank {index+1}: {competitor}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Which pattern(s) did this use? Try updating this to add another Agentic design pattern.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/business.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">Commercial implications</h2>\n",
" <span style=\"color:#00bfff;\">These kinds of patterns - to send a task to multiple models, and evaluate results,\n",
" are common where you need to improve the quality of your LLM response. This approach can be universally applied\n",
" to business projects where accuracy is critical.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Welcome to the Second Lab - Week 1, Day 3\n",
"\n",
"Today we will work with lots of models! This is a way to get comfortable with APIs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Important point - please read</h2>\n",
" <span style=\"color:#ff7800;\">The way I collaborate with you may be different to other courses you've taken. I prefer not to type code while you watch. Rather, I execute Jupyter Labs, like this, and give you an intuition for what's going on. My suggestion is that you carefully execute this yourself, <b>after</b> watching the lecture. Add print statements to understand what's going on, and then come up with your own variations.<br/><br/>If you have time, I'd love it if you submit a PR for changes in the community_contributions folder - instructions in the resources. Also, if you have a Github account, use this to showcase your variations. Not only is this essential practice, but it demonstrates your skills to others, including perhaps future clients or employers...\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Which pattern(s) did this use? Try updating this to add another Agentic design pattern.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ReAct Pattern"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"import openai\n",
"import os\n",
"from dotenv import load_dotenv\n",
"import io\n",
"from anthropic import Anthropic\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Print the key prefixes to help with any debugging\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n",
"groq_api_key = os.getenv('GROQ_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\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set (and this is optional)\")\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:2]}\")\n",
"else:\n",
" print(\"Google API Key not set (and this is optional)\")\n",
"\n",
"if deepseek_api_key:\n",
" print(f\"DeepSeek API Key exists and begins {deepseek_api_key[:3]}\")\n",
"else:\n",
" print(\"DeepSeek API Key not set (and this is optional)\")\n",
"\n",
"if groq_api_key:\n",
" print(f\"Groq API Key exists and begins {groq_api_key[:4]}\")\n",
"else:\n",
" print(\"Groq API Key not set (and this is optional)\")"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"\n",
"from openai import OpenAI\n",
"\n",
"openai = OpenAI()\n",
"\n",
"# Request prompt\n",
"request = (\n",
" \"Please come up with a challenging, nuanced question that I can ask a number of LLMs to evaluate their intelligence. \"\n",
" \"Answer only with the question, no explanation.\"\n",
")\n",
"\n",
"\n",
"\n",
"def generate_question(prompt: str) -> str:\n",
" response = openai.chat.completions.create(\n",
" model='gpt-4o-mini',\n",
" messages=[{'role': 'user', 'content': prompt}]\n",
" )\n",
" question = response.choices[0].message.content\n",
" return question\n",
"\n",
"def react_agent_decide_model(question: str) -> str:\n",
" prompt = f\"\"\"\n",
" You are an intelligent AI assistant tasked with evaluating which language model is most suitable to answer a given question.\n",
"\n",
" Available models:\n",
" - OpenAI: excels at reasoning and factual answers.\n",
" - Claude: better for philosophical, nuanced, and ethical topics.\n",
" - Gemini: good for concise and structured summaries.\n",
" - Groq: good for creative or exploratory tasks.\n",
" - DeepSeek: strong at coding, technical reasoning, and multilingual responses.\n",
"\n",
" Here is the question to answer:\n",
" \"{question}\"\n",
"\n",
" ### Thought:\n",
" Which model is best suited to answer this question, and why?\n",
"\n",
" ### Action:\n",
" Respond with only the model name you choose (e.g., \"Claude\").\n",
" \"\"\"\n",
"\n",
" response = openai.chat.completions.create(\n",
" model=\"o3-mini\",\n",
" messages=[{\"role\": \"user\", \"content\": prompt}]\n",
" )\n",
" model = response.choices[0].message.content.strip()\n",
" return model\n",
"\n",
"def generate_answer_openai(prompt):\n",
" answer = openai.chat.completions.create(\n",
" model='gpt-4o-mini',\n",
" messages=[{'role': 'user', 'content': prompt}]\n",
" ).choices[0].message.content\n",
" return answer\n",
"\n",
"def generate_answer_anthropic(prompt):\n",
" anthropic = Anthropic(api_key=anthropic_api_key)\n",
" model_name = \"claude-3-5-sonnet-20240620\"\n",
" answer = anthropic.messages.create(\n",
" model=model_name,\n",
" messages=[{'role': 'user', 'content': prompt}],\n",
" max_tokens=1000\n",
" ).content[0].text\n",
" return answer\n",
"\n",
"def generate_answer_deepseek(prompt):\n",
" deepseek = OpenAI(api_key=deepseek_api_key, base_url=\"https://api.deepseek.com/v1\")\n",
" model_name = \"deepseek-chat\" \n",
" answer = deepseek.chat.completions.create(\n",
" model=model_name,\n",
" messages=[{'role': 'user', 'content': prompt}],\n",
" base_url='https://api.deepseek.com/v1'\n",
" ).choices[0].message.content\n",
" return answer\n",
"\n",
"def generate_answer_gemini(prompt):\n",
" gemini=OpenAI(base_url='https://generativelanguage.googleapis.com/v1beta/openai/',api_key=google_api_key)\n",
" model_name = \"gemini-2.0-flash\"\n",
" answer = gemini.chat.completions.create(\n",
" model=model_name,\n",
" messages=[{'role': 'user', 'content': prompt}],\n",
" ).choices[0].message.content\n",
" return answer\n",
"\n",
"def generate_answer_groq(prompt):\n",
" groq=OpenAI(base_url='https://api.groq.com/openai/v1',api_key=groq_api_key)\n",
" model_name=\"llama3-70b-8192\"\n",
" answer = groq.chat.completions.create(\n",
" model=model_name,\n",
" messages=[{'role': 'user', 'content': prompt}],\n",
" base_url=\"https://api.groq.com/openai/v1\"\n",
" ).choices[0].message.content\n",
" return answer\n",
"\n",
"def main():\n",
" print(\"Generating question...\")\n",
" question = generate_question(request)\n",
" print(f\"\\n🧠 Question: {question}\\n\")\n",
" selected_model = react_agent_decide_model(question)\n",
" print(f\"\\n🔹 {selected_model}:\\n\")\n",
" \n",
" if selected_model.lower() == \"openai\":\n",
" answer = generate_answer_openai(question)\n",
" elif selected_model.lower() == \"deepseek\":\n",
" answer = generate_answer_deepseek(question)\n",
" elif selected_model.lower() == \"gemini\":\n",
" answer = generate_answer_gemini(question)\n",
" elif selected_model.lower() == \"groq\":\n",
" answer = generate_answer_groq(question)\n",
" elif selected_model.lower() == \"claude\":\n",
" answer = generate_answer_anthropic(question)\n",
" print(f\"\\n🔹 {selected_model}:\\n{answer}\\n\")\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"main()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/business.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">Commercial implications</h2>\n",
" <span style=\"color:#00bfff;\">These kinds of patterns - to send a task to multiple models, and evaluate results,\n",
" are common where you need to improve the quality of your LLM response. This approach can be universally applied\n",
" to business projects where accuracy is critical.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
}
],
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Welcome to the Second Lab - Week 1, Day 3\n",
"\n",
"Today we will work with lots of models! This is a way to get comfortable with APIs."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Start with imports - ask ChatGPT to explain any package that you don't know\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI, AsyncOpenAI\n",
"from IPython.display import Markdown, display\n",
"import asyncio\n",
"from functools import partial"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Always remember to do this!\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Print the key prefixes to help with any debugging\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"groq_api_key = os.getenv('GROQ_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\")\n",
"\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:2]}\")\n",
"else:\n",
" print(\"Google API Key not set (and this is optional)\")\n",
"\n",
"if groq_api_key:\n",
" print(f\"Groq API Key exists and begins {groq_api_key[:4]}\")\n",
"else:\n",
" print(\"Groq API Key not set (and this is optional)\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"request = \"Please come up with a challenging, nuanced question that I can ask a number of LLMs to evaluate their intelligence. \"\n",
"request += \"Answer only with the question, no explanation.\"\n",
"messages = [{\"role\": \"user\", \"content\": request}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai = AsyncOpenAI()\n",
"response = await openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=messages,\n",
")\n",
"question = response.choices[0].message.content\n",
"print(question)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"messages = [{\"role\": \"user\", \"content\": question}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from dataclasses import dataclass\n",
"\n",
"@dataclass\n",
"class LLMResource:\n",
" api_key: str\n",
" model: str\n",
" url: str = None # optional otherwise NOone\n",
"\n",
"llm_resources = [\n",
" LLMResource(api_key=openai_api_key, model=\"gpt-4o-mini\"),\n",
" LLMResource(api_key=google_api_key, model=\"gemini-2.5-flash\", url=\"https://generativelanguage.googleapis.com/v1beta/openai/\"),\n",
" LLMResource(api_key=groq_api_key, model=\"qwen/qwen3-32b\", url=\"https://api.groq.com/openai/v1\"),\n",
" LLMResource(api_key=\"ollama\", model=\"deepseek-r1:1.5b\", url=\"http://localhost:11434/v1\" )\n",
"]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"async def llm_call(key, model_name, url, messages) -> tuple:\n",
" if url is None:\n",
" llm = AsyncOpenAI(api_key=key)\n",
" else: \n",
" llm = AsyncOpenAI(base_url=url,api_key=key)\n",
" \n",
" response = await llm.chat.completions.create(\n",
" model=model_name, messages=messages)\n",
" \n",
" answer = (model_name, response.choices[0].message.content)\n",
"\n",
" return answer #returns tuple of modle and response from LLM\n",
"\n",
"llm_callable = partial(llm_call, messages=messages) #prefill with messages\n",
"# Always remember to do this!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#gather all responses concurrently\n",
"tasks = [llm_callable(res.api_key,res.model,res.url) for res in llm_resources]\n",
"results = await asyncio.gather(*tasks)\n",
"together = [f'Response from competitor {model}:{answer}' for model,answer in results]#gather results once all model finish running\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"judge = f\"\"\"You are judging a competition between {len(llm_resources)} competitors.\n",
"Each model has been given this question:\n",
"\n",
"{request}\n",
"\n",
"Your job is to evaluate each response for clarity and strength of argument, and rank them in order of best to worst.\n",
"Respond with JSON, and only JSON, with the following format:\n",
"{{\"results\": [\"best competitor number\", \"second best competitor number\", \"third best competitor number\", ...]}}\n",
"\n",
"Here are the responses from each competitor:\n",
"\n",
"{together} # all responses\n",
"\n",
"Now respond with the JSON with the ranked order of the competitors name, nothing else. Do not include markdown formatting or code blocks.\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(judge)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"judge_messages = [{\"role\": \"user\", \"content\": judge}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Judgement time!\n",
"\n",
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"o3-mini\",\n",
" messages=judge_messages,\n",
")\n",
"results = response.choices[0].message.content\n",
"print(results)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# OK let's turn this into results!\n",
"\n",
"results_dict = json.loads(results)\n",
"\n",
"ranks = results_dict[\"results\"]\n",
"\n",
"for index, result in enumerate(ranks):\n",
" print(f\"Rank {index+1}: {result}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Which pattern(s) did this use? Try updating this to add another Agentic design pattern.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/business.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">Commercial implications</h2>\n",
" <span style=\"color:#00bfff;\">These kinds of patterns - to send a task to multiple models, and evaluate results,\n",
" are common where you need to improve the quality of your LLM response. This approach can be universally applied\n",
" to business projects where accuracy is critical.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Welcome to the Second Lab - Week 1, Day 3\n",
"\n",
"Today we will work with lots of models! This is a way to get comfortable with APIs."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Start with imports - ask ChatGPT to explain any package that you don't know\n",
"\n",
"import os\n",
"import json\n",
"import asyncio\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI, AsyncOpenAI\n",
"from anthropic import AsyncAnthropic\n",
"from pydantic import BaseModel"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Always remember to do this!\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Print the key prefixes to help with any debugging\n",
"\n",
"OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')\n",
"ANTHROPIC_API_KEY = os.getenv('ANTHROPIC_API_KEY')\n",
"GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')\n",
"DEEPSEEK_API_KEY = os.getenv('DEEPSEEK_API_KEY')\n",
"GROQ_API_KEY = os.getenv('GROQ_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\")\n",
" \n",
"if ANTHROPIC_API_KEY:\n",
" print(f\"Anthropic API Key exists and begins {ANTHROPIC_API_KEY[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set (and this is optional)\")\n",
"\n",
"if GOOGLE_API_KEY:\n",
" print(f\"Google API Key exists and begins {GOOGLE_API_KEY[:2]}\")\n",
"else:\n",
" print(\"Google API Key not set (and this is optional)\")\n",
"\n",
"if DEEPSEEK_API_KEY:\n",
" print(f\"DeepSeek API Key exists and begins {DEEPSEEK_API_KEY[:3]}\")\n",
"else:\n",
" print(\"DeepSeek API Key not set (and this is optional)\")\n",
"\n",
"if GROQ_API_KEY:\n",
" print(f\"Groq API Key exists and begins {GROQ_API_KEY[:4]}\")\n",
"else:\n",
" print(\"Groq API Key not set (and this is optional)\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"request = \"Please come up with a challenging, nuanced question that I can ask a number of LLMs to evaluate their intelligence. \"\n",
"request += \"Answer only with the question, no explanation.\"\n",
"messages = [{\"role\": \"user\", \"content\": request}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(messages)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai = AsyncOpenAI()\n",
"response = await openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=messages,\n",
")\n",
"question = response.choices[0].message.content\n",
"print(question)\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Define Pydantic model for storing LLM results\n",
"class LLMResult(BaseModel):\n",
" model: str\n",
" answer: str\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"results: list[LLMResult] = []\n",
"messages = [{\"role\": \"user\", \"content\": question}]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# The API we know well\n",
"async def openai_answer() -> None:\n",
"\n",
" if OPENAI_API_KEY is None:\n",
" return None\n",
" \n",
" print(\"OpenAI starting!\")\n",
" model_name = \"gpt-4o-mini\"\n",
"\n",
" try:\n",
" response = await openai.chat.completions.create(model=model_name, messages=messages)\n",
" answer = response.choices[0].message.content\n",
" results.append(LLMResult(model=model_name, answer=answer))\n",
" except Exception as e:\n",
" print(f\"Error with OpenAI: {e}\")\n",
" return None\n",
"\n",
" print(\"OpenAI done!\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# Anthropic has a slightly different API, and Max Tokens is required\n",
"\n",
"async def anthropic_answer() -> None:\n",
"\n",
" if ANTHROPIC_API_KEY is None:\n",
" return None\n",
" \n",
" print(\"Anthropic starting!\")\n",
" model_name = \"claude-3-7-sonnet-latest\"\n",
"\n",
" claude = AsyncAnthropic()\n",
" try:\n",
" response = await claude.messages.create(model=model_name, messages=messages, max_tokens=1000)\n",
" answer = response.content[0].text\n",
" results.append(LLMResult(model=model_name, answer=answer))\n",
" except Exception as e:\n",
" print(f\"Error with Anthropic: {e}\")\n",
" return None\n",
"\n",
" print(\"Anthropic done!\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"async def google_answer() -> None:\n",
"\n",
" if GOOGLE_API_KEY is None:\n",
" return None\n",
" \n",
" print(\"Google starting!\")\n",
" model_name = \"gemini-2.0-flash\"\n",
"\n",
" gemini = AsyncOpenAI(api_key=GOOGLE_API_KEY, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")\n",
" try:\n",
" response = await gemini.chat.completions.create(model=model_name, messages=messages)\n",
" answer = response.choices[0].message.content\n",
" results.append(LLMResult(model=model_name, answer=answer))\n",
" except Exception as e:\n",
" print(f\"Error with Google: {e}\")\n",
" return None\n",
"\n",
" print(\"Google done!\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"async def deepseek_answer() -> None:\n",
"\n",
" if DEEPSEEK_API_KEY is None:\n",
" return None\n",
" \n",
" print(\"DeepSeek starting!\")\n",
" model_name = \"deepseek-chat\"\n",
"\n",
" deepseek = AsyncOpenAI(api_key=DEEPSEEK_API_KEY, base_url=\"https://api.deepseek.com/v1\")\n",
" try:\n",
" response = await deepseek.chat.completions.create(model=model_name, messages=messages)\n",
" answer = response.choices[0].message.content\n",
" results.append(LLMResult(model=model_name, answer=answer))\n",
" except Exception as e:\n",
" print(f\"Error with DeepSeek: {e}\")\n",
" return None\n",
"\n",
" print(\"DeepSeek done!\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"async def groq_answer() -> None:\n",
"\n",
" if GROQ_API_KEY is None:\n",
" return None\n",
" \n",
" print(\"Groq starting!\")\n",
" model_name = \"llama-3.3-70b-versatile\"\n",
"\n",
" groq = AsyncOpenAI(api_key=GROQ_API_KEY, base_url=\"https://api.groq.com/openai/v1\")\n",
" try:\n",
" response = await groq.chat.completions.create(model=model_name, messages=messages)\n",
" answer = response.choices[0].message.content\n",
" results.append(LLMResult(model=model_name, answer=answer))\n",
" except Exception as e:\n",
" print(f\"Error with Groq: {e}\")\n",
" return None\n",
"\n",
" print(\"Groq done!\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## For the next cell, we will use Ollama\n",
"\n",
"Ollama runs a local web service that gives an OpenAI compatible endpoint, \n",
"and runs models locally using high performance C++ code.\n",
"\n",
"If you don't have Ollama, install it here by visiting https://ollama.com then pressing Download and following the instructions.\n",
"\n",
"After it's installed, you should be able to visit here: http://localhost:11434 and see the message \"Ollama is running\"\n",
"\n",
"You might need to restart Cursor (and maybe reboot). Then open a Terminal (control+\\`) and run `ollama serve`\n",
"\n",
"Useful Ollama commands (run these in the terminal, or with an exclamation mark in this notebook):\n",
"\n",
"`ollama pull <model_name>` downloads a model locally \n",
"`ollama ls` lists all the models you've downloaded \n",
"`ollama rm <model_name>` deletes the specified model from your downloads"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Super important - ignore me at your peril!</h2>\n",
" <span style=\"color:#ff7800;\">The model called <b>llama3.3</b> is FAR too large for home computers - it's not intended for personal computing and will consume all your resources! Stick with the nicely sized <b>llama3.2</b> or <b>llama3.2:1b</b> and if you want larger, try llama3.1 or smaller variants of Qwen, Gemma, Phi or DeepSeek. See the <A href=\"https://ollama.com/models\">the Ollama models page</a> for a full list of models and sizes.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"async def ollama_answer() -> None:\n",
" model_name = \"llama3.2\"\n",
"\n",
" print(\"Ollama starting!\")\n",
" ollama = AsyncOpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
" try:\n",
" response = await ollama.chat.completions.create(model=model_name, messages=messages)\n",
" answer = response.choices[0].message.content\n",
" results.append(LLMResult(model=model_name, answer=answer))\n",
" except Exception as e:\n",
" print(f\"Error with Ollama: {e}\")\n",
" return None\n",
"\n",
" print(\"Ollama done!\") "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"async def gather_answers():\n",
" tasks = [\n",
" openai_answer(),\n",
" anthropic_answer(),\n",
" google_answer(),\n",
" deepseek_answer(),\n",
" groq_answer(),\n",
" ollama_answer()\n",
" ]\n",
" await asyncio.gather(*tasks)\n",
"\n",
"await gather_answers()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"together = \"\"\n",
"competitors = []\n",
"answers = []\n",
"\n",
"for res in results:\n",
" competitor = res.model\n",
" answer = res.answer\n",
" competitors.append(competitor)\n",
" answers.append(answer)\n",
" together += f\"# Response from competitor {competitor}\\n\\n\"\n",
" together += answer + \"\\n\\n\"\n",
"\n",
"print(f\"Number of competitors: {len(results)}\")\n",
"print(together)\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"judge = f\"\"\"You are judging a competition between {len(results)} competitors.\n",
"Each model has been given this question:\n",
"\n",
"{question}\n",
"\n",
"Your job is to evaluate each response for clarity and strength of argument, and rank them in order of best to worst.\n",
"Respond with JSON, and only JSON, with the following format:\n",
"{{\"results\": [\"best competitor number\", \"second best competitor number\", \"third best competitor number\", ...]}}\n",
"\n",
"Here are the responses from each competitor:\n",
"\n",
"{together}\n",
"\n",
"Now respond with the JSON with the ranked order of the competitors, nothing else. Do not include markdown formatting or code blocks.\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(judge)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"judge_messages = [{\"role\": \"user\", \"content\": judge}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Judgement time!\n",
"\n",
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"o3-mini\",\n",
" messages=judge_messages,\n",
")\n",
"judgement = response.choices[0].message.content\n",
"print(judgement)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# OK let's turn this into results!\n",
"\n",
"results_dict = json.loads(judgement)\n",
"ranks = results_dict[\"results\"]\n",
"for index, comp in enumerate(ranks):\n",
" print(f\"Rank {index+1}: {comp}\")"
]
}
],
"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.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Welcome to the Second Lab - Week 1, Day 3\n",
"\n",
"Today we will work with lots of models! This is a way to get comfortable with APIs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This was derived from 2_lab2_async. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Start with imports - ask ChatGPT to explain any package that you don't know\n",
"\n",
"import os\n",
"import json\n",
"import asyncio\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI, AsyncOpenAI\n",
"from anthropic import AsyncAnthropic\n",
"from pydantic import BaseModel"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Always remember to do this!\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Print the key prefixes to help with any debugging\n",
"\n",
"OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')\n",
"ANTHROPIC_API_KEY = os.getenv('ANTHROPIC_API_KEY')\n",
"GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')\n",
"DEEPSEEK_API_KEY = os.getenv('DEEPSEEK_API_KEY')\n",
"GROQ_API_KEY = os.getenv('GROQ_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\")\n",
" \n",
"if ANTHROPIC_API_KEY:\n",
" print(f\"Anthropic API Key exists and begins {ANTHROPIC_API_KEY[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set (and this is optional)\")\n",
"\n",
"if GOOGLE_API_KEY:\n",
" print(f\"Google API Key exists and begins {GOOGLE_API_KEY[:2]}\")\n",
"else:\n",
" print(\"Google API Key not set (and this is optional)\")\n",
"\n",
"if DEEPSEEK_API_KEY:\n",
" print(f\"DeepSeek API Key exists and begins {DEEPSEEK_API_KEY[:3]}\")\n",
"else:\n",
" print(\"DeepSeek API Key not set (and this is optional)\")\n",
"\n",
"if GROQ_API_KEY:\n",
" print(f\"Groq API Key exists and begins {GROQ_API_KEY[:4]}\")\n",
"else:\n",
" print(\"Groq API Key not set (and this is optional)\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"request = \"Please come up with a challenging, nuanced question that I can ask a number of LLMs to evaluate their intelligence. \"\n",
"request += \"Answer only with the question, no explanation.\"\n",
"messages = [{\"role\": \"user\", \"content\": request}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(messages)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai = AsyncOpenAI()\n",
"response = await openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=messages,\n",
")\n",
"question = response.choices[0].message.content\n",
"print(question)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Define Pydantic model for storing LLM results\n",
"class LLMResult(BaseModel):\n",
" model: str\n",
" answer: str\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"results: list[LLMResult] = []\n",
"messages = [{\"role\": \"user\", \"content\": question}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# The API we know well\n",
"async def openai_answer() -> None:\n",
"\n",
" if OPENAI_API_KEY is None:\n",
" return None\n",
" \n",
" print(\"OpenAI starting!\")\n",
" model_name = \"gpt-4o-mini\"\n",
"\n",
" try:\n",
" response = await openai.chat.completions.create(model=model_name, messages=messages)\n",
" answer = response.choices[0].message.content\n",
" results.append(LLMResult(model=model_name, answer=answer))\n",
" except Exception as e:\n",
" print(f\"Error with OpenAI: {e}\")\n",
" return None\n",
"\n",
" print(\"OpenAI done!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Anthropic has a slightly different API, and Max Tokens is required\n",
"\n",
"async def anthropic_answer() -> None:\n",
"\n",
" if ANTHROPIC_API_KEY is None:\n",
" return None\n",
" \n",
" print(\"Anthropic starting!\")\n",
" model_name = \"claude-3-7-sonnet-latest\"\n",
"\n",
" claude = AsyncAnthropic()\n",
" try:\n",
" response = await claude.messages.create(model=model_name, messages=messages, max_tokens=1000)\n",
" answer = response.content[0].text\n",
" results.append(LLMResult(model=model_name, answer=answer))\n",
" except Exception as e:\n",
" print(f\"Error with Anthropic: {e}\")\n",
" return None\n",
"\n",
" print(\"Anthropic done!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"async def google_answer() -> None:\n",
"\n",
" if GOOGLE_API_KEY is None:\n",
" return None\n",
" \n",
" print(\"Google starting!\")\n",
" model_name = \"gemini-2.0-flash\"\n",
"\n",
" gemini = AsyncOpenAI(api_key=GOOGLE_API_KEY, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")\n",
" try:\n",
" response = await gemini.chat.completions.create(model=model_name, messages=messages)\n",
" answer = response.choices[0].message.content\n",
" results.append(LLMResult(model=model_name, answer=answer))\n",
" except Exception as e:\n",
" print(f\"Error with Google: {e}\")\n",
" return None\n",
"\n",
" print(\"Google done!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"async def deepseek_answer() -> None:\n",
"\n",
" if DEEPSEEK_API_KEY is None:\n",
" return None\n",
" \n",
" print(\"DeepSeek starting!\")\n",
" model_name = \"deepseek-chat\"\n",
"\n",
" deepseek = AsyncOpenAI(api_key=DEEPSEEK_API_KEY, base_url=\"https://api.deepseek.com/v1\")\n",
" try:\n",
" response = await deepseek.chat.completions.create(model=model_name, messages=messages)\n",
" answer = response.choices[0].message.content\n",
" results.append(LLMResult(model=model_name, answer=answer))\n",
" except Exception as e:\n",
" print(f\"Error with DeepSeek: {e}\")\n",
" return None\n",
"\n",
" print(\"DeepSeek done!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"async def groq_answer() -> None:\n",
"\n",
" if GROQ_API_KEY is None:\n",
" return None\n",
" \n",
" print(\"Groq starting!\")\n",
" model_name = \"llama-3.3-70b-versatile\"\n",
"\n",
" groq = AsyncOpenAI(api_key=GROQ_API_KEY, base_url=\"https://api.groq.com/openai/v1\")\n",
" try:\n",
" response = await groq.chat.completions.create(model=model_name, messages=messages)\n",
" answer = response.choices[0].message.content\n",
" results.append(LLMResult(model=model_name, answer=answer))\n",
" except Exception as e:\n",
" print(f\"Error with Groq: {e}\")\n",
" return None\n",
"\n",
" print(\"Groq done!\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## For the next cell, we will use Ollama\n",
"\n",
"Ollama runs a local web service that gives an OpenAI compatible endpoint, \n",
"and runs models locally using high performance C++ code.\n",
"\n",
"If you don't have Ollama, install it here by visiting https://ollama.com then pressing Download and following the instructions.\n",
"\n",
"After it's installed, you should be able to visit here: http://localhost:11434 and see the message \"Ollama is running\"\n",
"\n",
"You might need to restart Cursor (and maybe reboot). Then open a Terminal (control+\\`) and run `ollama serve`\n",
"\n",
"Useful Ollama commands (run these in the terminal, or with an exclamation mark in this notebook):\n",
"\n",
"`ollama pull <model_name>` downloads a model locally \n",
"`ollama ls` lists all the models you've downloaded \n",
"`ollama rm <model_name>` deletes the specified model from your downloads"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Super important - ignore me at your peril!</h2>\n",
" <span style=\"color:#ff7800;\">The model called <b>llama3.3</b> is FAR too large for home computers - it's not intended for personal computing and will consume all your resources! Stick with the nicely sized <b>llama3.2</b> or <b>llama3.2:1b</b> and if you want larger, try llama3.1 or smaller variants of Qwen, Gemma, Phi or DeepSeek. See the <A href=\"https://ollama.com/models\">the Ollama models page</a> for a full list of models and sizes.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"async def ollama_answer() -> None:\n",
" model_name = \"llama3.2\"\n",
"\n",
" print(\"Ollama starting!\")\n",
" ollama = AsyncOpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
" try:\n",
" response = await ollama.chat.completions.create(model=model_name, messages=messages)\n",
" answer = response.choices[0].message.content\n",
" results.append(LLMResult(model=model_name, answer=answer))\n",
" except Exception as e:\n",
" print(f\"Error with Ollama: {e}\")\n",
" return None\n",
"\n",
" print(\"Ollama done!\") "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"async def gather_answers():\n",
" tasks = [\n",
" openai_answer(),\n",
" anthropic_answer(),\n",
" google_answer(),\n",
" deepseek_answer(),\n",
" groq_answer(),\n",
" ollama_answer()\n",
" ]\n",
" await asyncio.gather(*tasks)\n",
"\n",
"await gather_answers()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"together = \"\"\n",
"competitors = []\n",
"answers = []\n",
"\n",
"for res in results:\n",
" competitor = res.model\n",
" answer = res.answer\n",
" competitors.append(competitor)\n",
" answers.append(answer)\n",
" together += f\"# Response from competitor {competitor}\\n\\n\"\n",
" together += answer + \"\\n\\n\"\n",
"\n",
"print(f\"Number of competitors: {len(results)}\")\n",
"print(together)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"judge = f\"\"\"You are judging a competition between {len(results)} competitors.\n",
"Each model has been given this question:\n",
"\n",
"{question}\n",
"\n",
"Your job is to evaluate each response for clarity and strength of argument, and rank them in order of best to worst.\n",
"Respond with JSON, and only JSON, with the following format:\n",
"{{\"results\": [\"best competitor number\", \"second best competitor number\", \"third best competitor number\", ...],\n",
"\"explanations\": [\"explanation for each rank\", \"explanation for each rank\", \"explanation for each rank\", ...]}}\n",
"\n",
"Here are the responses from each competitor:\n",
"\n",
"{together}\n",
"\n",
"Now respond with the JSON with the ranked order of the competitors, nothing else. Do not include markdown formatting or code blocks.\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(judge)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"judge_messages = [{\"role\": \"user\", \"content\": judge}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Judgement time!\n",
"\n",
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"o3-mini\",\n",
" messages=judge_messages,\n",
")\n",
"judgement = response.choices[0].message.content\n",
"print(judgement)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# OK let's turn this into results!\n",
"\n",
"results_dict = json.loads(judgement)\n",
"ranks = results_dict[\"results\"]\n",
"explanations = results_dict[\"explanations\"]\n",
"for index, comp in enumerate(ranks):\n",
" print(f\"Rank {index+1}: {comp} \\n\\t{explanations[index]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Welcome to the Second Lab - Week 1, Day 3\n",
"\n",
"Today we will work with lots of models! This is a way to get comfortable with APIs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Important point - please read</h2>\n",
" <span style=\"color:#ff7800;\">The way I collaborate with you may be different to other courses you've taken. I prefer not to type code while you watch. Rather, I execute Jupyter Labs, like this, and give you an intuition for what's going on. My suggestion is that you carefully execute this yourself, <b>after</b> watching the lecture. Add print statements to understand what's going on, and then come up with your own variations.<br/><br/>If you have time, I'd love it if you submit a PR for changes in the community_contributions folder - instructions in the resources. Also, if you have a Github account, use this to showcase your variations. Not only is this essential practice, but it demonstrates your skills to others, including perhaps future clients or employers...\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Start with imports - ask ChatGPT to explain any package that you don't know\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from anthropic import Anthropic\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Always remember to do this!\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Print the key prefixes to help with any debugging\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n",
"groq_api_key = os.getenv('GROQ_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\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set (and this is optional)\")\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:2]}\")\n",
"else:\n",
" print(\"Google API Key not set (and this is optional)\")\n",
"\n",
"if deepseek_api_key:\n",
" print(f\"DeepSeek API Key exists and begins {deepseek_api_key[:3]}\")\n",
"else:\n",
" print(\"DeepSeek API Key not set (and this is optional)\")\n",
"\n",
"if groq_api_key:\n",
" print(f\"Groq API Key exists and begins {groq_api_key[:4]}\")\n",
"else:\n",
" print(\"Groq API Key not set (and this is optional)\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"request = \"Please come up with a challenging, nuanced question that I can ask a number of LLMs to evaluate their intelligence. \"\n",
"request += \"Answer only with the question, no explanation.\"\n",
"messages = [{\"role\": \"user\", \"content\": request}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"messages"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=messages,\n",
")\n",
"question = response.choices[0].message.content\n",
"print(question)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"competitors = []\n",
"answers = []\n",
"messages = [{\"role\": \"user\", \"content\": question}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# The API we know well\n",
"\n",
"model_name = \"gpt-4o-mini\"\n",
"\n",
"response = openai.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Anthropic has a slightly different API, and Max Tokens is required\n",
"\n",
"model_name = \"claude-3-7-sonnet-latest\"\n",
"\n",
"claude = Anthropic()\n",
"response = claude.messages.create(model=model_name, messages=messages, max_tokens=1000)\n",
"answer = response.content[0].text\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gemini = OpenAI(api_key=google_api_key, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")\n",
"model_name = \"gemini-2.0-flash\"\n",
"\n",
"response = gemini.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"deepseek = OpenAI(api_key=deepseek_api_key, base_url=\"https://api.deepseek.com/v1\")\n",
"model_name = \"deepseek-chat\"\n",
"\n",
"response = deepseek.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"groq = OpenAI(api_key=groq_api_key, base_url=\"https://api.groq.com/openai/v1\")\n",
"model_name = \"llama-3.3-70b-versatile\"\n",
"\n",
"response = groq.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## For the next cell, we will use Ollama\n",
"\n",
"Ollama runs a local web service that gives an OpenAI compatible endpoint, \n",
"and runs models locally using high performance C++ code.\n",
"\n",
"If you don't have Ollama, install it here by visiting https://ollama.com then pressing Download and following the instructions.\n",
"\n",
"After it's installed, you should be able to visit here: http://localhost:11434 and see the message \"Ollama is running\"\n",
"\n",
"You might need to restart Cursor (and maybe reboot). Then open a Terminal (control+\\`) and run `ollama serve`\n",
"\n",
"Useful Ollama commands (run these in the terminal, or with an exclamation mark in this notebook):\n",
"\n",
"`ollama pull <model_name>` downloads a model locally \n",
"`ollama ls` lists all the models you've downloaded \n",
"`ollama rm <model_name>` deletes the specified model from your downloads"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/stop.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Super important - ignore me at your peril!</h2>\n",
" <span style=\"color:#ff7800;\">The model called <b>llama3.3</b> is FAR too large for home computers - it's not intended for personal computing and will consume all your resources! Stick with the nicely sized <b>llama3.2</b> or <b>llama3.2:1b</b> and if you want larger, try llama3.1 or smaller variants of Qwen, Gemma, Phi or DeepSeek. See the <A href=\"https://ollama.com/models\">the Ollama models page</a> for a full list of models and sizes.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ollama = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
"model_name = \"llama3.2\"\n",
"\n",
"response = ollama.chat.completions.create(model=model_name, messages=messages)\n",
"answer = response.choices[0].message.content\n",
"\n",
"display(Markdown(answer))\n",
"competitors.append(model_name)\n",
"answers.append(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# So where are we?\n",
"\n",
"print(competitors)\n",
"print(answers)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# It's nice to know how to use \"zip\"\n",
"for competitor, answer in zip(competitors, answers):\n",
" print(f\"Competitor: {competitor}\\n\\n{answer}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Let's bring this together - note the use of \"enumerate\"\n",
"\n",
"together = \"\"\n",
"for index, answer in enumerate(answers):\n",
" together += f\"# Response from competitor {index+1}\\n\\n\"\n",
" together += answer + \"\\n\\n\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# print(together)\n",
"display(Markdown(together))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"judge = f\"\"\"You are judging a competition between {len(competitors)} competitors.\n",
"Each model has been given this question:\n",
"\n",
"{question}\n",
"\n",
"Your job is to evaluate each response for clarity and strength of argument, and rank them in order of best to worst.\n",
"Respond with JSON, and only JSON, with the following format:\n",
"{{\"results\": [\"best competitor number\", \"second best competitor number\", \"third best competitor number\", ...]}}\n",
"\n",
"Here are the responses from each competitor:\n",
"\n",
"{together}\n",
"\n",
"Now respond with the JSON with the ranked order of the competitors, nothing else. Do not include markdown formatting or code blocks.\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(judge)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"judge_messages = [{\"role\": \"user\", \"content\": judge}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Judgement time!\n",
"\n",
"openai = OpenAI()\n",
"response = openai.chat.completions.create(\n",
" model=\"o3-mini\",\n",
" messages=judge_messages,\n",
")\n",
"results = response.choices[0].message.content\n",
"print(results)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# OK let's turn this into results!\n",
"\n",
"results_dict = json.loads(results)\n",
"ranks = results_dict[\"results\"]\n",
"for index, result in enumerate(ranks):\n",
" competitor = competitors[int(result)-1]\n",
" print(f\"Rank {index+1}: {competitor}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/exercise.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#ff7800;\">Exercise</h2>\n",
" <span style=\"color:#ff7800;\">Which pattern(s) did this use? Try updating this to add another Agentic design pattern.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Implement Evaluator-Optimizer workflow design pattern - An Optimizer LLM analyzes the response of the top-ranked competitor\n",
"# and creates a system prompt designed to improve the response. The system prompot is then\n",
"# sent back to the top-ranked competitor to deliver a new response. \n",
"# The optimizer LLM then compares the new response to the old response and surmises\n",
"# what aspects of the system prompt may be responsible for the differences in the responses.\n",
"\n",
"\n",
"\n",
"# Get the top competitor (model name) and their response\n",
"top_rank_index = int(ranks[0]) - 1\n",
"top_competitor_name = competitors[top_rank_index]\n",
"top_competitor_response = answers[top_rank_index]\n",
"top_competitor_prompt = question\n",
"\n",
"# Compose a system prompt for GPT-5 to act as an expert evaluator of question quality and answer depth\n",
"system_prompt = (\n",
" \"You are an expert evaluator of LLM prompt quality and answer depth. \"\n",
" \"Your task is to analyze the comprehensiveness and depth of thought in the following answer, \"\n",
" \"which was generated by a language model in response to a challenging question. \"\n",
" \"Consider aspects such as completeness, insight, reasoning, and nuance. \"\n",
" \"Provide a detailed analysis of the answer's strengths and weaknesses and store in the 'markdown_analysis' property.\"\n",
" \"Generate a suggested system prompt that will improve the answer and store in the 'system_prompt' property.\"\n",
")\n",
"\n",
"# Compose the user prompt for GPT-5\n",
"user_prompt = (\n",
" f\"Prompt:\\n{top_competitor_prompt}\\n\\n\"\n",
" f\"Answer:\\n{top_competitor_response}\\n\\n\"\n",
" \"Please analyze the comprehensiveness and depth of thought of the above answer. \"\n",
" \"Discuss its strengths and weaknesses in detail.\"\n",
")\n",
"\n",
"# Call GPT-5 to perform the evaluation\n",
"gpt5 = OpenAI()\n",
"\n",
"# Define the tool schema\n",
"tools = [\n",
" {\n",
" \"type\": \"function\",\n",
" \"function\": {\n",
" \"name\": \"markdown_and_structured_data\",\n",
" \"description\": \"Provide both markdown analysis and structured data\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"markdown_analysis\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Detailed markdown analysis\"\n",
" },\n",
" \"system_prompt\": {\n",
" \"type\": \"string\"\n",
" }\n",
" },\n",
" \"required\": [\"markdown_analysis\", \"sentiment\", \"confidence\", \"key_phrases\"]\n",
" }\n",
" }\n",
" }\n",
"]\n",
"\n",
"gpt5_response = gpt5.chat.completions.create(\n",
" model=\"gpt-5\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt}\n",
" ],\n",
" tools=tools,\n",
" tool_choice={\"type\": \"function\", \"function\": {\"name\": \"markdown_and_structured_data\"}}\n",
")\n",
"\n",
"tool_call = gpt5_response.choices[0].message.tool_calls[0]\n",
"arguments = json.loads(tool_call.function.arguments)\n",
"\n",
"markdown_analysis = arguments[\"markdown_analysis\"]\n",
"system_prompt = arguments[\"system_prompt\"]\n",
"\n",
"\n",
"\n",
"\n",
"# Display the evaluation\n",
"from IPython.display import Markdown, display\n",
"display(Markdown(\"### GPT-5 Evaluation of Top Competitor's Answer\"))\n",
"display(Markdown(f\"Top Competitor: {top_competitor_name}\"))\n",
"display(Markdown(markdown_analysis))\n",
"display(Markdown(\"### Suggested System Prompt\"))\n",
"display(Markdown(system_prompt))\n",
"\n",
"\n",
"# The top competitor was gemini-2.0-flash, so send the original question and suggested system prompt to generate a new response\n",
"# Send the system_prompt and original question to gemini-2.0-flash to generate a new answer\n",
"\n",
"gemini_response = gemini.chat.completions.create(\n",
" model=\"gemini-2.0-flash\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": question}\n",
" ]\n",
")\n",
"\n",
"new_answer = gemini_response.choices[0].message.content\n",
"\n",
"display(Markdown(\"### Gemini-2.0-Flash New Answer with Suggested System Prompt\"))\n",
"display(Markdown(new_answer))\n",
"\n",
"comparison_prompt = f\"\"\"You are an expert LLM evaluator. Compare the following two answers to the same question, where the only difference is that the second answer was generated using a system prompt suggested by you (GPT-5) after evaluating the first answer.\n",
"\n",
"Original Answer (from {top_competitor_name}):\n",
"{top_competitor_response}\n",
"\n",
"New Answer (from {top_competitor_name} with your system prompt):\n",
"{new_answer}\n",
"\n",
"System Prompt Used for New Answer:\n",
"{system_prompt}\n",
"\n",
"Please analyze:\n",
"- What are the key differences between the two answers?\n",
"- What aspects of the system prompt likely contributed to these differences?\n",
"- Did the system prompt improve the quality, accuracy, or style of the answer? How?\n",
"- Any remaining limitations or further suggestions.\n",
"\n",
"Provide a detailed, structured analysis.\n",
"\"\"\"\n",
"\n",
"gpt5_comparison_response = gpt5.chat.completions.create(\n",
" model=\"gpt-5\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are an expert LLM evaluator.\"},\n",
" {\"role\": \"user\", \"content\": comparison_prompt}\n",
" ]\n",
")\n",
"\n",
"comparison_analysis = gpt5_comparison_response.choices[0].message.content\n",
"\n",
"display(Markdown(\"### GPT-5 Analysis: Impact of System Prompt on Gemini-2.0-Flash's Answer\"))\n",
"display(Markdown(comparison_analysis))\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left; width:100%\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../assets/business.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#00bfff;\">Commercial implications</h2>\n",
" <span style=\"color:#00bfff;\">These kinds of patterns - to send a task to multiple models, and evaluate results,\n",
" are common where you need to improve the quality of your LLM response. This approach can be universally applied\n",
" to business projects where accuracy is critical.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
}
],
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}.gitattributes+10
1_lab1.ipynbAdded+713
2_lab2 Personal.ipynbAdded
2_lab2.ipynbAdded
3_lab3.ipynbAdded+581
4_lab4.ipynbAdded+569
4_lab4Mc.ipynbAdded+576
5_extra.ipynbAdded+352
Lesson2.ipynb.code-workspaceAdded+8
Practice.ipynbAdded+50
README.md+3-9
app.pyAdded+138
community_contributions/1_foundations_using_gemini/1_lab1.ipynbAdded+406
community_contributions/1_foundations_using_gemini/2_lab2.ipynbAdded+492
community_contributions/1_foundations_using_gemini/3_lab3.ipynbAdded+382
community_contributions/1_foundations_using_gemini/4_lab4.ipynbAdded+464
community_contributions/1_foundations_using_gemini/app.pyAdded+136
community_contributions/1_foundations_using_gemini/me/linkedin.pdfAdded
community_contributions/1_foundations_using_gemini/me/summary.txtAdded+11
community_contributions/1_foundations_using_gemini/requirements.txtAdded+6
community_contributions/1_lab1_DA.ipynbAdded+396
community_contributions/1_lab1_Hy.ipynbAdded+688
community_contributions/1_lab1_Mohan_M.ipynbAdded+367
community_contributions/1_lab1_Mudassar.ipynbAdded+260
community_contributions/1_lab1_Thanh.ipynbAdded+165
community_contributions/1_lab1_chandra_chekuri.ipynbAdded+620
community_contributions/1_lab1_cm.ipynbAdded+305
community_contributions/1_lab1_gemini.ipynbAdded+305
community_contributions/1_lab1_groq.ipynbAdded+262
community_contributions/1_lab1_groq_llama.ipynbAdded+296
community_contributions/1_lab1_marstipton_mac.ipynbAdded+411
community_contributions/1_lab1_moneek.ipynbAdded+407
community_contributions/1_lab1_nv-ex.ipynbAdded+418
community_contributions/1_lab1_open_router.ipynbAdded+323
community_contributions/1_lab2_Kaushik_Parallelization.ipynbAdded+355
community_contributions/1_lab2_Routing_Workflow.ipynbAdded+514
community_contributions/1_medtech_opportunity_finder/01_medtech.ipynbAdded+133
community_contributions/1_psvasan/day1_exercise.ipynbAdded+113
community_contributions/2_lab2-Evaluator-AnnpaS18.ipynbAdded+474
community_contributions/2_lab2-judge-prompt-changed.ipynbAdded+476
community_contributions/2_lab2-nv-orch-worker-pattern.ipynbAdded+727
community_contributions/2_lab2-parallelization.ipynbAdded+440
community_contributions/2_lab2.ipynbAdded+517
community_contributions/2_lab2_Execution_measurement.pyAdded+401
community_contributions/2_lab2_Mohan_M.ipynbAdded+492
community_contributions/2_lab2_ReAct_Pattern.ipynbAdded+289
community_contributions/2_lab2_akash_parallelization.ipynbAdded+295
community_contributions/2_lab2_async.ipynbAdded+474
community_contributions/2_lab2_async_with_reasons.ipynbAdded+490
community_contributions/2_lab2_doclee99_gpt5_improves_gemini.25flash.ipynbAdded+620