Spaces:
Running on Zero
Running on Zero
Upload community_contributions/ederhion_week8/deal_agent_framework.py with huggingface_hub
Browse files
community_contributions/ederhion_week8/deal_agent_framework.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import logging
|
| 4 |
+
import json
|
| 5 |
+
from typing import List
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
import chromadb
|
| 8 |
+
from agents.planning_agent import PlanningAgent
|
| 9 |
+
from agents.deals import Opportunity
|
| 10 |
+
|
| 11 |
+
load_dotenv(override=True)
|
| 12 |
+
|
| 13 |
+
BG_BLUE = "\033[44m"
|
| 14 |
+
WHITE = "\033[37m"
|
| 15 |
+
RESET = "\033[0m"
|
| 16 |
+
|
| 17 |
+
def init_logging():
|
| 18 |
+
root = logging.getLogger()
|
| 19 |
+
root.setLevel(logging.INFO)
|
| 20 |
+
|
| 21 |
+
handler = logging.StreamHandler(sys.stdout)
|
| 22 |
+
handler.setLevel(logging.INFO)
|
| 23 |
+
formatter = logging.Formatter(
|
| 24 |
+
"[%(asctime)s] [Agents] [%(levelname)s] %(message)s",
|
| 25 |
+
datefmt="%Y-%m-%d %H:%M:%S %z",
|
| 26 |
+
)
|
| 27 |
+
handler.setFormatter(formatter)
|
| 28 |
+
root.addHandler(handler)
|
| 29 |
+
|
| 30 |
+
class DealAgentFramework:
|
| 31 |
+
DB = "products_vectorstore"
|
| 32 |
+
MEMORY_FILENAME = "memory.json"
|
| 33 |
+
COLLECTION_NAME = "used_cars"
|
| 34 |
+
|
| 35 |
+
def __init__(self):
|
| 36 |
+
init_logging()
|
| 37 |
+
client = chromadb.PersistentClient(path=self.DB)
|
| 38 |
+
self.memory = self.read_memory()
|
| 39 |
+
self.collection = client.get_or_create_collection(self.COLLECTION_NAME)
|
| 40 |
+
self.planner = None
|
| 41 |
+
|
| 42 |
+
def init_agents_as_needed(self):
|
| 43 |
+
if not self.planner:
|
| 44 |
+
self.log("Initializing Agent Framework")
|
| 45 |
+
self.planner = PlanningAgent(self.collection)
|
| 46 |
+
self.log("Agent Framework is ready")
|
| 47 |
+
|
| 48 |
+
def read_memory(self) -> List[Opportunity]:
|
| 49 |
+
if os.path.exists(self.MEMORY_FILENAME):
|
| 50 |
+
with open(self.MEMORY_FILENAME, "r") as file:
|
| 51 |
+
data = json.load(file)
|
| 52 |
+
opportunities = [Opportunity(**item) for item in data]
|
| 53 |
+
return opportunities
|
| 54 |
+
return []
|
| 55 |
+
|
| 56 |
+
def write_memory(self) -> None:
|
| 57 |
+
data = [opportunity.model_dump() for opportunity in self.memory]
|
| 58 |
+
with open(self.MEMORY_FILENAME, "w") as file:
|
| 59 |
+
json.dump(data, file, indent=2)
|
| 60 |
+
|
| 61 |
+
@classmethod
|
| 62 |
+
def reset_memory(cls) -> None:
|
| 63 |
+
data = []
|
| 64 |
+
if os.path.exists(cls.MEMORY_FILENAME):
|
| 65 |
+
with open(cls.MEMORY_FILENAME, "r") as file:
|
| 66 |
+
data = json.load(file)
|
| 67 |
+
truncated = data[:2]
|
| 68 |
+
with open(cls.MEMORY_FILENAME, "w") as file:
|
| 69 |
+
json.dump(truncated, file, indent=2)
|
| 70 |
+
|
| 71 |
+
def log(self, message: str):
|
| 72 |
+
text = BG_BLUE + WHITE + "[Agent Framework] " + message + RESET
|
| 73 |
+
logging.info(text)
|
| 74 |
+
|
| 75 |
+
def run(self) -> List[Opportunity]:
|
| 76 |
+
self.init_agents_as_needed()
|
| 77 |
+
logging.info("Kicking off Planning Agent")
|
| 78 |
+
result = self.planner.plan(memory=self.memory)
|
| 79 |
+
logging.info(f"Planning Agent has completed and returned: {result}")
|
| 80 |
+
if result:
|
| 81 |
+
self.memory.append(result)
|
| 82 |
+
self.write_memory()
|
| 83 |
+
return self.memory
|
| 84 |
+
|
| 85 |
+
@classmethod
|
| 86 |
+
def get_plot_data(cls, max_datapoints=2000):
|
| 87 |
+
client = chromadb.PersistentClient(path=cls.DB)
|
| 88 |
+
collection = client.get_or_create_collection(cls.COLLECTION_NAME)
|
| 89 |
+
result = collection.get(
|
| 90 |
+
include=["embeddings", "documents", "metadatas"], limit=max_datapoints
|
| 91 |
+
)
|
| 92 |
+
import numpy as np
|
| 93 |
+
from sklearn.manifold import TSNE
|
| 94 |
+
vectors = np.array(result["embeddings"])
|
| 95 |
+
documents = result["documents"]
|
| 96 |
+
categories = [metadata.get("category", "Automotive") for metadata in result["metadatas"]]
|
| 97 |
+
colors = ["blue" for _ in categories]
|
| 98 |
+
tsne = TSNE(n_components=3, random_state=42, n_jobs=-1)
|
| 99 |
+
reduced_vectors = tsne.fit_transform(vectors)
|
| 100 |
+
return documents, reduced_vectors, colors
|
| 101 |
+
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
DealAgentFramework().run()
|