craftpilot / agents /pricer.py
skamathramesh's picture
CraftPilot: multi-agent craft business assistant
98a986c verified
Raw
History Blame Contribute Delete
2.48 kB
"""Pricer agent: suggests fair pricing for handmade craft items."""
from jinja2 import Template
from agents.llm import LLMClient
from agents.models import AgentTrace, CatalogerOutput, PricerOutput
SYSTEM_PROMPT = (
"You are a pricing expert for handmade crafts sold on Etsy, Instagram, "
"and at craft fairs.\n\n"
"PRICING RULES (follow strictly):\n"
"1. The price MUST ALWAYS be HIGHER than the material cost. Never suggest "
"a price below material cost.\n"
"2. Labor rate: $15-25/hour for moderate work, $25-40/hour for complex work.\n"
"3. Formula: price = material_cost + (hours * labor_rate) + profit_margin.\n"
"4. Profit margin: add 20-40% on top.\n"
"5. If material_cost + labor exceeds $100, the price must reflect that.\n\n"
"Always respond with valid JSON."
)
USER_TEMPLATE = Template(
"Suggest a fair price range for this {{ craft_type }} item:\n\n"
"Category: {{ catalog.category }} / {{ catalog.sub_category }}\n"
"Materials: {{ catalog.materials | join(', ') }}\n"
"Size: {{ catalog.estimated_size }}\n"
"Complexity: {{ catalog.complexity }}\n"
"{% if material_cost %}Material cost: ${{ material_cost }}{% endif %}\n"
"{% if time_hours %}Time spent: {{ time_hours }} hours{% endif %}\n"
"{% if material_cost and time_hours %}\n"
"MINIMUM price calculation:\n"
"- Materials: ${{ material_cost }}\n"
"- Labor ({{ time_hours }}h x $20/hr): ${{ (time_hours * 20) | int }}\n"
"- Subtotal: ${{ (material_cost + time_hours * 20) | int }}\n"
"- The suggested_price_min MUST be at least ${{ (material_cost + time_hours * 20) | int }}\n"
"{% endif %}\n"
"Provide suggested_price_min, suggested_price_max, reasoning, and cost_breakdown."
)
async def run(
llm: LLMClient,
craft_type: str,
catalog: CatalogerOutput,
material_cost: float | None = None,
time_hours: float | None = None,
) -> tuple[PricerOutput, AgentTrace]:
prompt = USER_TEMPLATE.render(
craft_type=craft_type,
catalog=catalog,
material_cost=material_cost,
time_hours=time_hours,
)
result, duration_ms = await llm.agenerate(
system=SYSTEM_PROMPT,
prompt=prompt,
output_schema=PricerOutput,
)
trace = AgentTrace(
agent_name="pricer",
input_text=prompt,
output_data=result.model_dump(),
duration_ms=duration_ms,
model_id=llm.model_id,
)
return result, trace