Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/reports/ai_features/extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions apps/reports/ai_features/llms.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
41 changes: 27 additions & 14 deletions apps/reports/ai_features/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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".
"""