Allen Park commited on
Commit
37528ea
·
1 Parent(s): 2a3f17f

remove old json string cleaning function and use Patronus nemo guardrails code

Browse files
Files changed (1) hide show
  1. app.py +27 -26
app.py CHANGED
@@ -6,6 +6,7 @@ import spaces
6
  import openai
7
  import json
8
  import re
 
9
 
10
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
11
  LEPTON_API_TOKEN = os.environ.get("LEPTON_API_TOKEN", None)
@@ -66,25 +67,31 @@ HEADER = """
66
  **Getting Started**: Provide a question and document or context given to your model in addition to the answer given by the model and then click submit. The output panel will indicate whether the reponse is a hallucination (Fail) or if it is faithful to the given document or context (Pass) through the score Pass or Fail and provide reasoning behind the score.
67
  """
68
 
69
- def clean_json_string(json_str):
70
- # # Replace single quotes with double quotes, but not apostrophes within words
71
- # json_str = re.sub(r"(?<!\\)'([^']*)'", r'"\1"', json_str)
72
- # # Add quotes around PASS or FAIL if they're not already quoted
73
- # json_str = re.sub(r'"SCORE":\s*(PASS|FAIL)', r'"SCORE": "\1"', json_str)
74
-
75
- # # Escape double quotes
76
- # json_str = re.sub(r'(?<=: ")([^"]*)"([^"]*)"([^"]*)"', lambda m: m.group(1) + m.group(2).replace('"', '\\"') + m.group(3), json_str)
77
-
78
- # return json_str
79
-
80
- json_str = re.sub(r'"(\w+)"\s*:', r'\1:', json_str)
81
- json_str = re.sub(r':\s*"([^"]*)"', r':"\1"', json_str)
82
-
83
- def unescape_quotes(match):
84
- return match.group(0).replace('\\"', '"')
85
-
86
- json_str = re.sub(r'"([^"\\]*(?:\\.[^"\\]*)*)"', lambda m: '"' + unescape_quotes(m) + '"', json_str)
87
- return json_str
 
 
 
 
 
 
88
 
89
  def model_call(question, document, answer):
90
  if question == "" or document == "" or answer == "":
@@ -96,13 +103,7 @@ def model_call(question, document, answer):
96
  prompt=NEW_FORMAT
97
  )
98
  print("RESPONSE FROM CLIENT:", response)
99
- generated_text = clean_json_string(response.choices[0].text)
100
- print("CLEAN json", generated_text)
101
- generated_text = json.loads(generated_text)
102
- print("GENERATED TEXT", generated_text)
103
- print("type of GENERATED TEXT", type(generated_text))
104
- reasoning = generated_text["REASONING"][0]
105
- score = generated_text["SCORE"]
106
  return reasoning, score
107
 
108
  inputs = [
 
6
  import openai
7
  import json
8
  import re
9
+ from typing import List, Optional, Tuple, Union
10
 
11
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
12
  LEPTON_API_TOKEN = os.environ.get("LEPTON_API_TOKEN", None)
 
67
  **Getting Started**: Provide a question and document or context given to your model in addition to the answer given by the model and then click submit. The output panel will indicate whether the reponse is a hallucination (Fail) or if it is faithful to the given document or context (Pass) through the score Pass or Fail and provide reasoning behind the score.
68
  """
69
 
70
+ def parse_patronus_lynx_response(
71
+ response: str,
72
+ ) -> Tuple[bool, Union[List[str], None]]:
73
+ """
74
+ Parses the response from the Patronus Lynx LLM and returns a tuple of:
75
+ - Whether the response is hallucinated or not.
76
+ - A reasoning trace explaining the decision.
77
+ """
78
+ # Default to hallucinated
79
+ hallucination, reasoning = True, None
80
+ reasoning_pattern = r'"REASONING":\s*\[(.*?)\]'
81
+ score_pattern = r'"SCORE":\s*"?\b(PASS|FAIL)\b"?'
82
+
83
+ reasoning_match = re.search(reasoning_pattern, response, re.DOTALL)
84
+ score_match = re.search(score_pattern, response)
85
+
86
+ if score_match:
87
+ score = score_match.group(1)
88
+ if score == "PASS":
89
+ hallucination = False
90
+ if reasoning_match:
91
+ reasoning_content = reasoning_match.group(1)
92
+ reasoning = re.split(r"['\"],\s*['\"]", reasoning_content)
93
+
94
+ return hallucination, reasoning
95
 
96
  def model_call(question, document, answer):
97
  if question == "" or document == "" or answer == "":
 
103
  prompt=NEW_FORMAT
104
  )
105
  print("RESPONSE FROM CLIENT:", response)
106
+ score, reasoning = parse_patronus_lynx_response(response.choices[0].text)
 
 
 
 
 
 
107
  return reasoning, score
108
 
109
  inputs = [