Skip to content

Commit 4cd39f2

Browse files
updated the react method to returns a list of lists where each list comprises of llm_responses and respecitve hallucination score.
1 parent aa4e6f4 commit 4cd39f2

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

aimon/extensions/react.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ def detect_aimon_response(self,aimon_payload):
6262
## ReAct -> Reason and Act
6363
def react(self, user_query, user_instructions,):
6464

65+
result = []
66+
6567
llm_response = self.llm_app(user_query, user_instructions, reprompted_flag=False)
6668

6769
context = self.context_extractor(user_query, user_instructions, llm_response)
@@ -75,6 +77,10 @@ def react(self, user_query, user_instructions,):
7577
aimon_payload = self.create_payload(context, user_query, user_instructions, generated_text)
7678

7779
detect_response = self.detect_aimon_response(aimon_payload)
80+
81+
hallucination_score = detect_response.hallucination['score']
82+
83+
result.append([generated_text, hallucination_score])
7884

7985
for _ in range(self.react_configuration.max_attempts):
8086

@@ -84,8 +90,6 @@ def react(self, user_query, user_instructions,):
8490
if x['adherence'] == False:
8591
failed_instructions.append(x['instruction'])
8692

87-
hallucination_score = detect_response.hallucination['score']
88-
8993
## Check whether the hallucination score is greater than the required threshold OR if any of the supplied instructions are not complied with
9094
if self.react_configuration.hallucination_threshold > 0 and \
9195
(hallucination_score > self.react_configuration.hallucination_threshold or len(failed_instructions)>0):
@@ -104,8 +108,14 @@ def react(self, user_query, user_instructions,):
104108

105109
detect_response = self.detect_aimon_response(new_aimon_payload)
106110

111+
hallucination_score = detect_response.hallucination['score']
112+
113+
result.append([generated_text, hallucination_score])
114+
115+
else:
116+
break
107117

108118
if hallucination_score > self.react_configuration.hallucination_threshold:
109-
return f"The generated LLM response, even after {self.react_configuration.max_attempts} attempts of ReAct is still hallucinated. The response: {generated_text}"
119+
result.append([f"The generated LLM response, even after {self.react_configuration.max_attempts} attempts of AIMon ReAct is still hallucinated. Final LLM response: {generated_text}", hallucination_score])
110120

111-
return generated_text
121+
return result

0 commit comments

Comments
 (0)