Spaces:
Sleeping
Sleeping
File size: 11,880 Bytes
d232caa |
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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Welcome to the Second Lab - Week 1, Day 3\n",
"### Edited version (rodrigo)\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": [
"In this case "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Start with imports - ask ChatGPT to explain any package that you don't know\n",
"import json\n",
"from zroddeUtils import llmModels, openRouterUtils\n",
"from IPython.display import display, Markdown"
]
},
{
"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",
"prompt = request\n",
"model = llmModels.free_mistral_Small_31_24B"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"llmQuestion = openRouterUtils.getOpenrouterResponse(model, prompt)\n",
"print(llmQuestion)\n",
"#openRouterUtils.clearChatHistory()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"competitors = {} # In this dictionary, we will store the responses from each LLM\n",
" # competitors[model] = llmResponse"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# In this case I need to delete the history because I will to ask the same question to different models\n",
"openRouterUtils.clearChatHistory()\n",
"\n",
"# Set the model name which I'll use to get a response\n",
"#model_name = llmModels.free_gemini_20_flash_exp\n",
"model_name = llmModels.free_meta_Llama_4_Maverick\n",
"\n",
"# Use the same method to interact with the LLM as before\n",
"llmResponse = openRouterUtils.getOpenrouterResponse(model_name, llmQuestion)\n",
"\n",
"# Display the response in a Markdown format\n",
"display(Markdown(llmResponse))\n",
"\n",
"# Store the response in the competitors dictionary\n",
"competitors[model_name] = {\"Number\":len(competitors)+1, \"Response\":llmResponse}\n",
"\n",
"# The competitors dictionary stores each model's response using the model name as the key.\n",
"# The value is another dictionary with the model's assigned number and its response."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# In this case I need to delete the history because I will to ask the same question to different models\n",
"openRouterUtils.clearChatHistory()\n",
"\n",
"# Set the model name which I'll use to get a response\n",
"model_name = llmModels.free_nous_Hermes_3_Mistral_24B\n",
"\n",
"# Use the same method to interact with the LLM as before\n",
"llmResponse = openRouterUtils.getOpenrouterResponse(model_name, llmQuestion)\n",
"\n",
"# Display the response in a Markdown format\n",
"display(Markdown(llmResponse))\n",
"\n",
"# Store the response in the competitors dictionary\n",
"competitors[model_name] = {\"Number\":len(competitors)+1, \"Response\":llmResponse}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# In this case I need to delete the history because I will to ask the same question to different models\n",
"openRouterUtils.clearChatHistory()\n",
"\n",
"# Set the model name which I'll use to get a response\n",
"model_name = llmModels.free_deepSeek_V3_Base\n",
"\n",
"# Use the same method to interact with the LLM as before\n",
"llmResponse = openRouterUtils.getOpenrouterResponse(model_name, llmQuestion)\n",
"\n",
"# Display the response in a Markdown format\n",
"display(Markdown(llmResponse))\n",
"\n",
"# Store the response in the competitors dictionary\n",
"competitors[model_name] = {\"Number\":len(competitors)+1, \"Response\":llmResponse}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# In this case I need to delete the history because I will to ask the same question to different models\n",
"openRouterUtils.clearChatHistory()\n",
"\n",
"# Set the model name which I'll use to get a response\n",
"# Be careful with this model. Gemini 2.0 flash is a free model,\n",
"# but some times it is not available and you will get an error.\n",
"model_name = llmModels.free_gemini_20_flash_exp\n",
"\n",
"# Use the same method to interact with the LLM as before\n",
"llmResponse = openRouterUtils.getOpenrouterResponse(model_name, llmQuestion)\n",
"\n",
"# Display the response in a Markdown format\n",
"display(Markdown(llmResponse))\n",
"\n",
"# Store the response in the competitors dictionary\n",
"competitors[model_name] = {\"Number\":len(competitors)+1, \"Response\":llmResponse}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# In this case I need to delete the history because I will to ask the same question to different models\n",
"openRouterUtils.clearChatHistory()\n",
"\n",
"# Set the model name which I'll use to get a response\n",
"model_name = llmModels.Gpt_41_nano\n",
"\n",
"# Use the same method to interact with the LLM as before\n",
"llmResponse = openRouterUtils.getOpenrouterResponse(model_name, llmQuestion)\n",
"\n",
"# Display the response in a Markdown format\n",
"display(Markdown(llmResponse))\n",
"\n",
"# Store the response in the competitors dictionary\n",
"competitors[model_name] = {\"Number\":len(competitors)+1, \"Response\":llmResponse}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Loop through the competitors dictionary and print each model's name and its response,\n",
"# separated by a line for readability. Finally, print the total number of competitors.\n",
"for k, v in competitors.items():\n",
" print(f\"{k} \\n {v}\\n***********************************\\n\")\n",
"\n",
"print(len(competitors))"
]
},
{
"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",
"{llmQuestion}\n",
"You will get a dictionary coled \"competitors\" with the name, number and response of each competitor. \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",
"{competitors}\n",
"\n",
"Do not base your evaluation on the model name, but only on the content of the responses.\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": [
"openRouterUtils.chatWithOpenRouter(llmModels.Claude_37_sonnet, judge)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"prompt = \"Give me a breif argumentation about why you put them in this order.\"\n",
"openRouterUtils.chatWithOpenRouter(llmModels.Claude_37_sonnet, prompt)"
]
},
{
"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",
" and 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>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "UV_Py_3.12",
"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
}
|