# recipe.py
from praisonaiagents import Agent, Task, AgentTeam
def run(input_data: dict, config: dict = None) -> dict:
"""Generate a support reply draft."""
ticket_id = input_data.get("ticket_id")
message = input_data.get("message")
tone = input_data.get("tone", "professional")
if not ticket_id:
return {"ok": False, "error": {"code": "MISSING_INPUT", "message": "ticket_id is required"}}
if not message:
return {"ok": False, "error": {"code": "MISSING_INPUT", "message": "message is required"}}
try:
tone_guidelines = {
"professional": "Clear, courteous, and business-appropriate",
"friendly": "Warm, approachable, and conversational",
"formal": "Polished, respectful, and traditional"
}
# Create support agent
support_agent = Agent(
name="Support Specialist",
role="Customer Support Expert",
goal=f"Draft a {tone} reply to customer inquiries",
instructions=f"""
You are an expert customer support specialist.
Tone: {tone} - {tone_guidelines[tone]}
Guidelines:
- Be helpful and empathetic
- Address the customer's concern directly
- Provide clear next steps if applicable
- Keep responses concise but complete
- Never make promises you can't keep
- Acknowledge the customer's frustration if present
""",
)
# Create quality checker
quality_agent = Agent(
name="Quality Reviewer",
role="Support Quality Analyst",
goal="Ensure reply quality and assign confidence",
instructions="""
You are a support quality analyst.
- Check for completeness
- Verify tone consistency
- Ensure no inappropriate content
- Assign confidence score (0-1)
- Flag any concerns
""",
)
# Define tasks
draft_task = Task(
name="draft_reply",
description=f"""
Draft a {tone} reply to this customer message:
Ticket: {ticket_id}
Message: {message}
Provide a professional, helpful response.
""",
expected_output="A well-crafted support reply",
agent=support_agent,
)
review_task = Task(
name="review_reply",
description="""
Review the draft reply for quality.
Assign a confidence score from 0 to 1 where:
- 0.9-1.0: Excellent, ready to send
- 0.7-0.9: Good, minor review recommended
- 0.5-0.7: Acceptable, review before sending
- Below 0.5: Needs revision
Output format: CONFIDENCE: X.XX
""",
expected_output="Confidence score and any notes",
agent=quality_agent,
context=[draft_task],
)
# Execute
agents = AgentTeam(
agents=[support_agent, quality_agent],
tasks=[draft_task, review_task],
)
result = agents.start()
# Parse confidence
review_text = result.get("review_reply", "")
confidence = parse_confidence(review_text)
return {
"ok": True,
"reply": result.get("draft_reply", ""),
"confidence": confidence,
"artifacts": [],
"warnings": [] if confidence >= 0.7 else ["Low confidence - review before sending"],
}
except Exception as e:
return {"ok": False, "error": {"code": "PROCESSING_ERROR", "message": str(e)}}
def parse_confidence(text: str) -> float:
"""Extract confidence score from review text."""
import re
match = re.search(r'CONFIDENCE:\s*([\d.]+)', text, re.IGNORECASE)
if match:
try:
return min(1.0, max(0.0, float(match.group(1))))
except ValueError:
pass
# Default confidence based on text analysis
if "excellent" in text.lower() or "ready" in text.lower():
return 0.9
elif "good" in text.lower():
return 0.8
elif "acceptable" in text.lower():
return 0.6
return 0.7