First commit
Browse files
Litelines_Guaranteed_Valid_JSON.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Litelines_Guaranteed_Valid_Tool_Call.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Litelines_Multiple_Choice.ipynb
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nbformat": 4,
|
| 3 |
+
"nbformat_minor": 0,
|
| 4 |
+
"metadata": {
|
| 5 |
+
"colab": {
|
| 6 |
+
"provenance": [],
|
| 7 |
+
"gpuType": "T4"
|
| 8 |
+
},
|
| 9 |
+
"kernelspec": {
|
| 10 |
+
"name": "python3",
|
| 11 |
+
"display_name": "Python 3"
|
| 12 |
+
},
|
| 13 |
+
"language_info": {
|
| 14 |
+
"name": "python"
|
| 15 |
+
},
|
| 16 |
+
"accelerator": "GPU"
|
| 17 |
+
},
|
| 18 |
+
"cells": [
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "markdown",
|
| 21 |
+
"source": [
|
| 22 |
+
"# Multiple-choice question"
|
| 23 |
+
],
|
| 24 |
+
"metadata": {
|
| 25 |
+
"id": "bjbqHUTuNFyd"
|
| 26 |
+
}
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"cell_type": "markdown",
|
| 30 |
+
"source": [
|
| 31 |
+
"# 1. Install Litelines"
|
| 32 |
+
],
|
| 33 |
+
"metadata": {
|
| 34 |
+
"id": "B8jIr1KAKQWF"
|
| 35 |
+
}
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"cell_type": "code",
|
| 39 |
+
"execution_count": 1,
|
| 40 |
+
"metadata": {
|
| 41 |
+
"id": "cvgtiC0Kcl-z"
|
| 42 |
+
},
|
| 43 |
+
"outputs": [],
|
| 44 |
+
"source": [
|
| 45 |
+
"%pip install --quiet --upgrade litelines"
|
| 46 |
+
]
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"cell_type": "markdown",
|
| 50 |
+
"source": [
|
| 51 |
+
"## 2. Download a model and its tokenizer"
|
| 52 |
+
],
|
| 53 |
+
"metadata": {
|
| 54 |
+
"id": "-dLJS16ZNy97"
|
| 55 |
+
}
|
| 56 |
+
},
|
| 57 |
+
{
|
| 58 |
+
"cell_type": "code",
|
| 59 |
+
"source": [
|
| 60 |
+
"# Use cuda for faster inference\n",
|
| 61 |
+
"import torch\n",
|
| 62 |
+
"\n",
|
| 63 |
+
"device = torch.device(\"cuda\") if torch.cuda.is_available() else torch.device(\"cpu\")\n",
|
| 64 |
+
"assert device == torch.device(\"cuda\"), \"In the Runtime tab, please Change runtime type to GPU\""
|
| 65 |
+
],
|
| 66 |
+
"metadata": {
|
| 67 |
+
"id": "68zKdmOQcqLo"
|
| 68 |
+
},
|
| 69 |
+
"execution_count": 2,
|
| 70 |
+
"outputs": []
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"cell_type": "code",
|
| 74 |
+
"source": [
|
| 75 |
+
"from transformers import AutoModelForCausalLM, AutoTokenizer\n",
|
| 76 |
+
"\n",
|
| 77 |
+
"MODEL_ID = \"Qwen/Qwen2.5-0.5B-Instruct\"\n",
|
| 78 |
+
"tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)\n",
|
| 79 |
+
"model = AutoModelForCausalLM.from_pretrained(MODEL_ID).to(device)"
|
| 80 |
+
],
|
| 81 |
+
"metadata": {
|
| 82 |
+
"id": "U-a-w5jwc6Xg"
|
| 83 |
+
},
|
| 84 |
+
"execution_count": 3,
|
| 85 |
+
"outputs": []
|
| 86 |
+
},
|
| 87 |
+
{
|
| 88 |
+
"cell_type": "markdown",
|
| 89 |
+
"source": [
|
| 90 |
+
"## 3. Prepare the inputs to the LLM"
|
| 91 |
+
],
|
| 92 |
+
"metadata": {
|
| 93 |
+
"id": "ohUnAx9NJ7AV"
|
| 94 |
+
}
|
| 95 |
+
},
|
| 96 |
+
{
|
| 97 |
+
"cell_type": "code",
|
| 98 |
+
"source": [
|
| 99 |
+
"user_input = \"\"\"Which key combination exits Vim?\n",
|
| 100 |
+
"\n",
|
| 101 |
+
"A) Ctrl+X\n",
|
| 102 |
+
"B) Esc then :q!\n",
|
| 103 |
+
"C) Alt+F4\n",
|
| 104 |
+
"\"\"\"\n",
|
| 105 |
+
"messages = [{\"role\": \"user\", \"content\": user_input}]\n",
|
| 106 |
+
"inputs = tokenizer.apply_chat_template(\n",
|
| 107 |
+
" messages,\n",
|
| 108 |
+
" add_generation_prompt=True,\n",
|
| 109 |
+
" return_tensors=\"pt\",\n",
|
| 110 |
+
" return_dict=True\n",
|
| 111 |
+
").to(model.device)"
|
| 112 |
+
],
|
| 113 |
+
"metadata": {
|
| 114 |
+
"id": "Y8RtiZ7UefUx"
|
| 115 |
+
},
|
| 116 |
+
"execution_count": 4,
|
| 117 |
+
"outputs": []
|
| 118 |
+
},
|
| 119 |
+
{
|
| 120 |
+
"cell_type": "markdown",
|
| 121 |
+
"source": [
|
| 122 |
+
"## 4. Define a processor through a regular expression"
|
| 123 |
+
],
|
| 124 |
+
"metadata": {
|
| 125 |
+
"id": "9LaJrh0PKkcn"
|
| 126 |
+
}
|
| 127 |
+
},
|
| 128 |
+
{
|
| 129 |
+
"cell_type": "code",
|
| 130 |
+
"source": [
|
| 131 |
+
"from litelines.transformers import SchemaProcessor\n",
|
| 132 |
+
"\n",
|
| 133 |
+
"processor = SchemaProcessor(response_format=r\"A\\.|B\\.|C\\.\", tokenizer=tokenizer)\n",
|
| 134 |
+
"processor.show_graph()"
|
| 135 |
+
],
|
| 136 |
+
"metadata": {
|
| 137 |
+
"colab": {
|
| 138 |
+
"base_uri": "https://localhost:8080/",
|
| 139 |
+
"height": 331
|
| 140 |
+
},
|
| 141 |
+
"id": "sVx6IzInKHoL",
|
| 142 |
+
"outputId": "6b6a373e-91cd-48ce-ed31-e198d2584dce"
|
| 143 |
+
},
|
| 144 |
+
"execution_count": 5,
|
| 145 |
+
"outputs": [
|
| 146 |
+
{
|
| 147 |
+
"output_type": "display_data",
|
| 148 |
+
"data": {
|
| 149 |
+
"text/plain": [
|
| 150 |
+
"<IPython.core.display.SVG object>"
|
| 151 |
+
],
|
| 152 |
+
"image/svg+xml": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"401pt\" height=\"232pt\" viewBox=\"0.00 0.00 401.00 232.00\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 228)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-228 397,-228 397,4 -4,4\"/>\n<text text-anchor=\"middle\" x=\"92\" y=\"-208.8\" font-family=\"Times,serif\" font-size=\"14.00\">Allowed Paths</text>\n<text text-anchor=\"middle\" x=\"92\" y=\"-193.8\" font-family=\"Times,serif\" font-size=\"14.00\">Regular expression: A\\.|B\\.|C\\.</text>\n<!-- 0 -->\n<g id=\"node1\" class=\"node\">\n<title>0</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"109\" cy=\"-83\" rx=\"18\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"109\" y=\"-79.3\" font-family=\"Times,serif\" font-size=\"14.00\">0</text>\n</g>\n<!-- 1 -->\n<g id=\"node2\" class=\"node\">\n<title>1</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"238\" cy=\"-148\" rx=\"18\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"238\" y=\"-144.3\" font-family=\"Times,serif\" font-size=\"14.00\">1</text>\n</g>\n<!-- 0->1 -->\n<g id=\"edge2\" class=\"edge\">\n<title>0->1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M118.83,-98.31C125,-107.68 134.08,-119.14 145,-126 164.59,-138.31 190.49,-143.74 209.81,-146.13\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"209.48,-149.61 219.79,-147.16 210.21,-142.65 209.48,-149.61\"/>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"145.5,-165 145.5,-186 165.5,-186 165.5,-165 145.5,-165\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"145.5,-165 145.5,-186 165.5,-186 165.5,-165 145.5,-165\"/>\n<text text-anchor=\"start\" x=\"150\" y=\"-171.8\" font-family=\"Times,serif\" font-size=\"14.00\">id</text>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"165.5,-165 165.5,-186 202.5,-186 202.5,-165 165.5,-165\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"165.5,-165 165.5,-186 202.5,-186 202.5,-165 165.5,-165\"/>\n<text text-anchor=\"start\" x=\"168.5\" y=\"-171.8\" font-family=\"Times,serif\" font-size=\"14.00\">token</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"145.5,-144 145.5,-165 165.5,-165 165.5,-144 145.5,-144\"/>\n<text text-anchor=\"start\" x=\"148.5\" y=\"-150.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#00b4d8\">34</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"165.5,-144 165.5,-165 202.5,-165 202.5,-144 165.5,-144\"/>\n<text text-anchor=\"start\" x=\"179\" y=\"-150.8\" font-family=\"Times,serif\" font-size=\"14.00\">C</text>\n</g>\n<!-- 2 -->\n<g id=\"node3\" class=\"node\">\n<title>2</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"238\" cy=\"-83\" rx=\"18\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"238\" y=\"-79.3\" font-family=\"Times,serif\" font-size=\"14.00\">2</text>\n</g>\n<!-- 0->2 -->\n<g id=\"edge3\" class=\"edge\">\n<title>0->2</title>\n<path fill=\"none\" stroke=\"black\" d=\"M127.13,-83C148.24,-83 184.4,-83 209.44,-83\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"209.74,-86.5 219.74,-83 209.74,-79.5 209.74,-86.5\"/>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"145.5,-104 145.5,-125 165.5,-125 165.5,-104 145.5,-104\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"145.5,-104 145.5,-125 165.5,-125 165.5,-104 145.5,-104\"/>\n<text text-anchor=\"start\" x=\"150\" y=\"-110.8\" font-family=\"Times,serif\" font-size=\"14.00\">id</text>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"165.5,-104 165.5,-125 202.5,-125 202.5,-104 165.5,-104\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"165.5,-104 165.5,-125 202.5,-125 202.5,-104 165.5,-104\"/>\n<text text-anchor=\"start\" x=\"168.5\" y=\"-110.8\" font-family=\"Times,serif\" font-size=\"14.00\">token</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"145.5,-83 145.5,-104 165.5,-104 165.5,-83 145.5,-83\"/>\n<text text-anchor=\"start\" x=\"148.5\" y=\"-89.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#00b4d8\">33</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"165.5,-83 165.5,-104 202.5,-104 202.5,-83 165.5,-83\"/>\n<text text-anchor=\"start\" x=\"179\" y=\"-89.8\" font-family=\"Times,serif\" font-size=\"14.00\">B</text>\n</g>\n<!-- 3 -->\n<g id=\"node4\" class=\"node\">\n<title>3</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"238\" cy=\"-18\" rx=\"18\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"238\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">3</text>\n</g>\n<!-- 0->3 -->\n<g id=\"edge4\" class=\"edge\">\n<title>0->3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M117.98,-67.39C124.03,-57.17 133.28,-44.38 145,-37 164.45,-24.75 190.36,-20.23 209.73,-18.64\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"209.96,-22.13 219.73,-18.03 209.53,-15.15 209.96,-22.13\"/>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"145.5,-58 145.5,-79 165.5,-79 165.5,-58 145.5,-58\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"145.5,-58 145.5,-79 165.5,-79 165.5,-58 145.5,-58\"/>\n<text text-anchor=\"start\" x=\"150\" y=\"-64.8\" font-family=\"Times,serif\" font-size=\"14.00\">id</text>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"165.5,-58 165.5,-79 202.5,-79 202.5,-58 165.5,-58\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"165.5,-58 165.5,-79 202.5,-79 202.5,-58 165.5,-58\"/>\n<text text-anchor=\"start\" x=\"168.5\" y=\"-64.8\" font-family=\"Times,serif\" font-size=\"14.00\">token</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"145.5,-37 145.5,-58 165.5,-58 165.5,-37 145.5,-37\"/>\n<text text-anchor=\"start\" x=\"148.5\" y=\"-43.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#00b4d8\">32</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"165.5,-37 165.5,-58 202.5,-58 202.5,-37 165.5,-37\"/>\n<text text-anchor=\"start\" x=\"179\" y=\"-43.8\" font-family=\"Times,serif\" font-size=\"14.00\">A</text>\n</g>\n<!-- 4 -->\n<g id=\"node5\" class=\"node\">\n<title>4</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"371\" cy=\"-83\" rx=\"18\" ry=\"18\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"371\" cy=\"-83\" rx=\"22\" ry=\"22\"/>\n<text text-anchor=\"middle\" x=\"371\" y=\"-79.3\" font-family=\"Times,serif\" font-size=\"14.00\">4</text>\n</g>\n<!-- 1->4 -->\n<g id=\"edge5\" class=\"edge\">\n<title>1->4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M256.07,-146.94C275.44,-145.08 307.4,-139.97 331,-126 338.44,-121.6 345.27,-115.32 351.07,-108.91\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"354.08,-110.77 357.83,-100.86 348.72,-106.27 354.08,-110.77\"/>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"274.5,-165 274.5,-186 294.5,-186 294.5,-165 274.5,-165\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"274.5,-165 274.5,-186 294.5,-186 294.5,-165 274.5,-165\"/>\n<text text-anchor=\"start\" x=\"279\" y=\"-171.8\" font-family=\"Times,serif\" font-size=\"14.00\">id</text>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"294.5,-165 294.5,-186 331.5,-186 331.5,-165 294.5,-165\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"294.5,-165 294.5,-186 331.5,-186 331.5,-165 294.5,-165\"/>\n<text text-anchor=\"start\" x=\"297.5\" y=\"-171.8\" font-family=\"Times,serif\" font-size=\"14.00\">token</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"274.5,-144 274.5,-165 294.5,-165 294.5,-144 274.5,-144\"/>\n<text text-anchor=\"start\" x=\"277.5\" y=\"-150.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#00b4d8\">13</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"294.5,-144 294.5,-165 331.5,-165 331.5,-144 294.5,-144\"/>\n<text text-anchor=\"start\" x=\"311\" y=\"-150.8\" font-family=\"Times,serif\" font-size=\"14.00\">.</text>\n</g>\n<!-- 2->4 -->\n<g id=\"edge6\" class=\"edge\">\n<title>2->4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M256.13,-83C277.09,-83 313.01,-83 338.9,-83\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"338.97,-86.5 348.97,-83 338.97,-79.5 338.97,-86.5\"/>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"274.5,-104 274.5,-125 294.5,-125 294.5,-104 274.5,-104\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"274.5,-104 274.5,-125 294.5,-125 294.5,-104 274.5,-104\"/>\n<text text-anchor=\"start\" x=\"279\" y=\"-110.8\" font-family=\"Times,serif\" font-size=\"14.00\">id</text>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"294.5,-104 294.5,-125 331.5,-125 331.5,-104 294.5,-104\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"294.5,-104 294.5,-125 331.5,-125 331.5,-104 294.5,-104\"/>\n<text text-anchor=\"start\" x=\"297.5\" y=\"-110.8\" font-family=\"Times,serif\" font-size=\"14.00\">token</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"274.5,-83 274.5,-104 294.5,-104 294.5,-83 274.5,-83\"/>\n<text text-anchor=\"start\" x=\"277.5\" y=\"-89.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#00b4d8\">13</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"294.5,-83 294.5,-104 331.5,-104 331.5,-83 294.5,-83\"/>\n<text text-anchor=\"start\" x=\"311\" y=\"-89.8\" font-family=\"Times,serif\" font-size=\"14.00\">.</text>\n</g>\n<!-- 3->4 -->\n<g id=\"edge7\" class=\"edge\">\n<title>3->4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M256.13,-18.25C275.56,-19.25 307.58,-23.1 331,-37 339.15,-41.84 346.42,-49.01 352.4,-56.26\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"349.97,-58.84 358.82,-64.67 355.54,-54.6 349.97,-58.84\"/>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"274.5,-58 274.5,-79 294.5,-79 294.5,-58 274.5,-58\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"274.5,-58 274.5,-79 294.5,-79 294.5,-58 274.5,-58\"/>\n<text text-anchor=\"start\" x=\"279\" y=\"-64.8\" font-family=\"Times,serif\" font-size=\"14.00\">id</text>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"294.5,-58 294.5,-79 331.5,-79 331.5,-58 294.5,-58\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"294.5,-58 294.5,-79 331.5,-79 331.5,-58 294.5,-58\"/>\n<text text-anchor=\"start\" x=\"297.5\" y=\"-64.8\" font-family=\"Times,serif\" font-size=\"14.00\">token</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"274.5,-37 274.5,-58 294.5,-58 294.5,-37 274.5,-37\"/>\n<text text-anchor=\"start\" x=\"277.5\" y=\"-43.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#00b4d8\">13</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"294.5,-37 294.5,-58 331.5,-58 331.5,-37 294.5,-37\"/>\n<text text-anchor=\"start\" x=\"311\" y=\"-43.8\" font-family=\"Times,serif\" font-size=\"14.00\">.</text>\n</g>\n<g id=\"node6\" class=\"node\">\n<title/>\n</g>\n<!-- ->0 -->\n<g id=\"edge1\" class=\"edge\">\n<title>->0</title>\n<path fill=\"none\" stroke=\"black\" d=\"M54.19,-83C62.65,-83 72.05,-83 80.6,-83\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"80.83,-86.5 90.83,-83 80.83,-79.5 80.83,-86.5\"/>\n</g>\n</g>\n</svg>"
|
| 153 |
+
},
|
| 154 |
+
"metadata": {}
|
| 155 |
+
}
|
| 156 |
+
]
|
| 157 |
+
},
|
| 158 |
+
{
|
| 159 |
+
"cell_type": "markdown",
|
| 160 |
+
"source": [
|
| 161 |
+
"## 5. Generate a structured response"
|
| 162 |
+
],
|
| 163 |
+
"metadata": {
|
| 164 |
+
"id": "UUkc7wXcOQlN"
|
| 165 |
+
}
|
| 166 |
+
},
|
| 167 |
+
{
|
| 168 |
+
"cell_type": "code",
|
| 169 |
+
"source": [
|
| 170 |
+
"generated = model.generate(**inputs, logits_processor=[processor], temperature=0.1)\n",
|
| 171 |
+
"print(f\"Response: {tokenizer.decode(generated[0][inputs['input_ids'].shape[-1]:-1])}\")"
|
| 172 |
+
],
|
| 173 |
+
"metadata": {
|
| 174 |
+
"colab": {
|
| 175 |
+
"base_uri": "https://localhost:8080/"
|
| 176 |
+
},
|
| 177 |
+
"id": "4oIHOm-rMdG0",
|
| 178 |
+
"outputId": "d64d1505-aa01-4498-85fe-acc0112126c5"
|
| 179 |
+
},
|
| 180 |
+
"execution_count": 6,
|
| 181 |
+
"outputs": [
|
| 182 |
+
{
|
| 183 |
+
"output_type": "stream",
|
| 184 |
+
"name": "stdout",
|
| 185 |
+
"text": [
|
| 186 |
+
"Response: B.\n"
|
| 187 |
+
]
|
| 188 |
+
}
|
| 189 |
+
]
|
| 190 |
+
},
|
| 191 |
+
{
|
| 192 |
+
"cell_type": "markdown",
|
| 193 |
+
"source": [
|
| 194 |
+
"## 6. Visualize the selected path"
|
| 195 |
+
],
|
| 196 |
+
"metadata": {
|
| 197 |
+
"id": "7ANqIJQ_M9fH"
|
| 198 |
+
}
|
| 199 |
+
},
|
| 200 |
+
{
|
| 201 |
+
"cell_type": "code",
|
| 202 |
+
"source": [
|
| 203 |
+
"processor.show_graph()"
|
| 204 |
+
],
|
| 205 |
+
"metadata": {
|
| 206 |
+
"colab": {
|
| 207 |
+
"base_uri": "https://localhost:8080/",
|
| 208 |
+
"height": 331
|
| 209 |
+
},
|
| 210 |
+
"id": "Pqo5AVexMnJ6",
|
| 211 |
+
"outputId": "471d79a3-b43a-4f12-c756-32eecfb596e4"
|
| 212 |
+
},
|
| 213 |
+
"execution_count": 7,
|
| 214 |
+
"outputs": [
|
| 215 |
+
{
|
| 216 |
+
"output_type": "display_data",
|
| 217 |
+
"data": {
|
| 218 |
+
"text/plain": [
|
| 219 |
+
"<IPython.core.display.SVG object>"
|
| 220 |
+
],
|
| 221 |
+
"image/svg+xml": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"401pt\" height=\"232pt\" viewBox=\"0.00 0.00 401.00 232.00\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 228)\">\n<title>%3</title>\n<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-228 397,-228 397,4 -4,4\"/>\n<text text-anchor=\"middle\" x=\"92\" y=\"-208.8\" font-family=\"Times,serif\" font-size=\"14.00\">Allowed Paths</text>\n<text text-anchor=\"middle\" x=\"92\" y=\"-193.8\" font-family=\"Times,serif\" font-size=\"14.00\">Regular expression: A\\.|B\\.|C\\.</text>\n<!-- 0 -->\n<g id=\"node1\" class=\"node\">\n<title>0</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"109\" cy=\"-83\" rx=\"18\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"109\" y=\"-79.3\" font-family=\"Times,serif\" font-size=\"14.00\">0</text>\n</g>\n<!-- 1 -->\n<g id=\"node2\" class=\"node\">\n<title>1</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"238\" cy=\"-148\" rx=\"18\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"238\" y=\"-144.3\" font-family=\"Times,serif\" font-size=\"14.00\">1</text>\n</g>\n<!-- 0->1 -->\n<g id=\"edge2\" class=\"edge\">\n<title>0->1</title>\n<path fill=\"none\" stroke=\"black\" d=\"M118.83,-98.31C125,-107.68 134.08,-119.14 145,-126 164.59,-138.31 190.49,-143.74 209.81,-146.13\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"209.48,-149.61 219.79,-147.16 210.21,-142.65 209.48,-149.61\"/>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"145.5,-165 145.5,-186 165.5,-186 165.5,-165 145.5,-165\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"145.5,-165 145.5,-186 165.5,-186 165.5,-165 145.5,-165\"/>\n<text text-anchor=\"start\" x=\"150\" y=\"-171.8\" font-family=\"Times,serif\" font-size=\"14.00\">id</text>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"165.5,-165 165.5,-186 202.5,-186 202.5,-165 165.5,-165\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"165.5,-165 165.5,-186 202.5,-186 202.5,-165 165.5,-165\"/>\n<text text-anchor=\"start\" x=\"168.5\" y=\"-171.8\" font-family=\"Times,serif\" font-size=\"14.00\">token</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"145.5,-144 145.5,-165 165.5,-165 165.5,-144 145.5,-144\"/>\n<text text-anchor=\"start\" x=\"148.5\" y=\"-150.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#00b4d8\">34</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"165.5,-144 165.5,-165 202.5,-165 202.5,-144 165.5,-144\"/>\n<text text-anchor=\"start\" x=\"179\" y=\"-150.8\" font-family=\"Times,serif\" font-size=\"14.00\">C</text>\n</g>\n<!-- 2 -->\n<g id=\"node3\" class=\"node\">\n<title>2</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"238\" cy=\"-83\" rx=\"18\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"238\" y=\"-79.3\" font-family=\"Times,serif\" font-size=\"14.00\">2</text>\n</g>\n<!-- 0->2 -->\n<g id=\"edge3\" class=\"edge\">\n<title>0->2</title>\n<path fill=\"none\" stroke=\"red\" stroke-width=\"3\" d=\"M127.13,-83C148.24,-83 184.4,-83 209.44,-83\"/>\n<polygon fill=\"red\" stroke=\"red\" stroke-width=\"3\" points=\"209.74,-86.5 219.74,-83 209.74,-79.5 209.74,-86.5\"/>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"145.5,-104 145.5,-125 165.5,-125 165.5,-104 145.5,-104\"/>\n<polygon fill=\"none\" stroke=\"red\" points=\"145.5,-104 145.5,-125 165.5,-125 165.5,-104 145.5,-104\"/>\n<text text-anchor=\"start\" x=\"150\" y=\"-110.8\" font-family=\"Times,serif\" font-size=\"14.00\">id</text>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"165.5,-104 165.5,-125 202.5,-125 202.5,-104 165.5,-104\"/>\n<polygon fill=\"none\" stroke=\"red\" points=\"165.5,-104 165.5,-125 202.5,-125 202.5,-104 165.5,-104\"/>\n<text text-anchor=\"start\" x=\"168.5\" y=\"-110.8\" font-family=\"Times,serif\" font-size=\"14.00\">token</text>\n<polygon fill=\"none\" stroke=\"red\" points=\"145.5,-83 145.5,-104 165.5,-104 165.5,-83 145.5,-83\"/>\n<text text-anchor=\"start\" x=\"148.5\" y=\"-89.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#00b4d8\">33</text>\n<polygon fill=\"none\" stroke=\"red\" points=\"165.5,-83 165.5,-104 202.5,-104 202.5,-83 165.5,-83\"/>\n<text text-anchor=\"start\" x=\"179\" y=\"-89.8\" font-family=\"Times,serif\" font-size=\"14.00\">B</text>\n</g>\n<!-- 3 -->\n<g id=\"node4\" class=\"node\">\n<title>3</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"238\" cy=\"-18\" rx=\"18\" ry=\"18\"/>\n<text text-anchor=\"middle\" x=\"238\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">3</text>\n</g>\n<!-- 0->3 -->\n<g id=\"edge4\" class=\"edge\">\n<title>0->3</title>\n<path fill=\"none\" stroke=\"black\" d=\"M117.98,-67.39C124.03,-57.17 133.28,-44.38 145,-37 164.45,-24.75 190.36,-20.23 209.73,-18.64\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"209.96,-22.13 219.73,-18.03 209.53,-15.15 209.96,-22.13\"/>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"145.5,-58 145.5,-79 165.5,-79 165.5,-58 145.5,-58\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"145.5,-58 145.5,-79 165.5,-79 165.5,-58 145.5,-58\"/>\n<text text-anchor=\"start\" x=\"150\" y=\"-64.8\" font-family=\"Times,serif\" font-size=\"14.00\">id</text>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"165.5,-58 165.5,-79 202.5,-79 202.5,-58 165.5,-58\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"165.5,-58 165.5,-79 202.5,-79 202.5,-58 165.5,-58\"/>\n<text text-anchor=\"start\" x=\"168.5\" y=\"-64.8\" font-family=\"Times,serif\" font-size=\"14.00\">token</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"145.5,-37 145.5,-58 165.5,-58 165.5,-37 145.5,-37\"/>\n<text text-anchor=\"start\" x=\"148.5\" y=\"-43.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#00b4d8\">32</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"165.5,-37 165.5,-58 202.5,-58 202.5,-37 165.5,-37\"/>\n<text text-anchor=\"start\" x=\"179\" y=\"-43.8\" font-family=\"Times,serif\" font-size=\"14.00\">A</text>\n</g>\n<!-- 4 -->\n<g id=\"node5\" class=\"node\">\n<title>4</title>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"371\" cy=\"-83\" rx=\"18\" ry=\"18\"/>\n<ellipse fill=\"none\" stroke=\"black\" cx=\"371\" cy=\"-83\" rx=\"22\" ry=\"22\"/>\n<text text-anchor=\"middle\" x=\"371\" y=\"-79.3\" font-family=\"Times,serif\" font-size=\"14.00\">4</text>\n</g>\n<!-- 1->4 -->\n<g id=\"edge5\" class=\"edge\">\n<title>1->4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M256.07,-146.94C275.44,-145.08 307.4,-139.97 331,-126 338.44,-121.6 345.27,-115.32 351.07,-108.91\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"354.08,-110.77 357.83,-100.86 348.72,-106.27 354.08,-110.77\"/>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"274.5,-165 274.5,-186 294.5,-186 294.5,-165 274.5,-165\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"274.5,-165 274.5,-186 294.5,-186 294.5,-165 274.5,-165\"/>\n<text text-anchor=\"start\" x=\"279\" y=\"-171.8\" font-family=\"Times,serif\" font-size=\"14.00\">id</text>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"294.5,-165 294.5,-186 331.5,-186 331.5,-165 294.5,-165\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"294.5,-165 294.5,-186 331.5,-186 331.5,-165 294.5,-165\"/>\n<text text-anchor=\"start\" x=\"297.5\" y=\"-171.8\" font-family=\"Times,serif\" font-size=\"14.00\">token</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"274.5,-144 274.5,-165 294.5,-165 294.5,-144 274.5,-144\"/>\n<text text-anchor=\"start\" x=\"277.5\" y=\"-150.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#00b4d8\">13</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"294.5,-144 294.5,-165 331.5,-165 331.5,-144 294.5,-144\"/>\n<text text-anchor=\"start\" x=\"311\" y=\"-150.8\" font-family=\"Times,serif\" font-size=\"14.00\">.</text>\n</g>\n<!-- 2->4 -->\n<g id=\"edge6\" class=\"edge\">\n<title>2->4</title>\n<path fill=\"none\" stroke=\"red\" stroke-width=\"3\" d=\"M256.13,-83C277.09,-83 313.01,-83 338.9,-83\"/>\n<polygon fill=\"red\" stroke=\"red\" stroke-width=\"3\" points=\"338.97,-86.5 348.97,-83 338.97,-79.5 338.97,-86.5\"/>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"274.5,-104 274.5,-125 294.5,-125 294.5,-104 274.5,-104\"/>\n<polygon fill=\"none\" stroke=\"red\" points=\"274.5,-104 274.5,-125 294.5,-125 294.5,-104 274.5,-104\"/>\n<text text-anchor=\"start\" x=\"279\" y=\"-110.8\" font-family=\"Times,serif\" font-size=\"14.00\">id</text>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"294.5,-104 294.5,-125 331.5,-125 331.5,-104 294.5,-104\"/>\n<polygon fill=\"none\" stroke=\"red\" points=\"294.5,-104 294.5,-125 331.5,-125 331.5,-104 294.5,-104\"/>\n<text text-anchor=\"start\" x=\"297.5\" y=\"-110.8\" font-family=\"Times,serif\" font-size=\"14.00\">token</text>\n<polygon fill=\"none\" stroke=\"red\" points=\"274.5,-83 274.5,-104 294.5,-104 294.5,-83 274.5,-83\"/>\n<text text-anchor=\"start\" x=\"277.5\" y=\"-89.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#00b4d8\">13</text>\n<polygon fill=\"none\" stroke=\"red\" points=\"294.5,-83 294.5,-104 331.5,-104 331.5,-83 294.5,-83\"/>\n<text text-anchor=\"start\" x=\"311\" y=\"-89.8\" font-family=\"Times,serif\" font-size=\"14.00\">.</text>\n</g>\n<!-- 3->4 -->\n<g id=\"edge7\" class=\"edge\">\n<title>3->4</title>\n<path fill=\"none\" stroke=\"black\" d=\"M256.13,-18.25C275.56,-19.25 307.58,-23.1 331,-37 339.15,-41.84 346.42,-49.01 352.4,-56.26\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"349.97,-58.84 358.82,-64.67 355.54,-54.6 349.97,-58.84\"/>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"274.5,-58 274.5,-79 294.5,-79 294.5,-58 274.5,-58\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"274.5,-58 274.5,-79 294.5,-79 294.5,-58 274.5,-58\"/>\n<text text-anchor=\"start\" x=\"279\" y=\"-64.8\" font-family=\"Times,serif\" font-size=\"14.00\">id</text>\n<polygon fill=\"#ffebcd\" stroke=\"transparent\" points=\"294.5,-58 294.5,-79 331.5,-79 331.5,-58 294.5,-58\"/>\n<polygon fill=\"none\" stroke=\"black\" points=\"294.5,-58 294.5,-79 331.5,-79 331.5,-58 294.5,-58\"/>\n<text text-anchor=\"start\" x=\"297.5\" y=\"-64.8\" font-family=\"Times,serif\" font-size=\"14.00\">token</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"274.5,-37 274.5,-58 294.5,-58 294.5,-37 274.5,-37\"/>\n<text text-anchor=\"start\" x=\"277.5\" y=\"-43.8\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#00b4d8\">13</text>\n<polygon fill=\"none\" stroke=\"black\" points=\"294.5,-37 294.5,-58 331.5,-58 331.5,-37 294.5,-37\"/>\n<text text-anchor=\"start\" x=\"311\" y=\"-43.8\" font-family=\"Times,serif\" font-size=\"14.00\">.</text>\n</g>\n<g id=\"node6\" class=\"node\">\n<title/>\n</g>\n<!-- ->0 -->\n<g id=\"edge1\" class=\"edge\">\n<title>->0</title>\n<path fill=\"none\" stroke=\"black\" d=\"M54.19,-83C62.65,-83 72.05,-83 80.6,-83\"/>\n<polygon fill=\"black\" stroke=\"black\" points=\"80.83,-86.5 90.83,-83 80.83,-79.5 80.83,-86.5\"/>\n</g>\n</g>\n</svg>"
|
| 222 |
+
},
|
| 223 |
+
"metadata": {}
|
| 224 |
+
}
|
| 225 |
+
]
|
| 226 |
+
}
|
| 227 |
+
]
|
| 228 |
+
}
|