pricer-agent/scanning_agent.ipynb
2025-06-10 12:03:09 +05:30

337 lines
12 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "6478cf67",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from agents.deals import ScrapedDeal, DealSelection"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b1926a62",
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)\n",
"api_key = os.getenv(\"GROQ_API_KEY\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "12a606ac",
"metadata": {},
"outputs": [],
"source": [
"from groq import Groq\n",
"\n",
"groq_client = Groq(api_key=api_key)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c9f53242",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 0%| | 0/5 [00:00<?, ?it/s]"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 5/5 [03:44<00:00, 44.86s/it]\n"
]
}
],
"source": [
"deals = ScrapedDeal.fetch(show_progress=True)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6a875195",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"50"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(deals)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a5c72fe7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"Title: Craftsman Blow Gun Kit for $17 + free shipping w/ $35\\nDetails: Bag this nice low as part of today's Daily Deals. It's currently listed at $25 on Amazon. Buy Now at Lowe's\\nFeatures: \\nURL: https://www.dealnews.com/Craftsman-Blow-Gun-Kit-for-17-free-shipping-w-35/21740415.html?iref=rss-c196\""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"deals[44].describe()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "878d0428",
"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",
"{\"deals\": [\n",
" {\n",
" \"product_description\": \"Your clearly expressed summary of the product in 4-5 sentences. Details of the item are much more important than why it's a good deal. Avoid mentioning discounts and coupons; focus on the item itself. There should be a paragpraph of text for each item you choose.\",\n",
" \"price\": 99.99,\n",
" \"url\": \"the url as provided\"\n",
" },\n",
" ...\n",
"]}\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "1b414e02",
"metadata": {},
"outputs": [],
"source": [
"user_prompt = \"\"\"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.\n",
"Respond strictly in JSON, and only JSON. 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 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",
"user_prompt += '\\n\\n'.join([deal.describe() for deal in deals])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "aa33b1b6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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.\n",
"Respond strictly in JSON, and only JSON. 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 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",
"Title: Samsung S90F 77\" 4K OLED Vision AI Smart TV: Up to $400 off + free shipping\n",
"Details: Save at least $100 and as much as $400 on these three sizes. The deals:48\" for $1,399.99 ($100 off)77\" for $3,299.99 ($200 off)83\" for $4,999.99 ($400 off) Shop Now at Samsung\n",
"Features: \n",
"URL: https://www.dealnews.com/Samsung-S90-F-77-4-K-OLED-Vision-AI-Smart-TV-Up-to-400-off-free-shipping/21740356.html?iref=rss-c142\n",
"\n",
"Title: Samsung The Frame LS03D 4K HDR QLED TVs: Up to $1,800 off + free shipping\n",
"Details: Save at least $50 and as much as $1,800 on these seven sizes. The deals:32\" for $549.99 ($50 off)43\" for $749.99 ($250 off)50\" for $899.99 ($400 off)55\" for $999.99 ($500 off)65\" for $1,299.99 ($700 off)75\" for $2,199.99 ($800 off)85\" for $2,499.99 ($1,800 off) Shop Now at Samsung\n",
"Features: \n",
"URL: https://www.dealnews.com/Samsung-The-Frame-LS03-D-4-K-HDR-QLED-TVs-Up-to-1-800-off-free-shipping/21740352.html?iref=rss-c142\n",
"\n",
"Title: Unlocked Samsung Galaxy S25+ Android Smartphones: Up to $530 off w/ trade + free shipping\n",
"Details: Samsung's offers up to a $530 credit on the Galaxy S25+ when you trade in your current device. That puts starting prices as low as $470 after the trade-in. Shop Now at Samsung\n",
"Features: \n",
"URL: https://www.dealnews.com/Unlocked-Samsung-Galaxy-S25-Android-Smartphones-Up-to-530-off-w-trade-free-shipping/21740347.html?iref=rss-c142\n",
"\n",
"Title: Samsung\n"
]
}
],
"source": [
"print(user_prompt[:2000])"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "84ab67e5",
"metadata": {},
"outputs": [],
"source": [
"def get_recommendations():\n",
" completion = groq_client.chat.completions.create(\n",
" model=\"meta-llama/llama-4-scout-17b-16e-instruct\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt}\n",
" ],\n",
" temperature=0.0,\n",
" max_tokens=2048\n",
" )\n",
"\n",
" content = completion.choices[0].message.content\n",
"\n",
" # Strip Markdown code block markers\n",
" if content.startswith(\"```\"):\n",
" content = content.strip(\"```\").strip(\"json\").strip()\n",
"\n",
" # Now parse the cleaned content\n",
" try:\n",
" result = json.loads(content)\n",
" return DealSelection(**result)\n",
" except json.JSONDecodeError as e:\n",
" print(\"⚠️ Failed to parse JSON:\", e)\n",
" print(\"🔎 Raw content:\", content)\n",
" return None"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "74d744c8",
"metadata": {},
"outputs": [],
"source": [
"result = get_recommendations()"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "7cbbda37",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(result.deals)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "78ec26f5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Deal(product_description='The Open-box Apple MacBook Air M3 13\" Laptop (2024) features a 13.6\" Liquid Retina Display, Apple M3 8-Core CPU w/ 10-Core GPU, 16GB Unified Memory, and 256GB SSD. This model is identified as MC8K4LL/A and is a great option for those looking for a high-performance laptop.', price=722.0, url='https://www.dealnews.com/products/Apple/Apple-Mac-Book-Air-M3-13-Laptop-2024-w-16-GB-RAM-256-GB-SSD/484646.html?iref=rss-c39')"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result.deals[1]"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "909b23bd",
"metadata": {},
"outputs": [],
"source": [
"from agents.scanner_agent import ScannerAgent"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "cbc2b0a4",
"metadata": {},
"outputs": [],
"source": [
"agent = ScannerAgent()\n",
"result = agent.scan()"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "bae84d27",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DealSelection(deals=[Deal(product_description='The Dell Inspiron 16 Plus 7640 is a laptop featuring a 16-inch 1920x1200 IPS touchscreen, Intel Core Ultra 9 185H 2.3GHz Meteor Lake H 16-core CPU, 16GB RAM, and 512GB NVMe M.2 SSD. It runs on Windows 11 Pro.', price=900.0, url='https://www.dealnews.com/Dell-Inspiron-16-Plus-7640-Core-Ultra-9-185-H-16-Touch-Laptop-for-900-free-shipping/21740394.html?iref=rss-c142'), Deal(product_description='The Dell Tower ECT1250 Core Ultra 7 265 Desktop PC features an Intel Core Ultra 7 265 20-Core 1.8GHz to 5.3GHz CPU, 16GB RAM, and 512GB NVMe M.2 SSD. It comes with a wired keyboard and mouse and runs on Windows 11 Pro.', price=700.0, url='https://www.dealnews.com/Dell-Tower-ECT1250-Core-Ultra-7-265-Desktop-PC-for-700-free-shipping/21740397.html?iref=rss-c142'), Deal(product_description='The Open-box Apple MacBook Air M3 13\" Laptop (2024) features a 13.6\" Liquid Retina Display, Apple M3 8-Core CPU w/ 10-Core GPU, 16GB Unified Memory, and 256GB SSD. The model number is MC8K4LL/A.', price=722.0, url='https://www.dealnews.com/products/Apple/Apple-Mac-Book-Air-M3-13-Laptop-2024-w-16-GB-RAM-256-GB-SSD/484646.html?iref=rss-c39'), Deal(product_description='The Polti Vaporetto Corded Smart Mop Electric Steam Cleaner features 12 multi-purpose attachments, 5-channel swivel mop, up to 50-PSI adjustable steam control, and a 156\" power cord. The model number is PTNA0018.', price=80.0, url='https://www.dealnews.com/products/Polti-Vaporetto-Corded-Smart-Mop-Electric-Steam-Cleaner/489869.html?iref=rss-f1912'), Deal(product_description='The Jackery E5000 Plus Whole-Home Backup Kit includes two Jackery 5000 Plus Portable Power Stations, two Jackery Battery Pack 5000 Plus Units, and a Jackery Smart Transfer Switch.', price=9024.0, url='https://www.dealnews.com/Jackery-E5000-Plus-Whole-Home-Backup-Kit-for-9-024-free-shipping/21738592.html?iref=rss-f1912')])"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.9.22"
}
},
"nbformat": 4,
"nbformat_minor": 5
}