youssefleb commited on
Commit
66584f5
·
verified ·
1 Parent(s): 5701d46

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +15 -3
utils.py CHANGED
@@ -4,10 +4,8 @@
4
  def load_prompt(file_path: str) -> str:
5
  """
6
  Loads a prompt from a specified text file.
7
- We use this to keep prompts separate from code.
8
  """
9
  try:
10
- # We specify 'encoding="utf-8"' to ensure all characters load correctly
11
  with open(file_path, "r", encoding="utf-8") as f:
12
  return f.read()
13
  except FileNotFoundError:
@@ -15,4 +13,18 @@ def load_prompt(file_path: str) -> str:
15
  return f"ERROR: Prompt file not found: {file_path}"
16
  except Exception as e:
17
  print(f"FATAL ERROR: Failed to read prompt file {file_path}: {e}")
18
- return f"ERROR: Failed to read prompt file: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  def load_prompt(file_path: str) -> str:
5
  """
6
  Loads a prompt from a specified text file.
 
7
  """
8
  try:
 
9
  with open(file_path, "r", encoding="utf-8") as f:
10
  return f.read()
11
  except FileNotFoundError:
 
13
  return f"ERROR: Prompt file not found: {file_path}"
14
  except Exception as e:
15
  print(f"FATAL ERROR: Failed to read prompt file {file_path}: {e}")
16
+ return f"ERROR: Failed to read prompt file: {e}"
17
+
18
+ def extract_json_str(text: str) -> str:
19
+ """
20
+ Extracts the content inside a ```json ... ``` block.
21
+ If no block is found, returns the original text.
22
+ Used to clean 'Chatty' LLM responses before processing.
23
+ """
24
+ clean_text = text.strip()
25
+ if "```json" in clean_text:
26
+ # Split by ```json, take the second part, then split by ``` and take the first part
27
+ return clean_text.split("```json")[1].split("```")[0].strip()
28
+ elif "```" in clean_text:
29
+ return clean_text.split("```")[1].split("```")[0].strip()
30
+ return clean_text