Sohan Kshirsagar commited on
Commit
5e464c3
·
1 Parent(s): 5ff4a89

definging new api endpoint for chat summary

Browse files
multi_llm_chatbot_backend/app/api/routes.py CHANGED
@@ -19,6 +19,9 @@ from fastapi.responses import StreamingResponse
19
  from fastapi import Query
20
  from app.utils.file_export import export_chat_as_file
21
 
 
 
 
22
  import hashlib
23
  import logging
24
 
@@ -452,31 +455,67 @@ async def upload_document(file: UploadFile = File(...), request: Request = None)
452
  logger.error(f"Error processing document upload: {str(e)}")
453
  raise HTTPException(status_code=500, detail=f"Error processing document: {str(e)}")
454
 
455
-
456
  @router.get("/export-chat")
457
  async def export_chat(request: Request, format: str = Query(..., regex="^(txt|pdf|docx)$")):
458
  """
459
- Export the current chat context as a downloadable file in txt, pdf, or docx format.
460
  """
461
  try:
462
  session_id = get_or_create_session_for_request(request)
463
  session = session_manager.get_session(session_id)
464
 
465
  if not session.messages:
466
- return {"error": "No messages to export in current session."}
467
 
468
- # Generate the file using utility
469
- file_stream, filename, media_type = export_chat_as_file(session.messages, format)
470
-
471
- return StreamingResponse(
472
- file_stream,
473
- media_type=media_type,
474
- headers={"Content-Disposition": f"attachment; filename={filename}"}
475
- )
476
 
477
  except Exception as e:
478
  logger.error(f"Error exporting chat: {str(e)}")
479
  return {"error": "Failed to export chat.", "detail": str(e)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
480
 
481
 
482
  # Add new endpoint to get document statistics
 
19
  from fastapi import Query
20
  from app.utils.file_export import export_chat_as_file
21
 
22
+ from app.utils.chat_summary import generate_summary_from_messages, parse_summary_to_blocks
23
+ from app.utils.file_export import prepare_export_response, generate_pdf_file_from_blocks
24
+
25
  import hashlib
26
  import logging
27
 
 
455
  logger.error(f"Error processing document upload: {str(e)}")
456
  raise HTTPException(status_code=500, detail=f"Error processing document: {str(e)}")
457
 
458
+
459
  @router.get("/export-chat")
460
  async def export_chat(request: Request, format: str = Query(..., regex="^(txt|pdf|docx)$")):
461
  """
462
+ Export the current chat context in the requested format.
463
  """
464
  try:
465
  session_id = get_or_create_session_for_request(request)
466
  session = session_manager.get_session(session_id)
467
 
468
  if not session.messages:
469
+ return {"error": "No messages in this session."}
470
 
471
+ return prepare_export_response(session.messages, format)
 
 
 
 
 
 
 
472
 
473
  except Exception as e:
474
  logger.error(f"Error exporting chat: {str(e)}")
475
  return {"error": "Failed to export chat.", "detail": str(e)}
476
+
477
+
478
+ @router.get("/chat-summary")
479
+ async def chat_summary(
480
+ request: Request,
481
+ format: str = Query("text", regex="^(txt|pdf|docx)$")
482
+ ):
483
+ """
484
+ Generate and return a summary of the current session chat.
485
+ Can return as plain txt, PDF, or DOCX.
486
+ """
487
+ try:
488
+ session_id = get_or_create_session_for_request(request)
489
+ session = session_manager.get_session(session_id)
490
+
491
+ if not session.messages:
492
+ return {"error": "No messages in this session."}
493
+
494
+ llm = next(iter(chat_orchestrator.personas.values())).llm
495
+ summary_text = await generate_summary_from_messages(session.messages, llm)
496
+
497
+ if format == "txt":
498
+ return prepare_export_response(summary_text, "txt", filename_prefix="chat_summary")
499
+
500
+ elif format == "docx":
501
+ return prepare_export_response(summary_text, "docx", filename_prefix="chat_summary")
502
+
503
+ elif format == "pdf":
504
+ # Parse and render using block formatting
505
+ blocks = [{"type": "heading", "text": "Chat Summary"}] + parse_summary_to_blocks(summary_text)
506
+
507
+ file_stream = generate_pdf_file_from_blocks(blocks)
508
+ return StreamingResponse(
509
+ file_stream,
510
+ media_type="application/pdf",
511
+ headers={"Content-Disposition": "attachment; filename=chat_summary.pdf"}
512
+ )
513
+
514
+ except Exception as e:
515
+ logger.error(f"Error in chat-summary endpoint: {str(e)}")
516
+ return {"error": "Summary generation failed", "detail": str(e)}
517
+
518
+
519
 
520
 
521
  # Add new endpoint to get document statistics