From 6dc8672dd867281915ab0155a49e9b953c3ce0ab Mon Sep 17 00:00:00 2001 From: finn Date: Fri, 2 Jan 2026 16:13:06 -0700 Subject: [PATCH] working checkpoint before caching attempt --- ds_prompt.txt | 3 +- generate_prompts.py | 65 ++++++++++++++++++++++++++++++++++++++++---- settings.cfg | 2 +- simple_generate.py | 63 ++++++++++++++++++++++++++++++++++++++---- test_project.py | 4 +-- test_prompt_logic.py | 23 ++++++++++++++-- 6 files changed, 144 insertions(+), 16 deletions(-) diff --git a/ds_prompt.txt b/ds_prompt.txt index c8dc98a..a603372 100644 --- a/ds_prompt.txt +++ b/ds_prompt.txt @@ -4,5 +4,6 @@ Topics can be diverse. The prompts should be between 500 and 1000 characters. The previous 60 prompts have been provided as a JSON array for reference. Attempt minimal repetition, but some thematic overlap is acceptable. Newly generated prompts should try extra hard to avoid repetition in previous prompts with the lowest indices. -Output as a JSON array with key names from "newprompt0" to "newprompt5". +Output as a JSON array with key names from "newprompt0" to "newpromptN" where N is the number of prompts minus one. +Respond ONLY with valid JSON. No explanations, no markdown, no backticks. diff --git a/generate_prompts.py b/generate_prompts.py index a8b3ac9..be4d1d8 100644 --- a/generate_prompts.py +++ b/generate_prompts.py @@ -8,6 +8,7 @@ import os import json import sys import argparse +import configparser from datetime import datetime from typing import List, Dict, Any, Optional from pathlib import Path @@ -31,9 +32,11 @@ class JournalPromptGenerator: self.client = None self.historic_prompts = [] self.prompt_template = "" + self.settings = {} # Load configuration self._load_config() + self._load_settings() # Load data files self._load_prompt_template() @@ -62,11 +65,61 @@ class JournalPromptGenerator: base_url=self.base_url ) + def _load_settings(self): + """Load settings from settings.cfg configuration file.""" + config = configparser.ConfigParser() + + # Set default values + self.settings = { + 'min_length': 500, + 'max_length': 1000, + 'num_prompts': 6 + } + + try: + config.read('settings.cfg') + + if 'prompts' in config: + prompts_section = config['prompts'] + + # Load min_length + if 'min_length' in prompts_section: + self.settings['min_length'] = int(prompts_section['min_length']) + + # Load max_length + if 'max_length' in prompts_section: + self.settings['max_length'] = int(prompts_section['max_length']) + + # Load num_prompts + if 'num_prompts' in prompts_section: + self.settings['num_prompts'] = int(prompts_section['num_prompts']) + + except FileNotFoundError: + self.console.print("[yellow]Warning: settings.cfg not found, using default values[/yellow]") + except ValueError as e: + self.console.print(f"[yellow]Warning: Invalid value in settings.cfg: {e}, using default values[/yellow]") + except Exception as e: + 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.""" + """Load the prompt template from ds_prompt.txt and update with config values.""" try: with open("ds_prompt.txt", "r") as f: - self.prompt_template = f.read() + 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 except FileNotFoundError: self.console.print("[red]Error: ds_prompt.txt not found[/red]") sys.exit(1) @@ -154,7 +207,7 @@ class JournalPromptGenerator: def _parse_ai_response(self, response_content: str) -> List[Dict[str, str]]: """ Parse the AI response to extract new prompts. - Expected format: JSON array with keys "newprompt0" to "newprompt5" + Expected format: JSON array with keys "newprompt0" to "newpromptN" where N = num_prompts-1 """ try: # Try to parse as JSON @@ -162,7 +215,7 @@ class JournalPromptGenerator: # Convert to list of prompt dictionaries new_prompts = [] - for i in range(6): + for i in range(self.settings['num_prompts']): key = f"newprompt{i}" if key in data: prompt_text = data[key] @@ -176,12 +229,14 @@ class JournalPromptGenerator: 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]") + self.console.print(f"[yellow]Full response content for debugging:[/yellow]") + self.console.print(f"[yellow]{response_content}[/yellow]") # Look for patterns in the text lines = response_content.strip().split('\n') new_prompts = [] - for i, line in enumerate(lines[:6]): # Take first 6 non-empty lines + for i, line in enumerate(lines[:self.settings['num_prompts']]): # Take first N non-empty lines line = line.strip() if line and len(line) > 50: # Reasonable minimum length for a prompt prompt_obj = { diff --git a/settings.cfg b/settings.cfg index 9d6538d..b34f725 100644 --- a/settings.cfg +++ b/settings.cfg @@ -5,7 +5,7 @@ [prompts] min_length = 500 max_length = 1000 -num_prompts = 3 +num_prompts = 8 # Prefetch not yet implmented [prefetch] diff --git a/simple_generate.py b/simple_generate.py index ac86ba5..bf41dfd 100644 --- a/simple_generate.py +++ b/simple_generate.py @@ -8,6 +8,7 @@ import os import json import sys import argparse +import configparser from datetime import datetime from typing import List, Dict, Any @@ -24,9 +25,11 @@ class SimplePromptGenerator: self.client = None self.historic_prompts = [] self.prompt_template = "" + self.settings = {} # Load configuration self._load_config() + self._load_settings() # Load data files self._load_prompt_template() @@ -55,11 +58,61 @@ class SimplePromptGenerator: base_url=self.base_url ) + def _load_settings(self): + """Load settings from settings.cfg configuration file.""" + config = configparser.ConfigParser() + + # Set default values + self.settings = { + 'min_length': 500, + 'max_length': 1000, + 'num_prompts': 6 + } + + try: + config.read('settings.cfg') + + if 'prompts' in config: + prompts_section = config['prompts'] + + # Load min_length + if 'min_length' in prompts_section: + self.settings['min_length'] = int(prompts_section['min_length']) + + # Load max_length + if 'max_length' in prompts_section: + self.settings['max_length'] = int(prompts_section['max_length']) + + # Load num_prompts + if 'num_prompts' in prompts_section: + self.settings['num_prompts'] = int(prompts_section['num_prompts']) + + except FileNotFoundError: + print("Warning: settings.cfg not found, using default values") + except ValueError as e: + print(f"Warning: Invalid value in settings.cfg: {e}, using default values") + except Exception as e: + print(f"Warning: Error reading settings.cfg: {e}, using default values") + def _load_prompt_template(self): - """Load the prompt template from ds_prompt.txt.""" + """Load the prompt template from ds_prompt.txt and update with config values.""" try: with open("ds_prompt.txt", "r") as f: - self.prompt_template = f.read() + 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 except FileNotFoundError: print("Error: ds_prompt.txt not found") sys.exit(1) @@ -100,7 +153,7 @@ class SimplePromptGenerator: # Convert to list of prompt dictionaries new_prompts = [] - for i in range(6): + for i in range(self.settings['num_prompts']): key = f"newprompt{i}" if key in data: prompt_text = data[key] @@ -119,7 +172,7 @@ class SimplePromptGenerator: lines = response_content.strip().split('\n') new_prompts = [] - for i, line in enumerate(lines[:6]): + for i, line in enumerate(lines[:self.settings['num_prompts']]): line = line.strip() if line and len(line) > 50: prompt_obj = { @@ -226,7 +279,7 @@ def main(): parser.add_argument( "--save", "-S", type=int, - help="Save a specific prompt number to file (1-6)" + help="Save a specific prompt number to file" ) parser.add_argument( "--config", "-c", diff --git a/test_project.py b/test_project.py index bac9110..206c526 100644 --- a/test_project.py +++ b/test_project.py @@ -80,8 +80,8 @@ def test_prompt_template(): if len(content) > 0: print(f" ✓ ds_prompt.txt is readable ({len(content)} characters)") - # Check for key phrases - key_phrases = ["6 writing prompts", "500 and 1000 characters", "JSON array"] + # Check for key phrases (now configurable, so just check for basic structure) + key_phrases = ["writing prompts", "characters", "JSON array"] found_phrases = [] for phrase in key_phrases: if phrase.lower() in content.lower(): diff --git a/test_prompt_logic.py b/test_prompt_logic.py index 17d1bc2..99f5608 100644 --- a/test_prompt_logic.py +++ b/test_prompt_logic.py @@ -4,11 +4,29 @@ Test script to verify the prompt numbering logic. """ import json +import configparser from datetime import datetime +def get_num_prompts(): + """Get the number of prompts from settings.cfg or default.""" + config = configparser.ConfigParser() + num_prompts = 6 # Default value + + try: + config.read('settings.cfg') + if 'prompts' in config and 'num_prompts' in config['prompts']: + num_prompts = int(config['prompts']['num_prompts']) + except (FileNotFoundError, ValueError): + pass + + return num_prompts + def test_renumbering(): """Test the renumbering logic.""" + # Get number of prompts from config + num_prompts = get_num_prompts() + # Create a sample historic prompts list historic_prompts = [] for i in range(60): @@ -19,10 +37,11 @@ def test_renumbering(): print(f"Original prompts: {len(historic_prompts)}") print(f"First prompt key: {list(historic_prompts[0].keys())[0]}") print(f"Last prompt key: {list(historic_prompts[-1].keys())[0]}") + print(f"Number of prompts from config: {num_prompts}") - # Simulate adding 6 new prompts (as the current code would create them) + # Simulate adding new prompts (as the current code would create them) new_prompts = [] - for i in range(6): + for i in range(num_prompts): new_prompts.append({ f"prompt{len(historic_prompts) + i:02d}": f"New prompt {i}" })