Skip to content

Commit 5f199ef

Browse files
committed
use updated notebook
1 parent fce8266 commit 5f199ef

File tree

1 file changed

+63
-58
lines changed

1 file changed

+63
-58
lines changed

docs/examples/llamaindex-output-parsing.ipynb

Lines changed: 63 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
{
22
"cells": [
3+
{
4+
"attachments": {},
5+
"cell_type": "markdown",
6+
"id": "e1c59071",
7+
"metadata": {},
8+
"source": [
9+
"<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/output_parsing/GuardrailsDemo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
10+
]
11+
},
312
{
413
"cell_type": "markdown",
514
"id": "9c48213d-6e6a-4c10-838a-2a7c710c3a05",
615
"metadata": {},
716
"source": [
8-
"# Using `GuardrailsOutputParser` in `LlamaIndex`\n"
17+
"# Guardrails Output Parsing\n"
918
]
1019
},
1120
{
@@ -14,7 +23,7 @@
1423
"id": "cf54a5a8",
1524
"metadata": {},
1625
"source": [
17-
"If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.\n"
26+
"If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
1827
]
1928
},
2029
{
@@ -23,15 +32,34 @@
2332
"id": "39bc790a",
2433
"metadata": {},
2534
"source": [
26-
"#### Download Data\n"
35+
"#### Download Data"
2736
]
2837
},
2938
{
3039
"cell_type": "code",
3140
"execution_count": null,
3241
"id": "649bea0c",
3342
"metadata": {},
34-
"outputs": [],
43+
"outputs": [
44+
{
45+
"name": "stdout",
46+
"output_type": "stream",
47+
"text": [
48+
"--2023-12-01 10:30:22-- https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt\n",
49+
"Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'\n",
50+
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.109.133, 185.199.108.133, 185.199.110.133, ...\n",
51+
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.109.133|:443... connected.\n",
52+
"HTTP request sent, awaiting response... 200 OK\n",
53+
"Length: 75042 (73K) [text/plain]\n",
54+
"Saving to: ‘data/paul_graham/paul_graham_essay.txt’\n",
55+
"\n",
56+
"data/paul_graham/pa 100%[===================>] 73.28K --.-KB/s in 0.02s \n",
57+
"\n",
58+
"2023-12-01 10:30:23 (3.82 MB/s) - ‘data/paul_graham/paul_graham_essay.txt’ saved [75042/75042]\n",
59+
"\n"
60+
]
61+
}
62+
],
3563
"source": [
3664
"!mkdir -p 'data/paul_graham/'\n",
3765
"!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'"
@@ -59,11 +87,7 @@
5987
"logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))\n",
6088
"\n",
6189
"from llama_index import VectorStoreIndex, SimpleDirectoryReader\n",
62-
"from IPython.display import Markdown, display\n",
63-
"\n",
64-
"import openai\n",
65-
"\n",
66-
"openai.api_key = \"<YOUR_OPENAI_API_KEY>\""
90+
"from IPython.display import Markdown, display"
6791
]
6892
},
6993
{
@@ -87,10 +111,12 @@
87111
"name": "stdout",
88112
"output_type": "stream",
89113
"text": [
90-
"INFO:llama_index.token_counter.token_counter:> [build_index_from_documents] Total LLM token usage: 0 tokens\n",
91-
"> [build_index_from_documents] Total LLM token usage: 0 tokens\n",
92-
"INFO:llama_index.token_counter.token_counter:> [build_index_from_documents] Total embedding token usage: 18579 tokens\n",
93-
"> [build_index_from_documents] Total embedding token usage: 18579 tokens\n"
114+
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
115+
"HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
116+
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
117+
"HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
118+
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
119+
"HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n"
94120
]
95121
}
96122
],
@@ -170,20 +196,25 @@
170196
"# Guardrails recommends Pydantic\n",
171197
"\n",
172198
"from pydantic import BaseModel, Field\n",
173-
"from typing import List\n",
174199
"import guardrails as gd\n",
175200
"\n",
176201
"\n",
177202
"class Point(BaseModel):\n",
178203
" # In all the fields below, you can define validators as well\n",
179204
" # Left out for brevity\n",
180-
" explanation: str = Field()\n",
181-
" explanation2: str = Field()\n",
182-
" explanation3: str = Field()\n",
205+
" explanation: str = Field(\n",
206+
" description=\"The first thing the author worked on\"\n",
207+
" )\n",
208+
" explanation2: str = Field(\n",
209+
" description=\"The second thing the author worked on\"\n",
210+
" )\n",
211+
" explanation3: str = Field(\n",
212+
" description=\"The third thing the author worked on\"\n",
213+
" )\n",
183214
"\n",
184215
"\n",
185216
"class BulletPoints(BaseModel):\n",
186-
" points: List[Point] = Field(\n",
217+
" points: Point = Field(\n",
187218
" description=\"Bullet points regarding events in the author's life.\"\n",
188219
" )\n",
189220
"\n",
@@ -192,8 +223,6 @@
192223
"prompt = \"\"\"\n",
193224
"Query string here.\n",
194225
"\n",
195-
"${gr.xml_prefix_prompt}\n",
196-
"\n",
197226
"${output_schema}\n",
198227
"\n",
199228
"${gr.json_suffix_prompt_v2_wo_none}\n",
@@ -211,7 +240,7 @@
211240
"guard = gd.Guard.from_pydantic(output_class=BulletPoints, prompt=prompt)\n",
212241
"\n",
213242
"# Create output parse object\n",
214-
"output_parser = GuardrailsOutputParser(guard, llm=llm_predictor.llm)"
243+
"output_parser = GuardrailsOutputParser(guard, llm=llm_predictor.llm.complete)"
215244
]
216245
},
217246
{
@@ -249,18 +278,11 @@
249278
"Query: {query_str}\n",
250279
"Answer: \n",
251280
"\n",
252-
"\n",
253-
"Given below is XML that describes the information to extract from this document and the tags to extract it into.\n",
254-
"\n",
255-
"\n",
256-
"<output>\n",
257-
" <list name=\"points\" description=\"Bullet points regarding events in the author's life.\">\n",
258-
" <object>\n",
259-
" <string name=\"explanation\"/>\n",
260-
" <string name=\"explanation2\"/>\n",
261-
" <string name=\"explanation3\"/>\n",
262-
" </object>\n",
263-
" </list>\n",
281+
"ct name=\"points\" description=\"Bullet points regarding events in the author's life.\">\n",
282+
" <string name=\"explanation\" description=\"The first thing the author worked on\"/>\n",
283+
" <string name=\"explanation2\" description=\"The second thing the author worked on\"/>\n",
284+
" <string name=\"explanation3\" description=\"The third thing the author worked on\"/>\n",
285+
" </object>\n",
264286
"</output>\n",
265287
"\n",
266288
"\n",
@@ -294,10 +316,10 @@
294316
"name": "stdout",
295317
"output_type": "stream",
296318
"text": [
297-
"INFO:llama_index.token_counter.token_counter:> [query] Total LLM token usage: 754 tokens\n",
298-
"> [query] Total LLM token usage: 754 tokens\n",
299-
"INFO:llama_index.token_counter.token_counter:> [query] Total embedding token usage: 11 tokens\n",
300-
"> [query] Total embedding token usage: 11 tokens\n"
319+
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
320+
"HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
321+
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
322+
"HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n"
301323
]
302324
}
303325
],
@@ -323,27 +345,10 @@
323345
"output_type": "stream",
324346
"text": [
325347
"{\n",
326-
" \"output\": {\n",
327-
" \"list\": {\n",
328-
" \"name\": \"points\",\n",
329-
" \"description\": \"Bullet points regarding events in the author's life.\",\n",
330-
" \"object\": {\n",
331-
" \"string\": [\n",
332-
" {\n",
333-
" \"name\": \"explanation\",\n",
334-
" \"content\": \"Writing short stories\"\n",
335-
" },\n",
336-
" {\n",
337-
" \"name\": \"explanation2\",\n",
338-
" \"content\": \"Programming on the IBM 1401\"\n",
339-
" },\n",
340-
" {\n",
341-
" \"name\": \"explanation3\",\n",
342-
" \"content\": \"Building a microcomputer\"\n",
343-
" }\n",
344-
" ]\n",
345-
" }\n",
346-
" }\n",
348+
" \"points\": {\n",
349+
" \"explanation\": \"writing\",\n",
350+
" \"explanation2\": \"programming\",\n",
351+
" \"explanation3\": \"philosophy\"\n",
347352
" }\n",
348353
"}\n"
349354
]

0 commit comments

Comments
 (0)