MudabbirAI / utils.py
youssefleb's picture
Update utils.py
66584f5 verified
# 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