Skip to content

Commit

Permalink
updated the react method to returns a list of lists where each list c…
Browse files Browse the repository at this point in the history
…omprises of llm_responses and respecitve hallucination score.
  • Loading branch information
devvratbhardwaj committed Jan 11, 2025
1 parent aa4e6f4 commit 4cd39f2
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions aimon/extensions/react.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def detect_aimon_response(self,aimon_payload):
## ReAct -> Reason and Act
def react(self, user_query, user_instructions,):

result = []

llm_response = self.llm_app(user_query, user_instructions, reprompted_flag=False)

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

detect_response = self.detect_aimon_response(aimon_payload)

hallucination_score = detect_response.hallucination['score']

result.append([generated_text, hallucination_score])

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

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

hallucination_score = detect_response.hallucination['score']

## Check whether the hallucination score is greater than the required threshold OR if any of the supplied instructions are not complied with
if self.react_configuration.hallucination_threshold > 0 and \
(hallucination_score > self.react_configuration.hallucination_threshold or len(failed_instructions)>0):
Expand All @@ -104,8 +108,14 @@ def react(self, user_query, user_instructions,):

detect_response = self.detect_aimon_response(new_aimon_payload)

hallucination_score = detect_response.hallucination['score']

result.append([generated_text, hallucination_score])

else:
break

if hallucination_score > self.react_configuration.hallucination_threshold:
return f"The generated LLM response, even after {self.react_configuration.max_attempts} attempts of ReAct is still hallucinated. The response: {generated_text}"
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])

return generated_text
return result

0 comments on commit 4cd39f2

Please sign in to comment.