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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "0df0d850-49eb-4a0b-a27a-146969db710d",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"# The Price is Right\n",
|
| 9 |
+
"\n",
|
| 10 |
+
"## Week 8 Order of Play\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"Day 1: Modal.com and SpecialistAgent \n",
|
| 13 |
+
"Day 2: RAG, FrontierAgent, Ensemble Agent \n",
|
| 14 |
+
"Day 3: ScannerAgent, MessengerAgent \n",
|
| 15 |
+
"Day 4: AutonomousPlannerAgent and DealAgentFramework \n",
|
| 16 |
+
"Day 5: The Price Is Right Finale\n",
|
| 17 |
+
"\n",
|
| 18 |
+
"\n",
|
| 19 |
+
"Today we'll build another piece of the puzzle: a ScanningAgent that looks for promising deals by subscribing to RSS feeds."
|
| 20 |
+
]
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"cell_type": "code",
|
| 24 |
+
"execution_count": null,
|
| 25 |
+
"id": "d3763a79-8a5a-4300-8de4-93e85475af10",
|
| 26 |
+
"metadata": {},
|
| 27 |
+
"outputs": [],
|
| 28 |
+
"source": [
|
| 29 |
+
"import os\n",
|
| 30 |
+
"from dotenv import load_dotenv\n",
|
| 31 |
+
"from openai import OpenAI\n",
|
| 32 |
+
"from agents.deals import ScrapedDeal, DealSelection\n",
|
| 33 |
+
"import logging\n",
|
| 34 |
+
"import requests\n",
|
| 35 |
+
"load_dotenv(override=True)\n",
|
| 36 |
+
"openai = OpenAI()\n",
|
| 37 |
+
"MODEL = 'gpt-5-mini'"
|
| 38 |
+
]
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"cell_type": "code",
|
| 42 |
+
"execution_count": null,
|
| 43 |
+
"id": "afece9db-8cd4-46be-ac57-0b472e84da7d",
|
| 44 |
+
"metadata": {},
|
| 45 |
+
"outputs": [],
|
| 46 |
+
"source": [
|
| 47 |
+
"deals = ScrapedDeal.fetch(show_progress=True)"
|
| 48 |
+
]
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"cell_type": "code",
|
| 52 |
+
"execution_count": null,
|
| 53 |
+
"id": "8cd15c4d-eb44-4601-bf0c-f945c1d8e3ec",
|
| 54 |
+
"metadata": {},
|
| 55 |
+
"outputs": [],
|
| 56 |
+
"source": [
|
| 57 |
+
"len(deals)"
|
| 58 |
+
]
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"cell_type": "code",
|
| 62 |
+
"execution_count": null,
|
| 63 |
+
"id": "4259f30a-6455-49ed-8863-2f9ddd4776cb",
|
| 64 |
+
"metadata": {},
|
| 65 |
+
"outputs": [],
|
| 66 |
+
"source": [
|
| 67 |
+
"deals[10].describe()"
|
| 68 |
+
]
|
| 69 |
+
},
|
| 70 |
+
{
|
| 71 |
+
"cell_type": "markdown",
|
| 72 |
+
"id": "53984504",
|
| 73 |
+
"metadata": {},
|
| 74 |
+
"source": [
|
| 75 |
+
"### We are going to ask GPT-5-mini to summarize deals and identify their price"
|
| 76 |
+
]
|
| 77 |
+
},
|
| 78 |
+
{
|
| 79 |
+
"cell_type": "code",
|
| 80 |
+
"execution_count": null,
|
| 81 |
+
"id": "8100e5ac-38f5-40c1-a712-08ae12c85038",
|
| 82 |
+
"metadata": {},
|
| 83 |
+
"outputs": [],
|
| 84 |
+
"source": [
|
| 85 |
+
"SYSTEM_PROMPT = \"\"\"You identify and summarize the 5 most detailed deals from a list, by selecting deals that have the most detailed, high quality description and the most clear price.\n",
|
| 86 |
+
"Respond strictly in JSON with no explanation, using this format. You should provide the price as a number derived from the description. If the price of a deal isn't clear, do not include that deal in your response.\n",
|
| 87 |
+
"Most important is that you respond with the 5 deals that have the most detailed product description with price. It's not important to mention the terms of the deal; most important is a thorough description of the product.\n",
|
| 88 |
+
"Be careful with products that are described as \"$XXX off\" or \"reduced by $XXX\" - this isn't the actual price of the product. Only respond with products when you are highly confident about the price. \n",
|
| 89 |
+
"\"\"\"\n",
|
| 90 |
+
"\n",
|
| 91 |
+
"USER_PROMPT_PREFIX = \"\"\"Respond with the most promising 5 deals from this list, selecting those which have the most detailed, high quality product description and a clear price that is greater than 0.\n",
|
| 92 |
+
"You should rephrase the description to be a summary of the product itself, not the terms of the deal.\n",
|
| 93 |
+
"Remember to respond with a short paragraph of text in the product_description field for each of the 5 items that you select.\n",
|
| 94 |
+
"Be careful with products that are described as \"$XXX off\" or \"reduced by $XXX\" - this isn't the actual price of the product. Only respond with products when you are highly confident about the price. \n",
|
| 95 |
+
"\n",
|
| 96 |
+
"Deals:\n",
|
| 97 |
+
"\n",
|
| 98 |
+
"\"\"\"\n",
|
| 99 |
+
"\n",
|
| 100 |
+
"USER_PROMPT_SUFFIX = \"\\n\\nInclude exactly 5 deals, no more.\""
|
| 101 |
+
]
|
| 102 |
+
},
|
| 103 |
+
{
|
| 104 |
+
"cell_type": "code",
|
| 105 |
+
"execution_count": null,
|
| 106 |
+
"id": "f4bca170-af71-40c9-9597-1d72980c74d8",
|
| 107 |
+
"metadata": {},
|
| 108 |
+
"outputs": [],
|
| 109 |
+
"source": [
|
| 110 |
+
"# this makes a suitable user prompt given scraped deals\n",
|
| 111 |
+
"\n",
|
| 112 |
+
"def make_user_prompt(scraped):\n",
|
| 113 |
+
" user_prompt = USER_PROMPT_PREFIX\n",
|
| 114 |
+
" user_prompt += '\\n\\n'.join([scrape.describe() for scrape in scraped])\n",
|
| 115 |
+
" user_prompt += USER_PROMPT_SUFFIX\n",
|
| 116 |
+
" return user_prompt"
|
| 117 |
+
]
|
| 118 |
+
},
|
| 119 |
+
{
|
| 120 |
+
"cell_type": "code",
|
| 121 |
+
"execution_count": null,
|
| 122 |
+
"id": "020947a6-561b-417b-98a0-a085e31d2ce3",
|
| 123 |
+
"metadata": {},
|
| 124 |
+
"outputs": [],
|
| 125 |
+
"source": [
|
| 126 |
+
"# Let's create a user prompt for the deals we just scraped, and look at how it begins\n",
|
| 127 |
+
"\n",
|
| 128 |
+
"user_prompt = make_user_prompt(deals)\n",
|
| 129 |
+
"print(user_prompt[:2000])\n",
|
| 130 |
+
"messages = [{\"role\": \"system\", \"content\": SYSTEM_PROMPT}, {\"role\": \"user\", \"content\": user_prompt}]"
|
| 131 |
+
]
|
| 132 |
+
},
|
| 133 |
+
{
|
| 134 |
+
"cell_type": "code",
|
| 135 |
+
"execution_count": null,
|
| 136 |
+
"id": "7de46f74-868c-4127-8a68-cf2da7d600bb",
|
| 137 |
+
"metadata": {},
|
| 138 |
+
"outputs": [],
|
| 139 |
+
"source": [
|
| 140 |
+
"response = openai.chat.completions.parse(model=MODEL, messages=messages, response_format=DealSelection, reasoning_effort=\"minimal\")\n",
|
| 141 |
+
"results = response.choices[0].message.parsed\n",
|
| 142 |
+
"results"
|
| 143 |
+
]
|
| 144 |
+
},
|
| 145 |
+
{
|
| 146 |
+
"cell_type": "code",
|
| 147 |
+
"execution_count": null,
|
| 148 |
+
"id": "065cfe7a",
|
| 149 |
+
"metadata": {},
|
| 150 |
+
"outputs": [],
|
| 151 |
+
"source": [
|
| 152 |
+
"for deal in results.deals:\n",
|
| 153 |
+
" print(deal.product_description)\n",
|
| 154 |
+
" print(deal.price)\n",
|
| 155 |
+
" print(deal.url)\n",
|
| 156 |
+
" print()\n"
|
| 157 |
+
]
|
| 158 |
+
},
|
| 159 |
+
{
|
| 160 |
+
"cell_type": "code",
|
| 161 |
+
"execution_count": null,
|
| 162 |
+
"id": "9e04f2b2",
|
| 163 |
+
"metadata": {},
|
| 164 |
+
"outputs": [],
|
| 165 |
+
"source": [
|
| 166 |
+
"root = logging.getLogger()\n",
|
| 167 |
+
"root.setLevel(logging.INFO)"
|
| 168 |
+
]
|
| 169 |
+
},
|
| 170 |
+
{
|
| 171 |
+
"cell_type": "code",
|
| 172 |
+
"execution_count": null,
|
| 173 |
+
"id": "8bdc57fb-7497-47af-a643-6ba5a21cc17e",
|
| 174 |
+
"metadata": {},
|
| 175 |
+
"outputs": [],
|
| 176 |
+
"source": [
|
| 177 |
+
"from agents.scanner_agent import ScannerAgent"
|
| 178 |
+
]
|
| 179 |
+
},
|
| 180 |
+
{
|
| 181 |
+
"cell_type": "code",
|
| 182 |
+
"execution_count": null,
|
| 183 |
+
"id": "132278bc-217a-43a6-b6c4-724140c6a225",
|
| 184 |
+
"metadata": {},
|
| 185 |
+
"outputs": [],
|
| 186 |
+
"source": [
|
| 187 |
+
"agent = ScannerAgent()\n",
|
| 188 |
+
"result = agent.scan()"
|
| 189 |
+
]
|
| 190 |
+
},
|
| 191 |
+
{
|
| 192 |
+
"cell_type": "code",
|
| 193 |
+
"execution_count": null,
|
| 194 |
+
"id": "2e1d013a-c930-4dad-901b-41433379e14b",
|
| 195 |
+
"metadata": {},
|
| 196 |
+
"outputs": [],
|
| 197 |
+
"source": [
|
| 198 |
+
"result"
|
| 199 |
+
]
|
| 200 |
+
},
|
| 201 |
+
{
|
| 202 |
+
"cell_type": "markdown",
|
| 203 |
+
"id": "5ee2e837-1f1d-42d4-8bc4-51cccc343006",
|
| 204 |
+
"metadata": {},
|
| 205 |
+
"source": [
|
| 206 |
+
"### Introducing Pushover\n",
|
| 207 |
+
"\n",
|
| 208 |
+
"Pushover is a nifty tool for sending Push Notifications to your phone.\n",
|
| 209 |
+
"\n",
|
| 210 |
+
"It's super easy to set up and install!\n",
|
| 211 |
+
"\n",
|
| 212 |
+
"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",
|
| 213 |
+
"\n",
|
| 214 |
+
"Once you've signed up, on the home screen, click \"Create an Application/API Token\", and give it any name (like AIEngineer) and click Create Application.\n",
|
| 215 |
+
"\n",
|
| 216 |
+
"Then add 2 lines to your `.env` file:\n",
|
| 217 |
+
"\n",
|
| 218 |
+
"PUSHOVER_USER=_put the key that's on the top right of your Pushover home screen and probably starts with a u_ \n",
|
| 219 |
+
"PUSHOVER_TOKEN=_put the key when you click into your new application called Agents (or whatever) and probably starts with an a_\n",
|
| 220 |
+
"\n",
|
| 221 |
+
"Remember to save your `.env` file, and run `load_dotenv(override=True)` after saving, to set your environment variables.\n",
|
| 222 |
+
"\n",
|
| 223 |
+
"Finally, click \"Add Phone, Tablet or Desktop\" to install on your phone."
|
| 224 |
+
]
|
| 225 |
+
},
|
| 226 |
+
{
|
| 227 |
+
"cell_type": "code",
|
| 228 |
+
"execution_count": null,
|
| 229 |
+
"id": "761bc7d0",
|
| 230 |
+
"metadata": {},
|
| 231 |
+
"outputs": [],
|
| 232 |
+
"source": [
|
| 233 |
+
"load_dotenv(override=True)"
|
| 234 |
+
]
|
| 235 |
+
},
|
| 236 |
+
{
|
| 237 |
+
"cell_type": "code",
|
| 238 |
+
"execution_count": null,
|
| 239 |
+
"id": "d7fafd66",
|
| 240 |
+
"metadata": {},
|
| 241 |
+
"outputs": [],
|
| 242 |
+
"source": [
|
| 243 |
+
"pushover_user = os.getenv('PUSHOVER_USER')\n",
|
| 244 |
+
"pushover_token = os.getenv('PUSHOVER_TOKEN')\n",
|
| 245 |
+
"pushover_url = \"https://api.pushover.net/1/messages.json\""
|
| 246 |
+
]
|
| 247 |
+
},
|
| 248 |
+
{
|
| 249 |
+
"cell_type": "code",
|
| 250 |
+
"execution_count": null,
|
| 251 |
+
"id": "9d531ed4",
|
| 252 |
+
"metadata": {},
|
| 253 |
+
"outputs": [],
|
| 254 |
+
"source": [
|
| 255 |
+
"if pushover_user:\n",
|
| 256 |
+
" print(f\"Pushover user found and starts with {pushover_user[0]}\")\n",
|
| 257 |
+
"else:\n",
|
| 258 |
+
" print(\"Pushover user not found\")\n",
|
| 259 |
+
"\n",
|
| 260 |
+
"if pushover_token:\n",
|
| 261 |
+
" print(f\"Pushover token found and starts with {pushover_token[0]}\")\n",
|
| 262 |
+
"else:\n",
|
| 263 |
+
" print(\"Pushover token not found\")"
|
| 264 |
+
]
|
| 265 |
+
},
|
| 266 |
+
{
|
| 267 |
+
"cell_type": "code",
|
| 268 |
+
"execution_count": null,
|
| 269 |
+
"id": "003c858f",
|
| 270 |
+
"metadata": {},
|
| 271 |
+
"outputs": [],
|
| 272 |
+
"source": [
|
| 273 |
+
"def push(message):\n",
|
| 274 |
+
" print(f\"Push: {message}\")\n",
|
| 275 |
+
" payload = {\"user\": pushover_user, \"token\": pushover_token, \"message\": message}\n",
|
| 276 |
+
" requests.post(pushover_url, data=payload)"
|
| 277 |
+
]
|
| 278 |
+
},
|
| 279 |
+
{
|
| 280 |
+
"cell_type": "code",
|
| 281 |
+
"execution_count": null,
|
| 282 |
+
"id": "43fd1fa4",
|
| 283 |
+
"metadata": {},
|
| 284 |
+
"outputs": [],
|
| 285 |
+
"source": [
|
| 286 |
+
"push(\"MASSIVE DEAL!!\")"
|
| 287 |
+
]
|
| 288 |
+
},
|
| 289 |
+
{
|
| 290 |
+
"cell_type": "code",
|
| 291 |
+
"execution_count": null,
|
| 292 |
+
"id": "be0c3276",
|
| 293 |
+
"metadata": {},
|
| 294 |
+
"outputs": [],
|
| 295 |
+
"source": [
|
| 296 |
+
"from agents.messaging_agent import MessagingAgent\n",
|
| 297 |
+
"\n",
|
| 298 |
+
"agent = MessagingAgent()\n",
|
| 299 |
+
"agent.push(\"SUCH A MASSIVE DEAL!!\")"
|
| 300 |
+
]
|
| 301 |
+
},
|
| 302 |
+
{
|
| 303 |
+
"cell_type": "code",
|
| 304 |
+
"execution_count": null,
|
| 305 |
+
"id": "c1200929",
|
| 306 |
+
"metadata": {},
|
| 307 |
+
"outputs": [],
|
| 308 |
+
"source": [
|
| 309 |
+
"agent.notify(\"A special deal on Sumsung 60 inch LED TV going at a great bargain\", 300, 1000, \"www.samsung.com\")"
|
| 310 |
+
]
|
| 311 |
+
},
|
| 312 |
+
{
|
| 313 |
+
"cell_type": "code",
|
| 314 |
+
"execution_count": null,
|
| 315 |
+
"id": "46d59b1e",
|
| 316 |
+
"metadata": {},
|
| 317 |
+
"outputs": [],
|
| 318 |
+
"source": []
|
| 319 |
+
}
|
| 320 |
+
],
|
| 321 |
+
"metadata": {
|
| 322 |
+
"kernelspec": {
|
| 323 |
+
"display_name": ".venv",
|
| 324 |
+
"language": "python",
|
| 325 |
+
"name": "python3"
|
| 326 |
+
},
|
| 327 |
+
"language_info": {
|
| 328 |
+
"codemirror_mode": {
|
| 329 |
+
"name": "ipython",
|
| 330 |
+
"version": 3
|
| 331 |
+
},
|
| 332 |
+
"file_extension": ".py",
|
| 333 |
+
"mimetype": "text/x-python",
|
| 334 |
+
"name": "python",
|
| 335 |
+
"nbconvert_exporter": "python",
|
| 336 |
+
"pygments_lexer": "ipython3",
|
| 337 |
+
"version": "3.12.12"
|
| 338 |
+
}
|
| 339 |
+
},
|
| 340 |
+
"nbformat": 4,
|
| 341 |
+
"nbformat_minor": 5
|
| 342 |
+
}
|