Skip to content

Commit 0caed68

Browse files
committed
fix: Add logs and continue sumarizer on error
1 parent e8596b6 commit 0caed68

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

docker-compose.services.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ services:
101101
environment:
102102
- KEYCLOAK_ADMIN=${KEYCLOAK_SVC_ADMIN:-admin}
103103
- KEYCLOAK_ADMIN_PASSWORD=${KEYCLOAK_SVC_ADMIN_PASSWORD:-admin}
104+
- "_JAVA_OPTIONS=${JAVA_OPTIONS:-}" # Load _JAVA_OPTIONS from env, fallback to empty string
104105
networks:
105106
default:
106107
aliases:

learning_resources/content_summarizer.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
from langchain_community.chat_models import ChatLiteLLM
88
from typing_extensions import TypedDict
99

10+
from learning_resources.exceptions import (
11+
FlashcardsGenerationError,
12+
SummaryGenerationError,
13+
)
1014
from learning_resources.models import (
1115
ContentFile,
1216
ContentSummarizerConfiguration,
@@ -134,21 +138,26 @@ def summarize_content_files_by_ids(
134138
Returns:
135139
- None
136140
"""
141+
status_messages = []
137142
for content_file_id in content_file_ids:
138-
self.summarize_single_content_file(content_file_id, overwrite=overwrite)
143+
status_msg = self.summarize_single_content_file(
144+
content_file_id, overwrite=overwrite
145+
)
146+
status_messages.append(status_msg)
147+
return status_messages
139148

140149
def summarize_single_content_file(
141150
self,
142151
content_file_id: int,
143152
overwrite,
144-
) -> None:
153+
) -> tuple[bool, str]:
145154
"""Process a single content file
146155
Args:
147156
- content_file_id (int): Id of the content file to process
148157
- overwrite (bool): Whether to overwrite existing summary and flashcards
149158
150159
Returns:
151-
- None
160+
- str: A string message indicating the status of the summarization
152161
"""
153162
try:
154163
with transaction.atomic():
@@ -175,10 +184,20 @@ def summarize_single_content_file(
175184

176185
if updated:
177186
content_file.save()
187+
return f"Content file summarization succeeded for CONTENT_FILE_ID: {content_file_id}" # noqa: E501
188+
return f"Content file summarization skipped for CONTENT_FILE_ID: {content_file_id}" # noqa: E501
189+
190+
except SummaryGenerationError as exc:
191+
return f"Content file summary generation failed for CONTENT_FILE_ID: {content_file_id}\nError: {exc.args[0]}\n\n" # noqa: E501
178192

179-
except Exception:
193+
except FlashcardsGenerationError as exc:
194+
return f"Content file flashcards generation failed for CONTENT_FILE_ID: {content_file_id}\nError: {exc.args[0]}\n\n" # noqa: E501
195+
except Exception as exc:
180196
logger.exception("Error processing content: %d", content_file.id)
181-
raise
197+
return (
198+
False,
199+
f"Content file summarization failed for CONTENT_FILE_ID: {content_file_id}\nError: {exc.args[0]}\n\n", # noqa: E501
200+
)
182201

183202
def _get_llm(self, model=None, temperature=0.0, max_tokens=1000) -> ChatLiteLLM:
184203
"""Get the ChatLiteLLM instance"""
@@ -216,13 +235,14 @@ def _generate_summary(self, content: str, llm_model: str) -> str:
216235
generated_summary = response.content
217236
logger.info("Generated summary: %s", generated_summary)
218237

219-
except Exception:
238+
except Exception as exc:
220239
logger.exception(
221240
"An error occurred while generating summary using model: %s", llm_model
222241
)
223-
raise
242+
raise SummaryGenerationError(exc) from exc
243+
224244
else:
225-
return generated_summary
245+
return True, generated_summary
226246

227247
def _generate_flashcards(
228248
self, content: str, llm_model: str
@@ -243,12 +263,12 @@ def _generate_flashcards(
243263
)
244264
generated_flashcards = response.get("flashcards")
245265
logger.info("Generated flashcards: %s", generated_flashcards)
246-
247-
except Exception:
266+
except Exception as exc:
248267
logger.exception(
249268
"An error occurred while generating flashcards using model: %s",
250269
llm_model,
251270
)
252-
raise
271+
raise FlashcardsGenerationError(exc) from exc
272+
253273
else:
254274
return generated_flashcards

learning_resources/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@ class PostHogAuthenticationError(Exception):
1313

1414
class PostHogQueryError(Exception):
1515
"""Raised if the PostHog query API returns a non-authentication error."""
16+
17+
18+
class SummaryGenerationError(Exception):
19+
"""Raised if the summary generation fails for a content file."""
20+
21+
22+
class FlashcardsGenerationError(Exception):
23+
"""Raised if the flashcards generation fails for a content file."""

learning_resources/management/commands/generate_summary_flashcards.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Management command to run the content summarizer"""
22

3+
import itertools
4+
35
from django.conf import settings
46
from django.core.management import BaseCommand
57

@@ -91,9 +93,16 @@ def handle(self, *args, **options): # noqa: ARG002
9193
self.stdout.write("Waiting on task...")
9294

9395
start = now_in_utc()
94-
summarizer_task.get()
96+
results = summarizer_task.get()
97+
98+
# Log the summarization stats
99+
flat_results = list(itertools.chain(*results))
100+
for result in flat_results:
101+
self.stdout.write(f"{result}")
95102

96103
total_seconds = (now_in_utc() - start).total_seconds()
97104
self.stdout.write(
98-
f"Content file summarizer finished, took {total_seconds} seconds"
105+
self.style.SUCCESS(
106+
f"Content file summarizer finished, took {total_seconds} seconds"
107+
)
99108
)

learning_resources/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ def summarize_content_files_task(
424424
- None
425425
"""
426426
summarizer = ContentSummarizer()
427-
summarizer.summarize_content_files_by_ids(content_file_ids, overwrite)
427+
return summarizer.summarize_content_files_by_ids(content_file_ids, overwrite)
428428

429429

430430
@app.task(bind=True, acks_late=True)

0 commit comments

Comments
 (0)