File size: 10,343 Bytes
21329f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
332
333
334
335
336
337
338
339
340
341
342
343
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0df0d850-49eb-4a0b-a27a-146969db710d",
   "metadata": {},
   "source": [
    "# The Price is Right\n",
    "\n",
    "## Week 8 Order of Play\n",
    "\n",
    "Day 1: Modal.com and SpecialistAgent  \n",
    "Day 2: RAG, FrontierAgent, Ensemble Agent  \n",
    "Day 3: ScannerAgent, MessengerAgent  \n",
    "Day 4: AutonomousPlannerAgent and DealAgentFramework  \n",
    "Day 5: The Price Is Right Finale\n",
    "\n",
    "\n",
    "Today we'll build another piece of the puzzle: a ScanningAgent that looks for promising deals by subscribing to RSS feeds."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3763a79-8a5a-4300-8de4-93e85475af10",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv\n",
    "from openai import OpenAI\n",
    "from agents.deals import ScrapedDeal, DealSelection\n",
    "import logging\n",
    "import requests\n",
    "load_dotenv(override=True)\n",
    "openai = OpenAI()\n",
    "MODEL = 'gpt-5-mini'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "afece9db-8cd4-46be-ac57-0b472e84da7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "deals = ScrapedDeal.fetch(show_progress=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8cd15c4d-eb44-4601-bf0c-f945c1d8e3ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(deals)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4259f30a-6455-49ed-8863-2f9ddd4776cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "deals[10].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53984504",
   "metadata": {},
   "source": [
    "### We are going to ask GPT-5-mini to summarize deals and identify their price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8100e5ac-38f5-40c1-a712-08ae12c85038",
   "metadata": {},
   "outputs": [],
   "source": [
    "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",
    "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",
    "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",
    "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",
    "\"\"\"\n",
    "\n",
    "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",
    "You should rephrase the description to be a summary of the product itself, not the terms of the deal.\n",
    "Remember to respond with a short paragraph of text in the product_description field for each of the 5 items that you select.\n",
    "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",
    "\n",
    "Deals:\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "USER_PROMPT_SUFFIX = \"\\n\\nInclude exactly 5 deals, no more.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4bca170-af71-40c9-9597-1d72980c74d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# this makes a suitable user prompt given scraped deals\n",
    "\n",
    "def make_user_prompt(scraped):\n",
    "    user_prompt = USER_PROMPT_PREFIX\n",
    "    user_prompt += '\\n\\n'.join([scrape.describe() for scrape in scraped])\n",
    "    user_prompt += USER_PROMPT_SUFFIX\n",
    "    return user_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "020947a6-561b-417b-98a0-a085e31d2ce3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's create a user prompt for the deals we just scraped, and look at how it begins\n",
    "\n",
    "user_prompt = make_user_prompt(deals)\n",
    "print(user_prompt[:2000])\n",
    "messages = [{\"role\": \"system\", \"content\": SYSTEM_PROMPT}, {\"role\": \"user\", \"content\": user_prompt}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7de46f74-868c-4127-8a68-cf2da7d600bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = openai.chat.completions.parse(model=MODEL, messages=messages, response_format=DealSelection, reasoning_effort=\"minimal\")\n",
    "results = response.choices[0].message.parsed\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "065cfe7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "for deal in results.deals:\n",
    "    print(deal.product_description)\n",
    "    print(deal.price)\n",
    "    print(deal.url)\n",
    "    print()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e04f2b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "root = logging.getLogger()\n",
    "root.setLevel(logging.INFO)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bdc57fb-7497-47af-a643-6ba5a21cc17e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from agents.scanner_agent import ScannerAgent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "132278bc-217a-43a6-b6c4-724140c6a225",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent = ScannerAgent()\n",
    "result = agent.scan()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e1d013a-c930-4dad-901b-41433379e14b",
   "metadata": {},
   "outputs": [],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ee2e837-1f1d-42d4-8bc4-51cccc343006",
   "metadata": {},
   "source": [
    "### 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 AIEngineer) 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,
   "id": "761bc7d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "load_dotenv(override=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7fafd66",
   "metadata": {},
   "outputs": [],
   "source": [
    "pushover_user = os.getenv('PUSHOVER_USER')\n",
    "pushover_token = os.getenv('PUSHOVER_TOKEN')\n",
    "pushover_url = \"https://api.pushover.net/1/messages.json\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d531ed4",
   "metadata": {},
   "outputs": [],
   "source": [
    "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,
   "id": "003c858f",
   "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,
   "id": "43fd1fa4",
   "metadata": {},
   "outputs": [],
   "source": [
    "push(\"MASSIVE DEAL!!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be0c3276",
   "metadata": {},
   "outputs": [],
   "source": [
    "from agents.messaging_agent import MessagingAgent\n",
    "\n",
    "agent = MessagingAgent()\n",
    "agent.push(\"SUCH A MASSIVE DEAL!!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1200929",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent.notify(\"A special deal on Sumsung 60 inch LED TV going at a great bargain\", 300, 1000, \"www.samsung.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46d59b1e",
   "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": 5
}