full pool filling working

This commit is contained in:
2026-01-02 21:18:26 -07:00
parent e42b749baf
commit 5e89e730ee
5 changed files with 213 additions and 121 deletions

View File

@@ -75,7 +75,8 @@ class JournalPromptGenerator:
self.settings = {
'min_length': 500,
'max_length': 1000,
'num_prompts': 6
'num_prompts': 6,
'cached_pool_volume': 20 # Default value
}
try:
@@ -96,6 +97,12 @@ class JournalPromptGenerator:
if 'num_prompts' in prompts_section:
self.settings['num_prompts'] = int(prompts_section['num_prompts'])
# Load cached_pool_volume from prefetch section
if 'prefetch' in config:
prefetch_section = config['prefetch']
if 'cached_pool_volume' in prefetch_section:
self.settings['cached_pool_volume'] = int(prefetch_section['cached_pool_volume'])
except FileNotFoundError:
self.console.print("[yellow]Warning: settings.cfg not found, using default values[/yellow]")
except ValueError as e:
@@ -104,24 +111,10 @@ class JournalPromptGenerator:
self.console.print(f"[yellow]Warning: Error reading settings.cfg: {e}, using default values[/yellow]")
def _load_prompt_template(self):
"""Load the prompt template from ds_prompt.txt and update with config values."""
"""Load the prompt template from ds_prompt.txt."""
try:
with open("ds_prompt.txt", "r") as f:
template = f.read()
# Replace hardcoded values with config values
template = template.replace(
"between 500 and 1000 characters",
f"between {self.settings['min_length']} and {self.settings['max_length']} characters"
)
# Replace the number of prompts (6) with config value
template = template.replace(
"Please generate 6 writing prompts",
f"Please generate {self.settings['num_prompts']} writing prompts"
)
self.prompt_template = template
self.prompt_template = f.read()
except FileNotFoundError:
self.console.print("[red]Error: ds_prompt.txt not found[/red]")
sys.exit(1)
@@ -205,18 +198,7 @@ class JournalPromptGenerator:
table.add_row("Prompts in pool", str(total_prompts))
table.add_row("Prompts per session", str(self.settings['num_prompts']))
# Get cached_pool_volume from settings if available
cached_pool_volume = 20 # Default
try:
config = configparser.ConfigParser()
config.read('settings.cfg')
if 'prefetch' in config and 'cached_pool_volume' in config['prefetch']:
cached_pool_volume = int(config['prefetch']['cached_pool_volume'])
except:
pass
table.add_row("Target pool size", str(cached_pool_volume))
table.add_row("Target pool size", str(self.settings['cached_pool_volume']))
table.add_row("Available sessions", str(total_prompts // self.settings['num_prompts']))
self.console.print(table)
@@ -414,12 +396,12 @@ class JournalPromptGenerator:
return content.strip()
def generate_prompts(self) -> List[str]:
"""Generate new journal prompts using AI."""
self.console.print("\n[cyan]Generating new journal prompts...[/cyan]")
def generate_specific_number_of_prompts(self, count: int) -> List[str]:
"""Generate a specific number of journal prompts using AI."""
self.console.print(f"\n[cyan]Generating {count} new journal prompts...[/cyan]")
# Prepare the prompt
full_prompt = self._prepare_prompt()
# Prepare the prompt with specific count
full_prompt = self._prepare_prompt_with_count(count)
# Show progress
with Progress(
@@ -450,18 +432,111 @@ class JournalPromptGenerator:
return []
# Parse the response
new_prompts = self._parse_ai_response(response_content)
new_prompts = self._parse_ai_response_with_count(response_content, count)
if not new_prompts:
self.console.print("[red]Error: Could not parse any prompts from AI response[/red]")
return []
# Note: Prompts are NOT added to historic_prompts here
# They will be added only when the user chooses one in interactive mode
# via the add_prompt_to_history() method
return new_prompts
def _prepare_prompt_with_count(self, count: int) -> str:
"""Prepare the full prompt with historic context and specific count."""
# Start with the base template
template = self.prompt_template
# Add the instruction for the specific number of prompts
# This will be added to the prompt since it's being removed from ds_prompt.txt
prompt_instruction = f"Please generate {count} writing prompts, each between {self.settings['min_length']} and {self.settings['max_length']} characters."
# Format historic prompts for the AI
if self.historic_prompts:
historic_context = json.dumps(self.historic_prompts, indent=2)
full_prompt = f"{template}\n\n{prompt_instruction}\n\nPrevious prompts:\n{historic_context}"
else:
full_prompt = f"{template}\n\n{prompt_instruction}"
return full_prompt
def _parse_ai_response_with_count(self, response_content: str, expected_count: int) -> List[str]:
"""
Parse the AI response to extract new prompts with specific expected count.
"""
# First, try to clean up the response content
cleaned_content = self._clean_ai_response(response_content)
try:
# Try to parse as JSON
data = json.loads(cleaned_content)
# Check if data is a list
if isinstance(data, list):
# Return the list of prompt strings directly
# Ensure we have the correct number of prompts
if len(data) >= expected_count:
return data[:expected_count]
else:
self.console.print(f"[yellow]Warning: AI returned {len(data)} prompts, expected {expected_count}[/yellow]")
return data
elif isinstance(data, dict):
# Fallback for old format: dictionary with newprompt0, newprompt1, etc.
self.console.print("[yellow]Warning: AI returned dictionary format, expected list format[/yellow]")
new_prompts = []
for i in range(expected_count):
key = f"newprompt{i}"
if key in data:
new_prompts.append(data[key])
return new_prompts
else:
self.console.print(f"[yellow]Warning: AI returned unexpected data type: {type(data)}[/yellow]")
return []
except json.JSONDecodeError:
# If not valid JSON, try to extract prompts from text
self.console.print("[yellow]Warning: AI response is not valid JSON, attempting to extract prompts...[/yellow]")
# Look for patterns in the text
lines = response_content.strip().split('\n')
new_prompts = []
for i, line in enumerate(lines[:expected_count]): # Take first N non-empty lines
line = line.strip()
if line and len(line) > 50: # Reasonable minimum length for a prompt
new_prompts.append(line)
return new_prompts
def fill_pool_to_target(self) -> int:
"""Fill the prompt pool to reach cached_pool_volume target with a single API call."""
target_volume = self.settings['cached_pool_volume']
current_pool_size = len(self.pool_prompts)
if current_pool_size >= target_volume:
self.console.print(f"[green]Pool already has {current_pool_size} prompts, target is {target_volume}[/green]")
return 0
prompts_needed = target_volume - current_pool_size
self.console.print(f"[cyan]Current pool size: {current_pool_size}[/cyan]")
self.console.print(f"[cyan]Target pool size: {target_volume}[/cyan]")
self.console.print(f"[cyan]Prompts needed: {prompts_needed}[/cyan]")
# Make a single API call to generate exactly the number of prompts needed
self.console.print(f"\n[cyan]Making single API call to generate {prompts_needed} prompts...[/cyan]")
new_prompts = self.generate_specific_number_of_prompts(prompts_needed)
if new_prompts:
# Add all generated prompts to pool
self.pool_prompts.extend(new_prompts)
total_added = len(new_prompts)
self.console.print(f"[green]Added {total_added} prompts to pool[/green]")
# Save the updated pool
self._save_pool_prompts()
return total_added
else:
self.console.print("[red]Failed to generate prompts[/red]")
return 0
def display_prompts(self, prompts: List[str]):
"""Display generated prompts in a nice format."""
self.console.print("\n" + "="*60)
@@ -544,11 +619,12 @@ class JournalPromptGenerator:
self.console.print(f"[green]Prompt added to history as prompt00[/green]")
elif choice == "2":
# Fill prompt pool using API
new_prompts = self.generate_prompts()
if new_prompts:
self.add_prompts_to_pool(new_prompts)
self.console.print(f"[green]Added {len(new_prompts)} prompts to pool[/green]")
# Fill prompt pool to target volume using API
total_added = self.fill_pool_to_target()
if total_added > 0:
self.console.print(f"[green]Successfully added {total_added} prompts to pool[/green]")
else:
self.console.print("[yellow]No prompts were added to pool[/yellow]")
elif choice == "3":
self.show_pool_stats()
@@ -600,11 +676,12 @@ def main():
elif args.pool_stats:
generator.show_pool_stats()
elif args.fill_pool:
# Fill prompt pool using API
new_prompts = generator.generate_prompts()
if new_prompts:
generator.add_prompts_to_pool(new_prompts)
generator.console.print(f"[green]Added {len(new_prompts)} prompts to pool[/green]")
# Fill prompt pool to target volume using API
total_added = generator.fill_pool_to_target()
if total_added > 0:
generator.console.print(f"[green]Successfully added {total_added} prompts to pool[/green]")
else:
generator.console.print("[yellow]No prompts were added to pool[/yellow]")
elif args.interactive:
generator.interactive_mode()
else: