Skip to content

Commit b765e4d

Browse files
authored
Merge pull request #1160 from guardrails-ai/feat/llama-index
Feat/llama index
2 parents 93a6a36 + e0ac7a7 commit b765e4d

File tree

11 files changed

+1141
-15
lines changed

11 files changed

+1141
-15
lines changed

docs/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/.quarto/
22
lib64
3+
integrations/data
4+
integrations/storage

docs/examples/llamaindex-output-parsing.ipynb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
"id": "9c48213d-6e6a-4c10-838a-2a7c710c3a05",
1515
"metadata": {},
1616
"source": [
17-
"# Guardrails Output Parsing\n"
17+
"# Guardrails Output Parsing (Deprecated)\n",
18+
"\n",
19+
"## DEPRECATION NOTE\n",
20+
"This integration between LlamaIndex and Guardrails is only valid for llama-index ~0.9.x and guardrails-ai < 0.5.x. and thus has been deprecated. For an updated example of using Guardrails with LlamaIndex with their latest versions, see the [GuardrailsEngine](/docs/integrations/llama_index)\n"
1821
]
1922
},
2023
{

docs/integrations/llama_index.ipynb

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# LlamaIndex\n",
8+
"\n",
9+
"## Overview\n",
10+
"\n",
11+
"This is a Quick Start guide that shows how to use Guardrails alongside LlamaIndex. As you'll see, the LlamaIndex portion comes directly from their starter examples [here](https://docs.llamaindex.ai/en/stable/getting_started/starter_example/). Our approach to intergration for LlamaIndex, similar to our LangChain integration, is the make the interaction feel as native to the tool as possible."
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"## Installation\n",
19+
"Install LlamaIndex and a version of Guardrails with LlamaIndex support."
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 3,
25+
"metadata": {},
26+
"outputs": [
27+
{
28+
"name": "stdout",
29+
"output_type": "stream",
30+
"text": [
31+
"Found existing installation: guardrails-ai 0.6.0\n",
32+
"Uninstalling guardrails-ai-0.6.0:\n",
33+
" Successfully uninstalled guardrails-ai-0.6.0\n"
34+
]
35+
}
36+
],
37+
"source": [
38+
"! pip uninstall guardrails-ai -y"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": null,
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"! pip install llama-index -q\n",
48+
"# ! pip install \"guardrails-ai>=0.6.1\"\n",
49+
"! pip install /Users/calebcourier/Projects/guardrails -q"
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"metadata": {},
55+
"source": [
56+
"Install a couple validators from the Guardrails Hub that we'll use to guard the query outputs."
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 13,
62+
"metadata": {},
63+
"outputs": [
64+
{
65+
"name": "stdout",
66+
"output_type": "stream",
67+
"text": [
68+
"Installing hub:\u001b[35m/\u001b[0m\u001b[35m/guardrails/\u001b[0m\u001b[95mdetect_pii...\u001b[0m\n",
69+
"✅Successfully installed guardrails/detect_pii version \u001b[1;36m0.0\u001b[0m.\u001b[1;36m5\u001b[0m!\n",
70+
"\n",
71+
"\n",
72+
"Installing hub:\u001b[35m/\u001b[0m\u001b[35m/guardrails/\u001b[0m\u001b[95mcompetitor_check...\u001b[0m\n",
73+
"✅Successfully installed guardrails/competitor_check version \u001b[1;36m0.0\u001b[0m.\u001b[1;36m1\u001b[0m!\n",
74+
"\n",
75+
"\n"
76+
]
77+
}
78+
],
79+
"source": [
80+
"! guardrails hub install hub://guardrails/detect_pii --no-install-local-models -q\n",
81+
"! guardrails hub install hub://guardrails/competitor_check --no-install-local-models -q"
82+
]
83+
},
84+
{
85+
"cell_type": "markdown",
86+
"metadata": {},
87+
"source": [
88+
"Download some sample data from the LlamaIndex docs."
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"execution_count": 6,
94+
"metadata": {},
95+
"outputs": [
96+
{
97+
"name": "stdout",
98+
"output_type": "stream",
99+
"text": [
100+
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
101+
" Dload Upload Total Spent Left Speed\n",
102+
"100 75042 100 75042 0 0 959k 0 --:--:-- --:--:-- --:--:-- 964k\n"
103+
]
104+
}
105+
],
106+
"source": [
107+
"! mkdir -p ./data\n",
108+
"! curl https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt > ./data/paul_graham_essay.txt"
109+
]
110+
},
111+
{
112+
"cell_type": "markdown",
113+
"metadata": {},
114+
"source": [
115+
"## Index Setup\n",
116+
"\n",
117+
"First we'll load some data and build an index as shown in the [starter tutorial](https://docs.llamaindex.ai/en/stable/getting_started/starter_example/)"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": 7,
123+
"metadata": {},
124+
"outputs": [],
125+
"source": [
126+
"import os.path\n",
127+
"from llama_index.core import (\n",
128+
" VectorStoreIndex,\n",
129+
" SimpleDirectoryReader,\n",
130+
" StorageContext,\n",
131+
" load_index_from_storage,\n",
132+
")\n",
133+
"\n",
134+
"# check if storage already exists\n",
135+
"PERSIST_DIR = \"./storage\"\n",
136+
"if not os.path.exists(PERSIST_DIR):\n",
137+
" # load the documents and create the index\n",
138+
" documents = SimpleDirectoryReader(\"data\").load_data()\n",
139+
" index = VectorStoreIndex.from_documents(documents)\n",
140+
" # store it for later\n",
141+
" index.storage_context.persist(persist_dir=PERSIST_DIR)\n",
142+
"else:\n",
143+
" # load the existing index\n",
144+
" storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)\n",
145+
" index = load_index_from_storage(storage_context)"
146+
]
147+
},
148+
{
149+
"cell_type": "markdown",
150+
"metadata": {},
151+
"source": [
152+
"## Guard Setup\n",
153+
"\n",
154+
"Next we'll create our Guard and assign some validators to check the output from our queries."
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": 8,
160+
"metadata": {},
161+
"outputs": [],
162+
"source": [
163+
"from guardrails import Guard\n",
164+
"from guardrails.hub import CompetitorCheck, DetectPII\n",
165+
"\n",
166+
"guard = Guard().use(\n",
167+
" CompetitorCheck(\n",
168+
" competitors=[\"Fortran\", \"Ada\", \"Pascal\"],\n",
169+
" on_fail=\"fix\"\n",
170+
" )\n",
171+
").use(DetectPII(pii_entities=\"pii\", on_fail=\"fix\"))"
172+
]
173+
},
174+
{
175+
"cell_type": "markdown",
176+
"metadata": {},
177+
"source": [
178+
"## Querying The Index\n",
179+
"\n",
180+
"To demonstrate it's plug-and-play capabilities, first we'll query the index un-guarded."
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 9,
186+
"metadata": {},
187+
"outputs": [
188+
{
189+
"name": "stdout",
190+
"output_type": "stream",
191+
"text": [
192+
"The author worked on writing short stories and programming, starting with early attempts on an IBM 1401 using Fortran in 9th grade, and later transitioning to microcomputers like the TRS-80 and Apple II to write games, rocket prediction programs, and a word processor.\n"
193+
]
194+
}
195+
],
196+
"source": [
197+
"# Use index on it's own\n",
198+
"query_engine = index.as_query_engine()\n",
199+
"response = query_engine.query(\"What did the author do growing up?\")\n",
200+
"print(response)"
201+
]
202+
},
203+
{
204+
"cell_type": "markdown",
205+
"metadata": {},
206+
"source": [
207+
"Now we'll set up a guarded engine, and re-query the index to see how Guardrails applies the fixes we specified when assigning our validators to the Guard."
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"execution_count": 11,
213+
"metadata": {},
214+
"outputs": [
215+
{
216+
"name": "stdout",
217+
"output_type": "stream",
218+
"text": [
219+
"The author worked on writing short stories and programming, starting with early attempts on an IBM 1401 using [COMPETITOR] in 9th <URL>er, the author transitioned to microcomputers, building a Heathkit kit and eventually getting a TRS-80 to write simple games and <URL>spite enjoying programming, the author initially planned to study philosophy in college but eventually switched to AI due to a lack of interest in philosophy courses.\n"
220+
]
221+
}
222+
],
223+
"source": [
224+
"# Use index with Guardrails\n",
225+
"from guardrails.integrations.llama_index import GuardrailsQueryEngine\n",
226+
"\n",
227+
"guardrails_query_engine = GuardrailsQueryEngine(engine=query_engine, guard=guard)\n",
228+
"\n",
229+
"response = guardrails_query_engine.query(\"What did the author do growing up?\")\n",
230+
"print(response)\n",
231+
" "
232+
]
233+
},
234+
{
235+
"cell_type": "markdown",
236+
"metadata": {},
237+
"source": [
238+
"The GuardrailsEngine can also be used with LlamaIndex's chat engine, not just the query engine."
239+
]
240+
},
241+
{
242+
"cell_type": "code",
243+
"execution_count": 12,
244+
"metadata": {},
245+
"outputs": [
246+
{
247+
"name": "stdout",
248+
"output_type": "stream",
249+
"text": [
250+
"The author worked on writing short stories and programming while growing <URL>ey started with early attempts on an IBM 1401 using [COMPETITOR] in 9th <URL>er, they transitioned to microcomputers, building simple games and a word processor on a TRS-80 in <DATE_TIME>.\n"
251+
]
252+
}
253+
],
254+
"source": [
255+
"# For chat engine\n",
256+
"from guardrails.integrations.llama_index import GuardrailsChatEngine\n",
257+
"chat_engine = index.as_chat_engine()\n",
258+
"guardrails_chat_engine = GuardrailsChatEngine(engine=chat_engine, guard=guard)\n",
259+
"\n",
260+
"response = guardrails_chat_engine.chat(\"Tell me what the author did growing up.\")\n",
261+
"print(response)"
262+
]
263+
}
264+
],
265+
"metadata": {
266+
"kernelspec": {
267+
"display_name": ".venv",
268+
"language": "python",
269+
"name": "python3"
270+
},
271+
"language_info": {
272+
"codemirror_mode": {
273+
"name": "ipython",
274+
"version": 3
275+
},
276+
"file_extension": ".py",
277+
"mimetype": "text/x-python",
278+
"name": "python",
279+
"nbconvert_exporter": "python",
280+
"pygments_lexer": "ipython3",
281+
"version": "3.12.4"
282+
}
283+
},
284+
"nbformat": 4,
285+
"nbformat_minor": 2
286+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from guardrails.integrations.llama_index.guardrails_query_engine import (
2+
GuardrailsQueryEngine,
3+
)
4+
from guardrails.integrations.llama_index.guardrails_chat_engine import (
5+
GuardrailsChatEngine,
6+
)
7+
8+
__all__ = ["GuardrailsQueryEngine", "GuardrailsChatEngine"]

0 commit comments

Comments
 (0)