7
7
from langchain_community .chat_models import ChatLiteLLM
8
8
from typing_extensions import TypedDict
9
9
10
+ from learning_resources .exceptions import (
11
+ FlashcardsGenerationError ,
12
+ SummaryGenerationError ,
13
+ )
10
14
from learning_resources .models import (
11
15
ContentFile ,
12
16
ContentSummarizerConfiguration ,
@@ -134,21 +138,26 @@ def summarize_content_files_by_ids(
134
138
Returns:
135
139
- None
136
140
"""
141
+ status_messages = []
137
142
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
139
148
140
149
def summarize_single_content_file (
141
150
self ,
142
151
content_file_id : int ,
143
152
overwrite ,
144
- ) -> None :
153
+ ) -> tuple [ bool , str ] :
145
154
"""Process a single content file
146
155
Args:
147
156
- content_file_id (int): Id of the content file to process
148
157
- overwrite (bool): Whether to overwrite existing summary and flashcards
149
158
150
159
Returns:
151
- - None
160
+ - str: A string message indicating the status of the summarization
152
161
"""
153
162
try :
154
163
with transaction .atomic ():
@@ -175,10 +184,20 @@ def summarize_single_content_file(
175
184
176
185
if updated :
177
186
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 } \n Error: { exc .args [0 ]} \n \n " # noqa: E501
178
192
179
- except Exception :
193
+ except FlashcardsGenerationError as exc :
194
+ return f"Content file flashcards generation failed for CONTENT_FILE_ID: { content_file_id } \n Error: { exc .args [0 ]} \n \n " # noqa: E501
195
+ except Exception as exc :
180
196
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 } \n Error: { exc .args [0 ]} \n \n " , # noqa: E501
200
+ )
182
201
183
202
def _get_llm (self , model = None , temperature = 0.0 , max_tokens = 1000 ) -> ChatLiteLLM :
184
203
"""Get the ChatLiteLLM instance"""
@@ -216,13 +235,14 @@ def _generate_summary(self, content: str, llm_model: str) -> str:
216
235
generated_summary = response .content
217
236
logger .info ("Generated summary: %s" , generated_summary )
218
237
219
- except Exception :
238
+ except Exception as exc :
220
239
logger .exception (
221
240
"An error occurred while generating summary using model: %s" , llm_model
222
241
)
223
- raise
242
+ raise SummaryGenerationError (exc ) from exc
243
+
224
244
else :
225
- return generated_summary
245
+ return True , generated_summary
226
246
227
247
def _generate_flashcards (
228
248
self , content : str , llm_model : str
@@ -243,12 +263,12 @@ def _generate_flashcards(
243
263
)
244
264
generated_flashcards = response .get ("flashcards" )
245
265
logger .info ("Generated flashcards: %s" , generated_flashcards )
246
-
247
- except Exception :
266
+ except Exception as exc :
248
267
logger .exception (
249
268
"An error occurred while generating flashcards using model: %s" ,
250
269
llm_model ,
251
270
)
252
- raise
271
+ raise FlashcardsGenerationError (exc ) from exc
272
+
253
273
else :
254
274
return generated_flashcards
0 commit comments