Skip to content

Commit 0951b19

Browse files
committed
Update code
1 parent 8834fd6 commit 0951b19

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""AI app constants."""
22

3-
MIN_REQUEST_INTERVAL_SECONDS = 1.2
43
DEFAULT_LAST_REQUEST_OFFSET_SECONDS = 2
54
DELIMITER = "\n\n"
5+
MIN_REQUEST_INTERVAL_SECONDS = 1.2

backend/apps/ai/management/commands/ai_create_chapter_chunks.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
)

backend/apps/ai/models/chunk.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ class Meta:
1818
verbose_name = "Chunk"
1919
unique_together = ("content_type", "object_id", "text")
2020

21-
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, blank=True, null=True)
22-
object_id = models.PositiveIntegerField(default=0)
2321
content_object = GenericForeignKey("content_type", "object_id")
22+
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, blank=True, null=True)
2423
embedding = VectorField(verbose_name="Embedding", dimensions=1536)
24+
object_id = models.PositiveIntegerField(default=0)
2525
text = models.TextField(verbose_name="Text")
2626

2727
def __str__(self):

0 commit comments

Comments
 (0)