Use Guardrails to prevent hallucinations in generative AI applications
With Contextual grounding check, you can prevent hallucinations by detecting irrelevant and ungrounded LLM responses.
- Grounding – This checks if the model response is factually accurate based on the source and is grounded in the source. Any new information introduced in the response will be considered un-grounded.
- Relevance – This checks if the model response is relevant to the user query.
0.6
, all model responses with a grounding or relevance score of less than that will be detected as hallucinations and blocked.0.85
, relevance score threshold to 0.5
and configured the messages for blocked prompt and responses:0.01
- much lower than the configured threshold of 0.85
, which resulted in the model response getting blocked.boto3
), but it will work with any of the SDKs.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import boto3
guardrailId = "ENTER_GUARDRAIL_ID"
guardrailVersion= "ENTER_GUARDRAIL_VERSION"
knowledgeBaseId = "ENTER_KB_ID"
modelArn = 'arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-instant-v1'
def main():
client = boto3.client('bedrock-agent-runtime')
response = client.retrieve_and_generate(
input={
'text': 'what is amazon doing in the field of quantum computing?'
},
retrieveAndGenerateConfiguration={
'knowledgeBaseConfiguration': {
'generationConfiguration': {
'guardrailConfiguration': {
'guardrailId': guardrailId,
'guardrailVersion': guardrailVersion
}
},
'knowledgeBaseId': knowledgeBaseId,
'modelArn': modelArn,
'retrievalConfiguration': {
'vectorSearchConfiguration': {
'overrideSearchType': 'SEMANTIC'
}
}
},
'type': 'KNOWLEDGE_BASE'
},
)
action = response["guardrailAction"]
print(f'Guardrail action: {action}')
finalResponse = response["output"]["text"]
print(f'Final response:\n{finalResponse}')
if __name__ == "__main__":
main()
1
2
pip install boto3
python grounding.py
1
2
3
Guardrail action: INTERVENED
Final response:
Response blocked - Sorry, the model cannot answer this question.
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.