forked from bcefghj/multi-agent-ecommerce-system
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupervisor.py
More file actions
133 lines (111 loc) · 4.52 KB
/
Copy pathsupervisor.py
File metadata and controls
133 lines (111 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Supervisor编排器 — 并行分发 + 聚合模式
┌──────────────┐
│ Supervisor │
└──────┬───────┘
┌───────┬───────┼───────┬────────┐
▼ ▼ ▼ ▼ │
UserProfile ProdRec MktCopy Inventory │
│ │ │ │ │
└───────┴───────┴───────┘ │
│ │
▼ │
Aggregator ◄─────────────────┘
│
▼
A/B Test Engine
"""
from __future__ import annotations
import asyncio
import time
import uuid
from typing import Any
import structlog
from agents import (
InventoryAgent,
MarketingCopyAgent,
ProductRecAgent,
UserProfileAgent,
)
from models.schemas import (
Product,
RecommendationRequest,
RecommendationResponse,
UserProfile,
)
from services.ab_test import ABTestEngine
logger = structlog.get_logger()
class SupervisorOrchestrator:
"""Coordinates four agents in parallel-then-aggregate pattern."""
def __init__(self, ab_engine: ABTestEngine | None = None):
self.user_profile_agent = UserProfileAgent()
self.product_rec_agent = ProductRecAgent()
self.marketing_copy_agent = MarketingCopyAgent()
self.inventory_agent = InventoryAgent()
self.ab_engine = ab_engine or ABTestEngine()
async def recommend(self, request: RecommendationRequest) -> RecommendationResponse:
request_id = str(uuid.uuid4())
start = time.perf_counter()
logger.info(
"supervisor.start",
request_id=request_id,
user_id=request.user_id,
scene=request.scene,
)
experiment = self.ab_engine.assign(request.user_id)
# Phase 1: parallel — user profile + product recall
profile_result, rec_result = await asyncio.gather(
self.user_profile_agent.run(
user_id=request.user_id,
context=request.context,
),
self.product_rec_agent.run(
user_profile=None,
num_items=request.num_items * 2,
),
)
user_profile: UserProfile | None = getattr(profile_result, "profile", None)
raw_products: list[Product] = getattr(rec_result, "products", [])
# Phase 2: parallel — re-rank with profile + inventory check + copy generation
rerank_task = self.product_rec_agent.run(
user_profile=user_profile,
num_items=request.num_items,
)
inventory_task = self.inventory_agent.run(products=raw_products)
rerank_result, inventory_result = await asyncio.gather(
rerank_task, inventory_task
)
ranked_products: list[Product] = getattr(rerank_result, "products", raw_products)
available_ids = set(getattr(inventory_result, "available_products", []))
final_products = [p for p in ranked_products if p.product_id in available_ids]
if not final_products:
final_products = ranked_products[:request.num_items]
final_products = final_products[:request.num_items]
# Phase 3: marketing copy generation with final product list
copy_result = await self.marketing_copy_agent.run(
user_profile=user_profile,
products=final_products,
)
copies = getattr(copy_result, "copies", [])
total_latency = (time.perf_counter() - start) * 1000
logger.info(
"supervisor.complete",
request_id=request_id,
total_latency_ms=round(total_latency, 1),
product_count=len(final_products),
copy_count=len(copies),
)
return RecommendationResponse(
request_id=request_id,
user_id=request.user_id,
products=final_products,
marketing_copies=copies,
experiment_group=experiment.get("group", "control"),
agent_results={
"user_profile": profile_result,
"product_rec": rerank_result,
"marketing_copy": copy_result,
"inventory": inventory_result,
},
total_latency_ms=total_latency,
)