Skip to content

Commit

Permalink
use updated notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
zsimjee committed Dec 4, 2023
1 parent fce8266 commit 5f199ef
Showing 1 changed file with 63 additions and 58 deletions.
121 changes: 63 additions & 58 deletions docs/examples/llamaindex-output-parsing.ipynb
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "e1c59071",
"metadata": {},
"source": [
"<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>"
]
},
{
"cell_type": "markdown",
"id": "9c48213d-6e6a-4c10-838a-2a7c710c3a05",
"metadata": {},
"source": [
"# Using `GuardrailsOutputParser` in `LlamaIndex`\n"
"# Guardrails Output Parsing\n"
]
},
{
Expand All @@ -14,7 +23,7 @@
"id": "cf54a5a8",
"metadata": {},
"source": [
"If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.\n"
"If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
]
},
{
Expand All @@ -23,15 +32,34 @@
"id": "39bc790a",
"metadata": {},
"source": [
"#### Download Data\n"
"#### Download Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "649bea0c",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--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",
"Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'\n",
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.109.133, 185.199.108.133, 185.199.110.133, ...\n",
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.109.133|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 75042 (73K) [text/plain]\n",
"Saving to: ‘data/paul_graham/paul_graham_essay.txt’\n",
"\n",
"data/paul_graham/pa 100%[===================>] 73.28K --.-KB/s in 0.02s \n",
"\n",
"2023-12-01 10:30:23 (3.82 MB/s) - ‘data/paul_graham/paul_graham_essay.txt’ saved [75042/75042]\n",
"\n"
]
}
],
"source": [
"!mkdir -p 'data/paul_graham/'\n",
"!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'"
Expand Down Expand Up @@ -59,11 +87,7 @@
"logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))\n",
"\n",
"from llama_index import VectorStoreIndex, SimpleDirectoryReader\n",
"from IPython.display import Markdown, display\n",
"\n",
"import openai\n",
"\n",
"openai.api_key = \"<YOUR_OPENAI_API_KEY>\""
"from IPython.display import Markdown, display"
]
},
{
Expand All @@ -87,10 +111,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:llama_index.token_counter.token_counter:> [build_index_from_documents] Total LLM token usage: 0 tokens\n",
"> [build_index_from_documents] Total LLM token usage: 0 tokens\n",
"INFO:llama_index.token_counter.token_counter:> [build_index_from_documents] Total embedding token usage: 18579 tokens\n",
"> [build_index_from_documents] Total embedding token usage: 18579 tokens\n"
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
"HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
"HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
"HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n"
]
}
],
Expand Down Expand Up @@ -170,20 +196,25 @@
"# Guardrails recommends Pydantic\n",
"\n",
"from pydantic import BaseModel, Field\n",
"from typing import List\n",
"import guardrails as gd\n",
"\n",
"\n",
"class Point(BaseModel):\n",
" # In all the fields below, you can define validators as well\n",
" # Left out for brevity\n",
" explanation: str = Field()\n",
" explanation2: str = Field()\n",
" explanation3: str = Field()\n",
" explanation: str = Field(\n",
" description=\"The first thing the author worked on\"\n",
" )\n",
" explanation2: str = Field(\n",
" description=\"The second thing the author worked on\"\n",
" )\n",
" explanation3: str = Field(\n",
" description=\"The third thing the author worked on\"\n",
" )\n",
"\n",
"\n",
"class BulletPoints(BaseModel):\n",
" points: List[Point] = Field(\n",
" points: Point = Field(\n",
" description=\"Bullet points regarding events in the author's life.\"\n",
" )\n",
"\n",
Expand All @@ -192,8 +223,6 @@
"prompt = \"\"\"\n",
"Query string here.\n",
"\n",
"${gr.xml_prefix_prompt}\n",
"\n",
"${output_schema}\n",
"\n",
"${gr.json_suffix_prompt_v2_wo_none}\n",
Expand All @@ -211,7 +240,7 @@
"guard = gd.Guard.from_pydantic(output_class=BulletPoints, prompt=prompt)\n",
"\n",
"# Create output parse object\n",
"output_parser = GuardrailsOutputParser(guard, llm=llm_predictor.llm)"
"output_parser = GuardrailsOutputParser(guard, llm=llm_predictor.llm.complete)"
]
},
{
Expand Down Expand Up @@ -249,18 +278,11 @@
"Query: {query_str}\n",
"Answer: \n",
"\n",
"\n",
"Given below is XML that describes the information to extract from this document and the tags to extract it into.\n",
"\n",
"\n",
"<output>\n",
" <list name=\"points\" description=\"Bullet points regarding events in the author's life.\">\n",
" <object>\n",
" <string name=\"explanation\"/>\n",
" <string name=\"explanation2\"/>\n",
" <string name=\"explanation3\"/>\n",
" </object>\n",
" </list>\n",
"ct name=\"points\" description=\"Bullet points regarding events in the author's life.\">\n",
" <string name=\"explanation\" description=\"The first thing the author worked on\"/>\n",
" <string name=\"explanation2\" description=\"The second thing the author worked on\"/>\n",
" <string name=\"explanation3\" description=\"The third thing the author worked on\"/>\n",
" </object>\n",
"</output>\n",
"\n",
"\n",
Expand Down Expand Up @@ -294,10 +316,10 @@
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:llama_index.token_counter.token_counter:> [query] Total LLM token usage: 754 tokens\n",
"> [query] Total LLM token usage: 754 tokens\n",
"INFO:llama_index.token_counter.token_counter:> [query] Total embedding token usage: 11 tokens\n",
"> [query] Total embedding token usage: 11 tokens\n"
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
"HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
"HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n"
]
}
],
Expand All @@ -323,27 +345,10 @@
"output_type": "stream",
"text": [
"{\n",
" \"output\": {\n",
" \"list\": {\n",
" \"name\": \"points\",\n",
" \"description\": \"Bullet points regarding events in the author's life.\",\n",
" \"object\": {\n",
" \"string\": [\n",
" {\n",
" \"name\": \"explanation\",\n",
" \"content\": \"Writing short stories\"\n",
" },\n",
" {\n",
" \"name\": \"explanation2\",\n",
" \"content\": \"Programming on the IBM 1401\"\n",
" },\n",
" {\n",
" \"name\": \"explanation3\",\n",
" \"content\": \"Building a microcomputer\"\n",
" }\n",
" ]\n",
" }\n",
" }\n",
" \"points\": {\n",
" \"explanation\": \"writing\",\n",
" \"explanation2\": \"programming\",\n",
" \"explanation3\": \"philosophy\"\n",
" }\n",
"}\n"
]
Expand Down

0 comments on commit 5f199ef

Please sign in to comment.