def seed(self, model_seeder: Any, count: int = 1) -> list[dict]:
include_fields = list(self.seeder_fields.keys())
seed_intel_class = django_to_pydantic_model(
model_class=model_seeder.model_class,
base_class=BaseIntel,
include_fields=include_fields
)
class SeedingIntel(BaseIntel):
items: list[seed_intel_class]
def __iter__(self):
return iter(self.items)
# Base parts of the prompt that are common between batches
base_prompt = (
Prompt()
.heading('General Seeding Rules')
.list(['Create data for each field provided.'])
.heading('Field Rules & Context')
.prompt(self.field_prompt)
)
# Instantiate the bot once
bot = LlmFieldSeedingBot()
if count <= 25:
# Create a prompt for the full count since it's within a single batch
prompt = (
Prompt()
.heading('Seed Count')
.text(f'Create {count} {model_seeder.model_class.__name__}')
.prompt(base_prompt)
)
intel_data = bot.process(
prompt=prompt,
intel_class=SeedingIntel
)
else:
# Process in batches of 25 using futures. Seems like that is the limit for good results
futures = []
completed_futures = []
max_futures = 3
intel_data = []
batch_size = 15
total_batches = (count + batch_size - 1) // batch_size
for batch_index in range(total_batches):
batch_count = batch_size if (batch_index < total_batches - 1) else count - batch_size * batch_index
batch_prompt = (
Prompt()
.heading('Seed Count')
.text(f'Create {batch_count} {model_seeder.model_class.__name__}')
.prompt(base_prompt)
)
future = bot.process_to_future(
prompt=batch_prompt,
intel_class=SeedingIntel
)
futures.append(future)
# Only send max_futures calls at a time or when it's the last batch.
if len(futures) >= max_futures or batch_index == total_batches - 1:
print(f'-----> Seeding count {batch_count} for batch {batch_index + 1} of {total_batches}')
for future in futures:
intel_data.extend(future.result)
completed_futures.append(future)
futures = []
return intel_data