Spaces:
Running on Zero
Running on Zero
Upload community_contributions/ederhion_week8/agents/messaging_agent.py with huggingface_hub
Browse files
community_contributions/ederhion_week8/agents/messaging_agent.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from agents.deals import Opportunity
|
| 3 |
+
from agents.agent import Agent
|
| 4 |
+
from litellm import completion
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
pushover_url = "https://api.pushover.net/1/messages.json"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class MessagingAgent(Agent):
|
| 11 |
+
name = "Messaging Agent"
|
| 12 |
+
color = Agent.WHITE
|
| 13 |
+
MODEL = "claude-sonnet-4-5"
|
| 14 |
+
|
| 15 |
+
def __init__(self):
|
| 16 |
+
"""
|
| 17 |
+
Set up this object to either do push notifications via Pushover,
|
| 18 |
+
or SMS via Twilio,
|
| 19 |
+
whichever is specified in the constants
|
| 20 |
+
"""
|
| 21 |
+
self.log("Messaging Agent is initializing")
|
| 22 |
+
self.pushover_user = os.getenv("PUSHOVER_USER", "your-pushover-user-if-not-using-env")
|
| 23 |
+
self.pushover_token = os.getenv("PUSHOVER_TOKEN", "your-pushover-user-if-not-using-env")
|
| 24 |
+
self.log("Messaging Agent has initialized Pushover and Claude")
|
| 25 |
+
|
| 26 |
+
def push(self, text):
|
| 27 |
+
"""
|
| 28 |
+
Send a Push Notification using the Pushover API
|
| 29 |
+
"""
|
| 30 |
+
self.log("Messaging Agent is sending a push notification")
|
| 31 |
+
payload = {
|
| 32 |
+
"user": self.pushover_user,
|
| 33 |
+
"token": self.pushover_token,
|
| 34 |
+
"message": text,
|
| 35 |
+
"sound": "cashregister",
|
| 36 |
+
}
|
| 37 |
+
requests.post(pushover_url, data=payload)
|
| 38 |
+
|
| 39 |
+
def alert(self, opportunity: Opportunity):
|
| 40 |
+
"""
|
| 41 |
+
Make an alert about the specified Opportunity
|
| 42 |
+
"""
|
| 43 |
+
text = f"Deal Alert! Price=${opportunity.deal.price:.2f}, "
|
| 44 |
+
text += f"Estimate=${opportunity.estimate:.2f}, "
|
| 45 |
+
text += f"Discount=${opportunity.discount:.2f} :"
|
| 46 |
+
text += opportunity.deal.product_description[:10] + "... "
|
| 47 |
+
text += opportunity.deal.url
|
| 48 |
+
self.push(text)
|
| 49 |
+
self.log("Messaging Agent has completed")
|
| 50 |
+
|
| 51 |
+
def craft_message(
|
| 52 |
+
self, description: str, deal_price: float, estimated_true_value: float
|
| 53 |
+
) -> str:
|
| 54 |
+
user_prompt = "Please summarize this great deal in 2-3 sentences to be sent as an exciting push notification alerting the user about this deal.\n"
|
| 55 |
+
user_prompt += f"Item Description: {description}\nOffered Price: {deal_price}\nEstimated true value: {estimated_true_value}"
|
| 56 |
+
user_prompt += "\n\nRespond only with the 2-3 sentence message which will be used to alert & excite the user about this deal"
|
| 57 |
+
response = completion(
|
| 58 |
+
model=self.MODEL,
|
| 59 |
+
messages=[
|
| 60 |
+
{"role": "user", "content": user_prompt},
|
| 61 |
+
],
|
| 62 |
+
)
|
| 63 |
+
return response.choices[0].message.content
|
| 64 |
+
|
| 65 |
+
def notify(self, description: str, deal_price: float, estimated_true_value: float, url: str):
|
| 66 |
+
"""
|
| 67 |
+
Make an alert about the specified details
|
| 68 |
+
"""
|
| 69 |
+
self.log("Messaging Agent is using Claude to craft the message")
|
| 70 |
+
text = self.craft_message(description, deal_price, estimated_true_value)
|
| 71 |
+
self.push(text[:200] + "... " + url)
|
| 72 |
+
self.log("Messaging Agent has completed")
|