1- import abc
21from itertools import islice
2+ from math import ceil , floor , log
33from pathlib import Path
44from typing import Any , Dict , Generator , Iterable , List , Tuple
55
66from aws_doc_sdk_examples_tools .doc_gen import DocGen , Example
77from aws_doc_sdk_examples_tools .fs import Fs , PathFs
88from aws_doc_sdk_examples_tools .lliam .domain .model import Prompt
9- from aws_doc_sdk_examples_tools .lliam .shared_constants import BATCH_PREFIX
9+ from aws_doc_sdk_examples_tools .lliam .config import BATCH_PREFIX
1010
1111DEFAULT_METADATA_PREFIX = "DEFAULT"
1212DEFAULT_BATCH_SIZE = 150
@@ -23,7 +23,7 @@ def batched(iterable: Iterable, n: int) -> Generator[Tuple, Any, None]:
2323 yield batch
2424
2525
26- class FsPromptRepository :
26+ class PromptRepository :
2727 to_write : Dict [str , str ] = {}
2828
2929 def __init__ (self , fs : Fs = PathFs ()):
@@ -37,23 +37,30 @@ def rollback(self):
3737 def add (self , prompt : Prompt ):
3838 self .to_write [prompt .id ] = prompt .content
3939
40- def all_all (self , prompts : List [Prompt ]):
40+ def all_all (self , prompts : Iterable [Prompt ]):
4141 for prompt in prompts :
4242 self .add (prompt )
4343
44- def batch (self , prompts : List [Prompt ]):
44+ def batch (self , prompts : Iterable [Prompt ]):
45+ prompt_list = list (prompts )
46+
47+ if not prompt_list :
48+ return
49+
50+ batches_count = ceil (len (prompt_list ) / DEFAULT_BATCH_SIZE )
51+ padding = floor (log (batches_count , 10 )) + 1
4552 for batch_num , batch in enumerate (batched (prompts , DEFAULT_BATCH_SIZE )):
46- batch_name = f"{ BATCH_PREFIX } { (batch_num + 1 ):03 } "
53+ batch_name = f"{ BATCH_PREFIX } { (batch_num + 1 ):0{ padding } } "
4754 for prompt in batch :
4855 prompt .id = f"{ batch_name } /{ prompt .id } "
4956 self .add (prompt )
5057
5158 def commit (self ):
5259 base_path = Path (self .partition ) if self .partition else Path ("." )
5360
54- for file_path , content in self .to_write .items ():
61+ for id , content in self .to_write .items ():
5562 if content :
56- full_path = base_path / file_path
63+ full_path = base_path / id
5764 self .fs .mkdir (full_path .parent )
5865 self .fs .write (full_path , content )
5966
@@ -75,7 +82,7 @@ def partition(self):
7582 return self .partition_name or ""
7683
7784
78- class FsDocGenRepository :
85+ class DocGenRepository :
7986 def __init__ (self , fs : Fs = PathFs ()):
8087 self .fs = fs
8188
0 commit comments