From 34c6885d7a98797c7933a51670677511ef95eb0b Mon Sep 17 00:00:00 2001 From: zsimjee <zaydsimjee@gmail.com> Date: Tue, 9 Apr 2024 19:55:16 -0700 Subject: [PATCH] Modernize GuardrailsOutputParser (#12676) --- .../output_parsing/GuardrailsDemo.ipynb | 394 +++++++++++++++--- .../output_parsers/guardrails/base.py | 49 +-- .../pyproject.toml | 4 +- 3 files changed, 352 insertions(+), 95 deletions(-) diff --git a/docs/docs/examples/output_parsing/GuardrailsDemo.ipynb b/docs/docs/examples/output_parsing/GuardrailsDemo.ipynb index 7d0677679f..6d916a94bb 100644 --- a/docs/docs/examples/output_parsing/GuardrailsDemo.ipynb +++ b/docs/docs/examples/output_parsing/GuardrailsDemo.ipynb @@ -17,6 +17,26 @@ "# Guardrails Output Parsing\n" ] }, + { + "cell_type": "markdown", + "id": "dc25e9e7", + "metadata": {}, + "source": [ + "First, set your openai api keys" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f08573d6", + "metadata": {}, + "outputs": [], + "source": [ + "# import os\n", + "\n", + "# os.environ[\"OPENAI_API_KEY\"] = \"sk-...\"" + ] + }, { "attachments": {}, "cell_type": "markdown", @@ -31,7 +51,159 @@ "execution_count": null, "id": "a9c58d56", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: llama-index-llms-openai in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (0.1.14)\n", + "Requirement already satisfied: llama-index-core<0.11.0,>=0.10.24 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-llms-openai) (0.10.27)\n", + "Requirement already satisfied: PyYAML>=6.0.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (6.0.1)\n", + "Requirement already satisfied: SQLAlchemy[asyncio]>=1.4.49 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (2.0.29)\n", + "Requirement already satisfied: aiohttp<4.0.0,>=3.8.6 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (3.9.3)\n", + "Requirement already satisfied: dataclasses-json in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (0.6.4)\n", + "Requirement already satisfied: deprecated>=1.2.9.3 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (1.2.14)\n", + "Requirement already satisfied: dirtyjson<2.0.0,>=1.0.8 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (1.0.8)\n", + "Requirement already satisfied: fsspec>=2023.5.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (2024.3.1)\n", + "Requirement already satisfied: httpx in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (0.27.0)\n", + "Requirement already satisfied: llamaindex-py-client<0.2.0,>=0.1.16 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (0.1.16)\n", + "Requirement already satisfied: nest-asyncio<2.0.0,>=1.5.8 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (1.6.0)\n", + "Requirement already satisfied: networkx>=3.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (3.3)\n", + "Requirement already satisfied: nltk<4.0.0,>=3.8.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (3.8.1)\n", + "Requirement already satisfied: numpy in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (1.26.4)\n", + "Requirement already satisfied: openai>=1.1.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (1.16.2)\n", + "Requirement already satisfied: pandas in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (2.2.1)\n", + "Requirement already satisfied: pillow>=9.0.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (10.3.0)\n", + "Requirement already satisfied: requests>=2.31.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (2.31.0)\n", + "Requirement already satisfied: tenacity<9.0.0,>=8.2.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (8.2.3)\n", + "Requirement already satisfied: tiktoken>=0.3.3 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (0.5.2)\n", + "Requirement already satisfied: tqdm<5.0.0,>=4.66.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (4.66.2)\n", + "Requirement already satisfied: typing-extensions>=4.5.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (4.11.0)\n", + "Requirement already satisfied: typing-inspect>=0.8.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (0.9.0)\n", + "Requirement already satisfied: wrapt in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (1.16.0)\n", + "Requirement already satisfied: aiosignal>=1.1.2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (1.3.1)\n", + "Requirement already satisfied: attrs>=17.3.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (23.2.0)\n", + "Requirement already satisfied: frozenlist>=1.1.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (1.4.1)\n", + "Requirement already satisfied: multidict<7.0,>=4.5 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (6.0.5)\n", + "Requirement already satisfied: yarl<2.0,>=1.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (1.9.4)\n", + "Requirement already satisfied: pydantic>=1.10 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llamaindex-py-client<0.2.0,>=0.1.16->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (2.4.2)\n", + "Requirement already satisfied: anyio in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from httpx->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (4.3.0)\n", + "Requirement already satisfied: certifi in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from httpx->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (2024.2.2)\n", + "Requirement already satisfied: httpcore==1.* in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from httpx->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (1.0.5)\n", + "Requirement already satisfied: idna in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from httpx->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (3.6)\n", + "Requirement already satisfied: sniffio in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from httpx->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (1.3.1)\n", + "Requirement already satisfied: h11<0.15,>=0.13 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from httpcore==1.*->httpx->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (0.14.0)\n", + "Requirement already satisfied: click in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from nltk<4.0.0,>=3.8.1->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (8.1.7)\n", + "Requirement already satisfied: joblib in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from nltk<4.0.0,>=3.8.1->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (1.4.0)\n", + "Requirement already satisfied: regex>=2021.8.3 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from nltk<4.0.0,>=3.8.1->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (2023.12.25)\n", + "Requirement already satisfied: distro<2,>=1.7.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from openai>=1.1.0->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (1.9.0)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from requests>=2.31.0->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (3.3.2)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from requests>=2.31.0->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (2.2.1)\n", + "Requirement already satisfied: greenlet!=0.4.17 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from SQLAlchemy[asyncio]>=1.4.49->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (3.0.3)\n", + "Requirement already satisfied: mypy-extensions>=0.3.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from typing-inspect>=0.8.0->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (1.0.0)\n", + "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from dataclasses-json->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (3.21.1)\n", + "Requirement already satisfied: python-dateutil>=2.8.2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from pandas->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (2.9.0.post0)\n", + "Requirement already satisfied: pytz>=2020.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from pandas->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (2024.1)\n", + "Requirement already satisfied: tzdata>=2022.7 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from pandas->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (2024.1)\n", + "Requirement already satisfied: packaging>=17.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from marshmallow<4.0.0,>=3.18.0->dataclasses-json->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (23.2)\n", + "Requirement already satisfied: annotated-types>=0.4.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from pydantic>=1.10->llamaindex-py-client<0.2.0,>=0.1.16->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (0.6.0)\n", + "Requirement already satisfied: pydantic-core==2.10.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from pydantic>=1.10->llamaindex-py-client<0.2.0,>=0.1.16->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (2.10.1)\n", + "Requirement already satisfied: six>=1.5 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from python-dateutil>=2.8.2->pandas->llama-index-core<0.11.0,>=0.10.24->llama-index-llms-openai) (1.16.0)\n", + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.0\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", + "Note: you may need to restart the kernel to use updated packages.\n", + "Requirement already satisfied: llama-index-output-parsers-guardrails in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (0.1.2)\n", + "Collecting guardrails-ai<0.4.0,>=0.3.2 (from llama-index-output-parsers-guardrails)\n", + " Obtaining dependency information for guardrails-ai<0.4.0,>=0.3.2 from https://files.pythonhosted.org/packages/ae/5f/656bb84c9be700fff22719ddd1cb98b078dfbff51738f7e19998f4cf4d64/guardrails_ai-0.3.3-py3-none-any.whl.metadata\n", + " Using cached guardrails_ai-0.3.3-py3-none-any.whl.metadata (8.6 kB)\n", + "Requirement already satisfied: llama-index-core<0.11.0,>=0.10.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-output-parsers-guardrails) (0.10.27)\n", + "Requirement already satisfied: eliot<2.0.0,>=1.15.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (1.15.0)\n", + "Requirement already satisfied: eliot-tree<22.0.0,>=21.0.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (21.0.0)\n", + "Requirement already satisfied: griffe<0.37.0,>=0.36.9 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (0.36.9)\n", + "Requirement already satisfied: lxml<5.0.0,>=4.9.3 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (4.9.4)\n", + "Requirement already satisfied: openai<2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (1.16.2)\n", + "Requirement already satisfied: pydantic<2.5,>=1.10.9 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (2.4.2)\n", + "Requirement already satisfied: pydash<8.0.0,>=7.0.6 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (7.0.7)\n", + "Requirement already satisfied: python-dateutil<3.0.0,>=2.8.2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (2.9.0.post0)\n", + "Requirement already satisfied: regex<2024.0.0,>=2023.10.3 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (2023.12.25)\n", + "Requirement already satisfied: rich<14.0.0,>=13.6.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (13.7.1)\n", + "Requirement already satisfied: rstr<4.0.0,>=3.2.2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (3.2.2)\n", + "Requirement already satisfied: tenacity>=8.1.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (8.2.3)\n", + "Requirement already satisfied: tiktoken<0.6.0,>=0.5.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (0.5.2)\n", + "Requirement already satisfied: typer<0.10.0,>=0.9.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (0.9.4)\n", + "Requirement already satisfied: typing-extensions<5.0.0,>=4.8.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (4.11.0)\n", + "Requirement already satisfied: PyYAML>=6.0.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (6.0.1)\n", + "Requirement already satisfied: SQLAlchemy[asyncio]>=1.4.49 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (2.0.29)\n", + "Requirement already satisfied: aiohttp<4.0.0,>=3.8.6 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (3.9.3)\n", + "Requirement already satisfied: dataclasses-json in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (0.6.4)\n", + "Requirement already satisfied: deprecated>=1.2.9.3 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (1.2.14)\n", + "Requirement already satisfied: dirtyjson<2.0.0,>=1.0.8 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (1.0.8)\n", + "Requirement already satisfied: fsspec>=2023.5.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (2024.3.1)\n", + "Requirement already satisfied: httpx in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (0.27.0)\n", + "Requirement already satisfied: llamaindex-py-client<0.2.0,>=0.1.16 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (0.1.16)\n", + "Requirement already satisfied: nest-asyncio<2.0.0,>=1.5.8 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (1.6.0)\n", + "Requirement already satisfied: networkx>=3.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (3.3)\n", + "Requirement already satisfied: nltk<4.0.0,>=3.8.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (3.8.1)\n", + "Requirement already satisfied: numpy in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (1.26.4)\n", + "Requirement already satisfied: pandas in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (2.2.1)\n", + "Requirement already satisfied: pillow>=9.0.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (10.3.0)\n", + "Requirement already satisfied: requests>=2.31.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (2.31.0)\n", + "Requirement already satisfied: tqdm<5.0.0,>=4.66.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (4.66.2)\n", + "Requirement already satisfied: typing-inspect>=0.8.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (0.9.0)\n", + "Requirement already satisfied: wrapt in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (1.16.0)\n", + "Requirement already satisfied: aiosignal>=1.1.2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (1.3.1)\n", + "Requirement already satisfied: attrs>=17.3.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (23.2.0)\n", + "Requirement already satisfied: frozenlist>=1.1.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (1.4.1)\n", + "Requirement already satisfied: multidict<7.0,>=4.5 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (6.0.5)\n", + "Requirement already satisfied: yarl<2.0,>=1.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (1.9.4)\n", + "Requirement already satisfied: six in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot<2.0.0,>=1.15.0->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (1.16.0)\n", + "Requirement already satisfied: zope.interface in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot<2.0.0,>=1.15.0->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (6.2)\n", + "Requirement already satisfied: pyrsistent>=0.11.8 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot<2.0.0,>=1.15.0->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (0.20.0)\n", + "Requirement already satisfied: boltons>=19.0.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot<2.0.0,>=1.15.0->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (24.0.0)\n", + "Requirement already satisfied: orjson in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot<2.0.0,>=1.15.0->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (3.10.0)\n", + "Requirement already satisfied: jmespath>=0.7.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot-tree<22.0.0,>=21.0.0->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (1.0.1)\n", + "Requirement already satisfied: iso8601>=0.1.10 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot-tree<22.0.0,>=21.0.0->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (2.1.0)\n", + "Requirement already satisfied: colored>=1.4.2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot-tree<22.0.0,>=21.0.0->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (2.2.4)\n", + "Requirement already satisfied: toolz>=0.8.2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot-tree<22.0.0,>=21.0.0->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (0.12.1)\n", + "Requirement already satisfied: colorama>=0.4 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from griffe<0.37.0,>=0.36.9->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (0.4.6)\n", + "Requirement already satisfied: anyio in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from httpx->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (4.3.0)\n", + "Requirement already satisfied: certifi in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from httpx->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (2024.2.2)\n", + "Requirement already satisfied: httpcore==1.* in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from httpx->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (1.0.5)\n", + "Requirement already satisfied: idna in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from httpx->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (3.6)\n", + "Requirement already satisfied: sniffio in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from httpx->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (1.3.1)\n", + "Requirement already satisfied: h11<0.15,>=0.13 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from httpcore==1.*->httpx->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (0.14.0)\n", + "Requirement already satisfied: click in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from nltk<4.0.0,>=3.8.1->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (8.1.7)\n", + "Requirement already satisfied: joblib in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from nltk<4.0.0,>=3.8.1->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (1.4.0)\n", + "Requirement already satisfied: distro<2,>=1.7.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from openai<2->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (1.9.0)\n", + "Requirement already satisfied: annotated-types>=0.4.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from pydantic<2.5,>=1.10.9->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (0.6.0)\n", + "Requirement already satisfied: pydantic-core==2.10.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from pydantic<2.5,>=1.10.9->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (2.10.1)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from requests>=2.31.0->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (3.3.2)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from requests>=2.31.0->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (2.2.1)\n", + "Requirement already satisfied: markdown-it-py>=2.2.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from rich<14.0.0,>=13.6.0->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (3.0.0)\n", + "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from rich<14.0.0,>=13.6.0->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (2.17.2)\n", + "Requirement already satisfied: greenlet!=0.4.17 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from SQLAlchemy[asyncio]>=1.4.49->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (3.0.3)\n", + "Requirement already satisfied: mypy-extensions>=0.3.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from typing-inspect>=0.8.0->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (1.0.0)\n", + "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from dataclasses-json->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (3.21.1)\n", + "Requirement already satisfied: pytz>=2020.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from pandas->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (2024.1)\n", + "Requirement already satisfied: tzdata>=2022.7 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from pandas->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (2024.1)\n", + "Requirement already satisfied: mdurl~=0.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from markdown-it-py>=2.2.0->rich<14.0.0,>=13.6.0->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (0.1.2)\n", + "Requirement already satisfied: packaging>=17.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from marshmallow<4.0.0,>=3.18.0->dataclasses-json->llama-index-core<0.11.0,>=0.10.1->llama-index-output-parsers-guardrails) (23.2)\n", + "Requirement already satisfied: setuptools in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from zope.interface->eliot<2.0.0,>=1.15.0->guardrails-ai<0.4.0,>=0.3.2->llama-index-output-parsers-guardrails) (65.5.0)\n", + "Using cached guardrails_ai-0.3.3-py3-none-any.whl (148 kB)\n", + "Installing collected packages: guardrails-ai\n", + " Attempting uninstall: guardrails-ai\n", + " Found existing installation: guardrails-ai 0.4.2\n", + " Uninstalling guardrails-ai-0.4.2:\n", + " Successfully uninstalled guardrails-ai-0.4.2\n", + "Successfully installed guardrails-ai-0.3.3\n", + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.0\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], "source": [ "%pip install llama-index-llms-openai\n", "%pip install llama-index-output-parsers-guardrails" @@ -42,9 +214,65 @@ "execution_count": null, "id": "e716f66f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: guardrails-ai in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (0.3.3)\n", + "Requirement already satisfied: eliot<2.0.0,>=1.15.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai) (1.15.0)\n", + "Requirement already satisfied: eliot-tree<22.0.0,>=21.0.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai) (21.0.0)\n", + "Requirement already satisfied: griffe<0.37.0,>=0.36.9 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai) (0.36.9)\n", + "Requirement already satisfied: lxml<5.0.0,>=4.9.3 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai) (4.9.4)\n", + "Requirement already satisfied: openai<2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai) (1.16.2)\n", + "Requirement already satisfied: pydantic<2.5,>=1.10.9 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai) (2.4.2)\n", + "Requirement already satisfied: pydash<8.0.0,>=7.0.6 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai) (7.0.7)\n", + "Requirement already satisfied: python-dateutil<3.0.0,>=2.8.2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai) (2.9.0.post0)\n", + "Requirement already satisfied: regex<2024.0.0,>=2023.10.3 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai) (2023.12.25)\n", + "Requirement already satisfied: rich<14.0.0,>=13.6.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai) (13.7.1)\n", + "Requirement already satisfied: rstr<4.0.0,>=3.2.2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai) (3.2.2)\n", + "Requirement already satisfied: tenacity>=8.1.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai) (8.2.3)\n", + "Requirement already satisfied: tiktoken<0.6.0,>=0.5.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai) (0.5.2)\n", + "Requirement already satisfied: typer<0.10.0,>=0.9.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai) (0.9.4)\n", + "Requirement already satisfied: typing-extensions<5.0.0,>=4.8.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from guardrails-ai) (4.11.0)\n", + "Requirement already satisfied: six in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot<2.0.0,>=1.15.0->guardrails-ai) (1.16.0)\n", + "Requirement already satisfied: zope.interface in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot<2.0.0,>=1.15.0->guardrails-ai) (6.2)\n", + "Requirement already satisfied: pyrsistent>=0.11.8 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot<2.0.0,>=1.15.0->guardrails-ai) (0.20.0)\n", + "Requirement already satisfied: boltons>=19.0.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot<2.0.0,>=1.15.0->guardrails-ai) (24.0.0)\n", + "Requirement already satisfied: orjson in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot<2.0.0,>=1.15.0->guardrails-ai) (3.10.0)\n", + "Requirement already satisfied: jmespath>=0.7.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot-tree<22.0.0,>=21.0.0->guardrails-ai) (1.0.1)\n", + "Requirement already satisfied: iso8601>=0.1.10 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot-tree<22.0.0,>=21.0.0->guardrails-ai) (2.1.0)\n", + "Requirement already satisfied: colored>=1.4.2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot-tree<22.0.0,>=21.0.0->guardrails-ai) (2.2.4)\n", + "Requirement already satisfied: toolz>=0.8.2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from eliot-tree<22.0.0,>=21.0.0->guardrails-ai) (0.12.1)\n", + "Requirement already satisfied: colorama>=0.4 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from griffe<0.37.0,>=0.36.9->guardrails-ai) (0.4.6)\n", + "Requirement already satisfied: anyio<5,>=3.5.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from openai<2->guardrails-ai) (4.3.0)\n", + "Requirement already satisfied: distro<2,>=1.7.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from openai<2->guardrails-ai) (1.9.0)\n", + "Requirement already satisfied: httpx<1,>=0.23.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from openai<2->guardrails-ai) (0.27.0)\n", + "Requirement already satisfied: sniffio in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from openai<2->guardrails-ai) (1.3.1)\n", + "Requirement already satisfied: tqdm>4 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from openai<2->guardrails-ai) (4.66.2)\n", + "Requirement already satisfied: annotated-types>=0.4.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from pydantic<2.5,>=1.10.9->guardrails-ai) (0.6.0)\n", + "Requirement already satisfied: pydantic-core==2.10.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from pydantic<2.5,>=1.10.9->guardrails-ai) (2.10.1)\n", + "Requirement already satisfied: markdown-it-py>=2.2.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from rich<14.0.0,>=13.6.0->guardrails-ai) (3.0.0)\n", + "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from rich<14.0.0,>=13.6.0->guardrails-ai) (2.17.2)\n", + "Requirement already satisfied: requests>=2.26.0 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from tiktoken<0.6.0,>=0.5.1->guardrails-ai) (2.31.0)\n", + "Requirement already satisfied: click<9.0.0,>=7.1.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from typer<0.10.0,>=0.9.0->guardrails-ai) (8.1.7)\n", + "Requirement already satisfied: idna>=2.8 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from anyio<5,>=3.5.0->openai<2->guardrails-ai) (3.6)\n", + "Requirement already satisfied: certifi in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai<2->guardrails-ai) (2024.2.2)\n", + "Requirement already satisfied: httpcore==1.* in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai<2->guardrails-ai) (1.0.5)\n", + "Requirement already satisfied: h11<0.15,>=0.13 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai<2->guardrails-ai) (0.14.0)\n", + "Requirement already satisfied: mdurl~=0.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from markdown-it-py>=2.2.0->rich<14.0.0,>=13.6.0->guardrails-ai) (0.1.2)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken<0.6.0,>=0.5.1->guardrails-ai) (3.3.2)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken<0.6.0,>=0.5.1->guardrails-ai) (2.2.1)\n", + "Requirement already satisfied: setuptools in /Users/zaydsimjee/workspace/zayd_forks/llama_index/.venv/lib/python3.11/site-packages (from zope.interface->eliot<2.0.0,>=1.15.0->guardrails-ai) (65.5.0)\n", + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.0\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], "source": [ - "!pip install guardrails-ai" + "%pip install guardrails-ai" ] }, { @@ -61,10 +289,20 @@ "execution_count": null, "id": "649bea0c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " % Total % Received % Xferd Average Speed Time Time Time Current\n", + " Dload Upload Total Spent Left Speed\n", + "100 75042 100 75042 0 0 390k 0 --:--:-- --:--:-- --:--:-- 396k\n" + ] + } + ], "source": [ "!mkdir -p 'data/paul_graham/'\n", - "!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'" + "!curl 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' > 'data/paul_graham/paul_graham_essay.txt'" ] }, { @@ -88,12 +326,7 @@ "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n", "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))\n", "\n", - "from llama_index.core import VectorStoreIndex, SimpleDirectoryReader\n", - "from IPython.display import Markdown, display\n", - "\n", - "import os\n", - "\n", - "os.environ[\"OPENAI_API_KEY\"] = \"sk-...\"" + "from llama_index.core import VectorStoreIndex, SimpleDirectoryReader" ] }, { @@ -117,10 +350,8 @@ "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" ] } ], @@ -179,7 +410,7 @@ "import guardrails as gd\n", "\n", "\n", - "class Point(BaseModel):\n", + "class BulletPoints(BaseModel):\n", " # In all the fields below, you can define validators as well\n", " # Left out for brevity\n", " explanation: str = Field()\n", @@ -187,8 +418,8 @@ " explanation3: str = Field()\n", "\n", "\n", - "class BulletPoints(BaseModel):\n", - " points: List[Point] = Field(\n", + "class Explanation(BaseModel):\n", + " points: BulletPoints = Field(\n", " description=\"Bullet points regarding events in the author's life.\"\n", " )\n", "\n", @@ -215,10 +446,10 @@ "from llama_index.llms.openai import OpenAI\n", "\n", "# Create a guard object\n", - "guard = gd.Guard.from_pydantic(output_class=BulletPoints, prompt=prompt)\n", + "guard = gd.Guard.from_pydantic(output_class=Explanation, prompt=prompt)\n", "\n", "# Create output parse object\n", - "output_parser = GuardrailsOutputParser(guard, llm=OpenAI())\n", + "output_parser = GuardrailsOutputParser(guard)\n", "\n", "# attach to an llm object\n", "llm = OpenAI(output_parser=output_parser)" @@ -247,13 +478,11 @@ "\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", + " <object name=\"points\" description=\"Bullet points regarding events in the author's life.\">\n", + " <string name=\"explanation\"/>\n", + " <string name=\"explanation2\"/>\n", + " <string name=\"explanation3\"/>\n", + " </object>\n", "</output>\n", "\n", "\n", @@ -292,10 +521,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" ] } ], @@ -318,36 +547,91 @@ "name": "stdout", "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", - " }\n", - "}\n" + "{'points': {'explanation': 'Writing short stories', 'explanation2': 'Programming on the IBM 1401 in 9th grade', 'explanation3': 'Getting a TRS-80 computer and starting programming in 1980'}}\n" ] } ], "source": [ "print(response)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a5b06d22", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Logs\n", + "└── â•â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€ Step 0 ───────────────────────────────────────────────────╮\n", + " │ <span style=\"background-color: #f0f8ff\">â•â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€ Prompt ─────────────────────────────────────────────────╮</span> │\n", + " │ <span style=\"background-color: #f0f8ff\">│ No prompt │</span> │\n", + " │ <span style=\"background-color: #f0f8ff\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n", + " │ <span style=\"background-color: #e7dfeb\">â•â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€ Message History ────────────────────────────────────────────╮</span> │\n", + " │ <span style=\"background-color: #e7dfeb\">│ No message history. │</span> │\n", + " │ <span style=\"background-color: #e7dfeb\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n", + " │ <span style=\"background-color: #f5f5dc\">â•â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€ Raw LLM Output ─────────────────────────────────────────────╮</span> │\n", + " │ <span style=\"background-color: #f5f5dc\">│ { │</span> │\n", + " │ <span style=\"background-color: #f5f5dc\">│ \"points\": { │</span> │\n", + " │ <span style=\"background-color: #f5f5dc\">│ \"explanation\": \"Writing short stories\", │</span> │\n", + " │ <span style=\"background-color: #f5f5dc\">│ \"explanation2\": \"Programming on the IBM 1401 in 9th grade\", │</span> │\n", + " │ <span style=\"background-color: #f5f5dc\">│ \"explanation3\": \"Getting a TRS-80 computer and starting programming in 1980\" │</span> │\n", + " │ <span style=\"background-color: #f5f5dc\">│ } │</span> │\n", + " │ <span style=\"background-color: #f5f5dc\">│ } │</span> │\n", + " │ <span style=\"background-color: #f5f5dc\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n", + " │ <span style=\"background-color: #f0fff0\">â•â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€ Validated Output ────────────────────────────────────────────╮</span> │\n", + " │ <span style=\"background-color: #f0fff0\">│ { │</span> │\n", + " │ <span style=\"background-color: #f0fff0\">│ 'points': { │</span> │\n", + " │ <span style=\"background-color: #f0fff0\">│ 'explanation': 'Writing short stories', │</span> │\n", + " │ <span style=\"background-color: #f0fff0\">│ 'explanation2': 'Programming on the IBM 1401 in 9th grade', │</span> │\n", + " │ <span style=\"background-color: #f0fff0\">│ 'explanation3': 'Getting a TRS-80 computer and starting programming in 1980' │</span> │\n", + " │ <span style=\"background-color: #f0fff0\">│ } │</span> │\n", + " │ <span style=\"background-color: #f0fff0\">│ } │</span> │\n", + " │ <span style=\"background-color: #f0fff0\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span> │\n", + " ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n", + "</pre>\n" + ], + "text/plain": [ + "Logs\n", + "└── â•â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€ Step 0 ───────────────────────────────────────────────────╮\n", + " │ \u001b[48;2;240;248;255mâ•â”€\u001b[0m\u001b[48;2;240;248;255m───────────────────────────────────────────────\u001b[0m\u001b[48;2;240;248;255m Prompt \u001b[0m\u001b[48;2;240;248;255m────────────────────────────────────────────────\u001b[0m\u001b[48;2;240;248;255m─╮\u001b[0m │\n", + " │ \u001b[48;2;240;248;255m│\u001b[0m\u001b[48;2;240;248;255m \u001b[0m\u001b[48;2;240;248;255mNo prompt\u001b[0m\u001b[48;2;240;248;255m \u001b[0m\u001b[48;2;240;248;255m \u001b[0m\u001b[48;2;240;248;255m│\u001b[0m │\n", + " │ \u001b[48;2;240;248;255m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n", + " │ \u001b[48;2;231;223;235mâ•â”€\u001b[0m\u001b[48;2;231;223;235m───────────────────────────────────────────\u001b[0m\u001b[48;2;231;223;235m Message History \u001b[0m\u001b[48;2;231;223;235m───────────────────────────────────────────\u001b[0m\u001b[48;2;231;223;235m─╮\u001b[0m │\n", + " │ \u001b[48;2;231;223;235m│\u001b[0m\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235mNo message history.\u001b[0m\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235m│\u001b[0m │\n", + " │ \u001b[48;2;231;223;235m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n", + " │ \u001b[48;2;245;245;220mâ•â”€\u001b[0m\u001b[48;2;245;245;220m───────────────────────────────────────────\u001b[0m\u001b[48;2;245;245;220m Raw LLM Output \u001b[0m\u001b[48;2;245;245;220m────────────────────────────────────────────\u001b[0m\u001b[48;2;245;245;220m─╮\u001b[0m │\n", + " │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m{\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n", + " │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \"points\": {\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n", + " │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \"explanation\": \"Writing short stories\",\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n", + " │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \"explanation2\": \"Programming on the IBM 1401 in 9th grade\",\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n", + " │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \"explanation3\": \"Getting a TRS-80 computer and starting programming in 1980\"\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n", + " │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m }\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n", + " │ \u001b[48;2;245;245;220m│\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m}\u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m \u001b[0m\u001b[48;2;245;245;220m│\u001b[0m │\n", + " │ \u001b[48;2;245;245;220m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n", + " │ \u001b[48;2;240;255;240mâ•â”€\u001b[0m\u001b[48;2;240;255;240m──────────────────────────────────────────\u001b[0m\u001b[48;2;240;255;240m Validated Output \u001b[0m\u001b[48;2;240;255;240m───────────────────────────────────────────\u001b[0m\u001b[48;2;240;255;240m─╮\u001b[0m │\n", + " │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m{\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n", + " │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m 'points': {\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n", + " │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m 'explanation': 'Writing short stories',\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n", + " │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m 'explanation2': 'Programming on the IBM 1401 in 9th grade',\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n", + " │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m 'explanation3': 'Getting a TRS-80 computer and starting programming in 1980'\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n", + " │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m }\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n", + " │ \u001b[48;2;240;255;240m│\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m}\u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m \u001b[0m\u001b[48;2;240;255;240m│\u001b[0m │\n", + " │ \u001b[48;2;240;255;240m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m │\n", + " ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# View a summary of what the guard did\n", + "guard.history.last.tree" + ] } ], "metadata": { diff --git a/llama-index-integrations/output_parsers/llama-index-output-parsers-guardrails/llama_index/output_parsers/guardrails/base.py b/llama-index-integrations/output_parsers/llama-index-output-parsers-guardrails/llama_index/output_parsers/guardrails/base.py index 608a2c7c9d..3f275bd58f 100644 --- a/llama-index-integrations/output_parsers/llama-index-output-parsers-guardrails/llama_index/output_parsers/guardrails/base.py +++ b/llama-index-integrations/output_parsers/llama-index-output-parsers-guardrails/llama_index/output_parsers/guardrails/base.py @@ -1,26 +1,17 @@ -"""Guardrails output parser. +""" +Guardrails output parser. See https://github.com/ShreyaR/guardrails. """ + from copy import deepcopy -from typing import TYPE_CHECKING, Any, Callable, Optional +from typing import Any, Optional from deprecated import deprecated -from llama_index.core.output_parsers.base import ChainableOutputParser - from guardrails import Guard -if TYPE_CHECKING: - from llama_index.core.bridge.langchain import BaseLLM - - -def get_callable(llm: Optional["BaseLLM"]) -> Optional[Callable]: - """Get callable.""" - if llm is None: - return None - - return llm.__call__ +from llama_index.core.output_parsers.base import ChainableOutputParser class GuardrailsOutputParser(ChainableOutputParser): @@ -29,55 +20,37 @@ class GuardrailsOutputParser(ChainableOutputParser): def __init__( self, guard: Guard, - llm: Optional["BaseLLM"] = None, format_key: Optional[str] = None, ): """Initialize a Guardrails output parser.""" self.guard: Guard = guard - self.llm = llm self.format_key = format_key @classmethod @deprecated(version="0.8.46") - def from_rail( - cls, rail: str, llm: Optional["BaseLLM"] = None - ) -> "GuardrailsOutputParser": + def from_rail(cls, rail: str) -> "GuardrailsOutputParser": """From rail.""" if Guard is None: raise ImportError( "Guardrails is not installed. Run `pip install guardrails-ai`. " ) - return cls(Guard.from_rail(rail), llm=llm) + return cls(Guard.from_rail(rail)) @classmethod @deprecated(version="0.8.46") - def from_rail_string( - cls, rail_string: str, llm: Optional["BaseLLM"] = None - ) -> "GuardrailsOutputParser": + def from_rail_string(cls, rail_string: str) -> "GuardrailsOutputParser": """From rail string.""" if Guard is None: raise ImportError( "Guardrails is not installed. Run `pip install guardrails-ai`. " ) - return cls(Guard.from_rail_string(rail_string), llm=llm) + return cls(Guard.from_rail_string(rail_string)) - def parse( - self, - output: str, - llm: Optional["BaseLLM"] = None, - num_reasks: Optional[int] = 1, - *args: Any, - **kwargs: Any - ) -> Any: + def parse(self, output: str, *args: Any, **kwargs: Any) -> Any: """Parse, validate, and correct errors programmatically.""" - llm = llm or self.llm - llm_fn = get_callable(llm) - - return self.guard.parse( - output, llm_api=llm_fn, num_reasks=num_reasks, *args, **kwargs - ) + return self.guard.parse(output, *args, **kwargs).validated_output def format(self, query: str) -> str: """Format a query with structured output formatting instructions.""" diff --git a/llama-index-integrations/output_parsers/llama-index-output-parsers-guardrails/pyproject.toml b/llama-index-integrations/output_parsers/llama-index-output-parsers-guardrails/pyproject.toml index 774a2c7e5f..ed883e78a2 100644 --- a/llama-index-integrations/output_parsers/llama-index-output-parsers-guardrails/pyproject.toml +++ b/llama-index-integrations/output_parsers/llama-index-output-parsers-guardrails/pyproject.toml @@ -27,12 +27,12 @@ exclude = ["**/BUILD"] license = "MIT" name = "llama-index-output-parsers-guardrails" readme = "README.md" -version = "0.1.2" +version = "0.1.3" [tool.poetry.dependencies] python = ">=3.8.1,<4.0" llama-index-core = "^0.10.1" -guardrails-ai = "^0.3.2" +guardrails-ai = "^0.4.1" [tool.poetry.group.dev.dependencies] ipython = "8.10.0" -- GitLab