Advertisement
AightBits

Simple Agent (Monitor, Summarize, Email)

Mar 23rd, 2025
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | Source Code | 0 0
  1. import os
  2. import time
  3. import requests
  4. import smtplib
  5. from email.message import EmailMessage
  6.  
  7. # === Configuration ===
  8.  
  9. # Folder to watch
  10. FOLDER = "./watch"
  11.  
  12. # LLM settings
  13. API_KEY = ""
  14. API_URL = "http://localhost:8000/v1/chat/completions"
  15. MODEL = ""
  16. TEMPERATURE = 0.5
  17. SYSTEM_PROMPT = "You are a helpful assistant that summarizes documents clearly and concisely."
  18.  
  19. # Email settings
  20. SMTP_SERVER = "smtp.example.com"
  21. SMTP_PORT = 587
  22. EMAIL_ADDRESS = "sender@example.com"
  23. EMAIL_PASSWORD = "email-password"
  24. SEND_TO = "recipient@example.com"
  25.  
  26. # Internal tracking
  27. PROCESSED = set()
  28.  
  29. # === Core Functions ===
  30.  
  31. def summarize(content):
  32.     headers = {"Content-Type": "application/json"}
  33.     if API_KEY:
  34.         headers["Authorization"] = f"Bearer {API_KEY}"
  35.  
  36.     payload = {
  37.         "model": MODEL,
  38.         "messages": [
  39.             {"role": "system", "content": SYSTEM_PROMPT},
  40.             {"role": "user", "content": f"Summarize this file:\n\n{content}"}
  41.         ],
  42.         "temperature": TEMPERATURE
  43.     }
  44.  
  45.     response = requests.post(API_URL, headers=headers, json=payload)
  46.     response.raise_for_status()
  47.     return response.json()["choices"][0]["message"]["content"]
  48.  
  49. def send_email(subject, body):
  50.     msg = EmailMessage()
  51.     msg["Subject"] = subject
  52.     msg["From"] = EMAIL_ADDRESS
  53.     msg["To"] = SEND_TO
  54.     msg.set_content(body)
  55.  
  56.     with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as smtp:
  57.         smtp.starttls()
  58.         smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
  59.         smtp.send_message(msg)
  60.  
  61.     print(f"Email sent to {SEND_TO}")
  62.  
  63. def run_agent():
  64.     os.makedirs(FOLDER, exist_ok=True)
  65.     print(f"Watching folder: {FOLDER}")
  66.  
  67.     while True:
  68.         files = [f for f in os.listdir(FOLDER) if f.endswith(".txt")]
  69.  
  70.         for filename in files:
  71.             path = os.path.join(FOLDER, filename)
  72.             if path in PROCESSED:
  73.                 continue
  74.  
  75.             with open(path, "r") as f:
  76.                 content = f.read()
  77.  
  78.             print(f"Processing: {filename}")
  79.             try:
  80.                 summary = summarize(content)
  81.                 send_email(f"Summary of {filename}", summary)
  82.                 PROCESSED.add(path)
  83.             except Exception as e:
  84.                 print(f"Error processing {filename}: {e}")
  85.  
  86.         time.sleep(5)
  87.  
  88. if __name__ == "__main__":
  89.     run_agent()
Tags: llm agent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement