malformed deepseek response handling for formatted json
This commit is contained in:
@@ -310,10 +310,15 @@ class JournalPromptGenerator:
|
||||
"""
|
||||
Parse the AI response to extract new prompts.
|
||||
Expected format: JSON array with keys "newprompt0" to "newpromptN" where N = num_prompts-1
|
||||
|
||||
Handles DeepSeek API responses that may include backticks and leading "json" string.
|
||||
"""
|
||||
# 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(response_content)
|
||||
data = json.loads(cleaned_content)
|
||||
|
||||
# Convert to list of prompt dictionaries
|
||||
new_prompts = []
|
||||
@@ -333,6 +338,7 @@ class JournalPromptGenerator:
|
||||
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]")
|
||||
self.console.print(f"[yellow]Cleaned content: {cleaned_content}[/yellow]")
|
||||
|
||||
# Look for patterns in the text
|
||||
lines = response_content.strip().split('\n')
|
||||
@@ -346,8 +352,55 @@ class JournalPromptGenerator:
|
||||
}
|
||||
new_prompts.append(prompt_obj)
|
||||
|
||||
# If still no prompts could be parsed, dump the full payload for debugging
|
||||
if not new_prompts:
|
||||
self.console.print("[red]Error: Could not extract any prompts from AI response[/red]")
|
||||
self.console.print("[red]Full payload dump for debugging:[/red]")
|
||||
self.console.print(f"[red]{response_content}[/red]")
|
||||
|
||||
return new_prompts
|
||||
|
||||
def _clean_ai_response(self, response_content: str) -> str:
|
||||
"""
|
||||
Clean up AI response content to handle common formatting issues from DeepSeek API.
|
||||
|
||||
Handles:
|
||||
1. Leading/trailing backticks (```json ... ```)
|
||||
2. Leading "json" string on its own line
|
||||
3. Extra whitespace and newlines
|
||||
"""
|
||||
content = response_content.strip()
|
||||
|
||||
# Remove leading/trailing backticks (```json ... ```)
|
||||
if content.startswith('```'):
|
||||
# Find the first newline after the opening backticks
|
||||
lines = content.split('\n')
|
||||
if len(lines) > 1:
|
||||
# Check if first line contains "json" or other language specifier
|
||||
first_line = lines[0].strip()
|
||||
if 'json' in first_line.lower() or first_line == '```':
|
||||
# Remove the first line (```json or ```)
|
||||
content = '\n'.join(lines[1:])
|
||||
|
||||
# Remove trailing backticks if present
|
||||
if content.endswith('```'):
|
||||
content = content[:-3].rstrip()
|
||||
|
||||
# Remove leading "json" string on its own line (case-insensitive)
|
||||
lines = content.split('\n')
|
||||
if len(lines) > 0:
|
||||
first_line = lines[0].strip().lower()
|
||||
if first_line == 'json':
|
||||
content = '\n'.join(lines[1:])
|
||||
|
||||
# Also handle the case where "json" might be at the beginning of the first line
|
||||
# but not the entire line (e.g., "json\n{...}")
|
||||
content = content.strip()
|
||||
if content.lower().startswith('json\n'):
|
||||
content = content[4:].strip()
|
||||
|
||||
return content.strip()
|
||||
|
||||
def generate_prompts(self) -> List[Dict[str, str]]:
|
||||
"""Generate new journal prompts using AI."""
|
||||
self.console.print("\n[cyan]Generating new journal prompts...[/cyan]")
|
||||
|
||||
@@ -1 +1,5 @@
|
||||
[]
|
||||
[
|
||||
{
|
||||
"poolprompt004": "Recall a promise you made to yourself long ago\u2014something significant you vowed to do, be, or avoid. It might have been written down, solemnly sworn, or just a quiet internal pact. Have you kept it? If so, describe the journey of that fidelity. What did it cost you, and what did it give you? If not, explore the moment or the gradual process of breaking that promise. Was it a betrayal or a necessary evolution? Write a letter to your past self about that promise, explaining the current state of affairs with compassion and honesty."
|
||||
}
|
||||
]
|
||||
@@ -5,7 +5,7 @@
|
||||
[prompts]
|
||||
min_length = 500
|
||||
max_length = 1000
|
||||
num_prompts = 3
|
||||
num_prompts = 4
|
||||
|
||||
# Prefetch not yet implmented
|
||||
[prefetch]
|
||||
|
||||
@@ -24,6 +24,7 @@ class SimplePromptGenerator:
|
||||
self.config_path = config_path
|
||||
self.client = None
|
||||
self.historic_prompts = []
|
||||
self.pool_prompts = []
|
||||
self.prompt_template = ""
|
||||
self.settings = {}
|
||||
|
||||
@@ -34,6 +35,7 @@ class SimplePromptGenerator:
|
||||
# Load data files
|
||||
self._load_prompt_template()
|
||||
self._load_historic_prompts()
|
||||
self._load_pool_prompts()
|
||||
|
||||
def _load_config(self):
|
||||
"""Load configuration from environment file."""
|
||||
|
||||
Reference in New Issue
Block a user