From e5ba2697e44360cfac50102cc61ecf0925af64aa Mon Sep 17 00:00:00 2001 From: Ranjan Shrestha Date: Thu, 2 Jul 2026 12:30:45 +0545 Subject: [PATCH] fix: update the summary prompt; add timeout in llm args --- apps/reports/ai_features/extraction.py | 1 + apps/reports/ai_features/llms.py | 9 ++++++ apps/reports/ai_features/prompts.py | 41 +++++++++++++++++--------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/apps/reports/ai_features/extraction.py b/apps/reports/ai_features/extraction.py index 8a9ec3f..8af3749 100644 --- a/apps/reports/ai_features/extraction.py +++ b/apps/reports/ai_features/extraction.py @@ -70,6 +70,7 @@ def pdf_to_images(self, zoom: float = 2.0): self.handle_meta_info() for page_idx in range(len(doc)): + logger.info("Processing Page %s", page_idx + 1) page = doc[page_idx] pic = page.get_pixmap(matrix=fitz.Matrix(zoom, zoom), alpha=False) diff --git a/apps/reports/ai_features/llms.py b/apps/reports/ai_features/llms.py index 0251684..3f3192d 100644 --- a/apps/reports/ai_features/llms.py +++ b/apps/reports/ai_features/llms.py @@ -1,5 +1,6 @@ from dataclasses import dataclass +import httpx from django.conf import settings from langchain_core.messages import HumanMessage from langchain_ollama import ChatOllama, OllamaEmbeddings @@ -49,6 +50,14 @@ def load_chat_model(self): base_url=settings.LLM_OLLAMA_BASE_URL, temperature=self.temperature, format="json", + client_kwargs={ + "timeout": httpx.Timeout( + connect=30.0, + read=600.0, # Allow up to 10 minutes for generation + write=300.0, + pool=30.0, + ), + }, ) except Exception as e: raise Exception(f"Ollama LLM model is not successfully loaded. {str(e)}") from e diff --git a/apps/reports/ai_features/prompts.py b/apps/reports/ai_features/prompts.py index b4e1af7..a51225b 100644 --- a/apps/reports/ai_features/prompts.py +++ b/apps/reports/ai_features/prompts.py @@ -34,22 +34,35 @@ def get_doc_summary_prompt(page_summaries: list[str]): return f""" - You are an expert document analyst. + You are an expert document analyst. - You will be given summaries extracted from individual pages of a document. + You will be given summaries extracted from individual pages of a document. - Your task is to create a single, coherent document summary by: - 1. Combining information from all pages. - 2. Removing duplicate or repetitive information. - 3. Preserving important facts, findings, statistics, dates, and conclusions. - 4. Identifying the main themes discussed throughout the document. - 5. Highlighting key findings and recommendations. - 6. Maintaining factual accuracy and avoiding information that is not present in the provided summaries. + Your task is to create a single, coherent summary by: + 1. Combining information from all pages. + 2. Removing duplicate or repetitive information. + 3. Preserving important facts, findings, statistics, dates, and conclusions. + 4. Identifying the main themes discussed throughout the document. + 5. Highlighting key findings and recommendations where applicable. + 6. Maintaining factual accuracy and avoiding information that is not present in the provided summaries. - Page Summaries: + Writing style: + - Write the summary as a direct description of the subject matter, + not of the document itself. + - Do NOT begin with phrases such as "This document...", + "The document...", "This report...", "The report...", + "This presentation...", or similar meta-references. + - Start immediately with the primary topic or subject. + For example, write "The disaster response..." + instead of "This document provides an overview of disaster response." + - Use an informative, objective, and concise tone. + - Do not mention page numbers, sections, or that the information + was extracted from multiple pages. - {chr(10).join(f"Page {i + 1} : {summary}" for i, summary in enumerate(page_summaries))} + Page Summaries: - Return only a concise executive summary of the above given texts in 2-4 paragraphs - exactly in a dict with key 'doc_summary'. -""" + {chr(10).join(f"Page {i + 1}: {summary}" for i, summary in enumerate(page_summaries))} + + Return only a concise abstractive summary in 2–4 paragraphs, + formatted exactly as a JSON object with the single key "doc_summary". + """