Add configurable LLM provider adapters for email classification
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 5m3s

This commit is contained in:
Steve W
2026-04-09 17:36:46 +00:00
parent 9ed41a3dff
commit 7c9d851a9a
9 changed files with 426 additions and 149 deletions

View File

@@ -1,75 +1,11 @@
from fastapi import APIRouter
from pydantic import BaseModel
from app.helpers.extract_latest_message import extract_latest_message
from app.helpers.clean_email_html import clean_email_html
from app.helpers.remove_disclaimer import remove_disclaimer
from app.helpers.send_classify_request import send_classify_request
from app.models import EmailData
import json
class ClassifyRequest(BaseModel):
email_data: EmailData
from app.classifier import classify_email
from app.models import ClassificationResult, ClassifyRequest
router = APIRouter()
@router.post("/classify")
async def classify_route(request: ClassifyRequest):
email = request.email_data
clean_email = email.copy()
clean_email.subject = email.subject
clean_email.body = extract_latest_message(clean_email.body)
clean_email.body = clean_email_html(clean_email.body)
clean_email.body = remove_disclaimer(clean_email.body)
max_retries = 3
attempts = 0
valid_response = False
response_data = {}
# return await send_classify_request(clean_email)
while attempts < max_retries:
# 1. Get the raw string response
raw_response = await send_classify_request(clean_email)
try:
if raw_response is None:
print("Error: Received no response from classifier.")
continue # or handle the error
# 2. Parse the string into a Python dict
data = json.loads(raw_response)
needs_action = data.get("needs_action")
task_description = data.get("task_description")
# 3. Check your "re-do" condition
# Logic: If it needs action but the description is missing/empty, we retry.
if needs_action is True and not task_description:
print(f"Attempt {attempts + 1}: Needs action but description is empty. Retrying...")
attempts += 1
continue
# If we reach here, the response is either (needs_action=False)
# OR (needs_action=True AND has a description).
response_data = data
valid_response = True
break
except json.JSONDecodeError:
print("Response was not valid JSON. Retrying...")
attempts += 1
if not valid_response:
print("Failed to get a valid classification after maximum retries. Sending fallback.")
# Create a safe, default response
response_data = {
"needs_action": False,
"category": "uncategorized",
"priority": "low",
"task_description": "",
"reasoning": "System failed to classify after multiple attempts.",
"confidence": 0.0
}
return response_data
@router.post("/classify", response_model=ClassificationResult)
async def classify_route(request: ClassifyRequest) -> ClassificationResult:
return await classify_email(request)