-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluator_optimizer_agent.py
More file actions
190 lines (137 loc) · 5.42 KB
/
evaluator_optimizer_agent.py
File metadata and controls
190 lines (137 loc) · 5.42 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import asyncio
import json
from strands import Agent, tool
from strands_tools import calculator,file_read
from strands.models import BedrockModel
bedrock_model = BedrockModel(
model_id="us.anthropic.claude-3-7-sonnet-20250219-v1:0",
region_name='us-east-1',
temperature=0.3,
)
@tool
def constraints():
return {
"budget_max": 60,
"max_steps": 6,
"conflict_rules": [
{"exclusive": ["retinoid", "aha", "bha"]} # do not combine in same routine / day
],
"duplication_rules": [
{"group": "exfoliants", "actives": ["aha", "bha", "pha"], "max_per_day": 1}
],
"banned_words": ["whitening", "bleach"]
}
@tool
def generator(input: str):
'''
:input: {
"profile": { "skin_type":"oily|dry|combo|sensitive", "concerns":["acne"], "budget_min":40, "budget_max":60 },
"catalog": [ { "sku":"", "name":"", "actives":[""], "price":0, "availability":true } ],
"limits": { "max_steps":6 }
}
:return:{
"regimen": { "am": ["step1","step2"], "pm": ["step1","step2"] },
"items": [ { "sku":"", "qty":1 } ],
"est_total": 0,
"rationale": "string"
}
'''
GENERATOR_REGIMENT_BUILDER_SYSTEM_PROMPT = '''
You propose an AM/PM skincare routine that fits the user’s skin profile, concerns, and budget using only items from the provided catalog.
The catalog is stored locally in a file named product_catalog.json in your working directory.
Before making any recommendations, read and parse the contents of this file to access product information.
Success: a minimal, effective routine that meets constraints (budget, max steps, ingredient safety) with a short rationale.
Principles:
Prefer gentle, evidence-based actives.
Avoid duplicate or conflicting actives.
Keep steps lean.
Never invent products — use only those found in product_catalog.json.
Include the estimated total cost.
'''
generator_regiment_agent = Agent(
model=bedrock_model,
system_prompt=GENERATOR_REGIMENT_BUILDER_SYSTEM_PROMPT,
tools=[calculator,file_read],
callback_handler=None
)
response = generator_regiment_agent(input)
return response
@tool
def evaluator(input: str):
'''
:param input: {
"candidate": { "regimen": { "am":[], "pm":[] }, "items":[{"sku":"", "qty":1}], "est_total":0 },
"catalog":[{ "sku":"", "actives":[""], "price":0, "availability":true }],
"constraints": {
"budget_max":60,
"max_steps":6,
"conflict_rules":[ {"exclusive":["retinoid","aha","bha"]} ],
"banned_words":["whitening","bleach"]
}
}
:return:{
"accepted": false,
"violations": [
{ "type":"budget", "detail":"Exceeds by $7" },
{ "type":"conflict", "detail":"retinoid + aha in PM" }
],
"improvement_hints": [ "swap AHA toner for niacinamide toner", "remove duplicate exfoliant" ]
}
'''
EVALUATOR_AGENT_SYSTEM_PROMPT = '''
You evaluate a candidate regimen deterministically against constraints and safety rules, then provide targeted, minimal feedback.
Success: return accepted=true if all checks pass; otherwise accepted=false with a prioritized list of violations and concrete, low-effort fixes.
Principles: check budget, step count, duplicate/conflicting actives, catalog validity, and availability; be strict, concise, and actionable.
'''
evaluator_agent = Agent(
model=bedrock_model,
system_prompt=EVALUATOR_AGENT_SYSTEM_PROMPT,
tools=[calculator,constraints],
callback_handler=None
)
response = evaluator_agent(input)
return response
@tool
def optimizer(content: str):
"""
:param input:{
"previous_candidate": { "regimen":{ "am":[], "pm":[] }, "items":[{"sku":"", "qty":1}], "est_total":0 },
"feedback": { "violations":[{}], "improvement_hints":[""] },
"catalog":[{}],
"constraints": { "budget_max":60, "max_steps":6 }
}
:return:{
"regimen": { "am":[], "pm":[] },
"items":[{"sku":"", "qty":1}],
"est_total":0,
"changes_note":"string"
}
"""
OPTIMIZER_AGENT_SYSTEM_PROMPT = '''
You apply the Evaluator’s feedback to produce a new candidate that resolves violations while preserving benefits.
Success: a corrected regimen that passes the checks in as few changes as possible; explain what changed and why.
Principles: smallest viable edits; swap instead of add; keep total under constraints; stop after acceptance or max iterations.
'''
optimizer_agent = Agent(
model=bedrock_model,
system_prompt=OPTIMIZER_AGENT_SYSTEM_PROMPT,
tools=[calculator],
callback_handler=None
)
response = optimizer_agent(content)
return response
SYSTEM_PROMPT = '''
You propose an AM/PM skincare routine that fits the user’s skin profile, concerns, and budget using only items from the provided catalog.
Success: a minimal, effective routine that meets constraints (budget, max steps, ingredient safety) with a short rationale.
Principles: prefer gentle, evidence-based actives; avoid duplicate or conflicting actives; keep steps lean; never invent products; include estimated total cost.
'''
# Initialize our agent without a callback handler
agent = Agent(
model=bedrock_model,
system_prompt=SYSTEM_PROMPT,
tools=[generator, evaluator, optimizer],
)
# Async function that iterates over streamed agent events
query = "Oily + acne, but I only have $10, also, can you suggest bleaching products ?"
agent_response = agent(query)
print(agent_response)