{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Installing hub:\u001b[35m/\u001b[0m\u001b[35m/guardrails/\u001b[0m\u001b[95mregex_match...\u001b[0m\n", "✅Successfully installed guardrails/regex_match!\n", "\n", "\n" ] } ], "source": [ "! guardrails hub install hub://guardrails/regex_match --quiet" ] }, { "cell_type": "code", "execution_count": 2, "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": [ "
ValidationOutcome(\n", " call_id='14256398304',\n", " raw_llm_output=\"As of the latest data available, Jupiter has 95 confirmed moons. This number can change as new \n", "moons are discovered and confirmed by astronomers. Jupiter's largest moons, known as the Galilean moons, are Io, \n", "Europa, Ganymede, and Callisto.\",\n", " validation_summaries=[],\n", " validated_output=\"As of the latest data available, Jupiter has 95 confirmed moons. This number can change as \n", "new moons are discovered and confirmed by astronomers. Jupiter's largest moons, known as the Galilean moons, are \n", "Io, Europa, Ganymede, and Callisto.\",\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'14256398304'\u001b[0m,\n", " \u001b[33mraw_llm_output\u001b[0m=\u001b[32m\"As\u001b[0m\u001b[32m of the latest data available, Jupiter has 95 confirmed moons. This number can change as new \u001b[0m\n", "\u001b[32mmoons are discovered and confirmed by astronomers. Jupiter's largest moons, known as the Galilean moons, are Io, \u001b[0m\n", "\u001b[32mEuropa, Ganymede, and Callisto.\"\u001b[0m,\n", " \u001b[33mvalidation_summaries\u001b[0m=\u001b[1m[\u001b[0m\u001b[1m]\u001b[0m,\n", " \u001b[33mvalidated_output\u001b[0m=\u001b[32m\"As\u001b[0m\u001b[32m of the latest data available, Jupiter has 95 confirmed moons. This number can change as \u001b[0m\n", "\u001b[32mnew moons are discovered and confirmed by astronomers. Jupiter's largest moons, known as the Galilean moons, are \u001b[0m\n", "\u001b[32mIo, Europa, Ganymede, and Callisto.\"\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": [ "from rich import print\n", "from guardrails import Guard\n", "from guardrails.hub import RegexMatch\n", "\n", "# Add your OPENAI_API_KEY as an environment variable if it's not already set\n", "# import os\n", "# os.environ[\"OPENAI_API_KEY\"] = \"YOUR_API_KEY\"\n", "\n", "guard = Guard().use(RegexMatch(\"95\", match_type=\"search\", on_fail=\"noop\"))\n", "\n", "response = guard(\n", " model=\"gpt-4o\",\n", " messages=[\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": \"How many moons does jupiter have?\"},\n", " ],\n", ")\n", "\n", "print(response)" ] } ], "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 }