-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
214 lines (164 loc) · 6.05 KB
/
Copy pathmain.py
File metadata and controls
214 lines (164 loc) · 6.05 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import logging
import time
from datetime import datetime
from enum import Enum
from logging.config import dictConfig
from typing import List
import instructor
from config import Settings
from fastapi import Depends, FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from openai import OpenAI
from prometheus_fastapi_instrumentator import Instrumentator
from pydantic import BaseModel, Field
from security import authenticate_api_key
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.errors import RateLimitExceeded
from slowapi.util import get_remote_address
from starlette.middleware.base import BaseHTTPMiddleware
dictConfig(
{
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"default": {
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "default",
},
},
"root": {
"level": "INFO",
"handlers": ["console"],
},
}
)
logger = logging.getLogger(__name__)
settings = Settings()
client = instructor.patch(OpenAI(api_key=settings.OPENAI_API_KEY))
app = FastAPI(title="Ticket Classification API", version="1.0.0")
# Adding CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
limiter = Limiter(key_func=get_remote_address)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
class TimingMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
response.headers["X-Process-Time"] = str(process_time)
return response
app.add_middleware(TimingMiddleware)
# Initialize metrics
Instrumentator().instrument(app).expose(app)
# Enum for ticket types
class TicketCategory(str, Enum):
ORDER_ISSUE = "order_issue"
ACCOUNT_ACCESS = "account_access"
PRODUCT_INQUIRY = "product_inquiry"
TECHNICAL_SUPPORT = "technical_support"
BILLING = "billing"
OTHER = "other"
class CustomerSentiment(str, Enum):
ANGRY = "angry"
FRUSTRATED = "frustrated"
NEUTRAL = "neutral"
SATISFIED = "satisfied"
class TicketUrgency(str, Enum):
LOW = "low"
MEDIUM = "medium"
HIGH = "high"
CRITICAL = "critical"
class TicketClassification(BaseModel):
category: TicketCategory
urgency: TicketUrgency
sentiment: CustomerSentiment
confidence: float = Field(ge=0, le=1)
key_information: List[str]
suggested_action: str
class TicketRequest(BaseModel):
text: str = Field(..., min_length=10, max_length=1000)
# Classification logic
SYSTEM_PROMPT = """
You are an AI assistant for a large e-commerce platform's customer support team.
Your role is to analyze incoming customer support tickets and provide structured information to help our team respond quickly and effectively.
Business Context:
- We handle thousands of tickets daily across various categories (orders, accounts, products, technical issues, billing).
- Quick and accurate classification is crucial for customer satisfaction and operational efficiency.
- We prioritize based on urgency and customer sentiment.
Your tasks:
1. Categorize the ticket into the most appropriate category.
2. Assess the urgency of the issue (low, medium, high, critical).
3. Determine the customer's sentiment.
4. Extract key information that would be helpful for our support team.
5. Suggest an initial action for handling the ticket.
6. Provide a confidence score for your classification.
Remember:
- Be objective and base your analysis solely on the information provided in the ticket.
- If you're unsure about any aspect, reflect that in your confidence score.
- For 'key_information', extract specific details like order numbers, product names, or account issues.
- The 'suggested_action' should be a brief, actionable step for our support team.
Analyze the following customer support ticket and provide the requested information in the specified format.
"""
def classify_ticket(ticket_text: str) -> TicketClassification:
try:
response = client.chat.completions.create(
model="gpt-4o-mini",
response_model=TicketClassification,
temperature=0,
max_retries=3,
messages=[
{
"role": "system",
"content": SYSTEM_PROMPT,
},
{"role": "user", "content": ticket_text},
],
)
return response
except Exception as e:
logger.error(f"Error classifying ticket: {str(e)}")
raise HTTPException(status_code=500, detail="Error classifying ticket")
# API ENDPOINTS
@app.post("/classify_ticket", response_model=TicketClassification)
@limiter.limit("10/minute")
async def api_classify_ticket(
request: Request,
ticket: TicketRequest,
api_key: str = Depends(authenticate_api_key),
):
result = classify_ticket(ticket.text)
logger.info(f"Ticket classified: {result.category}")
return result
@app.get("/health")
async def health():
return {"status": "healthy", "timestamp": datetime.now().isoformat()}
# Error handling
app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.detail},
)
@app.exception_handler(Exception)
async def general_exception_handler(request: Request, exc: Exception):
logger.error(f"Unhandled exception: {str(exc)}")
return JSONResponse(
status_code=500,
content={"message": "An unexpected error occurred"},
)
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)