Spaces:
Sleeping
Sleeping
Allen Park
commited on
Commit
·
37528ea
1
Parent(s):
2a3f17f
remove old json string cleaning function and use Patronus nemo guardrails code
Browse files
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
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
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 = [
|