{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", "To disable this warning, you can either:\n", "\t- Avoid using `tokenizers` before the fork if possible\n", "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n" ] } ], "source": [ "! pip install ipywidgets \"transformers>=4.38.0,<5.0.0\" \"torch>=2.1.1,<3.0.0\" \"guardrails-jsonformer>=0.13.1,<1.0.0\" -q" ] }, { "cell_type": "code", "execution_count": null, "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" ] }, { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"dogs\": [\n", " {\n", " \"name\": \"Max\",\n", " \"color\": \"red\",\n", " \"weight_kg\": 20.555\n", " },\n", " {\n", " \"name\": \"Lola\",\n", " \"color\": \"blue\",\n", " \"weight_kg\": 15.234\n", " },\n", " {\n", " \"name\": \"Sarah\",\n", " \"color\": \"green\",\n", " \"weight_kg\": 10.567\n", " }\n", " ]\n", "}\n" ] } ], "source": [ "from guardrails import Guard\n", "from pydantic import BaseModel\n", "\n", "# JSONFormer is only compatible with HF Pipelines and HF Models:\n", "from transformers import pipeline\n", "\n", "\n", "class Dog(BaseModel):\n", " name: str\n", " color: str\n", " weight_kg: float\n", "\n", "\n", "class NewFriends(BaseModel):\n", " dogs: list[Dog]\n", "\n", "\n", "guard = Guard.for_pydantic(NewFriends, output_formatter=\"jsonformer\")\n", "\n", "\n", "tiny_llama_pipeline = pipeline(\"text-generation\", \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\")\n", "\n", "# Inference is straightforward:\n", "response = guard(\n", " tiny_llama_pipeline,\n", " messages=[{\"role\": \"user\", \"content\": \"Please enjoy this list of good dogs:\"}],\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# `out` is a dict. Format it as JSON for readability:\n", "import json\n", "\n", "print(json.dumps(response.validated_output, indent=2))" ] } ], "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 }