|
1 | 1 | { |
2 | 2 | "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 | + }, |
3 | 12 | { |
4 | 13 | "cell_type": "markdown", |
5 | 14 | "id": "9c48213d-6e6a-4c10-838a-2a7c710c3a05", |
6 | 15 | "metadata": {}, |
7 | 16 | "source": [ |
8 | | - "# Using `GuardrailsOutputParser` in `LlamaIndex`\n" |
| 17 | + "# Guardrails Output Parsing\n" |
9 | 18 | ] |
10 | 19 | }, |
11 | 20 | { |
|
14 | 23 | "id": "cf54a5a8", |
15 | 24 | "metadata": {}, |
16 | 25 | "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 🦙." |
18 | 27 | ] |
19 | 28 | }, |
20 | 29 | { |
|
23 | 32 | "id": "39bc790a", |
24 | 33 | "metadata": {}, |
25 | 34 | "source": [ |
26 | | - "#### Download Data\n" |
| 35 | + "#### Download Data" |
27 | 36 | ] |
28 | 37 | }, |
29 | 38 | { |
30 | 39 | "cell_type": "code", |
31 | 40 | "execution_count": null, |
32 | 41 | "id": "649bea0c", |
33 | 42 | "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 | + ], |
35 | 63 | "source": [ |
36 | 64 | "!mkdir -p 'data/paul_graham/'\n", |
37 | 65 | "!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 | 87 | "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))\n", |
60 | 88 | "\n", |
61 | 89 | "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" |
67 | 91 | ] |
68 | 92 | }, |
69 | 93 | { |
|
87 | 111 | "name": "stdout", |
88 | 112 | "output_type": "stream", |
89 | 113 | "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" |
94 | 120 | ] |
95 | 121 | } |
96 | 122 | ], |
|
170 | 196 | "# Guardrails recommends Pydantic\n", |
171 | 197 | "\n", |
172 | 198 | "from pydantic import BaseModel, Field\n", |
173 | | - "from typing import List\n", |
174 | 199 | "import guardrails as gd\n", |
175 | 200 | "\n", |
176 | 201 | "\n", |
177 | 202 | "class Point(BaseModel):\n", |
178 | 203 | " # In all the fields below, you can define validators as well\n", |
179 | 204 | " # 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", |
183 | 214 | "\n", |
184 | 215 | "\n", |
185 | 216 | "class BulletPoints(BaseModel):\n", |
186 | | - " points: List[Point] = Field(\n", |
| 217 | + " points: Point = Field(\n", |
187 | 218 | " description=\"Bullet points regarding events in the author's life.\"\n", |
188 | 219 | " )\n", |
189 | 220 | "\n", |
|
192 | 223 | "prompt = \"\"\"\n", |
193 | 224 | "Query string here.\n", |
194 | 225 | "\n", |
195 | | - "${gr.xml_prefix_prompt}\n", |
196 | | - "\n", |
197 | 226 | "${output_schema}\n", |
198 | 227 | "\n", |
199 | 228 | "${gr.json_suffix_prompt_v2_wo_none}\n", |
|
211 | 240 | "guard = gd.Guard.from_pydantic(output_class=BulletPoints, prompt=prompt)\n", |
212 | 241 | "\n", |
213 | 242 | "# Create output parse object\n", |
214 | | - "output_parser = GuardrailsOutputParser(guard, llm=llm_predictor.llm)" |
| 243 | + "output_parser = GuardrailsOutputParser(guard, llm=llm_predictor.llm.complete)" |
215 | 244 | ] |
216 | 245 | }, |
217 | 246 | { |
|
249 | 278 | "Query: {query_str}\n", |
250 | 279 | "Answer: \n", |
251 | 280 | "\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", |
264 | 286 | "</output>\n", |
265 | 287 | "\n", |
266 | 288 | "\n", |
|
294 | 316 | "name": "stdout", |
295 | 317 | "output_type": "stream", |
296 | 318 | "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" |
301 | 323 | ] |
302 | 324 | } |
303 | 325 | ], |
|
323 | 345 | "output_type": "stream", |
324 | 346 | "text": [ |
325 | 347 | "{\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", |
347 | 352 | " }\n", |
348 | 353 | "}\n" |
349 | 354 | ] |
|
0 commit comments