File size: 11,685 Bytes
0af0679 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
{
"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": [
"This version adds Reflection pattern where we ask each model to critique and improve its own answer."
]
},
{
"cell_type": "code",
"execution_count": 9,
"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": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 12,
"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": 14,
"metadata": {},
"outputs": [],
"source": [
"competitors = []\n",
"answers = []\n",
"messages = [{\"role\": \"user\", \"content\": question}]"
]
},
{
"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": [
"<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": 33,
"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": 36,
"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": 38,
"metadata": {},
"outputs": [],
"source": [
"judge_messages = [{\"role\": \"user\", \"content\": judge}]"
]
},
{
"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": [
"1. Ensemble (Model Competition) Pattern\n",
"Description: The same prompt/question is sent to multiple different LLMs (OpenAI, Anthropic, Ollama, etc.).\n",
"Purpose: To compare the quality, style, and content of responses from different models.\n",
"Where in notebook:\n",
"The code sends the same question to several models and collects their answers in the competitors and answers lists.\n",
"\n",
"2. Judging/Evaluator Pattern\n",
"Description: After collecting responses from all models, another LLM is used as a “judge” to evaluate and rank the responses.\n",
"Purpose: To automate the assessment of which model gave the best answer, based on clarity and strength of argument.\n",
"Where in notebook:\n",
"The judge prompt is constructed, and an LLM is asked to rank the responses in JSON format.\n",
"\n",
"3. Self-Improvement/Meta-Reasoning Pattern\n",
"Description: The system not only generates answers but also reflects on and evaluates its own outputs (or those of its peers).\n",
"Purpose: To iteratively improve or select the best output, often used in advanced agentic systems.\n",
"Where in notebook:\n",
"The “judge” LLM is an example of meta-reasoning, as it reasons about the quality of other LLMs’ outputs.\n",
"\n",
"4. Chain-of-Thought/Decomposition Pattern (to a lesser extent)\n",
"Description: Breaking down a complex task into subtasks (e.g., generate question → get answers → evaluate answers).\n",
"Purpose: To improve reliability and interpretability by structuring the workflow.\n",
"Where in notebook:\n",
"The workflow is decomposed into:\n",
"Generating a challenging question\n",
"Getting answers from multiple models\n",
"Judging the answers\n",
"\n",
"In short:\n",
"This notebook uses the Ensemble/Competition, Judging/Evaluator, and Meta-Reasoning agentic patterns, and also demonstrates a simple form of Decomposition by structuring the workflow into clear stages.\n",
"If you want to add more agentic patterns, you could try things like:\n",
"Reflexion (let models critique and revise their own answers)\n",
"Tool Use (let models call external tools or APIs)\n",
"Planning (let a model plan the steps before answering)"
]
},
{
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|