File size: 1,140 Bytes
4052248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66584f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# utils.py
# A helper file for common utility functions

def load_prompt(file_path: str) -> str:
    """
    Loads a prompt from a specified text file.
    """
    try:
        with open(file_path, "r", encoding="utf-8") as f:
            return f.read()
    except FileNotFoundError:
        print(f"FATAL ERROR: Prompt file not found at {file_path}")
        return f"ERROR: Prompt file not found: {file_path}"
    except Exception as e:
        print(f"FATAL ERROR: Failed to read prompt file {file_path}: {e}")
        return f"ERROR: Failed to read prompt file: {e}"

def extract_json_str(text: str) -> str:
    """
    Extracts the content inside a ```json ... ``` block.
    If no block is found, returns the original text.
    Used to clean 'Chatty' LLM responses before processing.
    """
    clean_text = text.strip()
    if "```json" in clean_text:
        # Split by ```json, take the second part, then split by ``` and take the first part
        return clean_text.split("```json")[1].split("```")[0].strip()
    elif "```" in clean_text:
        return clean_text.split("```")[1].split("```")[0].strip()
    return clean_text