{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Regular expression constraints\n", "\n", "While you can specify any constraint you want with raw guidance functions/grammars, it is often useful to enforce very simple patterns on our `gen()` calls. To do this you can specify regular expression formats to which generated text must adhere. Internally these expressions are converted to native guidance grammars (set of guidance functions).\n", "\n", "## Example\n", "In the following example we know that we want to get number for the generated `chapter` variable, but GPT2 does not know that, and instead makes up something else.\n", "\n", "### Invalid output without a regex guide" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "gpustat is not installed, run `pip install gpustat` to collect GPU stats.\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "3aec0964796543eda8105c6019797e94", "version_major": 2, "version_minor": 0 }, "text/plain": [ "StitchWidget(initial_height='auto', initial_width='100%', srcdoc='\\n\\n
\\n …" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "