{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Streaming Structured Data\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Few imports and global variables\n", "from rich import print\n", "import guardrails as gd\n", "from IPython.display import clear_output\n", "import time" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setup\n", "\n", "Install and import the necessary validators from Guardrails hub" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try:\n", " from guardrails.hub import LowerCase, UpperCase, ValidRange, OneLine\n", "except ImportError:\n", " gd.install(\"hub://guardrails/valid_range\")\n", " gd.install(\"hub://guardrails/uppercase\")\n", " gd.install(\"hub://guardrails/lowercase\")\n", " gd.install(\"hub://guardrails/one_line\")\n", " from guardrails.hub import LowerCase, UpperCase, ValidRange, OneLine" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. For structured JSON output\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Define the prompt and output schema\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from pydantic import BaseModel, Field\n", "from typing import List\n", "\n", "prompt = \"\"\"\n", "Given the following doctor's notes about a patient, please extract a dictionary that contains the patient's information.\n", "\n", "${doctors_notes}\n", "\n", "${gr.complete_xml_suffix_v2}\n", "\"\"\"\n", "\n", "doctors_notes = \"\"\"152 y/o female with chronic macular rash to face and hair, worse in beard, eyebrows and nares.\n", "The rash is itchy, flaky and slightly scaly. Moderate response to OTC steroid cream. Patient has been using cream for 2 weeks and also suffers from diabetes.\"\"\"\n", "\n", "\n", "class Symptom(BaseModel):\n", " symptom: str = Field(description=\"Symptom that a patient is experiencing\")\n", " affected_area: str = Field(\n", " description=\"What part of the body the symptom is affecting\",\n", " validators=[\n", " LowerCase(on_fail=\"fix\"),\n", " ],\n", " )\n", "\n", "\n", "class Medication(BaseModel):\n", " medication: str = Field(\n", " description=\"Name of the medication the patient is taking\",\n", " validators=[UpperCase(on_fail=\"fix\")],\n", " )\n", " response: str = Field(description=\"How the patient is responding to the medication\")\n", "\n", "\n", "class PatientInfo(BaseModel):\n", " gender: str = Field(description=\"Patient's gender\")\n", " age: int = Field(\n", " description=\"Patient's age\",\n", " validators=[ValidRange(min=0, max=100, on_fail=\"fix\")],\n", " )\n", " symptoms: List[Symptom] = Field(\n", " description=\"Symptoms that the patient is currently experiencing. Each symptom should be classified into separate item in the list.\"\n", " )\n", " current_meds: List[Medication] = Field(\n", " description=\"Medications the patient is currently taking and their response\"\n", " )\n", " miscellaneous: str = Field(\n", " description=\"Any other information that is relevant to the patient's health; something that doesn't fit into the other categories.\",\n", " validators=[LowerCase(on_fail=\"fix\"), OneLine(on_fail=\"fix\")],\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create the Guard object\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "guard = gd.Guard.for_pydantic(output_class=PatientInfo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Example 1: No streaming\n", "\n", "By default, the `stream` parameter is set to `False`\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/dtam/dev/guardrails/guardrails/validator_service/__init__.py:85: UserWarning: Could not obtain an event loop. Falling back to synchronous validation.\n", " warnings.warn(\n" ] }, { "data": { "text/html": [ "
{\n", " 'gender': 'female',\n", " 'age': 100,\n", " 'symptoms': [\n", " {'symptom': 'chronic macular rash', 'affected_area': 'face'},\n", " {'symptom': 'itchy', 'affected_area': 'beard'},\n", " {'symptom': 'flaky', 'affected_area': 'eyebrows'},\n", " {'symptom': 'slightly scaly', 'affected_area': 'nares'}\n", " ],\n", " 'current_meds': [{'medication': 'OTC STEROID CREAM', 'response': 'moderate'}],\n", " 'miscellaneous': 'patient also suffers from diabetes'\n", "}\n", "\n" ], "text/plain": [ "\u001b[1m{\u001b[0m\n", " \u001b[32m'gender'\u001b[0m: \u001b[32m'female'\u001b[0m,\n", " \u001b[32m'age'\u001b[0m: \u001b[1;36m100\u001b[0m,\n", " \u001b[32m'symptoms'\u001b[0m: \u001b[1m[\u001b[0m\n", " \u001b[1m{\u001b[0m\u001b[32m'symptom'\u001b[0m: \u001b[32m'chronic macular rash'\u001b[0m, \u001b[32m'affected_area'\u001b[0m: \u001b[32m'face'\u001b[0m\u001b[1m}\u001b[0m,\n", " \u001b[1m{\u001b[0m\u001b[32m'symptom'\u001b[0m: \u001b[32m'itchy'\u001b[0m, \u001b[32m'affected_area'\u001b[0m: \u001b[32m'beard'\u001b[0m\u001b[1m}\u001b[0m,\n", " \u001b[1m{\u001b[0m\u001b[32m'symptom'\u001b[0m: \u001b[32m'flaky'\u001b[0m, \u001b[32m'affected_area'\u001b[0m: \u001b[32m'eyebrows'\u001b[0m\u001b[1m}\u001b[0m,\n", " \u001b[1m{\u001b[0m\u001b[32m'symptom'\u001b[0m: \u001b[32m'slightly scaly'\u001b[0m, \u001b[32m'affected_area'\u001b[0m: \u001b[32m'nares'\u001b[0m\u001b[1m}\u001b[0m\n", " \u001b[1m]\u001b[0m,\n", " \u001b[32m'current_meds'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1m{\u001b[0m\u001b[32m'medication'\u001b[0m: \u001b[32m'OTC STEROID CREAM'\u001b[0m, \u001b[32m'response'\u001b[0m: \u001b[32m'moderate'\u001b[0m\u001b[1m}\u001b[0m\u001b[1m]\u001b[0m,\n", " \u001b[32m'miscellaneous'\u001b[0m: \u001b[32m'patient also suffers from diabetes'\u001b[0m\n", "\u001b[1m}\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Wrap the litellm OpenAI API call with the `guard` object\n", "raw_llm_output, validated_output, *rest = guard(\n", " model=\"gpt-3.5-turbo\",\n", " messages=[{\"role\": \"user\", \"content\": prompt}],\n", " prompt_params={\"doctors_notes\": doctors_notes},\n", " max_tokens=1024,\n", " temperature=0.3,\n", ")\n", "\n", "# Print the validated output from the LLM\n", "print(validated_output)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Logs\n",
"└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
" │ ╭─────────────────────────────────────────────── Messages ────────────────────────────────────────────────╮ │\n",
" │ │ ┏━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ │ │\n",
" │ │ ┃ Role ┃ Content ┃ │ │\n",
" │ │ ┡━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ │\n",
" │ │ │ user │ │ │ │\n",
" │ │ │ │ Given the following doctor's notes about a patient, please extract a dictionary that │ │ │\n",
" │ │ │ │ contains the patient's information. │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ │ │ 152 y/o female with chronic macular rash to face and hair, worse in beard, eyebrows and │ │ │\n",
" │ │ │ │ nares. │ │ │\n",
" │ │ │ │ The rash is itchy, flaky and slightly scaly. Moderate response to OTC steroid cream. Patient │ │ │\n",
" │ │ │ │ has been using cream for 2 weeks and also suffers from diabetes. │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ │ │ Given below is XML that describes the information to extract from this document and the tags │ │ │\n",
" │ │ │ │ to extract it into. │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ │ │ <output> │ │ │\n",
" │ │ │ │ <string description=\"Patient's gender\" name=\"gender\" required=\"true\"></string> │ │ │\n",
" │ │ │ │ <integer description=\"Patient's age\" format=\"guardrails/valid_range: 0 100\" name=\"age\" │ │ │\n",
" │ │ │ │ required=\"true\"></integer> │ │ │\n",
" │ │ │ │ <list description=\"Symptoms that the patient is currently experiencing. Each symptom │ │ │\n",
" │ │ │ │ should be classified into separate item in the list.\" name=\"symptoms\" required=\"true\"> │ │ │\n",
" │ │ │ │ <object required=\"true\"> │ │ │\n",
" │ │ │ │ <string description=\"Symptom that a patient is experiencing\" name=\"symptom\" │ │ │\n",
" │ │ │ │ required=\"true\"></string> │ │ │\n",
" │ │ │ │ <string description=\"What part of the body the symptom is affecting\" │ │ │\n",
" │ │ │ │ format=\"guardrails/lowercase\" name=\"affected_area\" required=\"true\"></string> │ │ │\n",
" │ │ │ │ </object> │ │ │\n",
" │ │ │ │ </list> │ │ │\n",
" │ │ │ │ <list description=\"Medications the patient is currently taking and their response\" │ │ │\n",
" │ │ │ │ name=\"current_meds\" required=\"true\"> │ │ │\n",
" │ │ │ │ <object required=\"true\"> │ │ │\n",
" │ │ │ │ <string description=\"Name of the medication the patient is taking\" │ │ │\n",
" │ │ │ │ format=\"guardrails/uppercase\" name=\"medication\" required=\"true\"></string> │ │ │\n",
" │ │ │ │ <string description=\"How the patient is responding to the medication\" name=\"response\" │ │ │\n",
" │ │ │ │ required=\"true\"></string> │ │ │\n",
" │ │ │ │ </object> │ │ │\n",
" │ │ │ │ </list> │ │ │\n",
" │ │ │ │ <string description=\"Any other information that is relevant to the patient's health; │ │ │\n",
" │ │ │ │ something that doesn't fit into the other categories.\" format=\"guardrails/lowercase; │ │ │\n",
" │ │ │ │ guardrails/one_line\" name=\"miscellaneous\" required=\"true\"></string> │ │ │\n",
" │ │ │ │ </output> │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ │ │ ONLY return a valid JSON object (no other text is necessary), where the key of the field in │ │ │\n",
" │ │ │ │ JSON is the `name` attribute of the corresponding XML, and the value is of the type │ │ │\n",
" │ │ │ │ specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including │ │ │\n",
" │ │ │ │ any types and format requests e.g. requests for lists, objects and specific types. Be │ │ │\n",
" │ │ │ │ correct and concise. │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ │ │ Here are examples of simple (XML, JSON) pairs that show the expected behavior: │ │ │\n",
" │ │ │ │ - `<string name='foo' format='two-words lower-case' />` => `{'foo': 'example one'}` │ │ │\n",
" │ │ │ │ - `<list name='bar'><string format='upper-case' /></list>` => `{\"bar\": ['STRING ONE', │ │ │\n",
" │ │ │ │ 'STRING TWO', etc.]}` │ │ │\n",
" │ │ │ │ - `<object name='baz'><string name=\"foo\" format=\"capitalize two-words\" /><integer │ │ │\n",
" │ │ │ │ name=\"index\" format=\"1-indexed\" /></object>` => `{'baz': {'foo': 'Some String', 'index': │ │ │\n",
" │ │ │ │ 1}}` │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ └──────┴──────────────────────────────────────────────────────────────────────────────────────────────┘ │ │\n",
" │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
" │ ╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮ │\n",
" │ │ { │ │\n",
" │ │ \"gender\": \"female\", │ │\n",
" │ │ \"age\": 152, │ │\n",
" │ │ \"symptoms\": [ │ │\n",
" │ │ { │ │\n",
" │ │ \"symptom\": \"chronic macular rash\", │ │\n",
" │ │ \"affected_area\": \"face\" │ │\n",
" │ │ }, │ │\n",
" │ │ { │ │\n",
" │ │ \"symptom\": \"itchy\", │ │\n",
" │ │ \"affected_area\": \"beard\" │ │\n",
" │ │ }, │ │\n",
" │ │ { │ │\n",
" │ │ \"symptom\": \"flaky\", │ │\n",
" │ │ \"affected_area\": \"eyebrows\" │ │\n",
" │ │ }, │ │\n",
" │ │ { │ │\n",
" │ │ \"symptom\": \"slightly scaly\", │ │\n",
" │ │ \"affected_area\": \"nares\" │ │\n",
" │ │ } │ │\n",
" │ │ ], │ │\n",
" │ │ \"current_meds\": [ │ │\n",
" │ │ { │ │\n",
" │ │ \"medication\": \"OTC steroid cream\", │ │\n",
" │ │ \"response\": \"moderate\" │ │\n",
" │ │ } │ │\n",
" │ │ ], │ │\n",
" │ │ \"miscellaneous\": \"patient also suffers from diabetes\" │ │\n",
" │ │ } │ │\n",
" │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
" │ ╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮ │\n",
" │ │ { │ │\n",
" │ │ 'gender': 'female', │ │\n",
" │ │ 'age': 100, │ │\n",
" │ │ 'symptoms': [ │ │\n",
" │ │ {'symptom': 'chronic macular rash', 'affected_area': 'face'}, │ │\n",
" │ │ {'symptom': 'itchy', 'affected_area': 'beard'}, │ │\n",
" │ │ {'symptom': 'flaky', 'affected_area': 'eyebrows'}, │ │\n",
" │ │ {'symptom': 'slightly scaly', 'affected_area': 'nares'} │ │\n",
" │ │ ], │ │\n",
" │ │ 'current_meds': [ │ │\n",
" │ │ {'medication': 'OTC STEROID CREAM', 'response': 'moderate'} │ │\n",
" │ │ ], │ │\n",
" │ │ 'miscellaneous': 'patient also suffers from diabetes' │ │\n",
" │ │ } │ │\n",
" │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
" ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
"\n"
],
"text/plain": [
"Logs\n",
"└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
" │ \u001b[48;2;231;223;235m╭─\u001b[0m\u001b[48;2;231;223;235m──────────────────────────────────────────────\u001b[0m\u001b[48;2;231;223;235m Messages \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;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\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235m┃\u001b[0m\u001b[1;48;2;231;223;235m \u001b[0m\u001b[1;48;2;231;223;235mRole\u001b[0m\u001b[1;48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235m┃\u001b[0m\u001b[1;48;2;231;223;235m \u001b[0m\u001b[1;48;2;231;223;235mContent \u001b[0m\u001b[1;48;2;231;223;235m \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\u001b[48;2;231;223;235m \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\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235m│\u001b[0m\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235muser\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235mGiven the following doctor's notes about a patient, please extract a dictionary that \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\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;235m│\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\u001b[48;2;231;223;235mcontains the patient's information. \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\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;235m│\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235m152 y/o female with chronic macular rash to face and hair, worse in beard, eyebrows and \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\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;235m│\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\u001b[48;2;231;223;235mnares. \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\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;235m│\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\u001b[48;2;231;223;235mThe rash is itchy, flaky and slightly scaly. Moderate response to OTC steroid cream. Patient\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\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;235m│\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\u001b[48;2;231;223;235mhas been using cream for 2 weeks and also suffers from diabetes. \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\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;235m│\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235mGiven below is XML that describes the information to extract from this document and the tags\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\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;235m│\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\u001b[48;2;231;223;235mto extract it into. \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\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;235m│\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235mONLY return a valid JSON object (no other text is necessary), where the key of the field in \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\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;235m│\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\u001b[48;2;231;223;235mJSON is the `name` attribute of the corresponding XML, and the value is of the type \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\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;235m│\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\u001b[48;2;231;223;235mspecified by the corresponding XML's tag. The JSON MUST conform to the XML format, including\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\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;235m│\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\u001b[48;2;231;223;235many types and format requests e.g. requests for lists, objects and specific types. Be \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\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;235m│\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\u001b[48;2;231;223;235mcorrect and concise. \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\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;235m│\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235mHere are examples of simple (XML, JSON) pairs that show the expected behavior: \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\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;235m│\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\u001b[48;2;231;223;235m- `ValidationOutcome(\n", " call_id='14411405568',\n", " raw_llm_output='{\\n \"gender\": \"female\",\\n \"age\": 152,\\n \"symptoms\": [\\n {\\n \n", "\"symptom\": \"chronic macular rash\",\\n \"affected_area\": \"face\"\\n },\\n {\\n \n", "\"symptom\": \"itchy\",\\n \"affected_area\": \"beard\"\\n },\\n {\\n \"symptom\": \"flaky\",\\n\n", "\"affected_area\": \"eyebrows\"\\n },\\n {\\n \"symptom\": \"slightly scaly\",\\n \n", "\"affected_area\": \"nares\"\\n }\\n ],\\n \"current_meds\": [\\n {\\n \"medication\": \"OTC \n", "steroid cream\",\\n \"response\": \"moderate\"\\n }\\n ],\\n \"miscellaneous\": \"patient also suffers \n", "from diabetes\"\\n}',\n", " validation_summaries=[],\n", " validated_output={\n", " 'gender': 'female',\n", " 'age': 100,\n", " 'symptoms': [\n", " {'symptom': 'chronic macular rash', 'affected_area': 'face'},\n", " {'symptom': 'itchy', 'affected_area': 'beard'},\n", " {'symptom': 'flaky', 'affected_area': 'eyebrows'},\n", " {'symptom': 'slightly scaly', 'affected_area': 'nares'}\n", " ],\n", " 'current_meds': [{'medication': 'OTC STEROID CREAM', 'response': 'moderate'}],\n", " 'miscellaneous': 'patient also suffers from diabetes'\n", " },\n", " reask=None,\n", " validation_passed=True,\n", " error=None\n", ")\n", "\n" ], "text/plain": [ "\u001b[1;35mValidationOutcome\u001b[0m\u001b[1m(\u001b[0m\n", " \u001b[33mcall_id\u001b[0m=\u001b[32m'14411405568'\u001b[0m,\n", " \u001b[33mraw_llm_output\u001b[0m=\u001b[32m'\u001b[0m\u001b[32m{\u001b[0m\u001b[32m\\n \"gender\": \"female\",\\n \"age\": 152,\\n \"symptoms\": \u001b[0m\u001b[32m[\u001b[0m\u001b[32m\\n \u001b[0m\u001b[32m{\u001b[0m\u001b[32m\\n \u001b[0m\n", "\u001b[32m\"symptom\": \"chronic macular rash\",\\n \"affected_area\": \"face\"\\n \u001b[0m\u001b[32m}\u001b[0m\u001b[32m,\\n \u001b[0m\u001b[32m{\u001b[0m\u001b[32m\\n \u001b[0m\n", "\u001b[32m\"symptom\": \"itchy\",\\n \"affected_area\": \"beard\"\\n \u001b[0m\u001b[32m}\u001b[0m\u001b[32m,\\n \u001b[0m\u001b[32m{\u001b[0m\u001b[32m\\n \"symptom\": \"flaky\",\\n\u001b[0m\n", "\u001b[32m\"affected_area\": \"eyebrows\"\\n \u001b[0m\u001b[32m}\u001b[0m\u001b[32m,\\n \u001b[0m\u001b[32m{\u001b[0m\u001b[32m\\n \"symptom\": \"slightly scaly\",\\n \u001b[0m\n", "\u001b[32m\"affected_area\": \"nares\"\\n \u001b[0m\u001b[32m}\u001b[0m\u001b[32m\\n \u001b[0m\u001b[32m]\u001b[0m\u001b[32m,\\n \"current_meds\": \u001b[0m\u001b[32m[\u001b[0m\u001b[32m\\n \u001b[0m\u001b[32m{\u001b[0m\u001b[32m\\n \"medication\": \"OTC \u001b[0m\n", "\u001b[32msteroid cream\",\\n \"response\": \"moderate\"\\n \u001b[0m\u001b[32m}\u001b[0m\u001b[32m\\n \u001b[0m\u001b[32m]\u001b[0m\u001b[32m,\\n \"miscellaneous\": \"patient also suffers \u001b[0m\n", "\u001b[32mfrom diabetes\"\\n\u001b[0m\u001b[32m}\u001b[0m\u001b[32m'\u001b[0m,\n", " \u001b[33mvalidation_summaries\u001b[0m=\u001b[1m[\u001b[0m\u001b[1m]\u001b[0m,\n", " \u001b[33mvalidated_output\u001b[0m=\u001b[1m{\u001b[0m\n", " \u001b[32m'gender'\u001b[0m: \u001b[32m'female'\u001b[0m,\n", " \u001b[32m'age'\u001b[0m: \u001b[1;36m100\u001b[0m,\n", " \u001b[32m'symptoms'\u001b[0m: \u001b[1m[\u001b[0m\n", " \u001b[1m{\u001b[0m\u001b[32m'symptom'\u001b[0m: \u001b[32m'chronic macular rash'\u001b[0m, \u001b[32m'affected_area'\u001b[0m: \u001b[32m'face'\u001b[0m\u001b[1m}\u001b[0m,\n", " \u001b[1m{\u001b[0m\u001b[32m'symptom'\u001b[0m: \u001b[32m'itchy'\u001b[0m, \u001b[32m'affected_area'\u001b[0m: \u001b[32m'beard'\u001b[0m\u001b[1m}\u001b[0m,\n", " \u001b[1m{\u001b[0m\u001b[32m'symptom'\u001b[0m: \u001b[32m'flaky'\u001b[0m, \u001b[32m'affected_area'\u001b[0m: \u001b[32m'eyebrows'\u001b[0m\u001b[1m}\u001b[0m,\n", " \u001b[1m{\u001b[0m\u001b[32m'symptom'\u001b[0m: \u001b[32m'slightly scaly'\u001b[0m, \u001b[32m'affected_area'\u001b[0m: \u001b[32m'nares'\u001b[0m\u001b[1m}\u001b[0m\n", " \u001b[1m]\u001b[0m,\n", " \u001b[32m'current_meds'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1m{\u001b[0m\u001b[32m'medication'\u001b[0m: \u001b[32m'OTC STEROID CREAM'\u001b[0m, \u001b[32m'response'\u001b[0m: \u001b[32m'moderate'\u001b[0m\u001b[1m}\u001b[0m\u001b[1m]\u001b[0m,\n", " \u001b[32m'miscellaneous'\u001b[0m: \u001b[32m'patient also suffers from diabetes'\u001b[0m\n", " \u001b[1m}\u001b[0m,\n", " \u001b[33mreask\u001b[0m=\u001b[3;35mNone\u001b[0m,\n", " \u001b[33mvalidation_passed\u001b[0m=\u001b[3;92mTrue\u001b[0m,\n", " \u001b[33merror\u001b[0m=\u001b[3;35mNone\u001b[0m\n", "\u001b[1m)\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Wrap the litellm OpenAI API call with the `guard` object\n", "fragment_generator = guard(\n", " model=\"gpt-3.5-turbo\",\n", " messages=[{\"role\": \"user\", \"content\": prompt}],\n", " prompt_params={\"doctors_notes\": doctors_notes},\n", " max_tokens=1024,\n", " temperature=0,\n", " stream=True,\n", ")\n", "\n", "\n", "for op in fragment_generator:\n", " clear_output(wait=True)\n", " print(op)\n", " time.sleep(0.5)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Logs\n",
"└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
" │ ╭─────────────────────────────────────────────── Messages ────────────────────────────────────────────────╮ │\n",
" │ │ ┏━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ │ │\n",
" │ │ ┃ Role ┃ Content ┃ │ │\n",
" │ │ ┡━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ │\n",
" │ │ │ user │ │ │ │\n",
" │ │ │ │ Given the following doctor's notes about a patient, please extract a dictionary that │ │ │\n",
" │ │ │ │ contains the patient's information. │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ │ │ 152 y/o female with chronic macular rash to face and hair, worse in beard, eyebrows and │ │ │\n",
" │ │ │ │ nares. │ │ │\n",
" │ │ │ │ The rash is itchy, flaky and slightly scaly. Moderate response to OTC steroid cream. Patient │ │ │\n",
" │ │ │ │ has been using cream for 2 weeks and also suffers from diabetes. │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ │ │ Given below is XML that describes the information to extract from this document and the tags │ │ │\n",
" │ │ │ │ to extract it into. │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ │ │ <output> │ │ │\n",
" │ │ │ │ <string description=\"Patient's gender\" name=\"gender\" required=\"true\"></string> │ │ │\n",
" │ │ │ │ <integer description=\"Patient's age\" format=\"guardrails/valid_range: 0 100\" name=\"age\" │ │ │\n",
" │ │ │ │ required=\"true\"></integer> │ │ │\n",
" │ │ │ │ <list description=\"Symptoms that the patient is currently experiencing. Each symptom │ │ │\n",
" │ │ │ │ should be classified into separate item in the list.\" name=\"symptoms\" required=\"true\"> │ │ │\n",
" │ │ │ │ <object required=\"true\"> │ │ │\n",
" │ │ │ │ <string description=\"Symptom that a patient is experiencing\" name=\"symptom\" │ │ │\n",
" │ │ │ │ required=\"true\"></string> │ │ │\n",
" │ │ │ │ <string description=\"What part of the body the symptom is affecting\" │ │ │\n",
" │ │ │ │ format=\"guardrails/lowercase\" name=\"affected_area\" required=\"true\"></string> │ │ │\n",
" │ │ │ │ </object> │ │ │\n",
" │ │ │ │ </list> │ │ │\n",
" │ │ │ │ <list description=\"Medications the patient is currently taking and their response\" │ │ │\n",
" │ │ │ │ name=\"current_meds\" required=\"true\"> │ │ │\n",
" │ │ │ │ <object required=\"true\"> │ │ │\n",
" │ │ │ │ <string description=\"Name of the medication the patient is taking\" │ │ │\n",
" │ │ │ │ format=\"guardrails/uppercase\" name=\"medication\" required=\"true\"></string> │ │ │\n",
" │ │ │ │ <string description=\"How the patient is responding to the medication\" name=\"response\" │ │ │\n",
" │ │ │ │ required=\"true\"></string> │ │ │\n",
" │ │ │ │ </object> │ │ │\n",
" │ │ │ │ </list> │ │ │\n",
" │ │ │ │ <string description=\"Any other information that is relevant to the patient's health; │ │ │\n",
" │ │ │ │ something that doesn't fit into the other categories.\" format=\"guardrails/lowercase; │ │ │\n",
" │ │ │ │ guardrails/one_line\" name=\"miscellaneous\" required=\"true\"></string> │ │ │\n",
" │ │ │ │ </output> │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ │ │ ONLY return a valid JSON object (no other text is necessary), where the key of the field in │ │ │\n",
" │ │ │ │ JSON is the `name` attribute of the corresponding XML, and the value is of the type │ │ │\n",
" │ │ │ │ specified by the corresponding XML's tag. The JSON MUST conform to the XML format, including │ │ │\n",
" │ │ │ │ any types and format requests e.g. requests for lists, objects and specific types. Be │ │ │\n",
" │ │ │ │ correct and concise. │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ │ │ Here are examples of simple (XML, JSON) pairs that show the expected behavior: │ │ │\n",
" │ │ │ │ - `<string name='foo' format='two-words lower-case' />` => `{'foo': 'example one'}` │ │ │\n",
" │ │ │ │ - `<list name='bar'><string format='upper-case' /></list>` => `{\"bar\": ['STRING ONE', │ │ │\n",
" │ │ │ │ 'STRING TWO', etc.]}` │ │ │\n",
" │ │ │ │ - `<object name='baz'><string name=\"foo\" format=\"capitalize two-words\" /><integer │ │ │\n",
" │ │ │ │ name=\"index\" format=\"1-indexed\" /></object>` => `{'baz': {'foo': 'Some String', 'index': │ │ │\n",
" │ │ │ │ 1}}` │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ └──────┴──────────────────────────────────────────────────────────────────────────────────────────────┘ │ │\n",
" │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
" │ ╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮ │\n",
" │ │ { │ │\n",
" │ │ \"gender\": \"female\", │ │\n",
" │ │ \"age\": 152, │ │\n",
" │ │ \"symptoms\": [ │ │\n",
" │ │ { │ │\n",
" │ │ \"symptom\": \"chronic macular rash\", │ │\n",
" │ │ \"affected_area\": \"face\" │ │\n",
" │ │ }, │ │\n",
" │ │ { │ │\n",
" │ │ \"symptom\": \"itchy\", │ │\n",
" │ │ \"affected_area\": \"beard\" │ │\n",
" │ │ }, │ │\n",
" │ │ { │ │\n",
" │ │ \"symptom\": \"flaky\", │ │\n",
" │ │ \"affected_area\": \"eyebrows\" │ │\n",
" │ │ }, │ │\n",
" │ │ { │ │\n",
" │ │ \"symptom\": \"slightly scaly\", │ │\n",
" │ │ \"affected_area\": \"nares\" │ │\n",
" │ │ } │ │\n",
" │ │ ], │ │\n",
" │ │ \"current_meds\": [ │ │\n",
" │ │ { │ │\n",
" │ │ \"medication\": \"OTC steroid cream\", │ │\n",
" │ │ \"response\": \"moderate\" │ │\n",
" │ │ } │ │\n",
" │ │ ], │ │\n",
" │ │ \"miscellaneous\": \"patient also suffers from diabetes\" │ │\n",
" │ │ } │ │\n",
" │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
" │ ╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮ │\n",
" │ │ { │ │\n",
" │ │ 'gender': 'female', │ │\n",
" │ │ 'age': 100, │ │\n",
" │ │ 'symptoms': [ │ │\n",
" │ │ {'symptom': 'chronic macular rash', 'affected_area': 'face'}, │ │\n",
" │ │ {'symptom': 'itchy', 'affected_area': 'beard'}, │ │\n",
" │ │ {'symptom': 'flaky', 'affected_area': 'eyebrows'}, │ │\n",
" │ │ {'symptom': 'slightly scaly', 'affected_area': 'nares'} │ │\n",
" │ │ ], │ │\n",
" │ │ 'current_meds': [ │ │\n",
" │ │ {'medication': 'OTC STEROID CREAM', 'response': 'moderate'} │ │\n",
" │ │ ], │ │\n",
" │ │ 'miscellaneous': 'patient also suffers from diabetes' │ │\n",
" │ │ } │ │\n",
" │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
" ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
"\n"
],
"text/plain": [
"Logs\n",
"└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
" │ \u001b[48;2;231;223;235m╭─\u001b[0m\u001b[48;2;231;223;235m──────────────────────────────────────────────\u001b[0m\u001b[48;2;231;223;235m Messages \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;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\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235m┃\u001b[0m\u001b[1;48;2;231;223;235m \u001b[0m\u001b[1;48;2;231;223;235mRole\u001b[0m\u001b[1;48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235m┃\u001b[0m\u001b[1;48;2;231;223;235m \u001b[0m\u001b[1;48;2;231;223;235mContent \u001b[0m\u001b[1;48;2;231;223;235m \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\u001b[48;2;231;223;235m \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\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235m│\u001b[0m\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235muser\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235mGiven the following doctor's notes about a patient, please extract a dictionary that \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\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;235m│\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\u001b[48;2;231;223;235mcontains the patient's information. \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\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;235m│\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235m152 y/o female with chronic macular rash to face and hair, worse in beard, eyebrows and \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\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;235m│\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\u001b[48;2;231;223;235mnares. \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\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;235m│\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\u001b[48;2;231;223;235mThe rash is itchy, flaky and slightly scaly. Moderate response to OTC steroid cream. Patient\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\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;235m│\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\u001b[48;2;231;223;235mhas been using cream for 2 weeks and also suffers from diabetes. \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\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;235m│\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235mGiven below is XML that describes the information to extract from this document and the tags\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\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;235m│\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\u001b[48;2;231;223;235mto extract it into. \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\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;235m│\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235mONLY return a valid JSON object (no other text is necessary), where the key of the field in \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\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;235m│\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\u001b[48;2;231;223;235mJSON is the `name` attribute of the corresponding XML, and the value is of the type \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\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;235m│\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\u001b[48;2;231;223;235mspecified by the corresponding XML's tag. The JSON MUST conform to the XML format, including\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\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;235m│\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\u001b[48;2;231;223;235many types and format requests e.g. requests for lists, objects and specific types. Be \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\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;235m│\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\u001b[48;2;231;223;235mcorrect and concise. \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\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;235m│\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235mHere are examples of simple (XML, JSON) pairs that show the expected behavior: \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\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;235m│\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\u001b[48;2;231;223;235m- `Raw output:\n",
"Large language models are advanced artificial intelligence systems that can generate human-like text. \n",
"These models are trained on vast amounts of data to understand and mimic natural language patterns. \n",
"They have the ability to generate coherent and contextually relevant responses to prompts or questions\n",
"\n"
],
"text/plain": [
"Raw output:\n",
"Large language models are advanced artificial intelligence systems that can generate human-like text. \n",
"These models are trained on vast amounts of data to understand and mimic natural language patterns. \n",
"They have the ability to generate coherent and contextually relevant responses to prompts or questions\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"Validated output:\n",
"LARGE LANGUAGE MODELS ARE ADVANCED ARTIFICIAL INTELLIGENCE SYSTEMS THAT CAN GENERATE HUMAN-LIKE TEXT. \n",
"\n"
],
"text/plain": [
"Validated output:\n",
"LARGE LANGUAGE MODELS ARE ADVANCED ARTIFICIAL INTELLIGENCE SYSTEMS THAT CAN GENERATE HUMAN-LIKE TEXT. \n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Wrap the litellm OpenAI API call with the `guard` object\n",
"raw, validated, *rest = guard(\n",
" model=\"gpt-3.5-turbo\",\n",
" max_tokens=50,\n",
" temperature=0.1,\n",
")\n",
"\n",
"# Print the raw and validated outputs\n",
"print(f\"Raw output:\\n{raw}\")\n",
"print(f\"Validated output:\\n{validated}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Example 2: With streaming\n",
"\n",
"Set the `stream` parameter to `True`\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"ValidationOutcome(\n", " call_id='14491466672',\n", " raw_llm_output=' or',\n", " validation_summaries=[],\n", " validated_output=' HEY HAVE THE ABILITY TO GENERATE COHERENT AND CONTEXTUALLY RELEVANT RESPONSES TO PROMPTS \n", "OR',\n", " reask=None,\n", " validation_passed=False,\n", " error=None\n", ")\n", "\n" ], "text/plain": [ "\u001b[1;35mValidationOutcome\u001b[0m\u001b[1m(\u001b[0m\n", " \u001b[33mcall_id\u001b[0m=\u001b[32m'14491466672'\u001b[0m,\n", " \u001b[33mraw_llm_output\u001b[0m=\u001b[32m' or'\u001b[0m,\n", " \u001b[33mvalidation_summaries\u001b[0m=\u001b[1m[\u001b[0m\u001b[1m]\u001b[0m,\n", " \u001b[33mvalidated_output\u001b[0m=\u001b[32m' HEY HAVE THE ABILITY TO GENERATE COHERENT AND CONTEXTUALLY RELEVANT RESPONSES TO PROMPTS \u001b[0m\n", "\u001b[32mOR'\u001b[0m,\n", " \u001b[33mreask\u001b[0m=\u001b[3;35mNone\u001b[0m,\n", " \u001b[33mvalidation_passed\u001b[0m=\u001b[3;91mFalse\u001b[0m,\n", " \u001b[33merror\u001b[0m=\u001b[3;35mNone\u001b[0m\n", "\u001b[1m)\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Wrap the litellm OpenAI API call with the `guard` object\n", "fragment_generator = guard(\n", " model=\"gpt-3.5-turbo\",\n", " max_tokens=50,\n", " temperature=0.1,\n", " stream=True,\n", ")\n", "\n", "for op in fragment_generator:\n", " clear_output(wait=True)\n", " print(op)\n", " time.sleep(0.1)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Logs\n",
"└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
" │ ╭─────────────────────────────────────────────── Messages ────────────────────────────────────────────────╮ │\n",
" │ │ ┏━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ │ │\n",
" │ │ ┃ Role ┃ Content ┃ │ │\n",
" │ │ ┡━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ │\n",
" │ │ │ user │ │ │ │\n",
" │ │ │ │ Generate a short description of large language models. Each new sentence should be on │ │ │\n",
" │ │ │ │ another line. │ │ │\n",
" │ │ │ │ │ │ │\n",
" │ │ └──────┴──────────────────────────────────────────────────────────────────────────────────────────────┘ │ │\n",
" │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
" │ ╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮ │\n",
" │ │ Large language models are advanced artificial intelligence systems that can generate human-like text. │ │\n",
" │ │ These models are trained on vast amounts of text data to understand and mimic human language patterns. │ │\n",
" │ │ They have the ability to generate coherent and contextually relevant responses to prompts or │ │\n",
" │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
" │ ╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮ │\n",
" │ │ 'LARGE LANGUAGE MODELS ARE ADVANCED ARTIFICIAL INTELLIGENCE SYSTEMS THAT CAN GENERATE HUMAN-LIKE TEXT. │ │\n",
" │ │ HEY HAVE THE ABILITY TO GENERATE COHERENT AND CONTEXTUALLY RELEVANT RESPONSES TO PROMPTS OR' │ │\n",
" │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
" ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
"\n"
],
"text/plain": [
"Logs\n",
"└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮\n",
" │ \u001b[48;2;231;223;235m╭─\u001b[0m\u001b[48;2;231;223;235m──────────────────────────────────────────────\u001b[0m\u001b[48;2;231;223;235m Messages \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;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\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235m┃\u001b[0m\u001b[1;48;2;231;223;235m \u001b[0m\u001b[1;48;2;231;223;235mRole\u001b[0m\u001b[1;48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235m┃\u001b[0m\u001b[1;48;2;231;223;235m \u001b[0m\u001b[1;48;2;231;223;235mContent \u001b[0m\u001b[1;48;2;231;223;235m \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\u001b[48;2;231;223;235m \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\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235m│\u001b[0m\u001b[48;2;231;223;235m \u001b[0m\u001b[48;2;231;223;235muser\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\u001b[48;2;231;223;235m \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\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;235m│\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\u001b[48;2;231;223;235mGenerate a short description of large language models. Each new sentence should be on \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\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;235m│\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\u001b[48;2;231;223;235manother line. \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\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;235m│\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\u001b[48;2;231;223;235m \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\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;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;220mLarge language models are advanced artificial intelligence systems that can generate human-like text. \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;220mThese models are trained on vast amounts of text data to understand and mimic human language patterns. \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;220mThey have the ability to generate coherent and contextually relevant responses to prompts or\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'LARGE LANGUAGE MODELS ARE ADVANCED ARTIFICIAL INTELLIGENCE SYSTEMS THAT CAN GENERATE HUMAN-LIKE TEXT. \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;240mHEY HAVE THE ABILITY TO GENERATE COHERENT AND CONTEXTUALLY RELEVANT RESPONSES TO PROMPTS OR'\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"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# See guard history\n",
"print(guard.history.last.tree)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see, the outputs in both examples match. The only difference is that, in the streaming example, the outputs are returned as soon as they are received and validated by Guardrails. In the non-streaming example, the outputs are returned only after the entire request has been processed by the API. In other words, when streaming is enabled, the API returns the outputs as soon as they are ready, rather than waiting for the entire request to be processed."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "litellm",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}