Skip to main content
LlamaIndex is an open source data orchestration framework that simplifies integrating private and public data to build new Large Language Models (LLMs). With this integration, you can use Guardrails AI to validate the output of LlamaIndex LLM calls with minimal code changes to your application. The sample below walks through setting up a single vector database for Retrieval-Augemnted Generation (RAG) and then querying the index, using Guardrails AI to ensure the answer doesn’t contain Personally Identifiable Information (PII) and doesn’t mention competitive products. Guardrails AI works with both LlamaIndex’s query engine and its chat engine. The query engine is a generic natural language interface for asking questions of data. The chat engine is a higher-level interface that enables a conversation around your data over time, leveraging both the general language capabilities of an LLM and your own private data to generate accurate, up-to-date responses.

Prerequisites

This document assumes you have set up Guardrails AI. You should also be familiar with foundational Guardrails AI concepts, such as guards and validators. For more information, see Quickstart: In-Application. You should be familiar with the basic concepts of RAG. For the basics, see our blog post on reducing hallucination issues in GenAI apps. This walkthrough uses Guardrails validators, which are installable from PyPI with pip. We’ll install the ones we need — guardrails-ai-detect-pii and guardrails-ai-competitor-check — in the steps below. For more information on validators, see the validators documentation. Unless you specify another LLM, LlamaIndex uses OpenAI for natural language queries as well as to generate vector embeddings. This requires generating and setting an OpenAI API key, which you can do on Linux using:
And in Windows Cmd or Powershell using:
You will need sufficient OpenAI credits to run this sample (between USD $0.01 and $0.03 per run-through). To use another hosted LLM or a locally-running LLM, see the LlamaIndex documentation.

Install LlamaIndex

Install the LlamaIndex package:
Next, install the Detect PII and Competitor Check validators if you don’t already have them installed:

Set up your data

Next, we’ll need some sample data to feed into a vector database. Download the essay located here using curl on the command line (or read it with your browser and save it):
This essay from Paul Graham, “What I Worked On,” contains details of Graham’s life and career, some of which qualify as PII. Additionally, Graham mentions several programming languages - Fortran, Pascal, and BASIC - which, for the purposes of this tutorial, we’ll treat as “competitors.” Next, use the following code from the LlamaIndex starter tutorial to create vector embeddings for this document and store the vector database to disk:
By default, this will save the embeddings in a simple document store on disk. You can also use an open-source or commercial vector database.

Validate LlamaIndex calls

Next, call LlamaIndex without any guards to see what values it returns if you don’t validate the output.
You should get back a response like this:
This replaces the call to LlamaIndex’s query engine with the guardrails.integrations.llama_index.GuardrailsQueryEngine class, which is a thin wrapper around the LlamaIndex query engine. The response will look something like this: