Browse Source

Week 8 updates

pull/40/head
Edward Donner 7 months ago
parent
commit
ba4c4a1a97
  1. 21
      week1/day1.ipynb
  2. 10
      week1/day5.ipynb
  3. 38
      week2/day5.ipynb
  4. 33
      week8/agents/agent.py
  5. 57
      week8/agents/deals.py
  6. 27
      week8/agents/ensemble_agent.py
  7. 52
      week8/agents/frontier_agent.py
  8. 78
      week8/agents/messaging_agent.py
  9. 57
      week8/agents/planning_agent.py
  10. 37
      week8/agents/random_forest_agent.py
  11. 94
      week8/agents/scanner_agent.py
  12. 29
      week8/agents/specialist_agent.py
  13. 187
      week8/day1.ipynb
  14. 186
      week8/day2.0.ipynb
  15. 174
      week8/day2.1.ipynb
  16. 174
      week8/day2.2.ipynb
  17. 345
      week8/day2.3.ipynb
  18. 408
      week8/day2.4.ipynb
  19. 47
      week8/day3.ipynb
  20. 74
      week8/day4.ipynb
  21. 163
      week8/day5.ipynb
  22. 94
      week8/deal_agent_framework.py
  23. 3
      week8/hello.py
  24. 0
      week8/items.py
  25. 0
      week8/llama.py
  26. 35
      week8/log_utils.py
  27. 164
      week8/memory.json
  28. 61
      week8/price_is_right.py
  29. 180
      week8/price_is_right_final.py
  30. 2
      week8/pricer_ephemeral.py
  31. 2
      week8/pricer_service.py
  32. 0
      week8/pricer_service2.py
  33. 0
      week8/testing.py
  34. 28
      week8_wip/agents/messaging_agent.py
  35. 24
      week8_wip/agents/planning_agent.py
  36. 18
      week8_wip/agents/random_forest_agent.py
  37. 46
      week8_wip/agents/scanner_agent.py
  38. 10
      week8_wip/agents/specialist_agent.py
  39. 31093
      week8_wip/day2.1.ipynb
  40. 41094
      week8_wip/day2.2.ipynb
  41. 708
      week8_wip/day2.3.ipynb
  42. 1660
      week8_wip/day2.4.ipynb

21
week1/day1.ipynb

@ -11,7 +11,13 @@
"\n", "\n",
"Our goal is to code a new kind of Web Browser. Give it a URL, and it will respond with a summary. The Reader's Digest of the internet!!\n", "Our goal is to code a new kind of Web Browser. Give it a URL, and it will respond with a summary. The Reader's Digest of the internet!!\n",
"\n", "\n",
"Before starting, be sure to have followed the instructions in the \"README\" file, including creating your API key with OpenAI and adding it to the `.env` file." "Before starting, be sure to have followed the instructions in the \"README\" file, including creating your API key with OpenAI and adding it to the `.env` file.\n",
"\n",
"## If you're new to Jupyer Lab\n",
"\n",
"Welcome to the wonderful world of Data Science experimentation! Once you've used Jupyter Lab, you'll wonder how you ever lived without it. Simply click in each \"cell\" with code in it, like the cell immediately below this text, and hit Shift+Return to execute that cell. As you wish, you can add a cell with the + button in the toolbar, and print values of variables, or try out variations.\n",
"\n",
"If you need to start again, go to Kernel menu >> Restart kernel."
] ]
}, },
{ {
@ -47,6 +53,7 @@
"- Pressing \"Create new secret key\" on the top right\n", "- Pressing \"Create new secret key\" on the top right\n",
"- Select **Owned by:** you, **Project:** Default project, **Permissions:** All\n", "- Select **Owned by:** you, **Project:** Default project, **Permissions:** All\n",
"- Click Create secret key, and use that new key in the code and the `.env` file (it might take a few minutes to activate)\n", "- Click Create secret key, and use that new key in the code and the `.env` file (it might take a few minutes to activate)\n",
"- Do a Kernel >> Restart kernel, and execute the cells in this Jupyter lab starting at the top\n",
"4. As a fallback, replace the line `openai = OpenAI()` with `openai = OpenAI(api_key=\"your-key-here\")` - while it's not recommended to hard code tokens in Jupyter lab, because then you can't share your lab with others, it's a workaround for now\n", "4. As a fallback, replace the line `openai = OpenAI()` with `openai = OpenAI(api_key=\"your-key-here\")` - while it's not recommended to hard code tokens in Jupyter lab, because then you can't share your lab with others, it's a workaround for now\n",
"5. Contact me! Message me or email ed@edwarddonner.com and we will get this to work.\n", "5. Contact me! Message me or email ed@edwarddonner.com and we will get this to work.\n",
"\n", "\n",
@ -259,12 +266,14 @@
] ]
}, },
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": null, "id": "36ed9f14-b349-40e9-a42c-b367e77f8bda",
"id": "49c4315f-340b-4371-b6cd-2a772f4b7bdd",
"metadata": {}, "metadata": {},
"outputs": [], "source": [
"source": [] "## An extra exercise for those who enjoy web scraping\n",
"\n",
"You may notice that if you try `display_summary(\"https://openai.com\")` - it doesn't work! That's because OpenAI has a fancy website that uses Javascript. There are many ways around this that some of you might be familiar with. For example, Selenium is a hugely popular framework that runs a browser behind the scenes, renders the page, and allows you to query it. If you have experience with Selenium, Playwright or similar, then feel free to improve the Website class to use them. Please push your code afterwards so I can share it with other students!"
]
} }
], ],
"metadata": { "metadata": {

10
week1/day5.ipynb

@ -227,8 +227,14 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"system_prompt = \"You are an assistant that analyzes the contents of several relevant pages from a company website \\\n", "system_prompt = \"You are an assistant that analyzes the contents of several relevant pages from a company website \\\n",
"and creates a short humorous, entertaining, jokey brochure about the company for prospective customers, investors and recruits. Respond in markdown.\\\n", "and creates a short brochure about the company for prospective customers, investors and recruits. Respond in markdown.\\\n",
"Include details of company culture, customers and careers/jobs if you have the information.\"" "Include details of company culture, customers and careers/jobs if you have the information.\"\n",
"\n",
"# Or uncomment the line below for a more humorous brochure:\n",
"\n",
"# system_prompt = \"You are an assistant that analyzes the contents of several relevant pages from a company website \\\n",
"# and creates a short humorous, entertaining, jokey brochure about the company for prospective customers, investors and recruits. Respond in markdown.\\\n",
"# Include details of company culture, customers and careers/jobs if you have the information.\"\n"
] ]
}, },
{ {

38
week2/day5.ipynb

@ -298,7 +298,39 @@
"source": [ "source": [
"## Audio\n", "## Audio\n",
"\n", "\n",
"And let's make a function talker that uses OpenAI's speech model to generate Audio" "And let's make a function talker that uses OpenAI's speech model to generate Audio\n",
"\n",
"### Troubleshooting Audio issues\n",
"\n",
"If you have any problems running this code below (like a FileNotFound error, or a warning of a missing package), you may need to install FFmpeg, a very popular audio utility.\n",
"\n",
"**For PC Users**\n",
"\n",
"1. Download FFmpeg from the official website: https://ffmpeg.org/download.html\n",
"\n",
"2. Extract the downloaded files to a location on your computer (e.g., `C:\\ffmpeg`)\n",
"\n",
"3. Add the FFmpeg bin folder to your system PATH:\n",
"- Right-click on 'This PC' or 'My Computer' and select 'Properties'\n",
"- Click on 'Advanced system settings'\n",
"- Click on 'Environment Variables'\n",
"- Under 'System variables', find and edit 'Path'\n",
"- Add a new entry with the path to your FFmpeg bin folder (e.g., `C:\\ffmpeg\\bin`)\n",
"- Restart your command prompt, and within Jupyter Lab do Kernel -> Restart kernel, to pick up the changes\n",
"\n",
"4. Open a new command prompt and run this to make sure it's installed OK\n",
"`ffmpeg -version`\n",
"\n",
"**For Mac Users**\n",
"\n",
"1. Install homebrew if you don't have it already by running this in a Terminal window and following any instructions: \n",
"`/bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"`\n",
"\n",
"2. Then install FFmpeg with `brew install ffmpeg`\n",
"\n",
"3. Verify your installation with `ffmpeg -version` and if everything is good, within Jupyter Lab do Kernel -> Restart kernel to pick up the changes\n",
"\n",
"Message me or email me at ed@edwarddonner.com with any problems!"
] ]
}, },
{ {
@ -314,7 +346,7 @@
"def talker(message):\n", "def talker(message):\n",
" response = openai.audio.speech.create(\n", " response = openai.audio.speech.create(\n",
" model=\"tts-1\",\n", " model=\"tts-1\",\n",
" voice=\"onyx\", #alloy onyx\n", " voice=\"onyx\", # Also, try replacing onyx with alloy\n",
" input=message\n", " input=message\n",
" )\n", " )\n",
" \n", " \n",
@ -348,7 +380,7 @@
"4. An LLM can act as the Planner, dividing bigger tasks into smaller ones for the specialists\n", "4. An LLM can act as the Planner, dividing bigger tasks into smaller ones for the specialists\n",
"5. The concept of an Agent having autonomy / agency, beyond just responding to a prompt - such as Memory\n", "5. The concept of an Agent having autonomy / agency, beyond just responding to a prompt - such as Memory\n",
"\n", "\n",
"We're showing 1 and 2 here, and to a lesser extent 3 and 5." "We're showing 1 and 2 here, and to a lesser extent 3 and 5. In week 8 we will do the lot!"
] ]
}, },
{ {

33
week8/agents/agent.py

@ -0,0 +1,33 @@
import logging
class Agent:
"""
An abstract superclass for Agents
Used to log messages in a way that can identify each Agent
"""
# Foreground colors
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
WHITE = '\033[37m'
# Background color
BG_BLACK = '\033[40m'
# Reset code to return to default color
RESET = '\033[0m'
name: str = ""
color: str = '\033[37m'
def log(self, message):
"""
Log this as an info message, identifying the agent
"""
color_code = self.BG_BLACK + self.color
message = f"[{self.name}] {message}"
logging.info(color_code + message + self.RESET)

57
week8_wip/agents/deals.py → week8/agents/deals.py

@ -1,5 +1,5 @@
from pydantic import BaseModel from pydantic import BaseModel
from typing import List from typing import List, Dict, Self
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import re import re
import feedparser import feedparser
@ -15,7 +15,10 @@ feeds = [
"https://www.dealnews.com/c196/Home-Garden/?rss=1", "https://www.dealnews.com/c196/Home-Garden/?rss=1",
] ]
def extract(html_snippet): def extract(html_snippet: str) -> str:
"""
Use Beautiful Soup to clean up this HTML snippet and extract useful text
"""
soup = BeautifulSoup(html_snippet, 'html.parser') soup = BeautifulSoup(html_snippet, 'html.parser')
snippet_div = soup.find('div', class_='snippet summary') snippet_div = soup.find('div', class_='snippet summary')
@ -28,20 +31,24 @@ def extract(html_snippet):
result = html_snippet result = html_snippet
return result.replace('\n', ' ') return result.replace('\n', ' ')
class Deal: class ScrapedDeal:
"""
A class to represent a Deal retrieved from an RSS feed
"""
category: str category: str
title: str title: str
summary: str summary: str
url: str url: str
item_id: int
details: str details: str
features: str features: str
def __init__(self, entry, id): def __init__(self, entry: Dict[str, str]):
"""
Populate this instance based on the provided dict
"""
self.title = entry['title'] self.title = entry['title']
self.summary = extract(entry['summary']) self.summary = extract(entry['summary'])
self.url = entry['links'][0]['href'] self.url = entry['links'][0]['href']
self.item_id = id
stuff = requests.get(self.url).content stuff = requests.get(self.url).content
soup = BeautifulSoup(stuff, 'html.parser') soup = BeautifulSoup(stuff, 'html.parser')
content = soup.find('div', class_='content-section').get_text() content = soup.find('div', class_='content-section').get_text()
@ -53,32 +60,50 @@ class Deal:
self.features = "" self.features = ""
def __repr__(self): def __repr__(self):
"""
Return a string to describe this deal
"""
return f"<{self.title}>" return f"<{self.title}>"
def describe(self): def describe(self):
"""
Return a longer string to describe this deal for use in calling a model
"""
return f"Title: {self.title}\nDetails: {self.details.strip()}\nFeatures: {self.features.strip()}\nURL: {self.url}" return f"Title: {self.title}\nDetails: {self.details.strip()}\nFeatures: {self.features.strip()}\nURL: {self.url}"
@classmethod @classmethod
def fetch(cls): def fetch(cls, show_progress : bool = False) -> List[Self]:
"""
Retrieve all deals from the selected RSS feeds
"""
deals = [] deals = []
item_id = 1001 feed_iter = tqdm(feeds) if show_progress else feeds
for feed_url in tqdm(feeds): for feed_url in feed_iter:
feed = feedparser.parse(feed_url) feed = feedparser.parse(feed_url)
for entry in feed.entries[:10]: for entry in feed.entries[:10]:
deals.append(cls(entry, item_id)) deals.append(cls(entry))
item_id += 1 time.sleep(0.5)
time.sleep(1)
return deals return deals
class QualityDeal(BaseModel): class Deal(BaseModel):
"""
A class to Represent a Deal with a summary description
"""
product_description: str product_description: str
price: float price: float
url: str url: str
class QualityDealSelection(BaseModel): class DealSelection(BaseModel):
quality_deals: List[QualityDeal] """
A class to Represent a list of Deals
"""
deals: List[Deal]
class Opportunity(BaseModel): class Opportunity(BaseModel):
quality_deal: QualityDeal """
A class to represent a possible opportunity: a Deal where we estimate
it should cost more than it's being offered
"""
deal: Deal
estimate: float estimate: float
discount: float discount: float

27
week8_wip/agents/ensemble_agent.py → week8/agents/ensemble_agent.py

@ -2,19 +2,37 @@ import pandas as pd
from sklearn.linear_model import LinearRegression from sklearn.linear_model import LinearRegression
import joblib import joblib
from agents.agent import Agent
from agents.specialist_agent import SpecialistAgent from agents.specialist_agent import SpecialistAgent
from agents.frontier_agent import FrontierAgent from agents.frontier_agent import FrontierAgent
from agents.random_forest_agent import RandomForestAgent from agents.random_forest_agent import RandomForestAgent
class EnsembleAgent: class EnsembleAgent(Agent):
name = "Ensemble Agent"
color = Agent.YELLOW
def __init__(self, collection): def __init__(self, collection):
"""
Create an instance of Ensemble, by creating each of the models
And loading the weights of the Ensemble
"""
self.log("Initializing Ensemble Agent")
self.specialist = SpecialistAgent() self.specialist = SpecialistAgent()
self.frontier = FrontierAgent(collection) self.frontier = FrontierAgent(collection)
self.random_forest = RandomForestAgent() self.random_forest = RandomForestAgent()
self.model = joblib.load('ensemble_model.pkl') self.model = joblib.load('ensemble_model.pkl')
self.log("Ensemble Agent is ready")
def price(self, description): def price(self, description: str) -> float:
"""
Run this ensemble model
Ask each of the models to price the product
Then use the Linear Regression model to return the weighted price
:param description: the description of a product
:return: an estimate of its price
"""
self.log("Running Ensemble Agent - collaborating with specialist, frontier and random forest agents")
specialist = self.specialist.price(description) specialist = self.specialist.price(description)
frontier = self.frontier.price(description) frontier = self.frontier.price(description)
random_forest = self.random_forest.price(description) random_forest = self.random_forest.price(description)
@ -25,5 +43,6 @@ class EnsembleAgent:
'Min': [min(specialist, frontier, random_forest)], 'Min': [min(specialist, frontier, random_forest)],
'Max': [max(specialist, frontier, random_forest)], 'Max': [max(specialist, frontier, random_forest)],
}) })
y = self.model.predict(X) y = self.model.predict(X)[0]
return y[0] self.log(f"Ensemble Agent complete - returning ${y:.2f}")
return y

52
week8_wip/agents/frontier_agent.py → week8/agents/frontier_agent.py

@ -4,30 +4,55 @@ import os
import re import re
import math import math
import json import json
from typing import List from typing import List, Dict
from openai import OpenAI from openai import OpenAI
from sentence_transformers import SentenceTransformer from sentence_transformers import SentenceTransformer
from datasets import load_dataset from datasets import load_dataset
import chromadb import chromadb
from items import Item from items import Item
from testing import Tester from testing import Tester
from agents.agent import Agent
class FrontierAgent:
class FrontierAgent(Agent):
name = "Frontier Agent"
color = Agent.BLUE
MODEL = "gpt-4o-mini" MODEL = "gpt-4o-mini"
def __init__(self, collection): def __init__(self, collection):
"""
Set up this instance by connecting to OpenAI, to the Chroma Datastore,
And setting up the vector encoding model
"""
self.log("Initializing Frontier Agent")
self.openai = OpenAI() self.openai = OpenAI()
self.collection = collection self.collection = collection
self.model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2') self.model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
self.log("Frontier Agent is ready")
def make_context(self, similars: List[str], prices: List[float]): def make_context(self, similars: List[str], prices: List[float]) -> str:
"""
Create context that can be inserted into the prompt
:param similars: similar products to the one being estimated
:param prices: prices of the similar products
:return: text to insert in the prompt that provides context
"""
message = "To provide some context, here are some other items that might be similar to the item you need to estimate.\n\n" message = "To provide some context, here are some other items that might be similar to the item you need to estimate.\n\n"
for similar, price in zip(similars, prices): for similar, price in zip(similars, prices):
message += f"Potentially related product:\n{similar}\nPrice is ${price:.2f}\n\n" message += f"Potentially related product:\n{similar}\nPrice is ${price:.2f}\n\n"
return message return message
def messages_for(self, description: str, similars: List[str], prices: List[float]): def messages_for(self, description: str, similars: List[str], prices: List[float]) -> List[Dict[str, str]]:
"""
Create the message list to be included in a call to OpenAI
With the system and user prompt
:param description: a description of the product
:param similars: similar products to this one
:param prices: prices of similar products
:return: the list of messages in the format expected by OpenAI
"""
system_message = "You estimate prices of items. Reply only with the price, no explanation" system_message = "You estimate prices of items. Reply only with the price, no explanation"
user_prompt = self.make_context(similars, prices) user_prompt = self.make_context(similars, prices)
user_prompt += "And now the question for you:\n\n" user_prompt += "And now the question for you:\n\n"
@ -39,19 +64,34 @@ class FrontierAgent:
] ]
def find_similars(self, description: str): def find_similars(self, description: str):
"""
Return a list of items similar to the given one by looking in the Chroma datastore
"""
self.log("Frontier Agent is performing a RAG search of the Chroma datastore to find 5 similar products")
vector = self.model.encode([description]) vector = self.model.encode([description])
results = self.collection.query(query_embeddings=vector.astype(float).tolist(), n_results=5) results = self.collection.query(query_embeddings=vector.astype(float).tolist(), n_results=5)
documents = results['documents'][0][:] documents = results['documents'][0][:]
prices = [m['price'] for m in results['metadatas'][0][:]] prices = [m['price'] for m in results['metadatas'][0][:]]
self.log("Frontier Agent has found similar products")
return documents, prices return documents, prices
def get_price(self, s) -> float: def get_price(self, s) -> float:
"""
A utility that plucks a floating point number out of a string
"""
s = s.replace('$','').replace(',','') s = s.replace('$','').replace(',','')
match = re.search(r"[-+]?\d*\.\d+|\d+", s) match = re.search(r"[-+]?\d*\.\d+|\d+", s)
return float(match.group()) if match else 0.0 return float(match.group()) if match else 0.0
def price(self, description: str) -> float: def price(self, description: str) -> float:
"""
Make a call to OpenAI to estimate the price of the described product,
by looking up 5 similar products and including them in the prompt to give context
:param description: a description of the product
:return: an estimate of the price
"""
documents, prices = self.find_similars(description) documents, prices = self.find_similars(description)
self.log("Frontier Agent is about to call OpenAI with context including 5 similar products")
response = self.openai.chat.completions.create( response = self.openai.chat.completions.create(
model=self.MODEL, model=self.MODEL,
messages=self.messages_for(description, documents, prices), messages=self.messages_for(description, documents, prices),
@ -59,5 +99,7 @@ class FrontierAgent:
max_tokens=5 max_tokens=5
) )
reply = response.choices[0].message.content reply = response.choices[0].message.content
return self.get_price(reply) result = self.get_price(reply)
self.log(f"Frontier Agent completed - predicting ${result:.2f}")
return result

78
week8/agents/messaging_agent.py

@ -0,0 +1,78 @@
import os
from twilio.rest import Client
from agents.deals import Opportunity
import http.client
import urllib
from agents.agent import Agent
DO_TEXT = False
DO_PUSH = True
class MessagingAgent(Agent):
name = "Messaging Agent"
color = Agent.WHITE
def __init__(self):
"""
Set up this object to either do push notifications via Pushover,
or SMS via Twilio,
whichever is specified in the constants
"""
self.log(f"Messaging Agent is initializing")
if DO_TEXT:
account_sid = os.getenv('TWILIO_ACCOUNT_SID', 'your-sid-if-not-using-env')
auth_token = os.getenv('TWILIO_AUTH_TOKEN', 'your-auth-if-not-using-env')
self.me_from = os.getenv('TWILIO_FROM', 'your-phone-number-if-not-using-env')
self.me_to = os.getenv('MY_PHONE_NUMBER', 'your-phone-number-if-not-using-env')
self.client = Client(account_sid, auth_token)
self.log("Messaging Agent has initialized Twilio")
if DO_PUSH:
self.pushover_user = os.getenv('PUSHOVER_USER', 'your-pushover-user-if-not-using-env')
self.pushover_token = os.getenv('PUSHOVER_TOKEN', 'your-pushover-user-if-not-using-env')
self.log("Messaging Agent has initialized Pushover")
def message(self, text):
"""
Send an SMS message using the Twilio API
"""
self.log("Messaging Agent is sending a text message")
message = self.client.messages.create(
from_=self.me_from,
body=text,
to=self.me_to
)
def push(self, text):
"""
Send a Push Notification using the Pushover API
"""
self.log("Messaging Agent is sending a push notification")
conn = http.client.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.parse.urlencode({
"token": self.pushover_token,
"user": self.pushover_user,
"message": text,
"sound": "cashregister"
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()
def alert(self, opportunity: Opportunity):
"""
Make an alert about the specified Opportunity
"""
text = f"Deal Alert! Price=${opportunity.deal.price:.2f}, "
text += f"Estimate=${opportunity.estimate:.2f}, "
text += f"Discount=${opportunity.discount:.2f} :"
text += opportunity.deal.product_description[:10]+'... '
text += opportunity.deal.url
if DO_TEXT:
self.message(text)
if DO_PUSH:
self.push(text)
self.log("Messaging Agent has completed")

57
week8/agents/planning_agent.py

@ -0,0 +1,57 @@
from typing import Optional, List
from agents.agent import Agent
from agents.deals import ScrapedDeal, DealSelection, Deal, Opportunity
from agents.scanner_agent import ScannerAgent
from agents.ensemble_agent import EnsembleAgent
from agents.messaging_agent import MessagingAgent
class PlanningAgent(Agent):
name = "Planning Agent"
color = Agent.GREEN
DEAL_THRESHOLD = 50
def __init__(self, collection):
"""
Create instances of the 3 Agents that this planner coordinates across
"""
self.log("Planning Agent is initializing")
self.scanner = ScannerAgent()
self.ensemble = EnsembleAgent(collection)
self.messenger = MessagingAgent()
self.log("Planning Agent is ready")
def run(self, deal: Deal) -> Opportunity:
"""
Run the workflow for a particular deal
:param deal: the deal, summarized from an RSS scrape
:returns: an opportunity including the discount
"""
self.log("Planning Agent is pricing up a potential deal")
estimate = self.ensemble.price(deal.product_description)
discount = estimate - deal.price
self.log(f"Planning Agent has processed a deal with discount ${discount:.2f}")
return Opportunity(deal=deal, estimate=estimate, discount=discount)
def plan(self, memory: List[str] = []) -> Optional[Opportunity]:
"""
Run the full workflow:
1. Use the ScannerAgent to find deals from RSS feeds
2. Use the EnsembleAgent to estimate them
3. Use the MessagingAgent to send a notification of deals
:param memory: a list of URLs that have been surfaced in the past
:return: an Opportunity if one was surfaced, otherwise None
"""
self.log("Planning Agent is kicking off a run")
selection = self.scanner.scan(memory=memory)
if selection:
opportunities = [self.run(deal) for deal in selection.deals[:5]]
opportunities.sort(key=lambda opp: opp.discount, reverse=True)
best = opportunities[0]
self.log(f"Planning Agent has identified the best deal has discount ${best.discount:.2f}")
if best.discount > self.DEAL_THRESHOLD:
self.messenger.alert(best)
self.log("Planning Agent has completed a run")
return best if best.discount > self.DEAL_THRESHOLD else None
return None

37
week8/agents/random_forest_agent.py

@ -0,0 +1,37 @@
# imports
import os
import re
from typing import List
from sentence_transformers import SentenceTransformer
import joblib
from agents.agent import Agent
class RandomForestAgent(Agent):
name = "Random Forest Agent"
color = Agent.MAGENTA
def __init__(self):
"""
Initialize this object by loading in the saved model weights
and the SentenceTransformer vector encoding model
"""
self.log("Random Forest Agent is initializing")
self.vectorizer = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
self.model = joblib.load('random_forest_model.pkl')
self.log("Random Forest Agent is ready")
def price(self, description: str) -> float:
"""
Use a Random Forest model to estimate the price of the described item
:param description: the product to be estimated
:return: the price as a float
"""
self.log("Random Forest Agent is starting a prediction")
vector = self.vectorizer.encode([description])
result = max(0, self.model.predict(vector)[0])
self.log(f"Random Forest Agent completed - predicting ${result:.2f}")
return result

94
week8/agents/scanner_agent.py

@ -0,0 +1,94 @@
import os
import json
from typing import Optional, List
from openai import OpenAI
from agents.deals import ScrapedDeal, DealSelection
from agents.agent import Agent
class ScannerAgent(Agent):
MODEL = "gpt-4o-mini"
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.
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.
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.
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.
{"deals": [
{
"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.",
"price": 99.99,
"url": "the url as provided"
},
...
]}"""
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.
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.
Remember to respond with a paragraph of text in the product_description field for each of the 5 items that you select.
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.
Deals:
"""
USER_PROMPT_SUFFIX = "\n\nStrictly respond in JSON and include exactly 5 deals, no more."
name = "Scanner Agent"
color = Agent.CYAN
def __init__(self):
"""
Set up this instance by initializing OpenAI
"""
self.log("Scanner Agent is initializing")
self.openai = OpenAI()
self.log("Scanner Agent is ready")
def fetch_deals(self, memory) -> List[ScrapedDeal]:
"""
Look up deals published on RSS feeds
Return any new deals that are not already in the memory provided
"""
self.log("Scanner Agent is about to fetch deals from RSS feed")
urls = [opp.deal.url for opp in memory]
scraped = ScrapedDeal.fetch()
result = [scrape for scrape in scraped if scrape.url not in urls]
self.log(f"Scanner Agent received {len(result)} deals not already scraped")
return result
def make_user_prompt(self, scraped) -> str:
"""
Create a user prompt for OpenAI based on the scraped deals provided
"""
user_prompt = self.USER_PROMPT_PREFIX
user_prompt += '\n\n'.join([scrape.describe() for scrape in scraped])
user_prompt += self.USER_PROMPT_SUFFIX
return user_prompt
def scan(self, memory: List[str]=[]) -> Optional[DealSelection]:
"""
Call OpenAI to provide a high potential list of deals with good descriptions and prices
Use StructuredOutputs to ensure it conforms to our specifications
:param memory: a list of URLs representing deals already raised
:return: a selection of good deals, or None if there aren't any
"""
scraped = self.fetch_deals(memory)
if scraped:
user_prompt = self.make_user_prompt(scraped)
self.log("Scanner Agent is calling OpenAI using Structured Output")
result = self.openai.beta.chat.completions.parse(
model=self.MODEL,
messages=[
{"role": "system", "content": self.SYSTEM_PROMPT},
{"role": "user", "content": user_prompt}
],
response_format=DealSelection
)
result = result.choices[0].message.parsed
result.deals = [deal for deal in result.deals if deal.price>0]
self.log(f"Scanner Agent received {len(result.deals)} selected deals with price>0 from OpenAI")
return result
return None

29
week8/agents/specialist_agent.py

@ -0,0 +1,29 @@
import modal
from agents.agent import Agent
class SpecialistAgent(Agent):
"""
An Agent that runs our fine-tuned LLM that's running remotely on Modal
"""
name = "Specialist Agent"
color = Agent.RED
def __init__(self):
"""
Set up this Agent by creating an instance of the modal class
"""
self.log("Specialist Agent is initializing - connecting to modal")
Pricer = modal.Cls.lookup("pricer-service", "Pricer")
self.pricer = Pricer()
self.log("Specialist Agent is ready")
def price(self, description: str) -> float:
"""
Make a remote call to return the estimate of the price of this item
"""
self.log("Specialist Agent is calling remote fine-tuned model")
result = self.pricer.price.remote(description)
self.log(f"Specialist Agent completed - predicting ${result:.2f}")
return result

187
week8_wip/day1.ipynb → week8/day1.ipynb

@ -5,7 +5,14 @@
"id": "e426cd04-c053-43e8-b505-63cee7956a53", "id": "e426cd04-c053-43e8-b505-63cee7956a53",
"metadata": {}, "metadata": {},
"source": [ "source": [
"May need to update environment if cloned git after Sep 26\n", "# Welcome to a very busy Week 8 folder\n",
"\n",
"## We have lots to do this week!\n",
"\n",
"We'll move at a faster pace than usual, particularly as you're becoming proficient LLM engineers.\n",
"\n",
"One quick admin thing: I've added a number of packages to the environment.yml file during September. To make sure you have the latest repo with the latest code, it's worth doing this from the `llm_engineering` project folder:\n",
"\n",
"```\n", "```\n",
"git pull\n", "git pull\n",
"conda env update --f environment.yml --prune\n", "conda env update --f environment.yml --prune\n",
@ -19,12 +26,14 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# Just one import to start with!!\n",
"\n",
"import modal" "import modal"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": null,
"id": "0d240622-8422-4c99-8464-c04d063e4cb6", "id": "0d240622-8422-4c99-8464-c04d063e4cb6",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -34,7 +43,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": null,
"id": "3b133701-f550-44a1-a67f-eb7ccc4769a9", "id": "3b133701-f550-44a1-a67f-eb7ccc4769a9",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -44,21 +53,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": null,
"id": "0f3f73ae-1295-49f3-9099-b8b41fc3429b", "id": "0f3f73ae-1295-49f3-9099-b8b41fc3429b",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"'Hello from Seaport, New York, US!!'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"with app.run(show_progress=False):\n", "with app.run(show_progress=False):\n",
" reply=hello.local()\n", " reply=hello.local()\n",
@ -67,27 +65,33 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": null,
"id": "c1d8c6f9-edc7-4e52-9b3a-c07d7cff1ac7", "id": "c1d8c6f9-edc7-4e52-9b3a-c07d7cff1ac7",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"'Hello from Frankfurt am Main, Hesse, DE!!'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"with app.run(show_progress=False):\n", "with app.run(show_progress=False):\n",
" reply=hello.remote()\n", " reply=hello.remote()\n",
"reply" "reply"
] ]
}, },
{
"cell_type": "markdown",
"id": "22e8d804-c027-45fb-8fef-06e7bba6295a",
"metadata": {},
"source": [
"# Before we move on -\n",
"\n",
"## We need to set your HuggingFace Token as a secret in Modal\n",
"\n",
"1. Go to modal.com, sign in and go to your dashboard\n",
"2. Click on Secrets in the nav bar\n",
"3. Create new secret, click on Hugging Face\n",
"4. Fill in your HF_TOKEN where it prompts you\n",
"\n",
"### And now back to business: time to work with Llama"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -95,7 +99,6 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"import modal\n",
"from llama import app, generate" "from llama import app, generate"
] ]
}, },
@ -136,6 +139,20 @@
"result" "result"
] ]
}, },
{
"cell_type": "markdown",
"id": "04d8747f-8452-4077-8af6-27e03888508a",
"metadata": {},
"source": [
"## Transitioning From Ephemeral Apps to Deployed Apps\n",
"\n",
"From a command line, `modal deploy xxx` will deploy your code as a Deployed App\n",
"\n",
"This is how you could package your AI service behind an API to be used in a Production System.\n",
"\n",
"You can also build REST endpoints easily, although we won't cover that as we'll be calling direct from Python."
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -169,73 +186,20 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "58f5d19f-8ffc-496c-832b-04e0d5892f54",
"metadata": {},
"outputs": [],
"source": [
"import modal"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f56d1e55-2a03-4ce2-bb47-2ab6b9175a02", "id": "f56d1e55-2a03-4ce2-bb47-2ab6b9175a02",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[2K\u001b[34m⠸\u001b[0m Creating objects.....\n",
"\u001b[38;5;244m└── \u001b[0m\u001b[34m⠋\u001b[0m Creating mount /Users/ed/dev/llm_engineering/week8/pricer_service2.py: \n",
"\u001b[2K\u001b[1A\u001b[2K\u001b[1A\u001b[2K\u001b[34m⠦\u001b[0m Creating objects...\n",
"\u001b[38;5;244m└── \u001b[0m\u001b[34m⠸\u001b[0m Creating mount /Users/ed/dev/llm_engineering/week8/pricer_service2.py: \n",
"\u001b[2K\u001b[1A\u001b[2K\u001b[1A\u001b[2K\u001b[34m⠏\u001b[0m Creating objects...\n",
"\u001b[38;5;244m└── \u001b[0m\u001b[34m⠦\u001b[0m Creating mount /Users/ed/dev/llm_engineering/week8/pricer_service2.py: \n",
"\u001b[2K\u001b[1A\u001b[2K\u001b[1A\u001b[2K\u001b[34m⠹\u001b[0m Creating objects...\n",
"\u001b[38;5;244m├── \u001b[0m🔨 Created mount /Users/ed/dev/llm_engineering/week8/pricer_service2.py\n",
"\u001b[2K\u001b[1A\u001b[2K\u001b[1A\u001b[2K\u001b[34m⠴\u001b[0m Creating objects...load_model_to_folder.\n",
"\u001b[38;5;244m├── \u001b[0m🔨 Created mount /Users/ed/dev/llm_engineering/week8/pricer_service2.py\n",
"\u001b[38;5;244m├── \u001b[0m🔨 Created function Pricer.download_model_to_folder.\n",
"\u001b[38;5;244m├── \u001b[0m🔨 Created function Pricer.*.\n",
"\u001b[2K\u001b[1A\u001b[2K\u001b[1A\u001b[2K\u001b[1A\u001b[2K\u001b[1A\u001b[2K\u001b[34m⠴\u001b[0m Creating objects...\n",
"\u001b[38;5;244m├── \u001b[0m🔨 Created mount /Users/ed/dev/llm_engineering/week8/pricer_service2.py\n",
"\u001b[38;5;244m├── \u001b[0m🔨 Created function Pricer.download_model_to_folder.\n",
"\u001b[38;5;244m├── \u001b[0m🔨 Created function Pricer.*.\n",
"\u001b[38;5;244m└── \u001b[0m🔨 Created function Pricer.price.\n",
"\u001b[1A\u001b[2K\u001b[1A\u001b[2K\u001b[1A\u001b[2K\u001b[1A\u001b[2K\u001b[1A\u001b[2K\u001b[32m✓\u001b[0m Created objects.\n",
"\u001b[38;5;244m├── \u001b[0m🔨 Created mount /Users/ed/dev/llm_engineering/week8/pricer_service2.py\n",
"\u001b[38;5;244m├── \u001b[0m🔨 Created function Pricer.download_model_to_folder.\n",
"\u001b[38;5;244m├── \u001b[0m🔨 Created function Pricer.*.\n",
"\u001b[38;5;244m└── \u001b[0m🔨 Created function Pricer.price.\n",
"\u001b[32m✓\u001b[0m App deployed in 1.570s! 🎉\n",
"\n",
"View Deployment: \u001b[35mhttps://modal.com/apps/ed-donner/main/deployed/pricer-service\u001b[0m\n"
]
}
],
"source": [ "source": [
"!modal deploy pricer_service2" "!modal deploy pricer_service2"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": null,
"id": "9e19daeb-1281-484b-9d2f-95cc6fed2622", "id": "9e19daeb-1281-484b-9d2f-95cc6fed2622",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"133.0\n"
]
}
],
"source": [ "source": [
"\n",
"import modal\n",
"\n",
"Pricer = modal.Cls.lookup(\"pricer-service\", \"Pricer\")\n", "Pricer = modal.Cls.lookup(\"pricer-service\", \"Pricer\")\n",
"pricer = Pricer()\n", "pricer = Pricer()\n",
"reply = pricer.price.remote(\"Quadcast HyperX condenser mic, connects via usb-c to your computer for crystal clear audio\")\n", "reply = pricer.price.remote(\"Quadcast HyperX condenser mic, connects via usb-c to your computer for crystal clear audio\")\n",
@ -243,65 +207,32 @@
] ]
}, },
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": 9, "id": "3754cfdd-ae28-47c8-91f2-6e060e2c91b3",
"id": "ac331454-21e2-4b37-9602-4667006e34ee",
"metadata": {},
"outputs": [],
"source": [
"reply = pricer.price.remote(\"iphone SE\")\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "c3a71dcd-b71d-4c48-b0d9-3ac296d2046a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"299.0"
]
},
"execution_count": 10,
"metadata": {}, "metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"reply" "## And now introducing our Agent class"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": null,
"id": "ba9aedca-6a7b-4d30-9f64-59d76f76fb6d", "id": "ba9aedca-6a7b-4d30-9f64-59d76f76fb6d",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from agents.specialist_agent import price" "from agents.specialist_agent import SpecialistAgent"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": null,
"id": "fe5843e5-e958-4a65-8326-8f5b4686de7f", "id": "fe5843e5-e958-4a65-8326-8f5b4686de7f",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"133.0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"price(\"Quadcast HyperX condenser mic, connects via usb-c to your computer for crystal clear audio\")" "agent = SpecialistAgent()\n",
"agent.price(\"iPad Pro 2nd generation\")"
] ]
}, },
{ {

186
week8_wip/day2.0.ipynb → week8/day2.0.ipynb

@ -1,8 +1,30 @@
{ {
"cells": [ "cells": [
{
"cell_type": "markdown",
"id": "046fd8f8-ad14-4c7f-b759-fec52f5b5306",
"metadata": {},
"source": [
"# The Price is Right\n",
"\n",
"Today we build a more complex solution for estimating prices of goods.\n",
"\n",
"1. This notebook: create a RAG database with our 400,000 training data\n",
"2. Day 2.1 notebook: visualize in 2D\n",
"3. Day 2.2 notebook: visualize in 3D\n",
"4. Day 2.3 notebook: build and test a RAG pipeline with GPT-4o-mini\n",
"5. Day 2.4 notebook: (a) bring back our Random Forest pricer (b) Create a Ensemble pricer that allows contributions from all the pricers\n",
"\n",
"Phew! That's a lot to get through in one day!\n",
"\n",
"## PLEASE NOTE:\n",
"\n",
"We already have a very powerful product estimator with our proprietary, fine-tuned LLM. Most people would be very satisfied with that! The main reason we're adding these extra steps is to deepen your expertise with RAG and with Agentic workflows.\n"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": null,
"id": "993a2a24-1a58-42be-8034-6d116fb8d786", "id": "993a2a24-1a58-42be-8034-6d116fb8d786",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -29,22 +51,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": null,
"id": "0e31676f-6f31-465f-a80e-02d51ff8425a",
"metadata": {},
"outputs": [],
"source": [
"# CONSTANTS\n",
"\n",
"HF_USER = \"ed-donner\" # your HF name here! Or use mine if you just want to reproduce my results.\n",
"DATASET_NAME = f\"{HF_USER}/pricer-data\"\n",
"QUESTION = \"How much does this cost to the nearest dollar?\\n\\n\"\n",
"DB = \"products_vectorstore\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2359ccc0-dbf2-4b1e-9473-e472b32f548b", "id": "2359ccc0-dbf2-4b1e-9473-e472b32f548b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -53,26 +60,16 @@
"\n", "\n",
"load_dotenv()\n", "load_dotenv()\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n", "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')" "os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')\n",
"DB = \"products_vectorstore\""
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": null,
"id": "a29fcc4e-e4d7-4c54-aa6b-e5d1111ea9c4", "id": "645167e6-cf0d-42d2-949f-1089a25a2841",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Token is valid (permission: write).\n",
"Your token has been saved in your configured git credential helpers (osxkeychain).\n",
"Your token has been saved to /Users/ed/.cache/huggingface/token\n",
"Login successful\n"
]
}
],
"source": [ "source": [
"# Log in to HuggingFace\n", "# Log in to HuggingFace\n",
"\n", "\n",
@ -82,7 +79,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": null,
"id": "688bd995-ec3e-43cd-8179-7fe14b275877", "id": "688bd995-ec3e-43cd-8179-7fe14b275877",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -95,29 +92,31 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": null,
"id": "2817eaf5-4302-4a18-9148-d1062e3b3dbb", "id": "2817eaf5-4302-4a18-9148-d1062e3b3dbb",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{ "source": [
"data": { "train[0].prompt"
"text/plain": [
"400000"
] ]
}, },
"execution_count": 6, {
"cell_type": "markdown",
"id": "9ae1ba16-7e80-4096-ac88-64ef8edcc80c",
"metadata": {}, "metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"items = train\n", "# Now create a Chroma Datastore\n",
"len(items)" "\n",
"In Week 5, we created a Chroma datastore with 123 documents representing chunks of objects from our fictional company Insurellm.\n",
"\n",
"Now we will create a Chroma datastore with 400,000 products from our training dataset! It's getting real!\n",
"\n",
"Note that we won't be using LangChain, but the API is very straightforward and consistent with before."
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": null,
"id": "f4aab95e-d719-4476-b6e7-e248120df25a", "id": "f4aab95e-d719-4476-b6e7-e248120df25a",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -127,18 +126,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": null,
"id": "5f95dafd-ab80-464e-ba8a-dec7a2424780", "id": "5f95dafd-ab80-464e-ba8a-dec7a2424780",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Deleted existing collection: products\n"
]
}
],
"source": [ "source": [
"# Check if the collection exists and delete it if it does\n", "# Check if the collection exists and delete it if it does\n",
"collection_name = \"products\"\n", "collection_name = \"products\"\n",
@ -150,9 +141,27 @@
"collection = client.create_collection(collection_name)" "collection = client.create_collection(collection_name)"
] ]
}, },
{
"cell_type": "markdown",
"id": "d392ed28-203d-4e73-be87-ac1390bdf722",
"metadata": {},
"source": [
"# Introducing the SentenceTransfomer\n",
"\n",
"The all-MiniLM is a very useful model from HuggingFace that maps sentences & paragraphs to a 384 dimensional dense vector space and is ideal for tasks like semantic search.\n",
"\n",
"https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2\n",
"\n",
"It can run pretty quickly locally.\n",
"\n",
"Last time we used OpenAI embeddings to produce vector embeddings. Benefits compared to OpenAI embeddings:\n",
"1. It's free and fast!\n",
"3. We can run it locally, so the data never leaves our box - might be useful if you're building a personal RAG\n"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": null,
"id": "a87db200-d19d-44bf-acbd-15c45c70f5c9", "id": "a87db200-d19d-44bf-acbd-15c45c70f5c9",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -162,7 +171,29 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": null,
"id": "9b23a025-4c35-4d3a-96ad-b956cad37b0a",
"metadata": {},
"outputs": [],
"source": [
"# Pass in a list of texts, get back a numpy array of vectors\n",
"\n",
"vector = model.encode([\"Well hi there\"])[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8adde63f-e732-4f7c-bba9-f8b2a469f14e",
"metadata": {},
"outputs": [],
"source": [
"vector"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "38de1bf8-c9b5-45b4-9f4b-86af93b3f80d", "id": "38de1bf8-c9b5-45b4-9f4b-86af93b3f80d",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -174,23 +205,25 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": null,
"id": "8c79e2fe-1f50-4ebf-9a93-34f3088f2996", "id": "8c1205bd-4692-44ef-8ea4-69f255354537",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{ "source": [
"name": "stderr", "description(train[0])"
"output_type": "stream",
"text": [
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 400/400 [21:47<00:00, 3.27s/it]\n"
] ]
} },
], {
"cell_type": "code",
"execution_count": null,
"id": "8c79e2fe-1f50-4ebf-9a93-34f3088f2996",
"metadata": {},
"outputs": [],
"source": [ "source": [
"for i in tqdm(range(0, len(items), 1000)):\n", "for i in tqdm(range(0, len(train), 1000)):\n",
" documents = [description(item) for item in items[i: i+1000]]\n", " documents = [description(item) for item in train[i: i+1000]]\n",
" vectors = model.encode(documents).astype(float).tolist()\n", " vectors = model.encode(documents).astype(float).tolist()\n",
" metadatas = [{\"category\": item.category, \"price\": item.price} for item in items[i: i+1000]]\n", " metadatas = [{\"category\": item.category, \"price\": item.price} for item in train[i: i+1000]]\n",
" ids = [f\"doc_{j}\" for j in range(i, i+1000)]\n", " ids = [f\"doc_{j}\" for j in range(i, i+1000)]\n",
" collection.add(\n", " collection.add(\n",
" ids=ids,\n", " ids=ids,\n",
@ -199,17 +232,6 @@
" metadatas=metadatas\n", " metadatas=metadatas\n",
" )" " )"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"id": "525fc313-8a16-4ac0-8c42-6a6d1ba1c9b8",
"metadata": {},
"outputs": [],
"source": [
"CATEGORIES = ['Appliances', 'Automotive', 'Cell_Phones_and_Accessories', 'Electronics','Musical_Instruments', 'Office_Products', 'Tools_and_Home_Improvement', 'Toys_and_Games']\n",
"COLORS = ['red', 'blue', 'brown', 'orange', 'yellow', 'green' , 'purple', 'cyan']"
]
} }
], ],
"metadata": { "metadata": {

174
week8/day2.1.ipynb

@ -0,0 +1,174 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "b577c1be-f7a4-4549-8d27-30cb35407225",
"metadata": {},
"source": [
"# The Price is Right\n",
"\n",
"Today we build a more complex solution for estimating prices of goods.\n",
"\n",
"1. Day 2.0 notebook: create a RAG database with our 400,000 training data\n",
"2. Day 2.1 notebook: visualize in 2D\n",
"3. Day 2.2 notebook: visualize in 3D\n",
"4. Day 2.3 notebook: build and test a RAG pipeline with GPT-4o-mini\n",
"5. Day 2.4 notebook: (a) bring back our Random Forest pricer (b) Create a Ensemble pricer that allows contributions from all the pricers\n",
"\n",
"Phew! That's a lot to get through in one day!\n",
"\n",
"## PLEASE NOTE:\n",
"\n",
"We already have a very powerful product estimator with our proprietary, fine-tuned LLM. Most people would be very satisfied with that! The main reason we're adding these extra steps is to deepen your expertise with RAG and with Agentic workflows.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "993a2a24-1a58-42be-8034-6d116fb8d786",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import re\n",
"import math\n",
"import json\n",
"from tqdm import tqdm\n",
"import random\n",
"from dotenv import load_dotenv\n",
"from huggingface_hub import login\n",
"import numpy as np\n",
"import pickle\n",
"from sentence_transformers import SentenceTransformer\n",
"from datasets import load_dataset\n",
"import chromadb\n",
"from items import Item\n",
"from sklearn.manifold import TSNE\n",
"import plotly.graph_objects as go"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1cc1fe53-612f-4228-aa02-8758f4c2098f",
"metadata": {},
"outputs": [],
"source": [
"# It is very fun turning this up to 400_000 and seeing the full dataset visualized,\n",
"# but it almost crashes my box every time so do that at your own risk!! 10_000 is safe!\n",
"\n",
"MAXIMUM_DATAPOINTS = 10_000"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4aab95e-d719-4476-b6e7-e248120df25a",
"metadata": {},
"outputs": [],
"source": [
"DB = \"products_vectorstore\"\n",
"client = chromadb.PersistentClient(path=DB)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f95dafd-ab80-464e-ba8a-dec7a2424780",
"metadata": {},
"outputs": [],
"source": [
"collection = client.get_or_create_collection('products')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "525fc313-8a16-4ac0-8c42-6a6d1ba1c9b8",
"metadata": {},
"outputs": [],
"source": [
"CATEGORIES = ['Appliances', 'Automotive', 'Cell_Phones_and_Accessories', 'Electronics','Musical_Instruments', 'Office_Products', 'Tools_and_Home_Improvement', 'Toys_and_Games']\n",
"COLORS = ['red', 'blue', 'brown', 'orange', 'yellow', 'green' , 'purple', 'cyan']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a4cf1c9a-1ced-48d4-974c-3c850905034e",
"metadata": {},
"outputs": [],
"source": [
"# Prework\n",
"result = collection.get(include=['embeddings', 'documents', 'metadatas'], limit=MAXIMUM_DATAPOINTS)\n",
"vectors = np.array(result['embeddings'])\n",
"documents = result['documents']\n",
"categories = [metadata['category'] for metadata in result['metadatas']]\n",
"colors = [COLORS[CATEGORIES.index(c)] for c in categories]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c54df150-c8d8-4bc3-8877-6759691eeb42",
"metadata": {},
"outputs": [],
"source": [
"# Let's try a 2D chart\n",
"\n",
"tsne = TSNE(n_components=2, random_state=42, n_jobs=-1)\n",
"reduced_vectors = tsne.fit_transform(vectors)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8fb2a63-24c5-4dce-9e63-aa208272f82d",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# Create the 2D scatter plot\n",
"fig = go.Figure(data=[go.Scatter(\n",
" x=reduced_vectors[:, 0],\n",
" y=reduced_vectors[:, 1],\n",
" mode='markers',\n",
" marker=dict(size=2, color=colors, opacity=0.7),\n",
")])\n",
"\n",
"fig.update_layout(\n",
" title='2D Chroma Vectorstore Visualization',\n",
" scene=dict(xaxis_title='x', yaxis_title='y'),\n",
" width=1200,\n",
" height=800,\n",
" margin=dict(r=20, b=10, l=10, t=40)\n",
")\n",
"\n",
"fig.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

174
week8/day2.2.ipynb

@ -0,0 +1,174 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "4e60bd8a-a4da-4db9-86a8-ac8c03f3e367",
"metadata": {},
"source": [
"# The Price is Right\n",
"\n",
"Today we build a more complex solution for estimating prices of goods.\n",
"\n",
"1. Day 2.0 notebook: create a RAG database with our 400,000 training data\n",
"2. Day 2.1 notebook: visualize in 2D\n",
"3. Day 2.2 notebook: visualize in 3D\n",
"4. Day 2.3 notebook: build and test a RAG pipeline with GPT-4o-mini\n",
"5. Day 2.4 notebook: (a) bring back our Random Forest pricer (b) Create a Ensemble pricer that allows contributions from all the pricers\n",
"\n",
"Phew! That's a lot to get through in one day!\n",
"\n",
"## PLEASE NOTE:\n",
"\n",
"We already have a very powerful product estimator with our proprietary, fine-tuned LLM. Most people would be very satisfied with that! The main reason we're adding these extra steps is to deepen your expertise with RAG and with Agentic workflows."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "993a2a24-1a58-42be-8034-6d116fb8d786",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import re\n",
"import math\n",
"import json\n",
"from tqdm import tqdm\n",
"import random\n",
"from dotenv import load_dotenv\n",
"from huggingface_hub import login\n",
"import numpy as np\n",
"import pickle\n",
"from sentence_transformers import SentenceTransformer\n",
"from datasets import load_dataset\n",
"import chromadb\n",
"from items import Item\n",
"from sklearn.manifold import TSNE\n",
"import plotly.graph_objects as go"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1cc1fe53-612f-4228-aa02-8758f4c2098f",
"metadata": {},
"outputs": [],
"source": [
"# Turn this up at your own risk! 10_000 is safe..\n",
"\n",
"MAXIMUM_DATAPOINTS = 10_000"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4aab95e-d719-4476-b6e7-e248120df25a",
"metadata": {},
"outputs": [],
"source": [
"DB = \"products_vectorstore\"\n",
"client = chromadb.PersistentClient(path=DB)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f95dafd-ab80-464e-ba8a-dec7a2424780",
"metadata": {},
"outputs": [],
"source": [
"collection = client.get_or_create_collection('products')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "525fc313-8a16-4ac0-8c42-6a6d1ba1c9b8",
"metadata": {},
"outputs": [],
"source": [
"CATEGORIES = ['Appliances', 'Automotive', 'Cell_Phones_and_Accessories', 'Electronics','Musical_Instruments', 'Office_Products', 'Tools_and_Home_Improvement', 'Toys_and_Games']\n",
"COLORS = ['red', 'blue', 'brown', 'orange', 'yellow', 'green' , 'purple', 'cyan']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a4cf1c9a-1ced-48d4-974c-3c850905034e",
"metadata": {},
"outputs": [],
"source": [
"# Prework\n",
"result = collection.get(include=['embeddings', 'documents', 'metadatas'], limit=MAXIMUM_DATAPOINTS)\n",
"vectors = np.array(result['embeddings'])\n",
"documents = result['documents']\n",
"categories = [metadata['category'] for metadata in result['metadatas']]\n",
"colors = [COLORS[CATEGORIES.index(c)] for c in categories]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c54df150-c8d8-4bc3-8877-6759691eeb42",
"metadata": {},
"outputs": [],
"source": [
"# Let's try 3D!\n",
"\n",
"tsne = TSNE(n_components=3, random_state=42, n_jobs=-1)\n",
"reduced_vectors = tsne.fit_transform(vectors)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8fb2a63-24c5-4dce-9e63-aa208272f82d",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# Create the 3D scatter plot\n",
"fig = go.Figure(data=[go.Scatter3d(\n",
" x=reduced_vectors[:, 0],\n",
" y=reduced_vectors[:, 1],\n",
" z=reduced_vectors[:, 2],\n",
" mode='markers',\n",
" marker=dict(size=3, color=colors, opacity=0.7),\n",
")])\n",
"\n",
"fig.update_layout(\n",
" title='3D Chroma Vector Store Visualization',\n",
" scene=dict(xaxis_title='x', yaxis_title='y', zaxis_title='z'),\n",
" width=1200,\n",
" height=800,\n",
" margin=dict(r=20, b=10, l=10, t=40)\n",
")\n",
"\n",
"fig.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

345
week8_wip/backup_day2.4.ipynb → week8/day2.3.ipynb

@ -1,8 +1,32 @@
{ {
"cells": [ "cells": [
{
"cell_type": "markdown",
"id": "2a0f44a9-37cd-4aa5-9b20-cfc0dc8dfc0a",
"metadata": {},
"source": [
"# The Price is Right\n",
"\n",
"Today we build a more complex solution for estimating prices of goods.\n",
"\n",
"1. Day 2.0 notebook: create a RAG database with our 400,000 training data\n",
"2. Day 2.1 notebook: visualize in 2D\n",
"3. Day 2.2 notebook: visualize in 3D\n",
"4. Day 2.3 notebook: build and test a RAG pipeline with GPT-4o-mini\n",
"5. Day 2.4 notebook: (a) bring back our Random Forest pricer (b) Create a Ensemble pricer that allows contributions from all the pricers\n",
"\n",
"Phew! That's a lot to get through in one day!\n",
"\n",
"## PLEASE NOTE:\n",
"\n",
"We already have a very powerful product estimator with our proprietary, fine-tuned LLM. Most people would be very satisfied with that! The main reason we're adding these extra steps is to deepen your expertise with RAG and with Agentic workflows.\n",
"\n",
"## We will go fast today! Hold on to your hat.."
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": null,
"id": "fbcdfea8-7241-46d7-a771-c0381a3e7063", "id": "fbcdfea8-7241-46d7-a771-c0381a3e7063",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -17,6 +41,7 @@
"import random\n", "import random\n",
"from dotenv import load_dotenv\n", "from dotenv import load_dotenv\n",
"from huggingface_hub import login\n", "from huggingface_hub import login\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n", "import numpy as np\n",
"import pickle\n", "import pickle\n",
"from openai import OpenAI\n", "from openai import OpenAI\n",
@ -24,30 +49,12 @@
"from datasets import load_dataset\n", "from datasets import load_dataset\n",
"import chromadb\n", "import chromadb\n",
"from items import Item\n", "from items import Item\n",
"from testing import Tester\n", "from testing import Tester"
"from agents.pricer_agent import price\n",
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.metrics import mean_squared_error, r2_score"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e6e88bd1-f89c-4b98-92fa-aa4bc1575bca",
"metadata": {},
"outputs": [],
"source": [
"# CONSTANTS\n",
"\n",
"QUESTION = \"How much does this cost to the nearest dollar?\\n\\n\"\n",
"DB = \"products_vectorstore\""
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": null,
"id": "98666e73-938e-469d-8987-e6e55ba5e034", "id": "98666e73-938e-469d-8987-e6e55ba5e034",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -61,7 +68,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": null,
"id": "9a25a5cf-8f6c-4b5d-ad98-fdd096f5adf8", "id": "9a25a5cf-8f6c-4b5d-ad98-fdd096f5adf8",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -71,7 +78,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": null,
"id": "dc696493-0b6f-48aa-9fa8-b1ae0ecaf3cd", "id": "dc696493-0b6f-48aa-9fa8-b1ae0ecaf3cd",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -84,7 +91,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": null,
"id": "33d38a06-0c0d-4e96-94d1-35ee183416ce", "id": "33d38a06-0c0d-4e96-94d1-35ee183416ce",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -121,7 +128,9 @@
"id": "b26f405d-6e1f-4caa-b97f-1f62cd9d1ebc", "id": "b26f405d-6e1f-4caa-b97f-1f62cd9d1ebc",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": [
"DB = \"products_vectorstore\""
]
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -146,6 +155,16 @@
" return text.split(\"\\n\\nPrice is $\")[0]" " return text.split(\"\\n\\nPrice is $\")[0]"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"id": "a1bd0c87-8bad-43d9-9461-bb69a9e0e22c",
"metadata": {},
"outputs": [],
"source": [
"description(test[0])"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -184,359 +203,171 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "d11f1c8d-7480-4d64-a274-b030d701f1b8", "id": "6f7b9ff9-fd90-4627-bb17-7c2f7bbd21f3",
"metadata": {},
"outputs": [],
"source": [
"def get_price(s):\n",
" s = s.replace('$','').replace(',','')\n",
" match = re.search(r\"[-+]?\\d*\\.\\d+|\\d+\", s)\n",
" return float(match.group()) if match else 0"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a919cf7d-b3d3-4968-8c96-54a0da0b0219",
"metadata": {},
"outputs": [],
"source": [
"# The function for gpt-4o-mini\n",
"\n",
"def gpt_4o_mini_rag(item):\n",
" documents, prices = find_similars(item)\n",
" response = openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\", \n",
" messages=messages_for(item, documents, prices),\n",
" seed=42,\n",
" max_tokens=5\n",
" )\n",
" reply = response.choices[0].message.content\n",
" return get_price(reply)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b918cfc-76c1-442a-8caa-bec500cd504b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"gpt_4o_mini_rag(test[1000])" "test[1].prompt"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "c92cfc0b-b36d-456f-94cc-fe3f315cc25e", "id": "ff1b2659-cc6b-47aa-a797-dd1cd3d1d6c3",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"test[1000]" "documents, prices = find_similars(test[1])"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "e6d5deb3-6a2a-4484-872c-37176c5e1f07", "id": "24756d4d-edac-41ce-bb80-c3b6f1cea7ee",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def proprietary(item):\n", "print(make_context(documents, prices))"
" text = item.prompt.split(\"to the nearest dollar?\\n\\n\")[1].split(\"\\n\\nPrice is $\")[0]\n",
" return price(text)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "bacdf607-37b9-4997-adb1-d63abfb645b1", "id": "0b81eca2-0b58-4fe8-9dd6-47f13ba5f8ee",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"print(proprietary(test[1]))\n", "print(messages_for(test[1], documents, prices))"
"print(gpt_4o_mini_rag(test[1]))"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "b35532e7-098a-4ab9-a8f7-8f101b437181", "id": "d11f1c8d-7480-4d64-a274-b030d701f1b8",
"metadata": {},
"outputs": [],
"source": [
"truths = []\n",
"proprietaries = []\n",
"rags = []\n",
"for i in tqdm(range(1000,1250)):\n",
" item = test[i]\n",
" truths.append(item.price)\n",
" proprietaries.append(proprietary(item))\n",
" rags.append(gpt_4o_mini_rag(item))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6ae54c7-6e8e-4333-b075-b59978fed560",
"metadata": {},
"outputs": [],
"source": [
"mins = [min(p,r) for p,r in zip(proprietaries, rags)]\n",
"maxes = [max(p,r) for p,r in zip(proprietaries, rags)]\n",
"\n",
"X = pd.DataFrame({\n",
" 'Proprietary': proprietaries,\n",
" 'RAG': rags,\n",
" 'Min': mins,\n",
" 'Max': maxes,\n",
"})\n",
"\n",
"# Convert y to a Series\n",
"y = pd.Series(truths)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e68684ed-d029-4d95-bb13-eead19b20e49",
"metadata": {},
"outputs": [],
"source": [
"# Train a Linear Regression\n",
"np.random.seed(42)\n",
"\n",
"lr = LinearRegression()\n",
"lr.fit(X, y)\n",
"\n",
"feature_columns = [\"Proprietary\", \"RAG\", \"Min\", \"Max\"]\n",
"\n",
"for feature, coef in zip(feature_columns, lr.coef_):\n",
" print(f\"{feature}: {coef:.2f}\")\n",
"print(f\"Intercept={lr.intercept_:.2f}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "28530362-97b8-42a0-bf89-967539b6f170",
"metadata": {},
"outputs": [],
"source": [
"def ensemble(item):\n",
" prop = proprietary(item)\n",
" rag = gpt_4o_mini_rag(item)\n",
" Xt = pd.DataFrame({\n",
" 'Proprietary': [prop],\n",
" 'RAG': [rag],\n",
" 'Min': [min(prop,rag)],\n",
" 'Max': [max(prop,rag)],\n",
" })\n",
" yt = lr.predict(Xt)\n",
" return yt[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "08021c05-340b-4ee2-9d11-4b280766976f",
"metadata": {},
"outputs": [],
"source": [
"ensemble(test[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8308c74-546f-4fc0-ada4-1974addacfd1",
"metadata": {},
"outputs": [],
"source": [
"test[0].price"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80792910-c59f-4d96-aa53-683464a8e60c",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"Tester.test(ensemble, test)" "def get_price(s):\n",
" s = s.replace('$','').replace(',','')\n",
" match = re.search(r\"[-+]?\\d*\\.\\d+|\\d+\", s)\n",
" return float(match.group()) if match else 0"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "d0c41043-2049-4883-947f-2aad2f6954c2", "id": "a919cf7d-b3d3-4968-8c96-54a0da0b0219",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from sklearn.ensemble import RandomForestRegressor\n", "# The function for gpt-4o-mini\n",
"\n", "\n",
"result = collection.get(include=['embeddings', 'documents', 'metadatas'])\n", "def gpt_4o_mini_rag(item):\n",
"vectors = np.array(result['embeddings'])\n", " documents, prices = find_similars(item)\n",
"documents = result['documents']\n", " response = openai.chat.completions.create(\n",
"prices = [metadata['price'] for metadata in result['metadatas']]" " model=\"gpt-4o-mini\", \n",
] " messages=messages_for(item, documents, prices),\n",
}, " seed=42,\n",
{ " max_tokens=5\n",
"cell_type": "code", " )\n",
"execution_count": null, " reply = response.choices[0].message.content\n",
"id": "e9c3276f-ae01-478d-bb27-dc73b567b41a", " return get_price(reply)"
"metadata": {},
"outputs": [],
"source": [
"rf_model = RandomForestRegressor(n_estimators=100, random_state=42, n_jobs=8)\n",
"rf_model.fit(vectors, prices)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "3e8f70cd-4147-40c6-9861-a3513b7e5499", "id": "3e519e26-ff15-4425-90bb-bfbf55deb39b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def new_rf(item):\n", "gpt_4o_mini_rag(test[1])"
" text = item.prompt.split(\"to the nearest dollar?\\n\\n\")[1].split(\"\\n\\nPrice is $\")[0]\n",
" vector = model.encode([text])\n",
" return max(0, rf_model.predict(vector)[0])"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "a2e3340f-7ed4-47eb-a5a9-dff4c0353f58", "id": "ce78741b-2966-41d2-9831-cbf8f8d176be",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"new_rf(test[0])" "test[1].price"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "f91c903b-8db1-4374-807e-3a8ce282ef30", "id": "16d90455-ff7d-4f5f-8b8c-8e061263d1c7",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"Tester.test(new_rf, test)" "Tester.test(gpt_4o_mini_rag, test)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "3c8e23c5-1ed3-4bd1-a3c0-129d4712c93a", "id": "e6d5deb3-6a2a-4484-872c-37176c5e1f07",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"forests = []\n", "from agents.frontier_agent import FrontierAgent"
"for i in tqdm(range(1000,1250)):\n",
" item = test[i]\n",
" forests.append(new_rf(item))"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "8e2eca63-8230-4904-9a79-7e779747479e", "id": "56e8dd5d-ed36-49d8-95f7-dc82e548255b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"truths2 = []\n", "agent = FrontierAgent(collection)"
"proprietaries2 = []\n",
"rags2 = []\n",
"forests2 = []\n",
"for i in tqdm(range(1000,2000)):\n",
" item = test[i]\n",
" truths2.append(item.price)\n",
" proprietaries2.append(proprietary(item))\n",
" rags2.append(gpt_4o_mini_rag(item))\n",
" forests2.append(new_rf(item))"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "0a3e057f-05c5-4f8f-8b3b-0afdfccc1412", "id": "980dd126-f675-4499-8817-0cc0bb73e247",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"mins2 = [min(p,r,f) for p,r,f in zip(proprietaries2, rags2, forests2)]\n", "agent.price(\"Quadcast HyperX condenser mic for high quality podcasting\")"
"maxes2 = [max(p,r,f) for p,r,f in zip(proprietaries2, rags2, forests2)]\n",
"\n",
"\n",
"\n",
"X2 = pd.DataFrame({\n",
" 'Proprietary': proprietaries2,\n",
" 'RAG': rags2,\n",
" 'Forest': forests2,\n",
" 'Min': mins2,\n",
" 'Max': maxes2,\n",
"})\n",
"\n",
"# Convert y to a Series\n",
"y2 = pd.Series(truths2)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "1ae62175-b955-428e-b077-705c49ee71bd", "id": "66c18a06-d0f1-4ec9-8aff-ec3ca294dd09",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# Train a Linear Regression\n", "from agents.specialist_agent import SpecialistAgent"
"np.random.seed(42)\n",
"\n",
"lr2 = LinearRegression()\n",
"lr2.fit(X2, y2)\n",
"\n",
"feature_columns = X2.columns.tolist()\n",
"\n",
"for feature, coef in zip(feature_columns, lr2.coef_):\n",
" print(f\"{feature}: {coef:.2f}\")\n",
"print(f\"Intercept={lr.intercept_:.2f}\")"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "214a3831-c464-4218-a349-534b6bda7f12", "id": "ba672fb4-2c3e-42ee-9ea0-21bfcfc5260c",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def ensemble2(item):\n", "agent2 = SpecialistAgent()"
" prop = proprietary(item)\n",
" rag = gpt_4o_mini_rag(item)\n",
" r_f = new_rf(item)\n",
" Xt2 = pd.DataFrame({\n",
" 'Proprietary': [prop],\n",
" 'RAG': [rag],\n",
" 'Forest': [r_f],\n",
" 'Min': [min(prop,rag, r_f)],\n",
" 'Max': [max(prop,rag, r_f)],\n",
" })\n",
" yt2 = lr.predict(Xt2)\n",
" return yt2[0]"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "b234cb68-af68-4475-ae18-8892aac6b74e", "id": "a5a97004-95b4-46ea-b12d-a4ead22fcb2a",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"Tester.test(ensemble2, test)" "agent2.price(\"Quadcast HyperX condenser mic for high quality podcasting\")"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "10a7275f-1aa9-4446-9100-a7a0ba0215f2", "id": "26d5ddc6-baa6-4760-a430-05671847ac47",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": []

408
week8/day2.4.ipynb

@ -0,0 +1,408 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "40d49349-faaa-420c-9b65-0bdc9edfabce",
"metadata": {},
"source": [
"# The Price is Right\n",
"\n",
"Today we build a more complex solution for estimating prices of goods.\n",
"\n",
"1. Day 2.0 notebook: create a RAG database with our 400,000 training data\n",
"2. Day 2.1 notebook: visualize in 2D\n",
"3. Day 2.2 notebook: visualize in 3D\n",
"4. Day 2.3 notebook: build and test a RAG pipeline with GPT-4o-mini\n",
"5. Day 2.4 notebook: (a) bring back our Random Forest pricer (b) Create a Ensemble pricer that allows contributions from all the pricers\n",
"\n",
"Phew! That's a lot to get through in one day!\n",
"\n",
"## PLEASE NOTE:\n",
"\n",
"We already have a very powerful product estimator with our proprietary, fine-tuned LLM. Most people would be very satisfied with that! The main reason we're adding these extra steps is to deepen your expertise with RAG and with Agentic workflows.\n",
"\n",
"## Finishing off with Random Forests & Ensemble"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fbcdfea8-7241-46d7-a771-c0381a3e7063",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import re\n",
"import math\n",
"import json\n",
"from tqdm import tqdm\n",
"import random\n",
"from dotenv import load_dotenv\n",
"from huggingface_hub import login\n",
"import numpy as np\n",
"import pickle\n",
"from openai import OpenAI\n",
"from sentence_transformers import SentenceTransformer\n",
"from datasets import load_dataset\n",
"import chromadb\n",
"from items import Item\n",
"from testing import Tester\n",
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.ensemble import RandomForestRegressor\n",
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.metrics import mean_squared_error, r2_score\n",
"import joblib\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6e88bd1-f89c-4b98-92fa-aa4bc1575bca",
"metadata": {},
"outputs": [],
"source": [
"# CONSTANTS\n",
"\n",
"QUESTION = \"How much does this cost to the nearest dollar?\\n\\n\"\n",
"DB = \"products_vectorstore\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "98666e73-938e-469d-8987-e6e55ba5e034",
"metadata": {},
"outputs": [],
"source": [
"# environment\n",
"\n",
"load_dotenv()\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dc696493-0b6f-48aa-9fa8-b1ae0ecaf3cd",
"metadata": {},
"outputs": [],
"source": [
"# Load in the test pickle file:\n",
"\n",
"with open('test.pkl', 'rb') as file:\n",
" test = pickle.load(file)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d26a1104-cd11-4361-ab25-85fb576e0582",
"metadata": {},
"outputs": [],
"source": [
"client = chromadb.PersistentClient(path=DB)\n",
"collection = client.get_or_create_collection('products')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e00b82a9-a8dc-46f1-8ea9-2f07cbc8e60d",
"metadata": {},
"outputs": [],
"source": [
"result = collection.get(include=['embeddings', 'documents', 'metadatas'])\n",
"vectors = np.array(result['embeddings'])\n",
"documents = result['documents']\n",
"prices = [metadata['price'] for metadata in result['metadatas']]"
]
},
{
"cell_type": "markdown",
"id": "bf6492cb-b11a-4ad5-859b-a71a78ffb949",
"metadata": {},
"source": [
"# Random Forest\n",
"\n",
"We will now train a Random Forest model.\n",
"\n",
"Can you spot the difference from what we did in Week 6? In week 6 we used the word2vec model to form vectors; this time we'll use the vectors we already have in Chroma, from the SentenceTransformer model."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "48894777-101f-4fe5-998c-47079407f340",
"metadata": {},
"outputs": [],
"source": [
"# This next line takes an hour on my M1 Mac!\n",
"\n",
"rf_model = RandomForestRegressor(n_estimators=100, random_state=42, n_jobs=-1)\n",
"rf_model.fit(vectors, prices)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "62eb7ddf-e1da-481e-84c6-1256547566bd",
"metadata": {},
"outputs": [],
"source": [
"# Save the model to a file\n",
"\n",
"joblib.dump(rf_model, 'random_forest_model.pkl')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d281dc5e-761e-4a5e-86b3-29d9c0a33d4a",
"metadata": {},
"outputs": [],
"source": [
"# Load it back in again\n",
"\n",
"rf_model = joblib.load('random_forest_model.pkl')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5d438dec-8e5b-4e60-bb6f-c3f82e522dd9",
"metadata": {},
"outputs": [],
"source": [
"from agents.specialist_agent import SpecialistAgent\n",
"from agents.frontier_agent import FrontierAgent\n",
"from agents.random_forest_agent import RandomForestAgent"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afc39369-b97b-4a90-b17e-b20ef501d3c9",
"metadata": {},
"outputs": [],
"source": [
"specialist = SpecialistAgent()\n",
"frontier = FrontierAgent(collection)\n",
"random_forest = RandomForestAgent()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8e2d0d0a-8bb8-4b39-b046-322828c39244",
"metadata": {},
"outputs": [],
"source": [
"def description(item):\n",
" return item.prompt.split(\"to the nearest dollar?\\n\\n\")[1].split(\"\\n\\nPrice is $\")[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bfe0434f-b29e-4cc0-bad9-b07624665727",
"metadata": {},
"outputs": [],
"source": [
"def rf(item):\n",
" return random_forest.price(description(item))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cdf233ec-264f-4b34-9f2b-27c39692137b",
"metadata": {},
"outputs": [],
"source": [
"Tester.test(rf, test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9f759bd2-7a7e-4c1a-80a0-e12470feca89",
"metadata": {},
"outputs": [],
"source": [
"product = \"Quadcast HyperX condenser mic for high quality audio for podcasting\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e44dbd25-fb95-4b6b-bbbb-8da5fc817105",
"metadata": {},
"outputs": [],
"source": [
"print(specialist.price(product))\n",
"print(frontier.price(product))\n",
"print(random_forest.price(product))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1779b353-e2bb-4fc7-be7c-93057e4d688a",
"metadata": {},
"outputs": [],
"source": [
"specialists = []\n",
"frontiers = []\n",
"random_forests = []\n",
"prices = []\n",
"for item in tqdm(test[1000:1250]):\n",
" text = description(item)\n",
" specialists.append(specialist.price(text))\n",
" frontiers.append(frontier.price(text))\n",
" random_forests.append(random_forest.price(text))\n",
" prices.append(item.price)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0bca725-4e34-405b-8d90-41d67086a25d",
"metadata": {},
"outputs": [],
"source": [
"mins = [min(s,f,r) for s,f,r in zip(specialists, frontiers, random_forests)]\n",
"maxes = [max(s,f,r) for s,f,r in zip(specialists, frontiers, random_forests)]\n",
"\n",
"X = pd.DataFrame({\n",
" 'Specialist': specialists,\n",
" 'Frontier': frontiers,\n",
" 'RandomForest': random_forests,\n",
" 'Min': mins,\n",
" 'Max': maxes,\n",
"})\n",
"\n",
"# Convert y to a Series\n",
"y = pd.Series(prices)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1be5be8a-3e7f-42a2-be54-0c7e380f7cc4",
"metadata": {},
"outputs": [],
"source": [
"# Train a Linear Regression\n",
"np.random.seed(42)\n",
"\n",
"lr = LinearRegression()\n",
"lr.fit(X, y)\n",
"\n",
"feature_columns = X.columns.tolist()\n",
"\n",
"for feature, coef in zip(feature_columns, lr.coef_):\n",
" print(f\"{feature}: {coef:.2f}\")\n",
"print(f\"Intercept={lr.intercept_:.2f}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0bdf6e68-28a3-4ed2-b17e-de0ede923d34",
"metadata": {},
"outputs": [],
"source": [
"joblib.dump(lr, 'ensemble_model.pkl')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e762441a-9470-4dd7-8a8f-ec0430e908c7",
"metadata": {},
"outputs": [],
"source": [
"from agents.ensemble_agent import EnsembleAgent\n",
"ensemble = EnsembleAgent(collection)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1a29f03c-8010-43b7-ae7d-1bc85ca6e8e2",
"metadata": {},
"outputs": [],
"source": [
"ensemble.price(product)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6a5e226-a508-43d5-aa42-cefbde72ffdf",
"metadata": {},
"outputs": [],
"source": [
"def ensemble_pricer(item):\n",
" return ensemble.price(description(item))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8397b1ef-2ea3-4af8-bb34-36594e0600cc",
"metadata": {},
"outputs": [],
"source": [
"Tester.test(ensemble_pricer, test)"
]
},
{
"cell_type": "markdown",
"id": "347c5350-d4b5-42ae-96f6-ec94f6ab41d7",
"metadata": {},
"source": [
"# WHAT A DAY!\n",
"\n",
"We got so much done - a Fronter RAG pipeline, a Random Forest model using transformer-based encodings, and an Ensemble model.\n",
"\n",
"You can do better, for sure!\n",
"\n",
"Tweak this, and try adding components into the ensemble, to beat my performance."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85009065-851e-44a2-b39f-4c116f7fbd22",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

47
week8_wip/day3.ipynb → week8/day3.ipynb

@ -1,5 +1,15 @@
{ {
"cells": [ "cells": [
{
"cell_type": "markdown",
"id": "0df0d850-49eb-4a0b-a27a-146969db710d",
"metadata": {},
"source": [
"# The Price is Right\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", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -13,7 +23,7 @@
"import json\n", "import json\n",
"from dotenv import load_dotenv\n", "from dotenv import load_dotenv\n",
"from openai import OpenAI\n", "from openai import OpenAI\n",
"from agents.deals import Deal, QualityDealSelection" "from agents.deals import ScrapedDeal, DealSelection"
] ]
}, },
{ {
@ -38,7 +48,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"deals = Deal.fetch()" "deals = ScrapedDeal.fetch(show_progress=True)"
] ]
}, },
{ {
@ -71,8 +81,9 @@
"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", "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", "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", "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",
"{\"quality_deals\": [\n", "{\"deals\": [\n",
" {\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", " \"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", " \"price\": 99.99,\n",
@ -92,6 +103,7 @@
"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", "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", "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", "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", "\n",
"Deals:\n", "Deals:\n",
"\n", "\n",
@ -123,7 +135,7 @@
" {\"role\": \"system\", \"content\": system_prompt},\n", " {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt}\n", " {\"role\": \"user\", \"content\": user_prompt}\n",
" ],\n", " ],\n",
" response_format=QualityDealSelection\n", " response_format=DealSelection\n",
" )\n", " )\n",
" result = completion.choices[0].message.parsed\n", " result = completion.choices[0].message.parsed\n",
" return result" " return result"
@ -139,6 +151,16 @@
"result = get_recommendations()" "result = get_recommendations()"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"id": "84e62845-3338-441a-8161-c70097af4773",
"metadata": {},
"outputs": [],
"source": [
"len(result.deals)"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -146,7 +168,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"result.quality_deals[0]" "result.deals[1]"
] ]
}, },
{ {
@ -156,7 +178,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from agents.scanner_agent import scan" "from agents.scanner_agent import ScannerAgent"
] ]
}, },
{ {
@ -166,7 +188,8 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"scan()" "agent = ScannerAgent()\n",
"result = agent.scan()"
] ]
}, },
{ {
@ -175,6 +198,16 @@
"id": "2e1d013a-c930-4dad-901b-41433379e14b", "id": "2e1d013a-c930-4dad-901b-41433379e14b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [
"result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ee2e837-1f1d-42d4-8bc4-51cccc343006",
"metadata": {},
"outputs": [],
"source": [] "source": []
} }
], ],

74
week8_wip/day4.ipynb → week8/day4.ipynb

@ -1,88 +1,78 @@
{ {
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": null, "id": "23f53670-1a73-46ba-a754-4a497e8e0e64",
"id": "80d683d9-9e92-44ae-af87-a413ca84db21",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from twilio.rest import Client\n",
"from dotenv import load_dotenv"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ba769cc-5301-4810-b01f-cab584cfb3b3",
"metadata": {}, "metadata": {},
"outputs": [],
"source": [ "source": [
"load_dotenv()\n", "# The Price is Right\n",
"os.environ['TWILIO_ACCOUNT_SID'] = os.getenv('TWILIO_ACCOUNT_SID', 'your-sid-if-not-using-env')\n", "\n",
"os.environ['TWILIO_AUTH_TOKEN'] = os.getenv('TWILIO_AUTH_TOKEN', 'your-auth-if-not-using-env')\n", "First we'll polish off 2 more simple agents:\n",
"os.environ['MY_PHONE_NUMBER'] = os.getenv('MY_PHONE_NUMBER', 'your-phone-if-not-using-env')" "\n",
"The **Messaging Agent** to send push notifications\n",
"\n",
"The **Planning Agent** to coordinate activities\n",
"\n",
"Then we'll put it all together into an Agent Framework.\n",
"\n",
"For the Push Notification, we will be using a nifty platform called Pushover. \n",
"You'll need to set up a free account and add 2 tokens to your `.env` file:\n",
"\n",
"```\n",
"PUSHOVER_USER=xxx\n",
"PUSHOVER_TOKEN=xxx\n",
"```"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "761e6460-d201-4f69-ba31-a641a059e47d", "id": "80d683d9-9e92-44ae-af87-a413ca84db21",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"ME_FROM = 'whatsapp:+14155238886'\n", "from dotenv import load_dotenv\n",
"ME_TO = f\"whatsapp:+1{os.environ['MY_PHONE_NUMBER']}\"" "from agents.messaging_agent import MessagingAgent"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "f77f8b08-6c92-47e2-9dd0-3ddaf01beb07", "id": "5ba769cc-5301-4810-b01f-cab584cfb3b3",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"account_sid = os.environ['TWILIO_ACCOUNT_SID']\n", "load_dotenv()\n",
"auth_token = os.environ['TWILIO_AUTH_TOKEN']\n", "DB = \"products_vectorstore\""
"client = Client(account_sid, auth_token)\n",
"\n",
"message = client.messages.create(\n",
" from_=ME_FROM,\n",
" body='hello, me!',\n",
" to=ME_TO\n",
")"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "6794a7de-352f-46d2-8451-ff79c9654b31", "id": "e05cc427-3d2c-4792-ade1-d356f95a82a9",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from agents.messaging_agent import MessagingAgent" "agent = MessagingAgent()"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "e05cc427-3d2c-4792-ade1-d356f95a82a9", "id": "5ec518f5-dae4-44b1-a185-d7eaf853ec00",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"agent = MessagingAgent()" "agent.push(\"MASSIVE NEWS!!!\")"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "5ec518f5-dae4-44b1-a185-d7eaf853ec00", "id": "0056a02f-06a3-4acc-99f3-cbe919ee936b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": []
"agent.message(\"Hi!!\")"
]
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -121,7 +111,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "70200a3c-64fb-4c34-bdd8-57aaf009ec60", "id": "8dd94a70-3202-452b-9ef0-551d6feb159b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": []

163
week8/day5.ipynb

@ -0,0 +1,163 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "a71ed017-e1b0-4299-88b3-f0eb05adc4df",
"metadata": {},
"source": [
"# The Price is Right\n",
"\n",
"The final step is to build a User Interface\n",
"\n",
"We will use more advanced aspects of Gradio - building piece by piece."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "614c6202-4575-448d-98ee-78b735775d2b",
"metadata": {},
"outputs": [],
"source": [
"import gradio as gr\n",
"from deal_agent_framework import DealAgentFramework\n",
"from agents.deals import Opportunity, Deal"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0534e714-5a9c-45c6-998c-3472ac0bb8b5",
"metadata": {},
"outputs": [],
"source": [
"with gr.Blocks(title=\"The Price is Right\", fill_width=True) as ui:\n",
"\n",
" with gr.Row():\n",
" gr.Markdown('<div style=\"text-align: center;font-size:24px\">The Price is Right - Deal Hunting Agentic AI</div>')\n",
" with gr.Row():\n",
" gr.Markdown('<div style=\"text-align: center;font-size:14px\">Autonomous agent framework that finds online deals, collaborating with a proprietary fine-tuned LLM deployed on Modal, and a RAG pipeline with a frontier model and Chroma.</div>')\n",
" \n",
"\n",
"ui.launch(inbrowser=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18c12c10-750c-4da3-8df5-f2bc3393f9e0",
"metadata": {},
"outputs": [],
"source": [
"with gr.Blocks(title=\"The Price is Right\", fill_width=True) as ui:\n",
"\n",
" initial_deal = Deal(product_description=\"Example description\", price=100.0, url=\"https://cnn.com\")\n",
" initial_opportunity = Opportunity(deal=initial_deal, estimate=200.0, discount=100.0)\n",
" opportunities = gr.State([initial_opportunity])\n",
"\n",
" def get_table(opps):\n",
" return [[opp.deal.product_description, opp.deal.price, opp.estimate, opp.discount, opp.deal.url] for opp in opps]\n",
"\n",
" with gr.Row():\n",
" gr.Markdown('<div style=\"text-align: center;font-size:24px\">\"The Price is Right\" - Deal Hunting Agentic AI</div>')\n",
" with gr.Row():\n",
" gr.Markdown('<div style=\"text-align: center;font-size:14px\">Deals surfaced so far:</div>')\n",
" with gr.Row():\n",
" opportunities_dataframe = gr.Dataframe(\n",
" headers=[\"Description\", \"Price\", \"Estimate\", \"Discount\", \"URL\"],\n",
" wrap=True,\n",
" column_widths=[4, 1, 1, 1, 2],\n",
" row_count=10,\n",
" col_count=5,\n",
" height=400,\n",
" )\n",
"\n",
" ui.load(get_table, inputs=[opportunities], outputs=[opportunities_dataframe])\n",
"\n",
"ui.launch(inbrowser=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "87106328-a17a-447e-90b9-c547613468da",
"metadata": {},
"outputs": [],
"source": [
"agent_framework = DealAgentFramework()\n",
"\n",
"with gr.Blocks(title=\"The Price is Right\", fill_width=True) as ui:\n",
"\n",
" initial_deal = Deal(product_description=\"Example description\", price=100.0, url=\"https://cnn.com\")\n",
" initial_opportunity = Opportunity(deal=initial_deal, estimate=200.0, discount=100.0)\n",
" opportunities = gr.State([initial_opportunity])\n",
"\n",
" def get_table(opps):\n",
" return [[opp.deal.product_description, opp.deal.price, opp.estimate, opp.discount, opp.deal.url] for opp in opps]\n",
"\n",
" def do_select(opportunities, selected_index: gr.SelectData):\n",
" row = selected_index.index[0]\n",
" opportunity = opportunities[row]\n",
" agent_framework.planner.messenger.alert(opportunity)\n",
"\n",
" with gr.Row():\n",
" gr.Markdown('<div style=\"text-align: center;font-size:24px\">\"The Price is Right\" - Deal Hunting Agentic AI</div>')\n",
" with gr.Row():\n",
" gr.Markdown('<div style=\"text-align: center;font-size:14px\">Deals surfaced so far:</div>')\n",
" with gr.Row():\n",
" opportunities_dataframe = gr.Dataframe(\n",
" headers=[\"Description\", \"Price\", \"Estimate\", \"Discount\", \"URL\"],\n",
" wrap=True,\n",
" column_widths=[4, 1, 1, 1, 2],\n",
" row_count=10,\n",
" col_count=5,\n",
" height=400,\n",
" )\n",
"\n",
" ui.load(get_table, inputs=[opportunities], outputs=[opportunities_dataframe])\n",
" opportunities_dataframe.select(do_select, inputs=[opportunities], outputs=[])\n",
"\n",
"ui.launch(inbrowser=True)"
]
},
{
"cell_type": "markdown",
"id": "ecfed67b-ebcb-4e17-ad15-a7151f940119",
"metadata": {},
"source": [
"# Time for the code\n",
"\n",
"And now we'll move to the price_is_right.py code, followed by price_is_right_final.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "48506465-1c7a-433f-a665-b277a8b4665c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

94
week8/deal_agent_framework.py

@ -0,0 +1,94 @@
import os
import sys
import logging
import json
from typing import List, Optional
from twilio.rest import Client
from dotenv import load_dotenv
import chromadb
from agents.planning_agent import PlanningAgent
from agents.deals import Opportunity
from sklearn.manifold import TSNE
import numpy as np
# Colors for logging
BG_BLUE = '\033[44m'
WHITE = '\033[37m'
RESET = '\033[0m'
# Colors for plot
CATEGORIES = ['Appliances', 'Automotive', 'Cell_Phones_and_Accessories', 'Electronics','Musical_Instruments', 'Office_Products', 'Tools_and_Home_Improvement', 'Toys_and_Games']
COLORS = ['red', 'blue', 'brown', 'orange', 'yellow', 'green' , 'purple', 'cyan']
def init_logging():
root = logging.getLogger()
root.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
formatter = logging.Formatter(
"[%(asctime)s] [Agents] [%(levelname)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S %z",
)
handler.setFormatter(formatter)
root.addHandler(handler)
class DealAgentFramework:
DB = "products_vectorstore"
MEMORY_FILENAME = "memory.json"
def __init__(self):
init_logging()
self.log("Initializing Agent Framework")
load_dotenv()
client = chromadb.PersistentClient(path=self.DB)
self.memory = self.read_memory()
self.collection = client.get_or_create_collection('products')
self.planner = PlanningAgent(self.collection)
self.log("Agent Framework is ready")
def read_memory(self) -> List[Opportunity]:
if os.path.exists(self.MEMORY_FILENAME):
with open(self.MEMORY_FILENAME, "r") as file:
data = json.load(file)
opportunities = [Opportunity(**item) for item in data]
return opportunities
return []
def write_memory(self) -> None:
data = [opportunity.dict() for opportunity in self.memory]
with open(self.MEMORY_FILENAME, "w") as file:
json.dump(data, file, indent=2)
def log(self, message: str):
text = BG_BLUE + WHITE + "[Agent Framework] " + message + RESET
logging.info(text)
def run(self) -> Optional[Opportunity]:
logging.info("Kicking off Planning Agent")
result = self.planner.plan(memory=self.memory)
logging.info(f"Planning Agent has completed and returned: {result}")
if result:
self.memory.append(result)
self.write_memory()
return result
@classmethod
def get_plot_data(cls, max_datapoints=10000):
client = chromadb.PersistentClient(path=cls.DB)
collection = client.get_or_create_collection('products')
result = collection.get(include=['embeddings', 'documents', 'metadatas'], limit=max_datapoints)
vectors = np.array(result['embeddings'])
documents = result['documents']
categories = [metadata['category'] for metadata in result['metadatas']]
colors = [COLORS[CATEGORIES.index(c)] for c in categories]
tsne = TSNE(n_components=3, random_state=42, n_jobs=-1)
reduced_vectors = tsne.fit_transform(vectors)
return documents, reduced_vectors, colors
if __name__=="__main__":
DealAgentFramework().run()

3
week8_wip/hello.py → week8/hello.py

@ -1,11 +1,10 @@
import modal import modal
from modal import App, Volume, Image from modal import App, Image
# Setup # Setup
app = modal.App("hello") app = modal.App("hello")
image = Image.debian_slim().pip_install("requests") image = Image.debian_slim().pip_install("requests")
gpu = "T4"
# Hello! # Hello!

0
week8_wip/items.py → week8/items.py

0
week8_wip/llama.py → week8/llama.py

35
week8/log_utils.py

@ -0,0 +1,35 @@
# Foreground colors
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
WHITE = '\033[37m'
# Background color
BG_BLACK = '\033[40m'
BG_BLUE = '\033[44m'
# Reset code to return to default color
RESET = '\033[0m'
mapper = {
BG_BLACK+RED: "#dd0000",
BG_BLACK+GREEN: "#00dd00",
BG_BLACK+YELLOW: "#dddd00",
BG_BLACK+BLUE: "#0000ee",
BG_BLACK+MAGENTA: "#aa00dd",
BG_BLACK+CYAN: "#00dddd",
BG_BLACK+WHITE: "#87CEEB",
BG_BLUE+WHITE: "#ff7800"
}
def reformat(message):
for key, value in mapper.items():
message = message.replace(key, f'<span style="color: {value}">')
message = message.replace(RESET, '</span>')
return message

164
week8/memory.json

@ -0,0 +1,164 @@
[
{
"deal": {
"product_description": "The Samsung Galaxy Watch Ultra is a premium 47mm LTE Titanium smartwatch designed for both style and functionality. It features a circular display made with durable materials suitable for outdoor activities, providing GPS tracking, health monitoring, and custom apps for various needs. The robust design integrates a range of smart features including notifications, music control, and heart rate tracking, making it an ideal companion for fitness enthusiasts and tech-savvy users alike.",
"price": 350.0,
"url": "https://www.dealnews.com/Samsung-Galaxy-Watch-Ultra-47-mm-LTE-Titanium-Smartwatch-up-to-350-off-w-Trade-in-free-shipping/21663266.html?iref=rss-c142"
},
"estimate": 773.8138460593241,
"discount": 423.8138460593241
},
{
"deal": {
"product_description": "The Refurbished Unlocked Apple iPhone 14 Pro Max offers an impressive 256GB storage and a huge display, perfect for both media consumption and productivity. Enjoy advanced camera technology for stunning photos. This model is designed to provide a seamless user experience with 5G capabilities for faster downloads and streaming. Refurbished to high standards, it comes in various colors and can support all the latest apps from the App Store, accommodating any Apple enthusiast's needs.",
"price": 705.0,
"url": "https://www.dealnews.com/products/Apple/Unlocked-Apple-iPhone-14-Pro-Max-256-GB-Smartphone/462808.html?iref=rss-c142"
},
"estimate": 930.8824204895075,
"discount": 225.88242048950747
},
{
"deal": {
"product_description": "The Certified Refurbished Acer Nitro V laptop boasts a powerful 13th Generation Intel Core i5 processor, perfect for gaming and multitasking. With its 15.6-inch 1080p IPS display and NVIDIA GeForce RTX 4050 GPU, expect stunning visuals and smooth performance for all your gaming needs. It comes with 8GB RAM and a 512GB SSD, ensuring fast load times and ample storage. This model is backed by a 2-year warranty from Allstate, ensuring reliable performance for years to come.",
"price": 560.0,
"url": "https://www.dealnews.com/products/Acer/Nitro-V-13-th-Gen-i5-15-6-Laptop-w-NVIDIA-Ge-Force-RTX-4050/480447.html?iref=rss-c39"
},
"estimate": 925.1468647365509,
"discount": 365.1468647365509
},
{
"deal": {
"product_description": "This EcoFlow DELTA 2 (950) Portable Power Station is designed to meet all your mobile energy needs. With a capacity of 1024Wh, it features six AC outlets and multiple USB ports, allowing you to charge various devices simultaneously. The included 800W alternator charger ensures quick recharging, making it an ideal power solution for camping trips or emergency situations. Its Wi-Fi and Bluetooth capabilities provide easy control and monitoring.",
"price": 699.0,
"url": "https://www.dealnews.com/Eco-Flow-DELTA-2-950-Portable-Power-Station-800-W-Alternator-Charger-for-699-free-shipping/21671420.html?iref=rss-c142"
},
"estimate": 869.9627492224658,
"discount": 170.96274922246585
},
{
"deal": {
"product_description": "This 12V 280Ah LiFePO4 battery offers advanced lithium iron phosphate technology, providing lightweight yet robust power storage for a variety of applications including solar, RV, and marine use. Its long cycle life ensures you can rely on it for consistent performance over time. With built-in BMS (Battery Management System) for safe operation and efficiency, this battery is designed for those who prioritize both reliability and energy efficiency.",
"price": 400.0,
"url": "https://www.dealnews.com/12-V-280-Ah-Li-Fe-PO4-Battery-for-400-free-shipping/21676885.html?iref=rss-c142"
},
"estimate": 744.6978640830646,
"discount": 344.6978640830646
},
{
"deal": {
"product_description": "This certified refurbished Apple iPhone 14 features 128GB of storage, providing ample space for apps, photos, and videos. Known for its performance, the iPhone 14 includes advanced camera capabilities and a long-lasting battery, setting standards in smartphone technology. The device comes with a one-year warranty from Allstate, ensuring reliability and support for customers. This refurbished option is an excellent choice for users looking for quality at a lower price point.",
"price": 425.0,
"url": "https://www.dealnews.com/products/Apple/Refurb-Unlocked-Apple-iPhone-14-128-GB-Phone-Excellent-Cond/472441.html?iref=rss-c142"
},
"estimate": 667.340418267145,
"discount": 242.34041826714497
},
{
"deal": {
"product_description": "The iRobot Roomba j7+ is a highly efficient WiFi self-emptying robot vacuum designed to make cleaning effortless. With advanced smart mapping technology, it can identify and avoid obstacles, ensuring a thorough cleaning of your home. This model offers a self-emptying base, eliminating the need for manual bag changes. A runtime of 90 minutes allows it to cover large areas, making it ideal for busy households.",
"price": 250.0,
"url": "https://www.dealnews.com/products/iRobot/iRobot-Roomba-j7-Wi-Fi-Self-Emptying-Robot-Vacuum/293669.html?iref=rss-f1912"
},
"estimate": 621.4089582478217,
"discount": 371.4089582478217
},
{
"deal": {
"product_description": "The certified refurbished iRobot Roomba 692 is a smart cleaning device that simplifies household upkeep. This vacuum operates with three-stage cleaning technology and has a compatibility feature with smart assistants such as Alexa and Google Home. It is designed for efficient cleaning across hard floors and carpets, providing a 90-minute runtime per charge. This model comes with a 2-year warranty from Allstate, ensuring peace of mind along with top-notch functionality for your cleaning needs.",
"price": 120.0,
"url": "https://www.dealnews.com/products/iRobot/iRobot-Roomba-692-Robot-Vacuum/143125.html?iref=rss-f1912"
},
"estimate": 304.1034980572389,
"discount": 184.10349805723888
},
{
"deal": {
"product_description": "The Certified Refurb Acer Swift Edge Ryzen 7 Laptop features an advanced AMD Ryzen 7 7735U 8-core processor, offering exceptional performance for both work and play. It is equipped with a 16GB RAM and a spacious 1TB SSD, ensuring smooth multitasking and ample storage for your files. The 16-inch display boasts a stunning 3840x2400 resolution, making it ideal for streaming and content creation. This laptop is certified refurbished, meaning it comes backed by a 2-year warranty for peace of mind.",
"price": 760.0,
"url": "https://www.dealnews.com/Certified-Refurb-Acer-Swift-Edge-Ryzen-7-16-Laptop-for-760-free-shipping/21682096.html?iref=rss-c39"
},
"estimate": 985.5902213719573,
"discount": 225.59022137195734
},
{
"deal": {
"product_description": "The Klipsch T5 II True Wireless ANC Earphones offer a high-fidelity audio experience combined with active noise-canceling technology. These earbuds come with six sizes of patented, color-coded oval ear tips to ensure a comfortable fit for all users. The device is equipped with a two-mic hybrid design, enhancing call quality while reducing background noise. With intuitive head gesture controls, you can easily manage your audio playback without needing to reach for your device.",
"price": 68.0,
"url": "https://www.dealnews.com/products/Klipsch/Klipsch-T5-II-True-Wireless-ANC-Earphones/482823.html?iref=rss-c142"
},
"estimate": 309.56921733594834,
"discount": 241.56921733594834
},
{
"deal": {
"product_description": "The Certified Refurbished Acer Aspire 3 laptop boasts a powerful 6th-generation AMD Ryzen 5 processor and a spacious 15.6-inch touchscreen display with full HD resolution. It is equipped with 16GB of RAM and a 1TB SSD, providing ample storage and fast performance. This model comes with Windows 11 Home and is backed by a two-year warranty.",
"price": 288.0,
"url": "https://www.dealnews.com/products/Acer/Acer-Aspire-3-6-th-Gen-Ryzen-5-15-6-Touchscreen-Laptop/476367.html?iref=rss-c39"
},
"estimate": 558.5214284656016,
"discount": 270.5214284656016
},
{
"deal": {
"product_description": "The Eufy eufyCam S330 (eufyCam 3) 4-Camera kit comes equipped with advanced security features, making it a top choice for home monitoring. Each camera offers high-resolution 4K video and includes an integrated solar panel for continuous powering, thus eliminating the need for frequent recharges. The system utilizes artificial intelligence for facial recognition, effectively distinguishing between family and strangers. Furthermore, it includes a 1TB hard drive for expandable local storage, ensuring that you have ample room to save recorded footage without additional fees.",
"price": 650.0,
"url": "https://www.dealnews.com/products/Eufy/eufy-Cam-S330-eufy-Cam-3-4-Camera-Kit-1-TB-HDD/464094.html?iref=rss-c196"
},
"estimate": 901.053559336033,
"discount": 251.053559336033
},
{
"deal": {
"product_description": "The Shark IQ Robot Vacuum is designed to simplify your cleaning routine with its 60-day capacity base, which allows it to store more dirt before needing to be emptied. It comes equipped with advanced navigation technology to efficiently clean your floors while avoiding obstacles. This self-emptying feature not only saves you time but also ensures your home remains clean with minimal effort on your part. It's a perfect solution for busy households looking for convenience.",
"price": 267.0,
"url": "https://www.dealnews.com/Shark-IQ-Robot-Vacuum-w-60-Day-Capacity-Base-for-267-free-shipping/21668563.html?iref=rss-f1912"
},
"estimate": 495.68476864134675,
"discount": 228.68476864134675
},
{
"deal": {
"product_description": "The Dell Inspiron 15 Laptop is equipped with a 12th Generation Intel Core i5 processor and a 15.6-inch touchscreen display, providing a dynamic computing experience for work and entertainment. With 8GB of RAM and a 512GB SSD, it offers sufficient memory for multitasking and fast boot-up times. This laptop runs Windows 11 Home in S Mode, enhancing its performance for everyday tasks. Its sleek design and robust features make it a practical choice for students and professionals alike.",
"price": 350.0,
"url": "https://www.dealnews.com/products/Dell/Dell-Inspiron-15-12-th-Gen-i5-15-6-Touchscreen-Laptop-w-512-GB-SSD/479057.html?iref=rss-c39"
},
"estimate": 577.1076076116793,
"discount": 227.10760761167933
},
{
"deal": {
"product_description": "The EcoFlow DELTA 2 (950) Portable Power Station is designed for convenience and reliability, boasting a 1024Wh capacity that can power multiple devices simultaneously. It features six AC outlets, two USB-A, and two USB-C ports, making it versatile for outdoor adventures, emergencies, or everyday use. With both Wi-Fi and Bluetooth connectivity, you can monitor the status of your power station right from your smartphone. This bundle also comes with an 800W alternator charger, ensuring that you have everything you need to stay powered up wherever you go.",
"price": 699.0,
"url": "https://www.dealnews.com/Eco-Flow-DELTA-2-950-Portable-Power-Station-800-W-Alternator-Charger-for-699-free-shipping/21673983.html?iref=rss-c142"
},
"estimate": 963.8626028683989,
"discount": 264.8626028683989
},
{
"deal": {
"product_description": "The Certified Refurb iRobot Roomba i4 EVO WiFi Robot Vacuum combines smart technology with powerful cleaning capabilities. This robot vacuum is designed to navigate effortlessly through your home, providing thorough cleaning on multiple surfaces. With WiFi connectivity, users can control the vacuum remotely via a smartphone app. The three-stage cleaning system ensures a deep clean, making it a perfect addition for busy households.",
"price": 130.0,
"url": "https://www.dealnews.com/products/iRobot/iRobot-Roomba-i4-EVO-Wi-Fi-Robot-Vacuum/431157.html?iref=rss-f1912"
},
"estimate": 341.23175777017946,
"discount": 211.23175777017946
},
{
"deal": {
"product_description": "The ZimaBoard 832 Single Board Server is a versatile and compact solution for home or office use, equipped with robust processing power suitable for a variety of applications, including media streaming and file servers. It is designed for ease of use, with capabilities for expansion and customization. Its lightweight and energy-efficient design makes it an ideal selection for developers and tech enthusiasts seeking a reliable platform for programming and digital projects.",
"price": 140.0,
"url": "https://www.dealnews.com/Zima-Board-832-Single-Board-Server-for-140-free-shipping/21676871.html?iref=rss-c39"
},
"estimate": 292.84135797094723,
"discount": 152.84135797094723
},
{
"deal": {
"product_description": "The EcoFlow DELTA 2 (950) Portable Power Station is a robust solution for all your electrical needs while on the go. With a 1024Wh capacity, this power station is versatile enough to charge multiple devices simultaneously, thanks to its six AC outlets. Additionally, it features two USB-A and two USB-C ports, enabling you to charge laptops, phones, and other electronics quickly. Bundled with an 800W Alternator Charger, it's designed to ensure you have power, wherever your adventures may lead you.",
"price": 699.0,
"url": "https://www.dealnews.com/Eco-Flow-DELTA-2-950-Portable-Power-Station-800-W-Alternator-Charger-for-699-free-shipping/21676798.html?iref=rss-c142"
},
"estimate": 870.8927901823207,
"discount": 171.8927901823207
}
]

61
week8/price_is_right.py

@ -0,0 +1,61 @@
import gradio as gr
from deal_agent_framework import DealAgentFramework
from agents.deals import Opportunity, Deal
class App:
def __init__(self):
self.agent_framework = None
def run(self):
with gr.Blocks(title="The Price is Right", fill_width=True) as ui:
def table_for(opps):
return [[opp.deal.product_description, f"${opp.deal.price:.2f}", f"${opp.estimate:.2f}", f"${opp.discount:.2f}", opp.deal.url] for opp in opps]
def start():
self.agent_framework = DealAgentFramework()
opportunities = self.agent_framework.memory
table = table_for(opportunities)
return table
def go():
self.agent_framework.run()
new_opportunities = self.agent_framework.memory
table = table_for(new_opportunities)
return table
def do_select(selected_index: gr.SelectData):
opportunities = self.agent_framework.memory
row = selected_index.index[0]
opportunity = opportunities[row]
self.agent_framework.planner.messenger.alert(opportunity)
with gr.Row():
gr.Markdown('<div style="text-align: center;font-size:24px">"The Price is Right" - Deal Hunting Agentic AI</div>')
with gr.Row():
gr.Markdown('<div style="text-align: center;font-size:14px">Autonomous agent framework that finds online deals, collaborating with a proprietary fine-tuned LLM deployed on Modal, and a RAG pipeline with a frontier model and Chroma.</div>')
with gr.Row():
gr.Markdown('<div style="text-align: center;font-size:14px">Deals surfaced so far:</div>')
with gr.Row():
opportunities_dataframe = gr.Dataframe(
headers=["Description", "Price", "Estimate", "Discount", "URL"],
wrap=True,
column_widths=[4, 1, 1, 1, 2],
row_count=10,
col_count=5,
height=400,
)
ui.load(start, inputs=[], outputs=[opportunities_dataframe])
timer = gr.Timer(value=60)
timer.tick(go, inputs=[], outputs=[opportunities_dataframe])
opportunities_dataframe.select(do_select)
ui.launch(share=False, inbrowser=True)
if __name__=="__main__":
App().run()

180
week8/price_is_right_final.py

@ -0,0 +1,180 @@
import logging
import queue
import threading
import time
import gradio as gr
from deal_agent_framework import DealAgentFramework
from agents.deals import Opportunity, Deal
from log_utils import reformat
import plotly.graph_objects as go
class QueueHandler(logging.Handler):
def __init__(self, log_queue):
super().__init__()
self.log_queue = log_queue
def emit(self, record):
self.log_queue.put(self.format(record))
def html_for(log_data):
output = '<br>'.join(log_data[-18:])
return f"""
<div id="scrollContent" style="height: 400px; overflow-y: auto; border: 1px solid #ccc; background-color: #222229; padding: 10px;">
{output}
</div>
"""
def setup_logging(log_queue):
handler = QueueHandler(log_queue)
formatter = logging.Formatter(
"[%(asctime)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S %z",
)
handler.setFormatter(formatter)
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.INFO)
class App:
def __init__(self):
self.agent_framework = None
def run(self):
with gr.Blocks(title="The Price is Right", fill_width=True) as ui:
log_data = gr.State([])
def table_for(opps):
return [[opp.deal.product_description, f"${opp.deal.price:.2f}", f"${opp.estimate:.2f}", f"${opp.discount:.2f}", opp.deal.url] for opp in opps]
def update_output(log_data, log_queue, result_queue):
final_result = None
while True:
try:
message = log_queue.get_nowait()
log_data.append(reformat(message))
yield log_data, html_for(log_data), final_result
except queue.Empty:
try:
final_result = result_queue.get_nowait()
yield log_data, html_for(log_data), final_result
except queue.Empty:
if final_result is not None:
break
time.sleep(0.1)
def get_initial_plot():
fig = go.Figure()
fig.update_layout(
title='Loading vector DB...',
height=400,
)
return fig
def get_plot():
documents, vectors, colors = DealAgentFramework.get_plot_data(max_datapoints=1000)
# Create the 3D scatter plot
fig = go.Figure(data=[go.Scatter3d(
x=vectors[:, 0],
y=vectors[:, 1],
z=vectors[:, 2],
mode='markers',
marker=dict(size=2, color=colors, opacity=0.7),
)])
fig.update_layout(
scene=dict(xaxis_title='x',
yaxis_title='y',
zaxis_title='z',
aspectmode='manual',
aspectratio=dict(x=2.2, y=2.2, z=1), # Make x-axis twice as long
camera=dict(
eye=dict(x=1.6, y=1.6, z=0.8) # Adjust camera position
)),
height=400,
margin=dict(r=5, b=1, l=5, t=2)
)
return fig
def start():
self.agent_framework = DealAgentFramework()
self.agent_framework.run()
opportunities = self.agent_framework.memory
table = table_for(opportunities)
return table
def do_run():
if not self.agent_framework:
self.agent_framework = DealAgentFramework()
self.agent_framework.run()
new_opportunities = self.agent_framework.memory
table = table_for(new_opportunities)
return table
def do_with_logging(function, initial_log_data):
log_queue = queue.Queue()
result_queue = queue.Queue()
setup_logging(log_queue)
def worker():
result = function()
result_queue.put(result)
thread = threading.Thread(target=worker)
thread.start()
for log_data, output, final_result in update_output(initial_log_data, log_queue, result_queue):
yield log_data, output, final_result
def start_with_logging(initial_log_data):
for log_data, output, final_result in do_with_logging(start, initial_log_data):
yield log_data, output, final_result
def run_with_logging(initial_log_data):
for log_data, output, final_result in do_with_logging(do_run, initial_log_data):
yield log_data, output, final_result
def do_select(selected_index: gr.SelectData):
opportunities = self.agent_framework.memory
row = selected_index.index[0]
opportunity = opportunities[row]
self.agent_framework.planner.messenger.alert(opportunity)
with gr.Row():
gr.Markdown('<div style="text-align: center;font-size:24px"><strong>The Price is Right</strong> - Autonomous Agent Framework that hunts for deals</div>')
with gr.Row():
gr.Markdown('<div style="text-align: center;font-size:14px">A proprietary fine-tuned LLM deployed on Modal and a RAG pipeline with a frontier model collaborate to send push notifications with great online deals.</div>')
with gr.Row():
opportunities_dataframe = gr.Dataframe(
headers=["Deals found so far", "Price", "Estimate", "Discount", "URL"],
wrap=True,
column_widths=[6, 1, 1, 1, 3],
row_count=10,
col_count=5,
height=400,
)
with gr.Row():
with gr.Column(scale=1):
logs = gr.HTML()
with gr.Column(scale=1):
plot = gr.Plot(value=get_plot(), show_label=False)
ui.load(start_with_logging, inputs=[log_data], outputs=[log_data, logs, opportunities_dataframe])
timer = gr.Timer(value=300, active=True)
timer.tick(run_with_logging, inputs=[log_data], outputs=[log_data, logs, opportunities_dataframe])
# timer2 = gr.Timer(value=5, active=True)
# timer2.tick(get_plot, inputs=[], outputs=[plot, timer2])
opportunities_dataframe.select(do_select)
ui.launch(share=False, inbrowser=True)
if __name__=="__main__":
App().run()

2
week8_wip/pricer_ephemeral.py → week8/pricer_ephemeral.py

@ -1,5 +1,5 @@
import modal import modal
from modal import App, Volume, Image from modal import App, Image
# Setup # Setup

2
week8_wip/pricer_service.py → week8/pricer_service.py

@ -1,5 +1,5 @@
import modal import modal
from modal import App, Volume, Image from modal import App, Image
# Setup - define our infrastructure with code! # Setup - define our infrastructure with code!

0
week8_wip/pricer_service2.py → week8/pricer_service2.py

0
week8_wip/testing.py → week8/testing.py

28
week8_wip/agents/messaging_agent.py

@ -1,28 +0,0 @@
import os
from twilio.rest import Client
from agents.deals import Opportunity
class MessagingAgent:
def __init__(self):
account_sid = os.getenv('TWILIO_ACCOUNT_SID', 'your-sid-if-not-using-env')
auth_token = os.getenv('TWILIO_AUTH_TOKEN', 'your-auth-if-not-using-env')
self.me_from = 'whatsapp:+14155238886'
self.me_to = f"whatsapp:+1{os.getenv('MY_PHONE_NUMBER', 'your-phone-number-if-not-using-env')}"
self.client = Client(account_sid, auth_token)
def message(self, text):
message = self.client.messages.create(
from_=self.me_from,
body=text,
to=self.me_to
)
def alert(self, opportunity: Opportunity):
text = f"Deal! Price=${opportunity.quality_deal.price:.2f}, "
text += f"Estimate=${opportunity.estimate:.2f} :"
text += opportunity.quality_deal.product_description[:10]+'... '
text += opportunity.quality_deal.url
self.message(text)

24
week8_wip/agents/planning_agent.py

@ -1,24 +0,0 @@
from agents.deals import Deal, QualityDealSelection, Opportunity
from agents.scanner_agent import ScannerAgent
from agents.ensemble_agent import EnsembleAgent
from agents.messaging_agent import MessagingAgent
class PlanningAgent:
def __init__(self, collection):
self.scanner = ScannerAgent()
self.ensemble = EnsembleAgent(collection)
self.messenger = MessagingAgent()
def plan(self):
opportunities = []
deal_selection = self.scanner.scan()
for deal in deal_selection.quality_deals[:5]:
estimate = self.ensemble.price(deal.product_description)
opportunities.append(Opportunity(deal, estimate, estimate - deal.price))
opportunities.sort(key=lambda opp: opp.discount, reverse=True)
print(opportunities)
if opportunities[0].discount > 50:
self.messenger.alert(opportunities[0])

18
week8_wip/agents/random_forest_agent.py

@ -1,18 +0,0 @@
# imports
import os
import re
from typing import List
from sentence_transformers import SentenceTransformer
import joblib
class RandomForestAgent:
def __init__(self):
self.vectorizer = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
self.model = joblib.load('random_forest_model.pkl')
def price(self, description: str) -> float:
vector = self.vectorizer.encode([description])
return max(0, self.model.predict(vector)[0])

46
week8_wip/agents/scanner_agent.py

@ -1,46 +0,0 @@
import os
import json
from openai import OpenAI
from agents.deals import Deal, QualityDealSelection
class ScannerAgent:
MODEL = "gpt-4o-mini"
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.
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.
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.
{"quality_deals": [
{
"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.",
"price": 99.99,
"url": "the url as provided"
},
...
]}"""
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.
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.
Remember to respond with a paragraph of text in the product_description field for each of the 5 items that you select.
Deals:
"""
def __init__(self):
self.openai = OpenAI()
def scan(self) -> QualityDealSelection:
deals = Deal.fetch()
user_prompt = self.USER_PROMPT_PREFIX + '\n\n'.join([deal.describe() for deal in deals])
completion = self.openai.beta.chat.completions.parse(
model=self.MODEL,
messages=[
{"role": "system", "content": self.SYSTEM_PROMPT},
{"role": "user", "content": user_prompt}
],
response_format=QualityDealSelection
)
result = completion.choices[0].message.parsed
return result

10
week8_wip/agents/specialist_agent.py

@ -1,10 +0,0 @@
import modal
class SpecialistAgent:
def __init__(self):
Pricer = modal.Cls.lookup("pricer-service", "Pricer")
self.pricer = Pricer()
def price(self, description: str) -> float:
return self.pricer.price.remote(description)

31093
week8_wip/day2.1.ipynb

File diff suppressed because one or more lines are too long

41094
week8_wip/day2.2.ipynb

File diff suppressed because one or more lines are too long

708
week8_wip/day2.3.ipynb

File diff suppressed because one or more lines are too long

1660
week8_wip/day2.4.ipynb

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save