Skip to content

Commit 1ca3f79

Browse files
committed
fix(hallucination-check): Rename check_hallucination to self_check_hallucination
This commit renames all instances of 'check_hallucination' to 'self_check_hallucination' across multiple files. This includes changes in function names, task names, and documentation.
1 parent 4fbddce commit 1ca3f79

File tree

11 files changed

+41
-26
lines changed

11 files changed

+41
-26
lines changed

docs/user_guides/guardrails-library.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,14 @@ To use the hallucination rail, you should:
323323
rails:
324324
input:
325325
flows:
326-
- self check hallucinations
326+
- self check hallucination
327327
```
328328

329-
2. Define a `self_check_hallucinations` prompt in the `prompts.yml` file:
329+
2. Define a `self_check_hallucination` prompt in the `prompts.yml` file:
330330

331331
```yaml
332332
prompts:
333-
- task: self_check_hallucinations
333+
- task: self_check_hallucination
334334
content: |-
335335
You are given a task to identify if the hypothesis is in agreement with the context below.
336336
You will only use the contents of the context and not rely on external knowledge.

examples/configs/rag/custom_rag_output_rails/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ rails:
77
output:
88
flows:
99
- self check facts
10-
- check hallucination
10+
- self check hallucination
1111

1212
prompts:
1313
- task: self_check_facts

examples/sample_config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ rails:
3434
enable_on_predefined_messages: false
3535
flows:
3636
- self check facts
37-
- check hallucination
37+
- self check hallucination
3838
- activefence moderation
3939
- check sensitive data
4040
- gotitai rag truthcheck

nemoguardrails/eval/evaluate_hallucination.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def get_extra_responses(self, prompt, num_responses=2):
102102

103103
return extra_responses
104104

105-
def check_hallucination(self):
105+
def self_check_hallucination(self):
106106
"""
107107
Run the hallucination rail evaluation.
108108
For each prompt, generate 2 extra responses from the LLM and check consistency with the bot response.
@@ -150,7 +150,7 @@ def check_hallucination(self):
150150
else:
151151
paragraph = ". ".join(extra_responses)
152152
hallucination_check_prompt = self.llm_task_manager.render_task_prompt(
153-
Task.CHECK_HALLUCINATION,
153+
Task.SELF_CHECK_HALLUCINATION,
154154
{"paragraph": paragraph, "statement": bot_response},
155155
)
156156
hallucination = self.llm(hallucination_check_prompt)
@@ -177,7 +177,7 @@ def run(self):
177177
hallucination_check_predictions,
178178
num_flagged,
179179
num_error,
180-
) = self.check_hallucination()
180+
) = self.self_check_hallucination()
181181
print(
182182
f"% of samples flagged as hallucinations: {num_flagged/len(self.dataset) * 100}"
183183
)

nemoguardrails/library/hallucination/actions.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141

4242
@action()
43-
async def check_hallucination(
43+
async def self_check_hallucination(
4444
llm_task_manager: LLMTaskManager,
4545
context: Optional[dict] = None,
4646
llm: Optional[BaseLLM] = None,
@@ -58,14 +58,17 @@ async def check_hallucination(
5858
"The langchain_openai module is not installed. Please install it using pip: pip install langchain_openai"
5959
)
6060

61+
print(hasattr(llm, "best_of"))
62+
print("***" * 100)
6163
bot_response = context.get("bot_message")
6264
last_bot_prompt_string = context.get("_last_bot_prompt")
6365

6466
if bot_response and last_bot_prompt_string:
6567
num_responses = HALLUCINATION_NUM_EXTRA_RESPONSES
6668
# Use beam search for the LLM call, to get several completions with only one call.
6769
# At the current moment, only OpenAI LLM engines are supported for computing the additional completions.
68-
if type(llm) != OpenAI:
70+
#
71+
if "openai" not in str(type(llm)).lower():
6972
log.warning(
7073
f"Hallucination rail can only be used with OpenAI LLM engines."
7174
f"Current LLM engine is {type(llm).__name__}."
@@ -77,7 +80,17 @@ async def check_hallucination(
7780
chain = LLMChain(prompt=last_bot_prompt, llm=llm)
7881

7982
# Generate multiple responses with temperature 1.
80-
with llm_params(llm, temperature=1.0, n=num_responses, best_of=num_responses):
83+
with llm_params(llm, temperature=1.0, n=num_responses):
84+
# best_of
85+
# integer or null
86+
#
87+
# Optional
88+
# Defaults to 1
89+
# Generates best_of completions server-side and returns the "best" (the one with the highest log probability per token). Results cannot be streamed.
90+
#
91+
# When used with n, best_of controls the number of candidate completions and n specifies how many to return – best_of must be greater than n.
92+
#
93+
# Note: Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for max_tokens and stop.
8194
extra_llm_response = await chain.agenerate(
8295
[{"text": last_bot_prompt_string}],
8396
run_manager=logging_callback_manager_for_chain,
@@ -112,16 +125,16 @@ async def check_hallucination(
112125
if use_llm_checking:
113126
# Only support LLM-based agreement check in current version
114127
prompt = llm_task_manager.render_task_prompt(
115-
task=Task.CHECK_HALLUCINATION,
128+
task=Task.SELF_CHECK_HALLUCINATION,
116129
context={
117130
"statement": bot_response,
118131
"paragraph": ". ".join(extra_responses),
119132
},
120133
)
121134

122135
# Initialize the LLMCallInfo object
123-
llm_call_info_var.set(LLMCallInfo(task=Task.CHECK_HALLUCINATION.value))
124-
stop = llm_task_manager.get_stop_tokens(task=Task.CHECK_HALLUCINATION)
136+
llm_call_info_var.set(LLMCallInfo(task=Task.SELF_CHECK_HALLUCINATION.value))
137+
stop = llm_task_manager.get_stop_tokens(task=Task.SELF_CHECK_HALLUCINATION)
125138

126139
with llm_params(llm, temperature=config.lowest_temperature):
127140
agreement = await llm_call(llm, prompt, stop=stop)

nemoguardrails/library/hallucination/flows.co

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ define flow hallucination warning
55
"""Warning rail for hallucination."""
66
bot ...
77
if $hallucination_warning == True
8-
$is_hallucination = execute check_hallucination
8+
$is_hallucination = execute self_check_hallucination
99
$hallucination_warning = False
1010

1111
if $is_hallucination
@@ -16,10 +16,10 @@ define bot inform answer prone to hallucination
1616
"The above response may have been hallucinated, and should be independently verified."
1717

1818

19-
define subflow check hallucination
19+
define subflow self check hallucination
2020
"""Output rail for checking hallucinations."""
2121
if $check_hallucination == True
22-
$is_hallucination = execute check_hallucination
22+
$is_hallucination = execute self_check_hallucination
2323
$check_hallucination = False
2424

2525
if $is_hallucination

nemoguardrails/llm/filters.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def co_v2(
6262
"zapier_nla_query",
6363
"call activefence api",
6464
"jailbreak_detection_heuristics",
65-
"check_hallucination",
65+
"self_check_hallucination",
6666
"llama_guard_check_input",
6767
"llama_guard_check_output",
6868
"alignscore_check_facts",

nemoguardrails/llm/prompts/nemollm.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,14 @@ prompts:
119119
You will only use the contents of the evidence and not rely on external knowledge.
120120
Answer with yes/no. "evidence": {{ evidence }} "hypothesis": {{ response }} "entails":
121121
122-
123-
- task: check_hallucination
122+
- task: self_check_hallucination
124123
models:
125124
- nemollm
126125
content: |-
127126
You are given a task to identify if the hypothesis is in agreement with the context below.
128127
You will only use the contents of the context and not rely on external knowledge.
129128
Answer with yes/no. "context": {{ paragraph }} "hypothesis": {{ statement }} "agreement":
130129
131-
132130
# Prompts for compact mode for dialogue rails
133131
# Prompt for detecting the user message canonical form in compact form.
134132
- task: generate_user_intent

nemoguardrails/llm/types.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ class Task(Enum):
4646
)
4747

4848
SELF_CHECK_FACTS = "fact_checking"
49-
CHECK_HALLUCINATION = "check_hallucination"
49+
SELF_CHECK_HALLUCINATION = "self_check_hallucination"

tests/test_configs/with_prompt_modes/prompts/prompts.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ prompts:
1313
content: |-
1414
<<This custom prompt generates the user intent>>
1515
16-
- task: check_hallucination
16+
- task: self_check_hallucination
1717
models:
1818
- nemollm
1919
content: |-
2020
<<This is a long placeholder prompt to check for hallucinations>>
2121
22-
- task: check_hallucination
22+
- task: self_check_hallucination
2323
models:
2424
- nemollm
2525
mode: compact

tests/test_prompt_modes.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@
3333
Task.GENERATE_USER_INTENT,
3434
"<<This is a placeholder for a custom prompt for generating the user intent using gpt-3.5-turbo>>",
3535
),
36-
("task3_nemo_compact", Task.CHECK_HALLUCINATION, "<<Check for hallucinations>>"),
36+
(
37+
"task3_nemo_compact",
38+
Task.SELF_CHECK_HALLUCINATION,
39+
"<<Check for hallucinations>>",
40+
),
3741
(
3842
"task4_nemo_standard",
39-
Task.CHECK_HALLUCINATION,
43+
Task.SELF_CHECK_HALLUCINATION,
4044
"<<This is a long placeholder prompt to check for hallucinations>>",
4145
),
4246
]

0 commit comments

Comments
 (0)