Skip to content

Commit 58a9e89

Browse files
committed
Improve summarization by including filename and # of chunks
1 parent 400e3db commit 58a9e89

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

packages/cosma-backend/src/cosma_backend/summarizer/summarizer.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,17 @@ def _get_system_prompt(self, include_title: bool = False):
459459
"Example: {{'summary': 'A concise summary of the file content', 'keywords': ['keyword1', 'keyword2', 'keyword3']}}"
460460
)
461461

462+
def _format_content_with_context(self, chunk: str, chunk_num: int, total_chunks: int, filename: str) -> str:
463+
"""Format content with filename and chunk context for the first chunk."""
464+
if chunk_num == 0:
465+
if total_chunks > 1:
466+
return f"Filename: {filename}\n(Part 1 of {total_chunks})\n\nContent:\n{chunk}"
467+
else:
468+
return f"Filename: {filename}\n\nContent:\n{chunk}"
469+
return chunk
470+
462471
@abstractmethod
463-
async def _get_ai_response(self, chunk: str, chunk_num: int, images: list[str]) -> str | None:
472+
async def _get_ai_response(self, chunk: str, chunk_num: int, total_chunks: int, images: list[str], filename: str) -> str | None:
464473
raise NotImplementedError
465474

466475
async def summarize(self, file_metadata: File) -> File:
@@ -479,9 +488,10 @@ async def summarize(self, file_metadata: File) -> File:
479488
images = await self._prepare_images(file_metadata)
480489

481490
# Process each chunk
491+
total_chunks = len(content_chunks)
482492
for i, chunk in enumerate(content_chunks):
483-
logger.info(f"Processing chunk {i+1}/{len(content_chunks)}", length=len(chunk), images=len(images))
484-
response = await self._get_ai_response(chunk, i, images)
493+
logger.info(f"Processing chunk {i+1}/{total_chunks}", length=len(chunk), images=len(images))
494+
response = await self._get_ai_response(chunk, i, total_chunks, images, file_metadata.filename)
485495

486496
if not response:
487497
logger.warning("Empty response for chunk", chunk_num=i+1)
@@ -573,8 +583,9 @@ async def is_available(self) -> bool:
573583
logger.debug(f"Ollama not available - error: {str(e)}")
574584
return False
575585

576-
async def _get_ai_response(self, chunk: str, chunk_num: int, images: list[str]) -> str | None:
577-
user_message: dict[str, Any] = {"role": "user", "content": chunk}
586+
async def _get_ai_response(self, chunk: str, chunk_num: int, total_chunks: int, images: list[str], filename: str) -> str | None:
587+
content = self._format_content_with_context(chunk, chunk_num, total_chunks, filename)
588+
user_message: dict[str, Any] = {"role": "user", "content": content}
578589
if images:
579590
user_message["images"] = images
580591

@@ -645,11 +656,12 @@ async def is_available(self) -> bool:
645656
# Assume OpenAI by default
646657
return bool(os.getenv("OPENAI_API_KEY"))
647658

648-
async def _get_ai_response(self, chunk: str, chunk_num: int, images: list[str]) -> str | None:
649-
user_message = {"role": "user", "content": chunk}
659+
async def _get_ai_response(self, chunk: str, chunk_num: int, total_chunks: int, images: list[str], filename: str) -> str | None:
660+
content = self._format_content_with_context(chunk, chunk_num, total_chunks, filename)
661+
user_message = {"role": "user", "content": content}
650662
if images:
651663
user_message["images"] = images
652-
664+
653665
response = litellm.completion(
654666
model=self.model,
655667
messages=[
@@ -751,11 +763,12 @@ async def is_available(self) -> bool:
751763
logger.debug(f"llama.cpp not available - error: {str(e)}")
752764
return False
753765

754-
async def _get_ai_response(self, chunk: str, chunk_num: int, images: list[str]) -> str | None:
766+
async def _get_ai_response(self, chunk: str, chunk_num: int, total_chunks: int, images: list[str], filename: str) -> str | None:
767+
content = self._format_content_with_context(chunk, chunk_num, total_chunks, filename)
755768
response = self.llm.create_chat_completion(
756769
messages=[
757770
{"role": "system", "content": self._get_system_prompt(include_title=(chunk_num == 0))},
758-
{"role": "user", "content": chunk},
771+
{"role": "user", "content": content},
759772
],
760773
max_tokens=500,
761774
temperature=0.1,

0 commit comments

Comments
 (0)