Skip to content

Commit ca63a89

Browse files
removed factcheck input guardrail
1 parent d1f93d7 commit ca63a89

File tree

6 files changed

+3
-120
lines changed

6 files changed

+3
-120
lines changed

examples/configs/autoguard/autoguard_factcheck_config/config.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ rails:
77
autoguard:
88
parameters:
99
fact_check_endpoint: "https://nvidia.autoalign.ai/factcheck"
10-
input:
11-
flows:
12-
- autoguard factcheck input
1310
output:
1411
flows:
1512
- autoguard factcheck output

nemoguardrails/library/autoguard/README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -563,18 +563,15 @@ rails:
563563
autoguard:
564564
parameters:
565565
fact_check_endpoint: "https://nvidia.autoalign.ai/factcheck"
566-
input:
567-
flows:
568-
- autoguard factcheck input
569566
output:
570567
flows:
571568
- autoguard factcheck output
572569
```
573570

574571
Specify the factcheck endpoint the parameters section of autoguard's config.
575-
Then, you have to call the corresponding subflows for input and output factcheck guardrails.
572+
Then, you have to call the corresponding subflows for factcheck guardrails.
576573

577-
Following is the format of the colang file:
574+
Following is the format of the colang file, which is present in the library:
578575
```colang
579576
define flow autoguard factcheck input
580577
execute autoguard_retrieve_relevant_chunks_input
@@ -595,9 +592,6 @@ define bot inform autoguard factcheck output violation
595592
"$bot_message Factcheck output violation has been detected by AutoGuard."
596593
```
597594

598-
Within the flow you can see we have an action for custom relevant chunk extraction, `autoguard_retrieve_relevant_chunks_input`,
599-
which ensures that the documents are passed in the context for the guardrail while using it for user input.
600-
601595
The output of the factcheck endpoint provides you with a factcheck score against which we can add a threshold which determines whether the given output is factually correct or not.
602596

603597
The supporting documents or the evidence has to be placed within a `kb` folder within `config` folder.

nemoguardrails/library/autoguard/actions.py

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -265,32 +265,6 @@ async def autoguard_output_api(
265265
return autoguard_response
266266

267267

268-
@action(name="autoguard_factcheck_input_api")
269-
async def autoguard_factcheck_input_api(
270-
llm_task_manager: LLMTaskManager, context: Optional[dict] = None
271-
):
272-
"""Calls AutoGuard factcheck API and checks whether the user message is factually correct according to given
273-
documents"""
274-
275-
user_message = context.get("user_message")
276-
documents = context.get("relevant_chunks", [])
277-
autoguard_config = llm_task_manager.config.rails.config.autoguard
278-
autoguard_fact_check_api_url = autoguard_config.parameters.get(
279-
"fact_check_endpoint"
280-
)
281-
if not autoguard_fact_check_api_url:
282-
raise ValueError("Provide the autoguard factcheck endpoint in the config")
283-
if isinstance(documents, str):
284-
documents = documents.split("\n")
285-
prompt = user_message
286-
if isinstance(documents, list) and len(documents) > 0:
287-
return await autoguard_factcheck_infer(
288-
autoguard_fact_check_api_url, prompt, documents
289-
)
290-
else:
291-
raise ValueError("Provide relevant documents in proper format")
292-
293-
294268
@action(name="autoguard_factcheck_output_api")
295269
async def autoguard_factcheck_output_api(
296270
llm_task_manager: LLMTaskManager, context: Optional[dict] = None
@@ -315,24 +289,3 @@ async def autoguard_factcheck_output_api(
315289
)
316290
else:
317291
raise ValueError("Provide relevant documents in proper format")
318-
319-
320-
@action(name="autoguard_retrieve_relevant_chunks_input")
321-
async def autoguard_retrieve_relevant_chunks_input(
322-
context: Optional[dict] = None,
323-
kb: Optional[KnowledgeBase] = None,
324-
):
325-
"""Retrieve knowledge chunks from knowledge base and update the context."""
326-
user_message = context.get("user_message")
327-
context_updates = {}
328-
chunks = await kb.search_relevant_chunks(user_message)
329-
chunks = [chunk["body"] for chunk in chunks]
330-
# 💡 Store the chunks for fact-checking
331-
332-
context_updates["relevant_chunks"] = "\n".join(chunks)
333-
context_updates["relevant_chunks_sep"] = chunks
334-
335-
return ActionResult(
336-
return_value=context_updates["relevant_chunks"],
337-
context_updates=context_updates,
338-
)

nemoguardrails/library/autoguard/flows.co

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
define flow autoguard check input
22
$input_result = execute autoguard_input_api(show_autoguard_message=True)
3-
43
if $input_result["guardrails_triggered"]
54
$autoguard_input_response = $input_result['combined_response']
65
bot refuse to respond
76
stop
87

98
define flow autoguard check output
109
$output_result = execute autoguard_output_api(show_autoguard_message=True)
11-
1210
if $output_result["guardrails_triggered"]
1311
bot refuse to respond
1412
stop
@@ -18,21 +16,12 @@ define flow autoguard check output
1816
bot respond pii output
1917
stop
2018

21-
define flow autoguard factcheck input
22-
execute autoguard_retrieve_relevant_chunks_input
23-
$input_result = execute autoguard_factcheck_input_api
24-
2519
define flow autoguard factcheck output
2620
$output_result = execute autoguard_factcheck_output_api
27-
if $input_result < 0.5
28-
bot inform autoguard factcheck input violation
2921
if $output_result < 0.5
3022
bot inform autoguard factcheck output violation
3123
stop
3224

33-
define bot inform autoguard factcheck input violation
34-
"Factcheck violation in user input has been detected by AutoGuard."
35-
3625
define bot inform autoguard factcheck output violation
3726
"Factcheck violation in llm response has been detected by AutoGuard."
3827

tests/test_autoguard_factcheck.py

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,6 @@ async def retrieve_relevant_chunks():
4747
)
4848

4949

50-
@action(is_system_action=True)
51-
async def autoguard_retrieve_relevant_chunks_input():
52-
"""Retrieve relevant chunks from the knowledge base and add them to the context."""
53-
context_updates = {}
54-
relevant_chunks = "\n".join(build_kb())
55-
context_updates["relevant_chunks"] = relevant_chunks
56-
57-
return ActionResult(
58-
return_value=context_updates["relevant_chunks"],
59-
context_updates=context_updates,
60-
)
61-
62-
6350
@pytest.mark.asyncio
6451
async def test_fact_checking_correct(httpx_mock):
6552
config = RailsConfig.from_path(os.path.join(CONFIGS_FOLDER, "autoguard_factcheck"))
@@ -75,19 +62,6 @@ async def test_fact_checking_correct(httpx_mock):
7562
],
7663
)
7764

78-
async def mock_autoguard_factcheck_input_api(
79-
context: Optional[dict] = None, **kwargs
80-
):
81-
query = context.get("user_message")
82-
if (
83-
query
84-
== "Pluto, with its eccentric orbit, comes closer to the Sun than Neptune at times, yet a stable "
85-
"orbital resonance ensures they do not collide."
86-
):
87-
return 1.0
88-
else:
89-
return 0.0
90-
9165
async def mock_autoguard_factcheck_output_api(
9266
context: Optional[dict] = None, **kwargs
9367
):
@@ -106,9 +80,6 @@ async def mock_autoguard_factcheck_output_api(
10680
else:
10781
return 0.0
10882

109-
chat.app.register_action(
110-
mock_autoguard_factcheck_input_api, "autoguard_factcheck_input_api"
111-
)
11283
chat.app.register_action(
11384
mock_autoguard_factcheck_output_api, "autoguard_factcheck_output_api"
11485
)
@@ -144,23 +115,6 @@ async def test_fact_checking_wrong(httpx_mock):
144115
],
145116
)
146117

147-
async def mock_autoguard_factcheck_input_api(
148-
context: Optional[dict] = None, **kwargs
149-
):
150-
query = context.get("user_message")
151-
if (
152-
query
153-
== "Pluto has no known moons; Charon, the smallest, has a diameter greater than Pluto's, along with "
154-
"the non-existent Styx, Nix, Kerberos, and Hydra."
155-
):
156-
return 0.0
157-
else:
158-
return 1.0
159-
160-
chat.app.register_action(
161-
mock_autoguard_factcheck_input_api, "autoguard_factcheck_input_api"
162-
)
163-
164118
async def mock_autoguard_factcheck_output_api(
165119
context: Optional[dict] = None, **kwargs
166120
):
@@ -186,6 +140,5 @@ async def mock_autoguard_factcheck_output_api(
186140
"non-existent Styx, Nix, Kerberos, and Hydra."
187141
)
188142
await chat.bot_async(
189-
"Factcheck violation in user input has been detected by AutoGuard.\nFactcheck violation in "
190-
"llm response has been detected by AutoGuard."
143+
"Factcheck violation in llm response has been detected by AutoGuard."
191144
)

tests/test_configs/autoguard_factcheck/config.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ rails:
77
autoguard:
88
parameters:
99
fact_check_endpoint: "https://nvidia.autoalign.ai/factcheck"
10-
input:
11-
flows:
12-
- autoguard factcheck input
1310
output:
1411
flows:
1512
- autoguard factcheck output

0 commit comments

Comments
 (0)