@@ -21,9 +21,15 @@ class Command(BaseCommand):
2121
2222 def add_arguments (self , parser ):
2323 parser .add_argument (
24- "--chapter-key" , type = str , help = "Process only the chapter with this key"
24+ "--chapter" ,
25+ type = str ,
26+ help = "Process only the chapter with this key" ,
27+ )
28+ parser .add_argument (
29+ "--all" ,
30+ action = "store_true" ,
31+ help = "Process all the chapters" ,
2532 )
26- parser .add_argument ("--all" , action = "store_true" , help = "Process all the chapters" )
2733 parser .add_argument (
2834 "--batch-size" ,
2935 type = int ,
@@ -40,34 +46,30 @@ def handle(self, *args, **options):
4046
4147 self .openai_client = openai .OpenAI (api_key = openai_api_key )
4248
43- if options ["chapter_key " ]:
44- queryset = Chapter .objects .filter (key = options [ "chapter_key" ] )
49+ if chapter := options ["chapter " ]:
50+ queryset = Chapter .objects .filter (key = chapter )
4551 elif options ["all" ]:
4652 queryset = Chapter .objects .all ()
4753 else :
4854 queryset = Chapter .objects .filter (is_active = True )
4955
50- total_chapters = queryset .count ()
51- self .stdout .write (f"Found { total_chapters } chapters to process" )
52-
53- if total_chapters == 0 :
56+ if not (total_chapters := queryset .count ()):
5457 self .stdout .write ("No chapters found to process" )
5558 return
5659
57- batch_size = options [ "batch_size" ]
60+ self . stdout . write ( f"Found { total_chapters } chapters to process" )
5861
62+ batch_size = options ["batch_size" ]
5963 for offset in range (0 , total_chapters , batch_size ):
6064 batch_chapters = queryset [offset : offset + batch_size ]
6165
6266 batch_chunks = []
6367 for chapter in batch_chapters :
64- chunks = self .create_chunks (chapter )
65- batch_chunks .extend (chunks )
68+ batch_chunks .extend (self .create_chunks (chapter ))
6669
6770 if batch_chunks :
68- chunks_count = len (batch_chunks )
6971 Chunk .bulk_save (batch_chunks )
70- self .stdout .write (f"Saved { chunks_count } chunks" )
72+ self .stdout .write (f"Saved { len ( batch_chunks ) } chunks" )
7173
7274 self .stdout .write (f"Completed processing all { total_chapters } chapters" )
7375
@@ -81,8 +83,7 @@ def create_chunks(self, chapter: Chapter) -> list[Chunk]:
8183 all_chunk_texts .append (metadata_content )
8284
8385 if prose_content .strip ():
84- prose_chunks = Chunk .split_text (prose_content )
85- all_chunk_texts .extend (prose_chunks )
86+ all_chunk_texts .extend (Chunk .split_text (prose_content ))
8687
8788 if not all_chunk_texts :
8889 self .stdout .write (f"No content to chunk for chapter { chapter .key } " )
@@ -191,7 +192,7 @@ def extract_chapter_content(self, chapter: Chapter) -> tuple[str, str]:
191192 leaders_info .append (leader_text )
192193
193194 if leaders_info :
194- metadata_parts .append ("Chapter Leaders: " + ", " .join (leaders_info ) )
195+ metadata_parts .append (f"Location Information: { ', ' .join (location_parts ) } " )
195196
196197 if chapter .related_urls :
197198 valid_urls = [
@@ -204,7 +205,7 @@ def extract_chapter_content(self, chapter: Chapter) -> tuple[str, str]:
204205
205206 metadata_parts .append (f"Active Chapter: { 'Yes' if chapter .is_active else 'No' } " )
206207
207- prose_content = DELIMITER . join ( filter ( None , prose_parts ))
208- metadata_content = DELIMITER .join (filter (None , metadata_parts ))
209-
210- return prose_content , metadata_content
208+ return (
209+ DELIMITER .join (filter (None , prose_parts )),
210+ DELIMITER . join ( filter ( None , metadata_parts )),
211+ )
0 commit comments