prabalGaur commited on
Commit
cfbe9a1
·
verified ·
1 Parent(s): 9b7245f

Upload community_contributions/ederhion_week8/agents/scanner_agent.py with huggingface_hub

Browse files
community_contributions/ederhion_week8/agents/scanner_agent.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional, List
2
+ from openai import OpenAI
3
+ from agents.deals import ScrapedDeal, DealSelection
4
+ from agents.agent import Agent
5
+
6
+ class ScannerAgent(Agent):
7
+ MODEL = "gpt-4o-mini"
8
+
9
+ SYSTEM_PROMPT = """You identify and summarize the 5 most detailed used car deals from a list.
10
+ Respond strictly in JSON (JavaScript Object Notation) format with no explanation. Provide the price as a number derived from the description.
11
+ Most important is that you respond with the 5 cars that have the most detailed product descriptions (make, model, year, mileage) and a clear price.
12
+ """
13
+
14
+ USER_PROMPT_PREFIX = """Respond with the most promising 5 vehicle deals from this list, selecting those which have detailed descriptions and a price greater than 0.
15
+ Rephrase the description to be a summary of the vehicle itself.
16
+
17
+ Deals:
18
+
19
+ """
20
+
21
+ USER_PROMPT_SUFFIX = "\n\nInclude exactly 5 deals, no more."
22
+
23
+ name = "Scanner Agent"
24
+ color = Agent.CYAN
25
+
26
+ def __init__(self):
27
+ self.log("Scanner Agent is initializing")
28
+ self.openai = OpenAI()
29
+ self.log("Scanner Agent is ready")
30
+
31
+ def fetch_deals(self, memory) -> List[ScrapedDeal]:
32
+ self.log("Scanner Agent is about to fetch deals from the automotive RSS feed")
33
+ urls = [opp.deal.url for opp in memory]
34
+ scraped = ScrapedDeal.fetch()
35
+ result = [scrape for scrape in scraped if scrape.url not in urls]
36
+ self.log(f"Scanner Agent received {len(result)} deals not already scraped")
37
+ return result
38
+
39
+ def make_user_prompt(self, scraped) -> str:
40
+ user_prompt = self.USER_PROMPT_PREFIX
41
+ user_prompt += "\n\n".join([scrape.describe() for scraped in scraped])
42
+ user_prompt += self.USER_PROMPT_SUFFIX
43
+ return user_prompt
44
+
45
+ def scan(self, memory: List[str] = []) -> Optional[DealSelection]:
46
+ scraped = self.fetch_deals(memory)
47
+ if scraped:
48
+ user_prompt = self.make_user_prompt(scraped)
49
+ self.log("Scanner Agent is calling OpenAI using Structured Outputs")
50
+ result = self.openai.chat.completions.parse(
51
+ model=self.MODEL,
52
+ messages=[
53
+ {"role": "system", "content": self.SYSTEM_PROMPT},
54
+ {"role": "user", "content": user_prompt},
55
+ ],
56
+ response_format=DealSelection,
57
+ reasoning_effort="none",
58
+ )
59
+ parsed_result = result.choices[0].message.parsed
60
+ parsed_result.deals = [deal for deal in parsed_result.deals if deal.price > 0]
61
+ self.log(f"Scanner Agent received {len(parsed_result.deals)} selected deals with price>0")
62
+ return parsed_result
63
+ return None
64
+
65
+ def test_scan(self, memory: List[str] = []) -> Optional[DealSelection]:
66
+ results = {
67
+ "deals": [
68
+ {
69
+ "product_description": "2018 Honda Accord EX-L with 45,000 miles. Features a 1.5L turbocharged engine, leather seats, and advanced safety features. Excellent condition.",
70
+ "price": 21000,
71
+ "url": "https://www.dealnews.com/fake-honda-accord"
72
+ },
73
+ {
74
+ "product_description": "2020 Toyota Camry SE with 30,000 miles. Includes sport suspension, 18-inch alloy wheels, and a 7-inch touchscreen infotainment system.",
75
+ "price": 23500,
76
+ "url": "https://www.dealnews.com/fake-toyota-camry"
77
+ }
78
+ ]
79
+ }
80
+ return DealSelection(**results)