Skip to content

Commit 12cc161

Browse files
added the instruction adherence logic to AIMon ReAct
1 parent 560324f commit 12cc161

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

aimon/extensions/react.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def react( llm_app,
2929
model_name = react_configuration.model_name,
3030
)
3131

32-
llm_response = llm_app(user_query, reprompted_flag=False)
32+
llm_response = llm_app(user_query, user_instructions, reprompted_flag=False)
3333

3434
## Decorating the context_extractor function with AIMon's "detect"
3535
context_extractor = detect(context_extractor)
@@ -38,25 +38,33 @@ def react( llm_app,
3838

3939
for _ in range(react_configuration.max_attempts):
4040

41-
if aimon_response.detect_response.hallucination['score'] > react_configuration.hallucination_threshold:
42-
llm_response = llm_app(user_query, reprompted_flag=True)
41+
failed_instructions = []
42+
## Loop to check for failed instructions
43+
for x in aimon_response.detect_response.instruction_adherence['results']:
44+
if x['adherence'] == False:
45+
failed_instructions.append(x['instruction'])
46+
47+
## Check whether the hallucination score is greater than the required threshold OR if any of the supplied instructions are not complied with
48+
if react_configuration.hallucination_threshold > 0 and \
49+
(aimon_response.detect_response.hallucination['score'] > react_configuration.hallucination_threshold or len(failed_instructions)>0):
50+
51+
llm_response = llm_app(user_query, user_instructions, reprompted_flag=True)
52+
4353
_, _, _, query_result, aimon_response = context_extractor(user_query, user_instructions, llm_response)
4454

4555
return query_result
4656

4757

48-
## To do:
49-
## Add instruction adherence logic in the next iteration
5058

5159

5260
## llm_app is a function that has both conservative and creative LLMs to its access
5361
## returns the LLM's response to the user's query
5462

5563
## Template for llm_app function
56-
# def llm_app(user_query, reprompted_flag=False):
57-
# creative_llm: function
58-
# conservative_llm: function
64+
# def llm_app(user_query, user_instructions, reprompted_flag=False):
65+
# from aimon_llamaindex import get_response
5966
# if reprompted_flag==False:
60-
# return creative_llm.query(user_query)
67+
# return get_response(user_query, retriever, llm_creative)
6168
# else:
62-
# return conservative_llm.query(user_query)
69+
# llm_conservative.system_prompt += user_instructions
70+
# return get_response(user_query, retriever, llm_conservative)

0 commit comments

Comments
 (0)