Spaces:
Paused

nexus / agents /analytics.py
dushyantsingh0
Beta 1.0: Pure Backend Release (Excluding Frontend)
5eb4857
Raw History Blame Contribute Delete
1.68 kB
from typing import Any, Dict, List
from langchain_core.prompts import ChatPromptTemplate
from core.llm import get_llm
from datetime import datetime
ANALYTICS_PROMPT = """
You are a Senior Service Delivery Manager. Analyze the FOLLOWING metrics and recent ticket data (last 48h).
Generate a concise, high-impact "Executive Health Narrative" for the workspace lead.
Current Metrics:
- SLA Compliance: {sla_rate}%
- Breached Count: {breaches}
- Analyzed Tickets: {ticket_count}
Ticket Data:
{ticket_data}
REPORT RULES:
1. MAXIMUM 4-5 LINES total.
2. Structure: One sentence on Overall Health (Green/Amber/Red), one on the primary volume/breach driver, and one on a specific tactical recommendation.
3. Use bolding to highlight critical metrics or actions. Professional Markdown format.
"""
async def generate_service_insights(metrics: Dict[str, Any], tickets: List[Dict[str, Any]]) -> str:
"""
Generates an executive summary of service performance using LLM analytics.
"""
llm = get_llm()
# Format ticket data for analysis (Privacy-Safe: No Titles)
formatted_data = "\n".join([
f"- [{t['priority']}] Category: {t.get('category' or 'General')} (Active for {t['duration_mins']:.1f}m)"
for t in tickets
])
prompt = ChatPromptTemplate.from_template(ANALYTICS_PROMPT)
chain = prompt | llm
response = await chain.ainvoke({
"sla_rate": round(metrics.get("sla_compliance_rate", 0) * 100, 1),
"breaches": metrics.get("breached_tickets", 0),
"ticket_count": len(tickets),
"ticket_data": formatted_data or "No recent ticket data available."
})
return response.content