Update flask_Character.py
Browse files- flask_Character.py +102 -193
flask_Character.py
CHANGED
|
@@ -11,10 +11,9 @@ from datetime import date, datetime, timedelta
|
|
| 11 |
from typing import List, Optional, Literal, Dict, Any, Tuple
|
| 12 |
import traceback
|
| 13 |
import asyncio
|
| 14 |
-
from uuid import uuid4, UUID
|
| 15 |
|
| 16 |
from fastapi import FastAPI, HTTPException, Response, Query, Depends, status
|
| 17 |
-
from fastapi.responses import FileResponse
|
| 18 |
from fastapi.exception_handlers import http_exception_handler
|
| 19 |
from starlette.exceptions import HTTPException as StarletteHTTPException
|
| 20 |
|
|
@@ -22,7 +21,7 @@ from langchain.prompts import PromptTemplate
|
|
| 22 |
from langchain_groq import ChatGroq
|
| 23 |
from pydantic import BaseModel, Field, BeforeValidator, model_serializer
|
| 24 |
from typing_extensions import Annotated
|
| 25 |
-
from pydantic_core import core_schema
|
| 26 |
|
| 27 |
from pymongo import MongoClient
|
| 28 |
from pymongo.errors import ConnectionFailure, OperationFailure
|
|
@@ -33,11 +32,9 @@ MAX_BATCH_SIZE = 20
|
|
| 33 |
BATCH_INTERVAL_SECONDS = 1.0
|
| 34 |
|
| 35 |
# --- Queues and pending request stores for batching ---
|
| 36 |
-
extract_data_queue: Optional[asyncio.Queue] = None
|
| 37 |
-
generate_reply_queue: Optional[asyncio.Queue] = None
|
| 38 |
|
| 39 |
-
# Dictionaries to store futures for pending requests, keyed by unique request ID
|
| 40 |
-
# The value will be an asyncio.Future that the endpoint handler will await
|
| 41 |
extract_pending_requests: Dict[UUID, asyncio.Future] = {}
|
| 42 |
generate_pending_requests: Dict[UUID, asyncio.Future] = {}
|
| 43 |
|
|
@@ -46,7 +43,7 @@ shutdown_event = asyncio.Event()
|
|
| 46 |
|
| 47 |
|
| 48 |
# --- MongoDB Configuration ---
|
| 49 |
-
MONGO_URI = "mongodb+srv://precison9:P1LhtFknkT75yg5L@cluster0.isuwpef.mongodb.net"
|
| 50 |
DB_NAME = "email_assistant_db"
|
| 51 |
EXTRACTED_EMAILS_COLLECTION = "extracted_emails"
|
| 52 |
GENERATED_REPLIES_COLLECTION = "generated_replies"
|
|
@@ -167,24 +164,23 @@ class GenerateReplyResponse(BaseModel):
|
|
| 167 |
stored_id: str = Field(..., description="The MongoDB ID of the stored reply.")
|
| 168 |
cached: bool = Field(..., description="True if the reply was retrieved from cache, False if newly generated.")
|
| 169 |
|
| 170 |
-
# --- Query Models for GET Endpoints ---
|
| 171 |
class ExtractedEmailQuery(BaseModel):
|
| 172 |
-
contact_name: Optional[str] = Query(None)
|
| 173 |
-
appointment_title: Optional[str] = Query(None)
|
| 174 |
-
task_title: Optional[str] = Query(None)
|
| 175 |
-
from_date: Optional[date] = Query(None)
|
| 176 |
-
to_date: Optional[date] = Query(None)
|
| 177 |
limit: int = Query(10, ge=1, le=100)
|
| 178 |
|
| 179 |
class GeneratedReplyQuery(BaseModel):
|
| 180 |
-
language: Optional[Literal["Italian", "English"]] = Query(None)
|
| 181 |
-
style: Optional[str] = Query(None)
|
| 182 |
-
tone: Optional[str] = Query(None)
|
| 183 |
-
from_date: Optional[date] = Query(None)
|
| 184 |
-
to_date: Optional[date] = Query(None)
|
| 185 |
limit: int = Query(10, ge=1, le=100)
|
| 186 |
|
| 187 |
-
#
|
| 188 |
def extract_last_json_block(text: str) -> Optional[str]:
|
| 189 |
pattern = r'```json\s*(.*?)\s*```'
|
| 190 |
matches = re.findall(pattern, text, re.DOTALL)
|
|
@@ -212,17 +208,17 @@ def normalize_llm_output(data: dict, current_date: date, original_email_text: st
|
|
| 212 |
tasks_data = [Task(task_title=t.get("task_title", "Untitled"), task_description=t.get("task_description", "No description"), due_date=parse_date(t.get("due_date"), current_date) or current_date) for t in data.get("tasks", [])]
|
| 213 |
return ExtractedData(contacts=contacts_data, appointments=appointments_data, tasks=tasks_data, original_email_text=original_email_text)
|
| 214 |
|
| 215 |
-
#
|
| 216 |
def _process_email_internal(email_text: str, api_key: str, current_date: date) -> ExtractedData:
|
| 217 |
if not email_text: raise ValueError("Email text cannot be empty for processing.")
|
| 218 |
llm = ChatGroq(model="meta-llama/llama-4-scout-17b-16e-instruct", temperature=0, max_tokens=2000, groq_api_key=api_key)
|
| 219 |
prompt_today_str = current_date.isoformat()
|
| 220 |
prompt_tomorrow_str = (current_date + timedelta(days=1)).isoformat()
|
| 221 |
prompt_template_str = f"""You are an expert email assistant tasked with extracting structured information from an Italian email.**Your response MUST be a single, complete JSON object, wrapped in a ```json``` block.****DO NOT include any conversational text, explanations, or preambles outside the JSON block.****The JSON should contain three top-level keys: "contacts", "appointments", and "tasks".**If a category has no items, its list should be empty (e.g., "contacts": []).Here is the required JSON schema for each category:- **contacts**: List of Contact objects. Each Contact object must have: - `name` (string, full name) - `last_name` (string, last name) - You should infer this from the full name. - `email` (string, optional, null if not present) - `phone_number` (string, optional, null if not present)- **appointments**: List of Appointment objects. Each Appointment object must have: - `title` (string, short, meaningful title in Italian based on the meeting's purpose) - `description` (string, summary of the meeting's goal) - `start_date` (string, YYYY-MM-DD. If not explicitly mentioned, use "{prompt_today_str}" for "today", or "{prompt_tomorrow_str}" for "tomorrow") - `start_time` (string, optional, e.g., "10:30 AM", null if not present) - `end_date` (string, YYYY-MM-DD, optional, null if unknown or not applicable) - `end_time` (string, optional, e.g., "11:00 AM", null if not present)- **tasks**: List of Task objects. Each Task object must have: - `task_title` (string, short summary of action item) - `task_description` (string, more detailed explanation) - `due_date` (string, YYYY-MM-DD. Infer from context, e.g., "entro domani" becomes "{prompt_tomorrow_str}", "today" becomes "{prompt_today_str}")---Email:{{email}}"""
|
| 222 |
-
prompt_template = PromptTemplate(input_variables=["email"], template=prompt_template_str)
|
| 223 |
chain = prompt_template | llm
|
| 224 |
try:
|
| 225 |
-
llm_output = chain.invoke({"email": email_text})
|
| 226 |
llm_output_str = llm_output.content
|
| 227 |
json_str = extract_last_json_block(llm_output_str)
|
| 228 |
if not json_str: raise ValueError(f"No JSON block found in LLM output. LLM response: {llm_output_str}")
|
|
@@ -243,7 +239,10 @@ def _generate_response_internal(email_text: str, api_key: str, language: Literal
|
|
| 243 |
except Exception as e: traceback.print_exc(); raise
|
| 244 |
|
| 245 |
# --- FastAPI Application ---
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
# --- Exception Handlers ---
|
| 249 |
@app.exception_handler(StarletteHTTPException)
|
|
@@ -255,130 +254,74 @@ async def global_exception_handler_wrapper(request, exc):
|
|
| 255 |
traceback.print_exc()
|
| 256 |
return Response(content=json.dumps({"detail": f"Internal Server Error: {str(exc)}", "type": "unhandled_exception"}), status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, media_type="application/json")
|
| 257 |
|
| 258 |
-
# --- Batch Worker Functions ---
|
| 259 |
async def _execute_single_extract_task(request_item: ProcessEmailRequest, request_id: UUID) -> Tuple[UUID, Any]:
|
| 260 |
-
"""Helper to run a single extract task and return its result or exception."""
|
| 261 |
current_date = date.today()
|
| 262 |
try:
|
| 263 |
-
|
| 264 |
-
result = await asyncio.to_thread(
|
| 265 |
-
_process_email_internal, request_item.email_text, request_item.groq_api_key, current_date
|
| 266 |
-
)
|
| 267 |
return request_id, result
|
| 268 |
except Exception as e:
|
| 269 |
return request_id, e
|
| 270 |
|
| 271 |
-
async def extract_data_batch_worker():
|
| 272 |
-
global extract_data_queue, extract_pending_requests, shutdown_event
|
| 273 |
-
print(f"[{datetime.now()}] Extract Data Batch Worker started.")
|
| 274 |
-
while not shutdown_event.is_set():
|
| 275 |
-
try:
|
| 276 |
-
await asyncio.wait_for(asyncio.sleep(BATCH_INTERVAL_SECONDS), timeout=BATCH_INTERVAL_SECONDS + 0.1) # Ensures it runs roughly every interval
|
| 277 |
-
except asyncio.TimeoutError: # woken up by shutdown
|
| 278 |
-
if shutdown_event.is_set(): break
|
| 279 |
-
|
| 280 |
-
batch_to_process: List[Tuple[ProcessEmailRequest, UUID]] = []
|
| 281 |
-
while len(batch_to_process) < MAX_BATCH_SIZE:
|
| 282 |
-
try:
|
| 283 |
-
request_obj, req_id = extract_data_queue.get_nowait()
|
| 284 |
-
batch_to_process.append((request_obj, req_id))
|
| 285 |
-
except asyncio.QueueEmpty:
|
| 286 |
-
break # No more items for this batch
|
| 287 |
-
|
| 288 |
-
if not batch_to_process:
|
| 289 |
-
continue
|
| 290 |
-
|
| 291 |
-
print(f"[{datetime.now()}] Extract Worker: Processing batch of {len(batch_to_process)} requests.")
|
| 292 |
-
|
| 293 |
-
# Concurrently execute all LLM calls for the current batch
|
| 294 |
-
llm_tasks = [_execute_single_extract_task(req_obj, req_id) for req_obj, req_id in batch_to_process]
|
| 295 |
-
results = await asyncio.gather(*llm_tasks) # Results are (request_id, result_or_exception)
|
| 296 |
-
|
| 297 |
-
for req_id, result_or_exc in results:
|
| 298 |
-
future = extract_pending_requests.pop(req_id, None) # Get and remove future
|
| 299 |
-
if future and not future.done():
|
| 300 |
-
if isinstance(result_or_exc, Exception):
|
| 301 |
-
future.set_exception(result_or_exc)
|
| 302 |
-
else:
|
| 303 |
-
future.set_result(result_or_exc) # This is ExtractedData object (pre-DB)
|
| 304 |
-
elif future and future.done():
|
| 305 |
-
print(f"[{datetime.now()}] Extract Worker: Future for {req_id} was already done (e.g. timed out).")
|
| 306 |
-
|
| 307 |
-
print(f"[{datetime.now()}] Extract Data Batch Worker shutting down.")
|
| 308 |
-
# Clear out any remaining requests in the queue by setting exception
|
| 309 |
-
while not extract_data_queue.empty():
|
| 310 |
-
try:
|
| 311 |
-
_, req_id = extract_data_queue.get_nowait()
|
| 312 |
-
future = extract_pending_requests.pop(req_id, None)
|
| 313 |
-
if future and not future.done():
|
| 314 |
-
future.set_exception(HTTPException(status_code=503, detail="Service shutting down, request cancelled."))
|
| 315 |
-
except asyncio.QueueEmpty:
|
| 316 |
-
break
|
| 317 |
-
|
| 318 |
-
|
| 319 |
async def _execute_single_generate_reply_task(request_item: GenerateReplyRequest, request_id: UUID) -> Tuple[UUID, Any]:
|
| 320 |
-
"""Helper to run a single generate reply task and return its result or exception."""
|
| 321 |
try:
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
_generate_response_internal,
|
| 325 |
-
request_item.email_text, request_item.groq_api_key, request_item.language,
|
| 326 |
-
request_item.length, request_item.style, request_item.tone, request_item.emoji
|
| 327 |
-
)
|
| 328 |
-
return request_id, result # This is the reply string
|
| 329 |
except Exception as e:
|
| 330 |
return request_id, e
|
| 331 |
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
while not shutdown_event.is_set():
|
| 336 |
try:
|
| 337 |
-
await asyncio.wait_for(
|
| 338 |
-
except asyncio.TimeoutError:
|
| 339 |
if shutdown_event.is_set(): break
|
|
|
|
|
|
|
| 340 |
|
| 341 |
-
batch_to_process
|
| 342 |
-
while len(batch_to_process) < MAX_BATCH_SIZE:
|
| 343 |
try:
|
| 344 |
-
|
| 345 |
-
batch_to_process.append((request_obj, req_id))
|
| 346 |
except asyncio.QueueEmpty:
|
| 347 |
break
|
| 348 |
-
|
| 349 |
if not batch_to_process:
|
| 350 |
continue
|
| 351 |
|
| 352 |
-
print(f"[{datetime.now()}]
|
| 353 |
|
| 354 |
-
|
| 355 |
-
results = await asyncio.gather(*
|
| 356 |
|
| 357 |
for req_id, result_or_exc in results:
|
| 358 |
-
future =
|
| 359 |
if future and not future.done():
|
| 360 |
if isinstance(result_or_exc, Exception):
|
| 361 |
future.set_exception(result_or_exc)
|
| 362 |
else:
|
| 363 |
-
future.set_result(result_or_exc)
|
| 364 |
-
elif future and future.done():
|
| 365 |
-
print(f"[{datetime.now()}] Reply Worker: Future for {req_id} was already done (e.g. timed out).")
|
| 366 |
|
| 367 |
-
print(f"[{datetime.now()}]
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
future.set_exception(HTTPException(status_code=503, detail="Service shutting down, request cancelled."))
|
| 374 |
-
except asyncio.QueueEmpty:
|
| 375 |
-
break
|
| 376 |
|
| 377 |
# --- FastAPI Event Handlers ---
|
|
|
|
|
|
|
| 378 |
@app.on_event("startup")
|
| 379 |
async def startup_event():
|
| 380 |
global client, db, extracted_emails_collection, generated_replies_collection
|
| 381 |
-
global extract_data_queue, generate_reply_queue
|
| 382 |
|
| 383 |
print(f"[{datetime.now()}] FastAPI app startup sequence initiated.")
|
| 384 |
try:
|
|
@@ -389,43 +332,42 @@ async def startup_event():
|
|
| 389 |
generated_replies_collection = db[GENERATED_REPLIES_COLLECTION]
|
| 390 |
print(f"[{datetime.now()}] Successfully connected to MongoDB: {DB_NAME}")
|
| 391 |
|
| 392 |
-
# Initialize queues and start worker tasks
|
| 393 |
extract_data_queue = asyncio.Queue()
|
| 394 |
generate_reply_queue = asyncio.Queue()
|
| 395 |
-
|
| 396 |
-
asyncio.create_task(
|
|
|
|
|
|
|
| 397 |
print(f"[{datetime.now()}] Batch processing workers started.")
|
| 398 |
|
| 399 |
except (ConnectionFailure, OperationFailure) as e:
|
| 400 |
print(f"[{datetime.now()}] ERROR: MongoDB Connection/Operation Failure: {e}")
|
| 401 |
-
# Critical error, prevent app from fully starting or indicate non-operational state
|
| 402 |
-
# For simplicity, we'll let it run but endpoints relying on DB will fail
|
| 403 |
client = db = extracted_emails_collection = generated_replies_collection = None
|
| 404 |
except Exception as e:
|
| 405 |
print(f"[{datetime.now()}] ERROR: An unexpected error during startup: {e}")
|
| 406 |
traceback.print_exc()
|
| 407 |
client = db = extracted_emails_collection = generated_replies_collection = None
|
| 408 |
finally:
|
| 409 |
-
|
| 410 |
-
if not (client and db and extracted_emails_collection and generated_replies_collection):
|
| 411 |
-
print(f"[{datetime.now()}] MongoDB
|
| 412 |
print(f"[{datetime.now()}] FastAPI app startup sequence completed.")
|
| 413 |
|
| 414 |
|
| 415 |
@app.on_event("shutdown")
|
| 416 |
-
async def shutdown_event_handler():
|
| 417 |
-
global client, shutdown_event
|
| 418 |
print(f"[{datetime.now()}] FastAPI app shutting down.")
|
| 419 |
|
| 420 |
-
|
| 421 |
-
|
| 422 |
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
|
| 428 |
-
if client:
|
| 429 |
client.close()
|
| 430 |
print(f"[{datetime.now()}] MongoDB client closed.")
|
| 431 |
print(f"[{datetime.now()}] FastAPI app shutdown sequence completed.")
|
|
@@ -435,52 +377,42 @@ async def shutdown_event_handler(): # Renamed to avoid conflict with global shut
|
|
| 435 |
async def health_check():
|
| 436 |
db_status = "MongoDB not connected."
|
| 437 |
db_ok = False
|
| 438 |
-
|
|
|
|
| 439 |
try:
|
| 440 |
-
await asyncio.to_thread(
|
| 441 |
db_status = "MongoDB connection OK."
|
| 442 |
db_ok = True
|
| 443 |
except Exception as e:
|
| 444 |
db_status = f"MongoDB connection error: {e}"
|
| 445 |
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
|
| 451 |
if db_ok:
|
| 452 |
-
return {"status": "ok", "message": "
|
| 453 |
else:
|
| 454 |
-
raise HTTPException(status_code=503, detail={"message": "Service unavailable.", "database": db_status
|
| 455 |
|
| 456 |
@app.post("/extract-data", response_model=ExtractedData, summary="Extract structured data (batched)")
|
| 457 |
async def extract_email_data(request: ProcessEmailRequest):
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
if not extracted_emails_collection or not extract_data_queue:
|
| 461 |
raise HTTPException(status_code=503, detail="Service not available (DB or batch queue).")
|
| 462 |
|
| 463 |
request_id = uuid4()
|
| 464 |
-
future = asyncio.get_event_loop().create_future()
|
| 465 |
extract_pending_requests[request_id] = future
|
| 466 |
|
| 467 |
try:
|
| 468 |
await extract_data_queue.put((request, request_id))
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
# Wait for the future to be resolved by the worker, with a timeout
|
| 472 |
-
try:
|
| 473 |
-
# Timeout should be configurable, longer than batch interval + processing time
|
| 474 |
-
extracted_data_obj = await asyncio.wait_for(future, timeout=60.0)
|
| 475 |
-
except asyncio.TimeoutError:
|
| 476 |
-
print(f"[{datetime.now()}] /extract-data: Request {request_id} timed out waiting for worker.")
|
| 477 |
-
# The future might still be in extract_pending_requests if worker hasn't processed it
|
| 478 |
-
# Worker will try to pop it; if already popped here, it's fine.
|
| 479 |
-
extract_pending_requests.pop(request_id, None) # Clean up if timed out
|
| 480 |
-
raise HTTPException(status_code=504, detail="Request timed out while awaiting processing in batch.")
|
| 481 |
-
|
| 482 |
-
# If here, extracted_data_obj is the ExtractedData model instance from the worker
|
| 483 |
-
print(f"[{datetime.now()}] /extract-data: Worker processed {request_id}. Inserting to DB.")
|
| 484 |
|
| 485 |
data_to_insert = extracted_data_obj.model_dump(by_alias=True, exclude_none=True, exclude={'id'})
|
| 486 |
if 'appointments' in data_to_insert:
|
|
@@ -492,55 +424,35 @@ async def extract_email_data(request: ProcessEmailRequest):
|
|
| 492 |
if isinstance(task_item.get('due_date'), date): task_item['due_date'] = datetime.combine(task_item['due_date'], datetime.min.time())
|
| 493 |
|
| 494 |
insert_result = await asyncio.to_thread(extracted_emails_collection.insert_one, data_to_insert)
|
| 495 |
-
extracted_data_obj.id = str(insert_result.inserted_id)
|
| 496 |
|
| 497 |
return extracted_data_obj
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
raise
|
| 503 |
-
except Exception as e: # Any other exception from the future or this handler
|
| 504 |
-
traceback.print_exc()
|
| 505 |
-
raise HTTPException(status_code=500, detail=f"Internal server error during extract data: {e}")
|
| 506 |
finally:
|
| 507 |
-
# Ensure future is removed if it wasn't already (e.g. successful completion)
|
| 508 |
extract_pending_requests.pop(request_id, None)
|
| 509 |
|
| 510 |
-
|
| 511 |
@app.post("/generate-reply", response_model=GenerateReplyResponse, summary="Generate smart reply (batched)")
|
| 512 |
async def generate_email_reply(request: GenerateReplyRequest):
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
if not generated_replies_collection or not generate_reply_queue:
|
| 516 |
raise HTTPException(status_code=503, detail="Service not available (DB or batch queue).")
|
| 517 |
|
| 518 |
-
# --- Check cache first (remains outside batching) ---
|
| 519 |
cache_query = {"original_email_text": request.email_text, "language": request.language, "length": request.length, "style": request.style, "tone": request.tone, "emoji": request.emoji}
|
| 520 |
cached_reply_doc = await asyncio.to_thread(generated_replies_collection.find_one, cache_query)
|
| 521 |
if cached_reply_doc:
|
| 522 |
-
print(f"[{datetime.now()}] /generate-reply: Reply found in cache. ID: {str(cached_reply_doc['_id'])}")
|
| 523 |
return GenerateReplyResponse(reply=cached_reply_doc["generated_reply_text"], stored_id=str(cached_reply_doc["_id"]), cached=True)
|
| 524 |
|
| 525 |
-
# --- If not cached, queue for generation ---
|
| 526 |
request_id = uuid4()
|
| 527 |
-
future = asyncio.get_event_loop().create_future()
|
| 528 |
generate_pending_requests[request_id] = future
|
| 529 |
|
| 530 |
try:
|
| 531 |
await generate_reply_queue.put((request, request_id))
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
try:
|
| 535 |
-
# Timeout should be configurable
|
| 536 |
-
reply_content_str = await asyncio.wait_for(future, timeout=60.0)
|
| 537 |
-
except asyncio.TimeoutError:
|
| 538 |
-
print(f"[{datetime.now()}] /generate-reply: Request {request_id} timed out waiting for worker.")
|
| 539 |
-
generate_pending_requests.pop(request_id, None)
|
| 540 |
-
raise HTTPException(status_code=504, detail="Request timed out while awaiting reply generation in batch.")
|
| 541 |
-
|
| 542 |
-
# If here, reply_content_str is the generated string from the worker
|
| 543 |
-
print(f"[{datetime.now()}] /generate-reply: Worker generated reply for {request_id}. Storing to DB.")
|
| 544 |
|
| 545 |
reply_data_to_store = GeneratedReplyData(
|
| 546 |
original_email_text=request.email_text, generated_reply_text=reply_content_str,
|
|
@@ -553,16 +465,13 @@ async def generate_email_reply(request: GenerateReplyRequest):
|
|
| 553 |
stored_id = str(insert_result.inserted_id)
|
| 554 |
|
| 555 |
return GenerateReplyResponse(reply=reply_content_str, stored_id=stored_id, cached=False)
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
raise
|
| 559 |
except Exception as e:
|
| 560 |
-
|
| 561 |
-
raise HTTPException(status_code=500, detail=f"Internal server error during generate reply: {e}")
|
| 562 |
finally:
|
| 563 |
generate_pending_requests.pop(request_id, None)
|
| 564 |
|
| 565 |
-
|
| 566 |
@app.get("/query-extracted-emails", response_model=List[ExtractedData], summary="Query stored extracted email data")
|
| 567 |
async def query_extracted_emails(query_params: ExtractedEmailQuery = Depends()):
|
| 568 |
if extracted_emails_collection is None: raise HTTPException(status_code=503, detail="MongoDB not available.")
|
|
|
|
| 11 |
from typing import List, Optional, Literal, Dict, Any, Tuple
|
| 12 |
import traceback
|
| 13 |
import asyncio
|
| 14 |
+
from uuid import uuid4, UUID
|
| 15 |
|
| 16 |
from fastapi import FastAPI, HTTPException, Response, Query, Depends, status
|
|
|
|
| 17 |
from fastapi.exception_handlers import http_exception_handler
|
| 18 |
from starlette.exceptions import HTTPException as StarletteHTTPException
|
| 19 |
|
|
|
|
| 21 |
from langchain_groq import ChatGroq
|
| 22 |
from pydantic import BaseModel, Field, BeforeValidator, model_serializer
|
| 23 |
from typing_extensions import Annotated
|
| 24 |
+
from pydantic_core import core_schema
|
| 25 |
|
| 26 |
from pymongo import MongoClient
|
| 27 |
from pymongo.errors import ConnectionFailure, OperationFailure
|
|
|
|
| 32 |
BATCH_INTERVAL_SECONDS = 1.0
|
| 33 |
|
| 34 |
# --- Queues and pending request stores for batching ---
|
| 35 |
+
extract_data_queue: Optional[asyncio.Queue[Tuple[Any, UUID]]] = None
|
| 36 |
+
generate_reply_queue: Optional[asyncio.Queue[Tuple[Any, UUID]]] = None
|
| 37 |
|
|
|
|
|
|
|
| 38 |
extract_pending_requests: Dict[UUID, asyncio.Future] = {}
|
| 39 |
generate_pending_requests: Dict[UUID, asyncio.Future] = {}
|
| 40 |
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
# --- MongoDB Configuration ---
|
| 46 |
+
MONGO_URI = "mongodb+srv://precison9:P1LhtFknkT75yg5L@cluster0.isuwpef.mongodb.net"
|
| 47 |
DB_NAME = "email_assistant_db"
|
| 48 |
EXTRACTED_EMAILS_COLLECTION = "extracted_emails"
|
| 49 |
GENERATED_REPLIES_COLLECTION = "generated_replies"
|
|
|
|
| 164 |
stored_id: str = Field(..., description="The MongoDB ID of the stored reply.")
|
| 165 |
cached: bool = Field(..., description="True if the reply was retrieved from cache, False if newly generated.")
|
| 166 |
|
|
|
|
| 167 |
class ExtractedEmailQuery(BaseModel):
|
| 168 |
+
contact_name: Optional[str] = Query(None, description="Filter by contact name.")
|
| 169 |
+
appointment_title: Optional[str] = Query(None, description="Filter by appointment title.")
|
| 170 |
+
task_title: Optional[str] = Query(None, description="Filter by task title.")
|
| 171 |
+
from_date: Optional[date] = Query(None, description="Filter by processed date (start).")
|
| 172 |
+
to_date: Optional[date] = Query(None, description="Filter by processed date (end).")
|
| 173 |
limit: int = Query(10, ge=1, le=100)
|
| 174 |
|
| 175 |
class GeneratedReplyQuery(BaseModel):
|
| 176 |
+
language: Optional[Literal["Italian", "English"]] = Query(None, description="Filter by language.")
|
| 177 |
+
style: Optional[str] = Query(None, description="Filter by style.")
|
| 178 |
+
tone: Optional[str] = Query(None, description="Filter by tone.")
|
| 179 |
+
from_date: Optional[date] = Query(None, description="Filter by generation date (start).")
|
| 180 |
+
to_date: Optional[date] = Query(None, description="Filter by generation date (end).")
|
| 181 |
limit: int = Query(10, ge=1, le=100)
|
| 182 |
|
| 183 |
+
# --- Utility Functions ---
|
| 184 |
def extract_last_json_block(text: str) -> Optional[str]:
|
| 185 |
pattern = r'```json\s*(.*?)\s*```'
|
| 186 |
matches = re.findall(pattern, text, re.DOTALL)
|
|
|
|
| 208 |
tasks_data = [Task(task_title=t.get("task_title", "Untitled"), task_description=t.get("task_description", "No description"), due_date=parse_date(t.get("due_date"), current_date) or current_date) for t in data.get("tasks", [])]
|
| 209 |
return ExtractedData(contacts=contacts_data, appointments=appointments_data, tasks=tasks_data, original_email_text=original_email_text)
|
| 210 |
|
| 211 |
+
# --- Core Logic (Internal Functions) ---
|
| 212 |
def _process_email_internal(email_text: str, api_key: str, current_date: date) -> ExtractedData:
|
| 213 |
if not email_text: raise ValueError("Email text cannot be empty for processing.")
|
| 214 |
llm = ChatGroq(model="meta-llama/llama-4-scout-17b-16e-instruct", temperature=0, max_tokens=2000, groq_api_key=api_key)
|
| 215 |
prompt_today_str = current_date.isoformat()
|
| 216 |
prompt_tomorrow_str = (current_date + timedelta(days=1)).isoformat()
|
| 217 |
prompt_template_str = f"""You are an expert email assistant tasked with extracting structured information from an Italian email.**Your response MUST be a single, complete JSON object, wrapped in a ```json``` block.****DO NOT include any conversational text, explanations, or preambles outside the JSON block.****The JSON should contain three top-level keys: "contacts", "appointments", and "tasks".**If a category has no items, its list should be empty (e.g., "contacts": []).Here is the required JSON schema for each category:- **contacts**: List of Contact objects. Each Contact object must have: - `name` (string, full name) - `last_name` (string, last name) - You should infer this from the full name. - `email` (string, optional, null if not present) - `phone_number` (string, optional, null if not present)- **appointments**: List of Appointment objects. Each Appointment object must have: - `title` (string, short, meaningful title in Italian based on the meeting's purpose) - `description` (string, summary of the meeting's goal) - `start_date` (string, YYYY-MM-DD. If not explicitly mentioned, use "{prompt_today_str}" for "today", or "{prompt_tomorrow_str}" for "tomorrow") - `start_time` (string, optional, e.g., "10:30 AM", null if not present) - `end_date` (string, YYYY-MM-DD, optional, null if unknown or not applicable) - `end_time` (string, optional, e.g., "11:00 AM", null if not present)- **tasks**: List of Task objects. Each Task object must have: - `task_title` (string, short summary of action item) - `task_description` (string, more detailed explanation) - `due_date` (string, YYYY-MM-DD. Infer from context, e.g., "entro domani" becomes "{prompt_tomorrow_str}", "today" becomes "{prompt_today_str}")---Email:{{email}}"""
|
| 218 |
+
prompt_template = PromptTemplate(input_variables=["email"], template=prompt_template_str)
|
| 219 |
chain = prompt_template | llm
|
| 220 |
try:
|
| 221 |
+
llm_output = chain.invoke({"email": email_text})
|
| 222 |
llm_output_str = llm_output.content
|
| 223 |
json_str = extract_last_json_block(llm_output_str)
|
| 224 |
if not json_str: raise ValueError(f"No JSON block found in LLM output. LLM response: {llm_output_str}")
|
|
|
|
| 239 |
except Exception as e: traceback.print_exc(); raise
|
| 240 |
|
| 241 |
# --- FastAPI Application ---
|
| 242 |
+
# NOTE: To make this batching system work correctly with asyncio.Queue,
|
| 243 |
+
# you must run the server with a SINGLE WORKER. For example:
|
| 244 |
+
# uvicorn main:app --workers 1
|
| 245 |
+
app = FastAPI(title="Email Assistant API", description="API with batch processing.", version="1.3.0", docs_url="/", redoc_url="/redoc")
|
| 246 |
|
| 247 |
# --- Exception Handlers ---
|
| 248 |
@app.exception_handler(StarletteHTTPException)
|
|
|
|
| 254 |
traceback.print_exc()
|
| 255 |
return Response(content=json.dumps({"detail": f"Internal Server Error: {str(exc)}", "type": "unhandled_exception"}), status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, media_type="application/json")
|
| 256 |
|
| 257 |
+
# --- Batch Worker Helper Functions ---
|
| 258 |
async def _execute_single_extract_task(request_item: ProcessEmailRequest, request_id: UUID) -> Tuple[UUID, Any]:
|
|
|
|
| 259 |
current_date = date.today()
|
| 260 |
try:
|
| 261 |
+
result = await asyncio.to_thread(_process_email_internal, request_item.email_text, request_item.groq_api_key, current_date)
|
|
|
|
|
|
|
|
|
|
| 262 |
return request_id, result
|
| 263 |
except Exception as e:
|
| 264 |
return request_id, e
|
| 265 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
async def _execute_single_generate_reply_task(request_item: GenerateReplyRequest, request_id: UUID) -> Tuple[UUID, Any]:
|
|
|
|
| 267 |
try:
|
| 268 |
+
result = await asyncio.to_thread(_generate_response_internal, request_item.email_text, request_item.groq_api_key, request_item.language, request_item.length, request_item.style, request_item.tone, request_item.emoji)
|
| 269 |
+
return request_id, result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
except Exception as e:
|
| 271 |
return request_id, e
|
| 272 |
|
| 273 |
+
# --- Batch Worker Functions ---
|
| 274 |
+
async def batch_worker(
|
| 275 |
+
queue: asyncio.Queue,
|
| 276 |
+
pending_requests: Dict[UUID, asyncio.Future],
|
| 277 |
+
execution_function: callable,
|
| 278 |
+
worker_name: str
|
| 279 |
+
):
|
| 280 |
+
print(f"[{datetime.now()}] {worker_name} started.")
|
| 281 |
while not shutdown_event.is_set():
|
| 282 |
try:
|
| 283 |
+
await asyncio.wait_for(shutdown_event.wait(), timeout=BATCH_INTERVAL_SECONDS)
|
|
|
|
| 284 |
if shutdown_event.is_set(): break
|
| 285 |
+
except asyncio.TimeoutError:
|
| 286 |
+
pass # Interval elapsed, time to process
|
| 287 |
|
| 288 |
+
batch_to_process = []
|
| 289 |
+
while len(batch_to_process) < MAX_BATCH_SIZE and not queue.empty():
|
| 290 |
try:
|
| 291 |
+
batch_to_process.append(queue.get_nowait())
|
|
|
|
| 292 |
except asyncio.QueueEmpty:
|
| 293 |
break
|
| 294 |
+
|
| 295 |
if not batch_to_process:
|
| 296 |
continue
|
| 297 |
|
| 298 |
+
print(f"[{datetime.now()}] {worker_name}: Processing batch of {len(batch_to_process)} requests.")
|
| 299 |
|
| 300 |
+
tasks = [execution_function(req_obj, req_id) for req_obj, req_id in batch_to_process]
|
| 301 |
+
results = await asyncio.gather(*tasks)
|
| 302 |
|
| 303 |
for req_id, result_or_exc in results:
|
| 304 |
+
future = pending_requests.pop(req_id, None)
|
| 305 |
if future and not future.done():
|
| 306 |
if isinstance(result_or_exc, Exception):
|
| 307 |
future.set_exception(result_or_exc)
|
| 308 |
else:
|
| 309 |
+
future.set_result(result_or_exc)
|
|
|
|
|
|
|
| 310 |
|
| 311 |
+
print(f"[{datetime.now()}] {worker_name} shutting down. Cancelling pending requests...")
|
| 312 |
+
for req_id, future in list(pending_requests.items()):
|
| 313 |
+
if not future.done():
|
| 314 |
+
future.set_exception(HTTPException(status_code=503, detail="Service shutting down"))
|
| 315 |
+
pending_requests.pop(req_id, None)
|
| 316 |
+
print(f"[{datetime.now()}] {worker_name} stopped.")
|
|
|
|
|
|
|
|
|
|
| 317 |
|
| 318 |
# --- FastAPI Event Handlers ---
|
| 319 |
+
worker_tasks: List[asyncio.Task] = []
|
| 320 |
+
|
| 321 |
@app.on_event("startup")
|
| 322 |
async def startup_event():
|
| 323 |
global client, db, extracted_emails_collection, generated_replies_collection
|
| 324 |
+
global extract_data_queue, generate_reply_queue, worker_tasks
|
| 325 |
|
| 326 |
print(f"[{datetime.now()}] FastAPI app startup sequence initiated.")
|
| 327 |
try:
|
|
|
|
| 332 |
generated_replies_collection = db[GENERATED_REPLIES_COLLECTION]
|
| 333 |
print(f"[{datetime.now()}] Successfully connected to MongoDB: {DB_NAME}")
|
| 334 |
|
|
|
|
| 335 |
extract_data_queue = asyncio.Queue()
|
| 336 |
generate_reply_queue = asyncio.Queue()
|
| 337 |
+
|
| 338 |
+
task1 = asyncio.create_task(batch_worker(extract_data_queue, extract_pending_requests, _execute_single_extract_task, "Extract Worker"))
|
| 339 |
+
task2 = asyncio.create_task(batch_worker(generate_reply_queue, generate_pending_requests, _execute_single_generate_reply_task, "Reply Worker"))
|
| 340 |
+
worker_tasks.extend([task1, task2])
|
| 341 |
print(f"[{datetime.now()}] Batch processing workers started.")
|
| 342 |
|
| 343 |
except (ConnectionFailure, OperationFailure) as e:
|
| 344 |
print(f"[{datetime.now()}] ERROR: MongoDB Connection/Operation Failure: {e}")
|
|
|
|
|
|
|
| 345 |
client = db = extracted_emails_collection = generated_replies_collection = None
|
| 346 |
except Exception as e:
|
| 347 |
print(f"[{datetime.now()}] ERROR: An unexpected error during startup: {e}")
|
| 348 |
traceback.print_exc()
|
| 349 |
client = db = extracted_emails_collection = generated_replies_collection = None
|
| 350 |
finally:
|
| 351 |
+
# CORRECTED: Use 'is not None' for pymongo objects
|
| 352 |
+
if not (client is not None and db is not None and extracted_emails_collection is not None and generated_replies_collection is not None):
|
| 353 |
+
print(f"[{datetime.now()}] WARNING: MongoDB might not be fully initialized.")
|
| 354 |
print(f"[{datetime.now()}] FastAPI app startup sequence completed.")
|
| 355 |
|
| 356 |
|
| 357 |
@app.on_event("shutdown")
|
| 358 |
+
async def shutdown_event_handler():
|
| 359 |
+
global client, shutdown_event, worker_tasks
|
| 360 |
print(f"[{datetime.now()}] FastAPI app shutting down.")
|
| 361 |
|
| 362 |
+
if not shutdown_event.is_set():
|
| 363 |
+
shutdown_event.set()
|
| 364 |
|
| 365 |
+
if worker_tasks:
|
| 366 |
+
print(f"[{datetime.now()}] Waiting for worker tasks to complete...")
|
| 367 |
+
await asyncio.gather(*worker_tasks, return_exceptions=True)
|
| 368 |
+
print(f"[{datetime.now()}] Worker tasks completed.")
|
| 369 |
|
| 370 |
+
if client is not None:
|
| 371 |
client.close()
|
| 372 |
print(f"[{datetime.now()}] MongoDB client closed.")
|
| 373 |
print(f"[{datetime.now()}] FastAPI app shutdown sequence completed.")
|
|
|
|
| 377 |
async def health_check():
|
| 378 |
db_status = "MongoDB not connected."
|
| 379 |
db_ok = False
|
| 380 |
+
# CORRECTED: Use 'is not None' for pymongo objects
|
| 381 |
+
if client is not None and db is not None:
|
| 382 |
try:
|
| 383 |
+
await asyncio.to_thread(client.admin.command, 'ping')
|
| 384 |
db_status = "MongoDB connection OK."
|
| 385 |
db_ok = True
|
| 386 |
except Exception as e:
|
| 387 |
db_status = f"MongoDB connection error: {e}"
|
| 388 |
|
| 389 |
+
queues = {}
|
| 390 |
+
if extract_data_queue is not None and generate_reply_queue is not None:
|
| 391 |
+
queues = {
|
| 392 |
+
"extract_data_queue_size": extract_data_queue.qsize(),
|
| 393 |
+
"generate_reply_queue_size": generate_reply_queue.qsize(),
|
| 394 |
+
"extract_pending_requests": len(extract_pending_requests),
|
| 395 |
+
"generate_pending_requests": len(generate_pending_requests)
|
| 396 |
+
}
|
| 397 |
|
| 398 |
if db_ok:
|
| 399 |
+
return {"status": "ok", "message": "API is up.", "database": db_status, "queues": queues}
|
| 400 |
else:
|
| 401 |
+
raise HTTPException(status_code=503, detail={"message": "Service unavailable.", "database": db_status})
|
| 402 |
|
| 403 |
@app.post("/extract-data", response_model=ExtractedData, summary="Extract structured data (batched)")
|
| 404 |
async def extract_email_data(request: ProcessEmailRequest):
|
| 405 |
+
# CORRECTED: Use 'is None' check
|
| 406 |
+
if extracted_emails_collection is None or extract_data_queue is None:
|
|
|
|
| 407 |
raise HTTPException(status_code=503, detail="Service not available (DB or batch queue).")
|
| 408 |
|
| 409 |
request_id = uuid4()
|
| 410 |
+
future: asyncio.Future[ExtractedData] = asyncio.get_event_loop().create_future()
|
| 411 |
extract_pending_requests[request_id] = future
|
| 412 |
|
| 413 |
try:
|
| 414 |
await extract_data_queue.put((request, request_id))
|
| 415 |
+
extracted_data_obj = await asyncio.wait_for(future, timeout=60.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
|
| 417 |
data_to_insert = extracted_data_obj.model_dump(by_alias=True, exclude_none=True, exclude={'id'})
|
| 418 |
if 'appointments' in data_to_insert:
|
|
|
|
| 424 |
if isinstance(task_item.get('due_date'), date): task_item['due_date'] = datetime.combine(task_item['due_date'], datetime.min.time())
|
| 425 |
|
| 426 |
insert_result = await asyncio.to_thread(extracted_emails_collection.insert_one, data_to_insert)
|
| 427 |
+
extracted_data_obj.id = str(insert_result.inserted_id)
|
| 428 |
|
| 429 |
return extracted_data_obj
|
| 430 |
+
except asyncio.TimeoutError:
|
| 431 |
+
raise HTTPException(status_code=504, detail="Request timed out while awaiting processing in batch.")
|
| 432 |
+
except Exception as e:
|
| 433 |
+
# Re-raise exceptions set by the worker (like HTTPException or others)
|
| 434 |
+
raise e
|
|
|
|
|
|
|
|
|
|
| 435 |
finally:
|
|
|
|
| 436 |
extract_pending_requests.pop(request_id, None)
|
| 437 |
|
|
|
|
| 438 |
@app.post("/generate-reply", response_model=GenerateReplyResponse, summary="Generate smart reply (batched)")
|
| 439 |
async def generate_email_reply(request: GenerateReplyRequest):
|
| 440 |
+
# CORRECTED: Use 'is None' check
|
| 441 |
+
if generated_replies_collection is None or generate_reply_queue is None:
|
|
|
|
| 442 |
raise HTTPException(status_code=503, detail="Service not available (DB or batch queue).")
|
| 443 |
|
|
|
|
| 444 |
cache_query = {"original_email_text": request.email_text, "language": request.language, "length": request.length, "style": request.style, "tone": request.tone, "emoji": request.emoji}
|
| 445 |
cached_reply_doc = await asyncio.to_thread(generated_replies_collection.find_one, cache_query)
|
| 446 |
if cached_reply_doc:
|
|
|
|
| 447 |
return GenerateReplyResponse(reply=cached_reply_doc["generated_reply_text"], stored_id=str(cached_reply_doc["_id"]), cached=True)
|
| 448 |
|
|
|
|
| 449 |
request_id = uuid4()
|
| 450 |
+
future: asyncio.Future[str] = asyncio.get_event_loop().create_future()
|
| 451 |
generate_pending_requests[request_id] = future
|
| 452 |
|
| 453 |
try:
|
| 454 |
await generate_reply_queue.put((request, request_id))
|
| 455 |
+
reply_content_str = await asyncio.wait_for(future, timeout=60.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 456 |
|
| 457 |
reply_data_to_store = GeneratedReplyData(
|
| 458 |
original_email_text=request.email_text, generated_reply_text=reply_content_str,
|
|
|
|
| 465 |
stored_id = str(insert_result.inserted_id)
|
| 466 |
|
| 467 |
return GenerateReplyResponse(reply=reply_content_str, stored_id=stored_id, cached=False)
|
| 468 |
+
except asyncio.TimeoutError:
|
| 469 |
+
raise HTTPException(status_code=504, detail="Request timed out while awaiting reply generation in batch.")
|
|
|
|
| 470 |
except Exception as e:
|
| 471 |
+
raise e
|
|
|
|
| 472 |
finally:
|
| 473 |
generate_pending_requests.pop(request_id, None)
|
| 474 |
|
|
|
|
| 475 |
@app.get("/query-extracted-emails", response_model=List[ExtractedData], summary="Query stored extracted email data")
|
| 476 |
async def query_extracted_emails(query_params: ExtractedEmailQuery = Depends()):
|
| 477 |
if extracted_emails_collection is None: raise HTTPException(status_code=503, detail="MongoDB not available.")
|