Notebooks
A
Arize AI
Guardrails Rag

Guardrails Rag

arize-tutorialsguardrailsLLMPython

arize logo
Docs | GitHub | Community

Setting up guardrails with RAG

This guide helps you create a RAG agent and setup a guardrail before returning LLM responses to customers.

Install Dependencies

[ ]

Add Open AI API Key to Environment

[ ]

Register OTEL and Instrument LlamaIndex to Send Traces to Arize

[ ]

Install Guardrails AI

Note that Guardrails AI needs to be installed after setting up the LlamaIndexInstrumentor due to dependency issues.

[ ]

Install LlmRagEvaluator Guard Repo

[ ]

Instantiate LlmRagEvaluator Guard with HallucinationPrompt

The LlmRagEvaluator can be instantiated using an off-the-shelf prompt from Arize (HallucinationPrompt, ContextRelevancyPrompt or QACorrectnessPrompt). Alternatively, it can be instantiated with a custom prompt that inherits from ArizeRagEvalPromptBase. This tutorial will use the prompt from HallucinationPrompt. For visibility, the code is copied from the repo below.

[ ]

Set Up Guarded LLM Call

[ ]

Set Up RAG Application

Create a LlamaIndex VectorStore for a RAG application over the Arize docs.

[ ]

Send Message to Query Engine

Below is an example where we send a message to the LLM that's outside of the scope of the docs. The LLM hallucinates a response and the LLM Judge Guard flags that the LLM hallucinated.

Replace logging.INFO with logging.DEBUG to see the calls logged inside the Guard.

[ ]

View Traces in Arize

We can view the user message and context sent to the original LLM in Arize.

Screenshot 2024-07-27 at 10.25.46 AM.png

The assistant message shows the original LLM response that the Guard evaluated.

Screenshot 2024-07-27 at 10.26.01 AM.png

The exception below shows the response from the Guard, where it flags that the LLM response is a hallucination, since NeMo is not referenced in the context.

Screenshot 2024-07-27 at 10.26.27 AM.png

We can navigate over to the Prompt Playground to improve the LLM system prompt so that it is less likely to hallucinate in the future.

Screenshot 2024-07-27 at 8.53.06 PM.png

We add the sentence IF YOU DO NOT KNOW THE ANSWER, RESPOND WITH "I DON'T KNOW" to the system prompt. When we re-run the LLM, the model responds with "I DON'T KNOW" instead of hallucinating a response.

Screenshot 2024-07-27 at 8.54.13 PM.png