Spaces:
Sleeping
Sleeping
Girish Jeswani commited on
Commit ·
946b29c
1
Parent(s): 7205ce3
update summary
Browse files
multi_llm_chatbot_backend/app/api/routes/documents.py
CHANGED
|
@@ -5,7 +5,7 @@ from app.core.session_manager import get_session_manager
|
|
| 5 |
from app.core.rag_manager import get_rag_manager
|
| 6 |
from app.api.utils import get_or_create_session_for_request
|
| 7 |
from fastapi.responses import StreamingResponse
|
| 8 |
-
from app.utils.chat_summary import generate_summary_from_messages, parse_summary_to_blocks
|
| 9 |
from app.utils.file_export import prepare_export_response
|
| 10 |
from app.core.session_manager import get_session_manager
|
| 11 |
from app.api.utils import get_or_create_session_for_request
|
|
@@ -200,7 +200,14 @@ async def export_chat(request: Request, format: str = Query(..., regex="^(txt|pd
|
|
| 200 |
return {"error": "Failed to export chat.", "detail": str(e)}
|
| 201 |
|
| 202 |
@router.get("/chat-summary")
|
| 203 |
-
async def chat_summary(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
try:
|
| 205 |
session_id = get_or_create_session_for_request(request)
|
| 206 |
session = session_manager.get_session(session_id)
|
|
@@ -208,22 +215,24 @@ async def chat_summary(request: Request, format: str = Query("text", regex="^(tx
|
|
| 208 |
if not session.messages:
|
| 209 |
return {"error": "No messages in this session."}
|
| 210 |
|
| 211 |
-
llm = next(iter(session_manager.get_session(session_id).messages), {}).get("llm")
|
| 212 |
-
if not llm:
|
| 213 |
-
llm = next(iter(session_manager.sessions.values())).messages[0].get("llm")
|
| 214 |
-
|
| 215 |
llm = next(iter(chat_orchestrator.personas.values())).llm
|
| 216 |
summary_text = await generate_summary_from_messages(session.messages, llm)
|
| 217 |
|
| 218 |
if format == "txt":
|
| 219 |
-
|
|
|
|
|
|
|
| 220 |
|
| 221 |
elif format == "docx":
|
| 222 |
-
|
|
|
|
|
|
|
| 223 |
|
| 224 |
elif format == "pdf":
|
|
|
|
| 225 |
blocks = [{"type": "heading", "text": "Chat Summary"}] + parse_summary_to_blocks(summary_text)
|
| 226 |
-
|
|
|
|
| 227 |
return StreamingResponse(
|
| 228 |
file_stream,
|
| 229 |
media_type="application/pdf",
|
|
|
|
| 5 |
from app.core.rag_manager import get_rag_manager
|
| 6 |
from app.api.utils import get_or_create_session_for_request
|
| 7 |
from fastapi.responses import StreamingResponse
|
| 8 |
+
from app.utils.chat_summary import generate_summary_from_messages, parse_summary_to_blocks, format_summary_for_text_export
|
| 9 |
from app.utils.file_export import prepare_export_response
|
| 10 |
from app.core.session_manager import get_session_manager
|
| 11 |
from app.api.utils import get_or_create_session_for_request
|
|
|
|
| 200 |
return {"error": "Failed to export chat.", "detail": str(e)}
|
| 201 |
|
| 202 |
@router.get("/chat-summary")
|
| 203 |
+
async def chat_summary(
|
| 204 |
+
request: Request,
|
| 205 |
+
format: str = Query("text", regex="^(txt|pdf|docx)$")
|
| 206 |
+
):
|
| 207 |
+
"""
|
| 208 |
+
Generate and return a summary of the current session chat.
|
| 209 |
+
Can return as plain txt, PDF, or DOCX.
|
| 210 |
+
"""
|
| 211 |
try:
|
| 212 |
session_id = get_or_create_session_for_request(request)
|
| 213 |
session = session_manager.get_session(session_id)
|
|
|
|
| 215 |
if not session.messages:
|
| 216 |
return {"error": "No messages in this session."}
|
| 217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
llm = next(iter(chat_orchestrator.personas.values())).llm
|
| 219 |
summary_text = await generate_summary_from_messages(session.messages, llm)
|
| 220 |
|
| 221 |
if format == "txt":
|
| 222 |
+
# Use improved formatting for text export
|
| 223 |
+
formatted_summary = format_summary_for_text_export(summary_text)
|
| 224 |
+
return prepare_export_response(formatted_summary, "txt", filename_prefix="chat_summary")
|
| 225 |
|
| 226 |
elif format == "docx":
|
| 227 |
+
# Use improved formatting for DOCX export
|
| 228 |
+
formatted_summary = format_summary_for_text_export(summary_text)
|
| 229 |
+
return prepare_export_response(formatted_summary, "docx", filename_prefix="chat_summary")
|
| 230 |
|
| 231 |
elif format == "pdf":
|
| 232 |
+
# Parse and render using block formatting
|
| 233 |
blocks = [{"type": "heading", "text": "Chat Summary"}] + parse_summary_to_blocks(summary_text)
|
| 234 |
+
|
| 235 |
+
file_stream = generate_pdf_file_from_blocks(blocks)
|
| 236 |
return StreamingResponse(
|
| 237 |
file_stream,
|
| 238 |
media_type="application/pdf",
|