File size: 5,282 Bytes
64fb1e7 6e978f7 64fb1e7 6e978f7 64fb1e7 6e978f7 64fb1e7 6e978f7 64fb1e7 6e978f7 64fb1e7 6e978f7 64fb1e7 | 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 | import json
import logging
logger = logging.getLogger(__name__)
_client = None
_DEFAULT_COACHING_PROMPT = """You are a Voice AI performance analyst.
You will receive a list of completed call evaluations for a Voice AI agent.
Generate an optimization coaching pack that helps the product team improve the Voice AI's performance.
Be specific — identify recurring patterns across calls:
- Which rubric criteria consistently score low?
- What violations appear most often?
- Where does the AI fail to resolve before escalating?
- What types of calls show the worst satisfaction scores?
Output actionable recommendations for the team: prompt tuning, workflow node adjustments, KB content gaps.
Never be generic. Every recommendation must be grounded in the evaluation data provided.
Return ONLY valid JSON in this exact schema:
{
"overall_trend": "improving" | "declining" | "stable",
"strengths": ["what the Voice AI consistently does well — pattern-based, specific"],
"improvements": ["specific failure pattern with data backing — e.g. 'resolution_rate averages 52% — AI fails to confirm outcomes before ending calls'"],
"action_items": [
{"priority": "high" | "medium" | "low", "action": "specific optimization step for the team", "metric": "what to measure to confirm improvement"},
{"priority": "high" | "medium" | "low", "action": "specific optimization step for the team", "metric": "what to measure to confirm improvement"}
],
"score_summary": {
"avg_overall": 75,
"avg_guardrail": 80,
"avg_resolution": 70,
"avg_containment": 65,
"avg_satisfaction": 0.72,
"pass_rate": 0.67,
"calls_analyzed": 3
},
"coaching_note": "One paragraph analysis for the team: what patterns emerged, what to prioritize in the next iteration of the Voice AI."
}
Temperature 0.4. Ground every finding in the evaluation data provided."""
def _get_coaching_prompt() -> str:
try:
from config_loader import get_config
p = get_config().coaching_prompt
if p:
return p
except Exception:
pass
return _DEFAULT_COACHING_PROMPT
def init_coaching_agent(openai_client):
global _client
_client = openai_client
async def generate_coaching_pack(agent_name: str, eval_records: list[dict]) -> dict:
"""
Generate a coaching pack from >= 3 eval records for an agent.
Returns the parsed coaching pack dict.
"""
if not _client:
logger.warning("Coaching agent not initialized — returning default")
return _default_pack(agent_name, eval_records)
eval_summary = json.dumps(eval_records, indent=2, default=str)
user_content = (
f"Agent: {agent_name}\n"
f"Number of calls evaluated: {len(eval_records)}\n\n"
f"Evaluation records:\n{eval_summary}"
)
try:
response = await _client.chat.completions.create(
model="gpt-4o-mini",
temperature=0.4,
max_tokens=900,
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": _get_coaching_prompt()},
{"role": "user", "content": user_content},
],
)
pack = json.loads(response.choices[0].message.content)
return _normalize_pack(pack, eval_records)
except Exception as e:
logger.error(f"Coaching agent error: {e}")
return _default_pack(agent_name, eval_records)
def _normalize_pack(pack: dict, eval_records: list[dict]) -> dict:
n = len(eval_records)
scores = [r.get("overall_score", 0) for r in eval_records]
pass_count = sum(1 for r in eval_records if r.get("pass_fail") == "PASS")
# Ensure score_summary is populated with real averages
def avg(key):
vals = [r.get(key, 0) or 0 for r in eval_records]
return round(sum(vals) / n, 1) if n else 0
pack["score_summary"] = {
"avg_overall": round(sum(scores) / n, 1) if n else 0,
"avg_guardrail": avg("guardrail_adherence"),
"avg_resolution": avg("resolution_rate"),
"avg_containment": avg("containment"),
"avg_satisfaction": round(avg("caller_satisfaction"), 2),
"pass_rate": round(pass_count / n, 2) if n else 0,
"calls_analyzed": n,
}
# Ensure required list fields exist
pack.setdefault("strengths", [])
pack.setdefault("improvements", [])
pack.setdefault("action_items", [])
pack.setdefault("overall_trend", "stable")
pack.setdefault("coaching_note", "Keep working on consistency across all rubric criteria.")
return pack
def _default_pack(agent_name: str, eval_records: list[dict]) -> dict:
return _normalize_pack({
"overall_trend": "stable",
"strengths": ["Maintained professional tone", "Followed escalation protocol"],
"improvements": ["Improve resolution rate", "Reduce average handle time"],
"action_items": [
{"priority": "high", "action": "Practice refund resolution scripts", "metric": "resolution_rate >= 80"},
{"priority": "medium", "action": "Review KB articles before shifts", "metric": "kb_hit rate"},
],
"coaching_note": f"{agent_name} shows consistent effort. Focus on boosting resolution rate.",
}, eval_records)
|