-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
102 lines (75 loc) · 3.02 KB
/
main.py
File metadata and controls
102 lines (75 loc) · 3.02 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
import logging
from typing import Optional
from pathlib import Path
from fastapi import FastAPI, HTTPException, Request
from llm_client import LLMClient
from structured_output import ParsedEmailList, EmailRequest, EmailResponse
# ------------------------
# Logging Configuration
# ------------------------
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
)
logger = logging.getLogger("email-parser-api")
# ------------------------
# FastAPI App
# ------------------------
app = FastAPI(title="Email Parser API")
llm: Optional[LLMClient] = None
@app.on_event("startup")
async def startup_event():
global llm
logger.info("Starting Email Parser API...")
try:
# Initialize LLMClient with proper paths
oci_config_path = Path("app/config/config")
llm = LLMClient(OciPath=oci_config_path)
logger.info("LLMClient initialized successfully")
except Exception as e:
logger.exception("LLM initialization failed – service in maintenance mode")
llm = None
# ------------------------
# Middleware (optional but very useful)
# ------------------------
@app.middleware("http")
async def log_requests(request: Request, call_next):
logger.info(f"Incoming request: {request.method} {request.url.path}")
response = await call_next(request)
logger.info(f"Response status: {response.status_code} for {request.url.path}")
return response
# ------------------------
# Routes
# ------------------------
@app.get("/")
async def root():
return {"message": "Email Parser API is running", "docs": "/docs"}
@app.get("/health")
async def health_check():
if llm is None:
raise HTTPException(status_code=503, detail="Service unavailable - LLM not initialized")
return {"status": "healthy"}
@app.post("/parse-email", response_model=EmailResponse)
async def parse_email(request: EmailRequest):
logger.info("Received /parse-email request")
if not request.email_body.strip():
logger.warning("Empty email_body received")
raise HTTPException(status_code=400, detail="email_body cannot be empty")
if llm is None:
logger.error("LLM unavailable – under maintenance")
raise HTTPException(status_code=503, detail="Service under maintenance")
try:
logger.info("Calling LLMClient.complete()")
response = llm.complete(request)
if response is None:
logger.error("LLM returned None – treating as maintenance")
raise HTTPException(status_code=500, detail="Service under maintenance")
logger.info("Email parsed successfully")
return response
except HTTPException:
# Re-raise FastAPI exceptions as-is
raise
except Exception as e:
# Log full stack trace
logger.exception("Unexpected error while parsing email")
raise HTTPException(status_code=500, detail="Internal server error")