working checkpoint before caching attempt

This commit is contained in:
2026-01-02 16:13:06 -07:00
parent 5cea0ba44c
commit 6dc8672dd8
6 changed files with 144 additions and 16 deletions

View File

@@ -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 = {