Guardrails Rag
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.
The assistant message shows the original LLM response that the Guard evaluated.
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.
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.
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.