File size: 2,551 Bytes
369ed63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import boto3
import json
import os
from dotenv import load_dotenv
from models import Priority, Category

load_dotenv()

_bedrock = None


def get_bedrock_client():
    global _bedrock
    if _bedrock is None:
        _bedrock = boto3.client(
            "bedrock-runtime",
            region_name=os.getenv("AWS_REGION", "us-east-1"),
        )
    return _bedrock


def invoke_model(prompt: str) -> str:
    model_id = os.getenv("BEDROCK_MODEL_ID", "amazon.titan-text-express-v1")
    body = json.dumps({
        "inputText": prompt,
        "textGenerationConfig": {
            "maxTokenCount": 512,
            "temperature": 0.3,
            "topP": 0.9,
        },
    })
    try:
        response = get_bedrock_client().invoke_model(modelId=model_id, body=body)
        result = json.loads(response["body"].read())
        return result["results"][0]["outputText"].strip()
    except Exception as e:
        return f"AI service unavailable: {str(e)}"


def classify_ticket(subject: str, description: str) -> dict:
    prompt = f"""Analyze this customer support ticket and respond ONLY with a JSON object.

Subject: {subject}
Description: {description}

Respond with exactly this JSON format (no extra text):
{{
  "category": "<one of: billing, technical, account, shipping, general>",
  "priority": "<one of: low, medium, high, critical>",
  "confidence": <float between 0.0 and 1.0>
}}"""

    raw = invoke_model(prompt)
    try:
        start = raw.find("{")
        end = raw.rfind("}") + 1
        data = json.loads(raw[start:end])
        return {
            "category": Category(data.get("category", "general")),
            "priority": Priority(data.get("priority", "medium")),
            "confidence": float(data.get("confidence", 0.7)),
        }
    except Exception:
        return {"category": Category.general, "priority": Priority.medium, "confidence": 0.5}


def generate_resolution(subject: str, description: str, category: str) -> str:
    prompt = f"""You are a helpful customer support AI. Provide a clear, concise resolution for this ticket.

Category: {category}
Subject: {subject}
Issue: {description}

Write a professional response (2-4 sentences) that directly addresses the customer's issue with actionable steps."""

    return invoke_model(prompt)


def analyze_ticket(subject: str, description: str) -> dict:
    classification = classify_ticket(subject, description)
    resolution = generate_resolution(subject, description, classification["category"])
    return {**classification, "ai_resolution": resolution}