Add retrying when task has no description
Signed-off-by: Daniel Henry <iamdanhenry@gmail.com>
This commit is contained in:
@@ -5,8 +5,7 @@ from app.helpers.clean_email_html import clean_email_html
|
|||||||
from app.helpers.remove_disclaimer import remove_disclaimer
|
from app.helpers.remove_disclaimer import remove_disclaimer
|
||||||
from app.helpers.send_classify_request import send_classify_request
|
from app.helpers.send_classify_request import send_classify_request
|
||||||
from app.models import EmailData
|
from app.models import EmailData
|
||||||
|
import json
|
||||||
logging = True
|
|
||||||
|
|
||||||
class ClassifyRequest(BaseModel):
|
class ClassifyRequest(BaseModel):
|
||||||
email_data: EmailData
|
email_data: EmailData
|
||||||
@@ -14,37 +13,64 @@ class ClassifyRequest(BaseModel):
|
|||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
@router.post("/classify")
|
@router.post("/classify")
|
||||||
async def classify_route(request: ClassifyRequest):
|
async def classify_route(request: ClassifyRequest):
|
||||||
|
|
||||||
if logging:
|
|
||||||
print("Payload Recieved:")
|
|
||||||
print(request.email_data)
|
|
||||||
print("\n\n-----")
|
|
||||||
|
|
||||||
|
|
||||||
email = request.email_data
|
email = request.email_data
|
||||||
|
|
||||||
clean_email = email.copy()
|
clean_email = email.copy()
|
||||||
|
|
||||||
clean_email.subject = email.subject
|
clean_email.subject = email.subject
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
clean_email.body = extract_latest_message(clean_email.body)
|
clean_email.body = extract_latest_message(clean_email.body)
|
||||||
clean_email.body = clean_email_html(clean_email.body)
|
clean_email.body = clean_email_html(clean_email.body)
|
||||||
if logging:
|
|
||||||
print("Cleaned Email Body:")
|
|
||||||
print(clean_email_html(clean_email.body))
|
|
||||||
print("\n\n-----\n\n\n")
|
|
||||||
clean_email.body = remove_disclaimer(clean_email.body)
|
clean_email.body = remove_disclaimer(clean_email.body)
|
||||||
|
|
||||||
if logging:
|
max_retries = 3
|
||||||
print("Cleaned Payload:")
|
attempts = 0
|
||||||
print(clean_email.subject)
|
valid_response = False
|
||||||
print("\n\n-----")
|
response_data = {}
|
||||||
|
|
||||||
response = await send_classify_request(clean_email)
|
response = 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 = False # data.get("task_description")
|
||||||
|
|
||||||
return response
|
# 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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user