Skip to content

Commit 26304fd

Browse files
authored
Merge pull request #566 from decypher-ai/rebased_develop-factcheck
Autoalign factcheck examples
2 parents e03244c + eba1413 commit 26304fd

File tree

10 files changed

+281
-67
lines changed

10 files changed

+281
-67
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
models:
22
- type: main
33
engine: openai
4-
model: gpt-3.5-turbo
4+
model: gpt-3.5-turbo-instruct
55
rails:
6-
config:
7-
autoalign:
8-
parameters:
9-
fact_check_endpoint: "https://<AUTOALIGN_ENDPOINT>/factcheck"
10-
output:
11-
flows:
12-
- autoalign factcheck output
6+
config:
7+
autoalign:
8+
parameters:
9+
fact_check_endpoint: "https://<AUTOALIGN_ENDPOINT>/factcheck"
10+
output:
11+
guardrails_config:
12+
{
13+
"factcheck": {
14+
"verify_response": false
15+
},
16+
}
17+
output:
18+
flows:
19+
- autoalign factcheck output
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
define user ask about pluto
2+
"What is pluto?"
3+
"How many moons does pluto have?"
4+
"Is pluto a planet?"
5+
6+
define flow answer pluto question
7+
user ask about pluto
8+
# For pluto questions, we activate the fact checking.
9+
$check_facts = True
10+
bot provide pluto answer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
define user express greeting
2+
"hi"
3+
"hello"
4+
"hey"
5+
6+
define user ask name
7+
"What is your name?"
8+
9+
define user ask capabilities
10+
"What can you do?"
11+
"help"
12+
13+
define bot inform capabilities
14+
"I am an example bot that illustrates the fact checking and hallucination detection capabilities. Ask me about the documents in my knowledge base to test my fact checking abilities, or about other topics to test my hallucination detection."
15+
16+
define flow capabilities
17+
user ask capabilities
18+
bot inform capabilities
19+
20+
define user ask knowledge base
21+
"What is in your knowledge base?"
22+
"What do you know?"
23+
"What can I ask you about?"
24+
25+
define bot inform knowledge base
26+
"You can ask me about anything! My knowledge base includes information about the pluto for which I have documents in knowledge base, which I can use for fact checking."
27+
28+
define flow knowledge base
29+
user ask knowledge base
30+
bot inform knowledge base
31+
32+
define user request repeat
33+
"Please repeat that"
34+
"repeat"
35+
"What was that?"
36+
37+
define flow
38+
user express greeting
39+
bot express greeting
40+
41+
define bot offer additional help
42+
"If you have any more questions or if there's anything else I can help you with, please don't hesitate to ask."
43+
44+
define user ask general question
45+
"What stocks should I buy?"
46+
"Can you recommend the best stocks to buy?"
47+
"Can you recommend a place to eat?"
48+
"Do you know any restaurants?"
49+
"Can you tell me your name?"
50+
"What's your name?"
51+
"Can you paint?"
52+
"Can you tell me a joke?"
53+
"What is the biggest city in the world"
54+
"Can you write an email?"
55+
"I need you to write an email for me."
56+
"Who is the president?"
57+
"What party will win the elections?"
58+
"Who should I vote with?"
59+
60+
define flow
61+
user ask general question
62+
bot provide response

nemoguardrails/library/autoalign/README.md

+62-28
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ define bot refuse to respond
316316
The actions `autoalign_input_api` and `autoalign_output_api` takes in two arguments `show_autoalign_message` and
317317
`show_toxic_phrases`. Both the arguments expect boolean value being passed to them. The default value of
318318
`show_autoalign_message` is `True` and for `show_toxic_phrases` is False. The `show_autoalign_message` controls whether
319-
we will any output from autoalign or not. The response from AutoAlign would be presented as a subtext, when
319+
we will show any output from autoalign or not. The response from AutoAlign would be presented as a subtext, when
320320
`show_autoalign_message` is kept `True`. Details regarding the second argument can be found in `text_toxicity_extraction`
321321
section.
322322

@@ -449,33 +449,34 @@ For text toxicity detection, the matching score has to be following format:
449449
Can extract toxic phrases by changing the colang file a bit:
450450

451451
```colang
452-
define flow call autoalign input
452+
define subflow autoalign check input
453453
$input_result = execute autoalign_input_api(show_autoalign_message=True, show_toxic_phrases=True)
454-
455454
if $input_result["guardrails_triggered"]
456455
$autoalign_input_response = $input_result['combined_response']
457456
bot refuse to respond
458457
stop
458+
else if $input_result["pii_fast"] and $input_result["pii_fast"]["guarded"]:
459+
$user_message = $input_result["pii_fast"]["response"]
459460
460-
define flow call autoalign output
461+
define subflow autoalign check output
461462
$output_result = execute autoalign_output_api(show_autoalign_message=True, show_toxic_phrases=True)
462-
463463
if $output_result["guardrails_triggered"]
464464
bot refuse to respond
465465
stop
466466
else
467467
$pii_message_output = $output_result["pii_fast"]["response"]
468468
if $output_result["pii_fast"]["guarded"]
469-
bot respond pii output
470-
stop
471-
472-
473-
define bot respond pii output
474-
"$pii_message_output"
469+
$bot_message = $pii_message_output
475470
471+
define subflow autoalign factcheck output
472+
if $check_facts == True
473+
$check_facts = False
474+
$threshold = 0.5
475+
$output_result = execute autoalign_factcheck_output_api(factcheck_threshold=$threshold, show_autoalign_message=True)
476+
bot provide response
476477
477478
define bot refuse to respond
478-
"I'm sorry I can't respond."
479+
"I'm sorry, I can't respond to that."
479480
```
480481

481482

@@ -561,6 +562,12 @@ To use AutoAlign's factcheck module, you have to modify the `config.yml` in the
561562
rails:
562563
config:
563564
autoalign:
565+
guardrails_config:
566+
{
567+
"factcheck":{
568+
"verify_response": false
569+
}
570+
}
564571
parameters:
565572
fact_check_endpoint: "https://<AUTOALIGN_ENDPOINT>/factcheck"
566573
output:
@@ -571,28 +578,55 @@ rails:
571578
Specify the factcheck endpoint the parameters section of autoalign's config.
572579
Then, you have to call the corresponding subflows for factcheck guardrails.
573580

581+
In the guardrails config for factcheck you can toggle "verify_response" flag
582+
which will enable(true) / disable (false) additional processing of LLM Response.
583+
This processing ensures that only relevant LLM responses undergo fact-checking
584+
and responses like greetings ('Hi', 'Hello' etc.) do not go through fact-checking
585+
process.
586+
587+
Note that the verify_response is set to False by default as it requires additional
588+
computation, and we encourage users to determine which LLM responses should go through
589+
AutoAlign fact checking whenever possible.
590+
591+
574592
Following is the format of the colang file, which is present in the library:
575593
```colang
576-
define flow autoalign factcheck input
577-
execute autoalign_retrieve_relevant_chunks_input
578-
$input_result = execute autoalign_factcheck_input_api
579-
580-
define flow autoalign factcheck output
581-
execute retrieve_relevant_chunks
582-
$output_result = execute autoalign_factcheck_output_api
583-
if $input_result < 0.5
584-
bot inform autoalign factcheck input violation
585-
if $output_result < 0.5
586-
bot inform autoalign factcheck output violation
587-
stop
594+
define subflow autoalign factcheck output
595+
if $check_facts == True
596+
$check_facts = False
597+
$threshold = 0.5
598+
$output_result = execute autoalign_factcheck_output_api(factcheck_threshold=$threshold)
599+
```
600+
601+
The `threshold` can be changed depending upon the use-case, the `output_result`
602+
variable stores the factcheck score which can be used for further processing.
603+
The `show_autoalign_message` controls whether we will show any output from autoalign
604+
or not. The response from AutoAlign would be presented as a subtext, when
605+
`show_autoalign_message` is kept `True`.
588606

589-
define bot inform autoalign factcheck input violation
590-
"Factcheck input violation has been detected by AutoAlign."
607+
To use this flow you need to have colang file of the following format:
591608

592-
define bot inform autoalign factcheck output violation
593-
"$bot_message Factcheck output violation has been detected by AutoAlign."
609+
```colang
610+
define user ask about pluto
611+
"What is pluto?"
612+
"How many moons does pluto have?"
613+
"Is pluto a planet?"
614+
615+
define flow answer report question
616+
user ask about pluto
617+
# For pluto questions, we activate the fact checking.
618+
$check_facts = True
619+
bot provide report answer
594620
```
595621

622+
The above example is of a flow related to a case where the
623+
knowledge base is about pluto. You need to define the flow
624+
for use case by following the above example, this ensures that
625+
the fact-check takes place only for particular topics and not
626+
for ideal chit-chat.
627+
628+
629+
596630
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.
597631

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

nemoguardrails/library/autoalign/actions.py

+27-15
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
"intellectual_property": {"mode": "OFF"},
7878
}
7979

80+
default_factcheck_config = {"factcheck": {"verify_response": False}}
81+
8082

8183
def process_autoalign_output(responses: List[Any], show_toxic_phrases: bool = False):
8284
"""Processes the output provided AutoAlign API"""
@@ -174,16 +176,17 @@ async def autoalign_factcheck_infer(
174176
request_url: str,
175177
text: str,
176178
documents: List[str],
179+
guardrails_config: Optional[Dict[Any, Any]] = None,
177180
):
178181
"""Checks the facts for the text using the given documents and provides a fact-checking score"""
182+
factcheck_config = default_factcheck_config.copy()
179183
api_key = os.environ.get("AUTOALIGN_API_KEY")
180184
if api_key is None:
181185
raise ValueError("AUTOALIGN_API_KEY environment variable not set.")
182186
headers = {"x-api-key": api_key}
183-
request_body = {
184-
"prompt": text,
185-
"documents": documents,
186-
}
187+
if guardrails_config:
188+
factcheck_config.update(guardrails_config)
189+
request_body = {"prompt": text, "documents": documents, "config": factcheck_config}
187190
async with aiohttp.ClientSession() as session:
188191
async with session.post(
189192
url=request_url,
@@ -198,7 +201,8 @@ async def autoalign_factcheck_infer(
198201
async for line in response.content:
199202
resp = json.loads(line)
200203
if resp["task"] == "factcheck":
201-
return float(resp["response"][17:])
204+
if resp["response"].startswith("Factcheck Score: "):
205+
return float(resp["response"][17:])
202206
return 1.0
203207

204208

@@ -267,25 +271,33 @@ async def autoalign_output_api(
267271

268272
@action(name="autoalign_factcheck_output_api")
269273
async def autoalign_factcheck_output_api(
270-
llm_task_manager: LLMTaskManager, context: Optional[dict] = None
274+
llm_task_manager: LLMTaskManager,
275+
context: Optional[dict] = None,
276+
factcheck_threshold: float = 0.0,
277+
show_autoalign_message: bool = True,
271278
):
272279
"""Calls AutoAlign factcheck API and checks whether the bot message is factually correct according to given
273280
documents"""
274281

275282
bot_message = context.get("bot_message")
276-
documents = context.get("relevant_chunks", [])
283+
documents = context.get("relevant_chunks_sep", [])
284+
277285
autoalign_config = llm_task_manager.config.rails.config.autoalign
278286
autoalign_fact_check_api_url = autoalign_config.parameters.get(
279287
"fact_check_endpoint"
280288
)
289+
guardrails_config = getattr(autoalign_config.output, "guardrails_config", None)
281290
if not autoalign_fact_check_api_url:
282291
raise ValueError("Provide the autoalign factcheck endpoint in the config")
283-
if isinstance(documents, str):
284-
documents = documents.split("\n")
285-
prompt = bot_message
286-
if isinstance(documents, list) and len(documents) > 0:
287-
return await autoalign_factcheck_infer(
288-
autoalign_fact_check_api_url, prompt, documents
292+
text = bot_message
293+
score = await autoalign_factcheck_infer(
294+
request_url=autoalign_fact_check_api_url,
295+
text=text,
296+
documents=documents,
297+
guardrails_config=guardrails_config,
298+
)
299+
if score < factcheck_threshold and show_autoalign_message:
300+
log.warning(
301+
f"Factcheck violation in llm response has been detected by AutoAlign with fact check score {score}"
289302
)
290-
else:
291-
raise ValueError("Provide relevant documents in proper format")
303+
return score

nemoguardrails/library/autoalign/flows.co

+5-8
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ define subflow autoalign check output
1818
$bot_message = $pii_message_output
1919

2020
define subflow autoalign factcheck output
21-
execute retrieve_relevant_chunks
22-
$output_result = execute autoalign_factcheck_output_api
23-
if $output_result < 0.5
24-
bot inform autoalign factcheck output violation
25-
stop
26-
27-
define bot inform autoalign factcheck output violation
28-
"Factcheck violation in llm response has been detected by AutoAlign."
21+
if $check_facts == True
22+
$check_facts = False
23+
$threshold = 0.5
24+
$output_result = execute autoalign_factcheck_output_api(factcheck_threshold=$threshold, show_autoalign_message=True)
25+
bot provide response
2926

3027
define bot refuse to respond
3128
"I'm sorry, I can't respond to that."

0 commit comments

Comments
 (0)