{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# autoreload your package\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/media/wassname/SGIronWolf/projects5/2024/prob_jsonformer/.venv/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Loading model and tokenizer...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/media/wassname/SGIronWolf/projects5/2024/prob_jsonformer/.venv/lib/python3.9/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n", " warnings.warn(\n", "/media/wassname/SGIronWolf/projects5/2024/prob_jsonformer/.venv/lib/python3.9/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n", " warnings.warn(\n", "Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Loaded model and tokenizer\n" ] } ], "source": [ "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "import torch\n", "\n", "print(\"Loading model and tokenizer...\")\n", "model_name = \"databricks/dolly-v2-3b\"\n", "model = AutoModelForCausalLM.from_pretrained(\n", " model_name,\n", " use_cache=True,\n", " torch_dtype=torch.float16,\n", " attn_implementation='eager',\n", ").to(\"cuda:0\")\n", "tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True, use_cache=True)\n", "print(\"Loaded model and tokenizer\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Continue" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating...\n", "{\n", " store: {\n", " name: \u001b[32m\"Mike's Ski Shop\"\u001b[0m,\n", " location: \u001b[32m\"Somewhere\"\u001b[0m,\n", " choice_probs: [\n", " {\n", " prob: \u001b[32m0.01739501953125\u001b[0m,\n", " choice: \u001b[32m\"pretend\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.002094268798828125\u001b[0m,\n", " choice: \u001b[32m\"snowboard\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.0007467269897460938\u001b[0m,\n", " choice: \u001b[32m\"walk\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.97998046875\u001b[0m,\n", " choice: \u001b[32m\"ski\"\u001b[0m\n", " }\n", " ],\n", " inventory: [\n", " {\n", " productId: \u001b[32m\"1\"\u001b[0m,\n", " name: \u001b[32m\"Snowboard X-15\"\u001b[0m,\n", " description: \u001b[32m\"Snowboard for all levels\"\u001b[0m,\n", " category: \u001b[32m\"Snowboards\"\u001b[0m,\n", " price: \u001b[32m20.0375\u001b[0m,\n", " inStock: \u001b[32mTrue\u001b[0m,\n", " rating: \u001b[32m5.0\u001b[0m,\n", " images: [\n", " \u001b[32m\"https://s3.amazonaws.com/mikesskisport/images/Snow\"\u001b[0m\n", " ]\n", " }\n", " ]\n", " }\n", "}\n" ] } ], "source": [ "from prob_jsonformer.format import highlight_values\n", "from prob_jsonformer.main import Jsonformer\n", "\n", "ecomm = {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"store\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"name\": {\"type\": \"string\"},\n", " \"location\": {\"type\": \"string\"},\n", " \"choice_probs\": {\"type\": \"choice_probs\", \"enum\": [\"ski\", \"snowboard\", \"walk\", \"pretend\"]},\n", " \"inventory\": {\n", " \"type\": \"array\",\n", " \"items\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"productId\": {\"type\": \"string\"},\n", " \"name\": {\"type\": \"string\"},\n", " \"description\": {\"type\": \"string\"},\n", " \"category\": {\"type\": \"string\"},\n", " \"price\": {\"type\": \"number\"},\n", " \"inStock\": {\"type\": \"boolean\"},\n", " \"rating\": {\"type\": \"number\"},\n", " \"images\": {\"type\": \"array\", \"items\": {\"type\": \"string\"}},\n", " },\n", " },\n", " },\n", " },\n", " }\n", " },\n", "}\n", "\n", "\n", "builder = Jsonformer(\n", " model=model,\n", " tokenizer=tokenizer,\n", " json_schema=ecomm,\n", " prompt=\"write a description about mike's ski shop which sells premium skis and snowboards\",\n", " max_string_token_length=20,\n", ")\n", "\n", "print(\"Generating...\")\n", "output = builder()\n", "\n", "highlight_values(output)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating...\n", "{\n", " make: \u001b[32m\"Mazda\"\u001b[0m,\n", " model: [\n", " {\n", " prob: \u001b[32m0.8154296875\u001b[0m,\n", " choice: \u001b[32m\"Kea\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.184814453125\u001b[0m,\n", " choice: \u001b[32m\"Mazda\"\u001b[0m\n", " }\n", " ],\n", " new: [\n", " {\n", " prob: \u001b[32m0.90185546875\u001b[0m,\n", " choice: \u001b[32m\"true\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.09808349609375\u001b[0m,\n", " choice: \u001b[32m\"false\"\u001b[0m\n", " }\n", " ],\n", " rating: [\n", " {\n", " prob: \u001b[32m0.221435546875\u001b[0m,\n", " choice: \u001b[32m\"1\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.394775390625\u001b[0m,\n", " choice: \u001b[32m\"2\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.382568359375\u001b[0m,\n", " choice: \u001b[32m\"3\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.0013370513916015625\u001b[0m,\n", " choice: \u001b[32m\"4\"\u001b[0m\n", " }\n", " ],\n", " year: \u001b[32m2016.0\u001b[0m,\n", " colors_available: [\n", " \u001b[32m\"red\"\u001b[0m\n", " ]\n", "}\n" ] } ], "source": [ "car = {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"make\": {\"type\": \"string\"},\n", " \"model\": {\"type\": \"choice_probs\", \"enum\": [\"Mazda\", \"Kea\"]},\n", " \"new\": {\"type\": \"choice_probs\", \"enum\": [\"true\", \"false\"]},\n", " \"rating\": {\"type\": \"choice_probs\", \"enum\": [\"1\", \"2\", \"3\", \"4\"]},\n", " \"year\": {\"type\": \"number\"},\n", " \"colors_available\": {\n", " \"type\": \"array\",\n", " \"items\": {\"type\": \"string\"},\n", " },\n", " },\n", "}\n", "\n", "builder = Jsonformer(\n", " model=model,\n", " tokenizer=tokenizer,\n", " json_schema=car,\n", " prompt=\"generate an example car\",\n", ")\n", "\n", "print(\"Generating...\")\n", "output = builder()\n", "\n", "highlight_values(output)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating...\n", "{\n", " car: {\n", " make: \u001b[32m\"Rolls Royce\"\u001b[0m,\n", " model: \u001b[32m\"Phantom\"\u001b[0m,\n", " year: \u001b[32m2014.0\u001b[0m,\n", " colors: [\n", " {\n", " prob: \u001b[32m0.001560211181640625\u001b[0m,\n", " choice: \u001b[32m\"white\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.833984375\u001b[0m,\n", " choice: \u001b[32m\"red\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.0865478515625\u001b[0m,\n", " choice: \u001b[32m\"black\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.048553466796875\u001b[0m,\n", " choice: \u001b[32m\"blue\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.0294342041015625\u001b[0m,\n", " choice: \u001b[32m\"green\"\u001b[0m\n", " }\n", " ],\n", " as_new: [\n", " {\n", " prob: \u001b[32m0.96533203125\u001b[0m,\n", " choice: \u001b[32m\"true\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.03460693359375\u001b[0m,\n", " choice: \u001b[32m\"false\"\u001b[0m\n", " }\n", " ],\n", " rating: [\n", " {\n", " prob: \u001b[32m0.05462646484375\u001b[0m,\n", " choice: \u001b[32m\"1\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.233642578125\u001b[0m,\n", " choice: \u001b[32m\"2\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.2093505859375\u001b[0m,\n", " choice: \u001b[32m\"3\"\u001b[0m\n", " },\n", " {\n", " prob: \u001b[32m0.50244140625\u001b[0m,\n", " choice: \u001b[32m\"4\"\u001b[0m\n", " }\n", " ],\n", " features: {\n", " audio: {\n", " brand: \u001b[32m\"Mercedes-Benz\"\u001b[0m,\n", " speakers: \u001b[32m2.09999\u001b[0m,\n", " hasBluetooth: \u001b[32mTrue\u001b[0m\n", " },\n", " safety: {\n", " airbags: \u001b[32m2.09999\u001b[0m,\n", " parkingSensors: \u001b[32mTrue\u001b[0m,\n", " laneAssist: \u001b[32mTrue\u001b[0m\n", " },\n", " performance: {\n", " engine: \u001b[32m\"Mercedes-Benz 6.2 L\"\u001b[0m,\n", " horsepower: \u001b[32m423.09999\u001b[0m,\n", " topSpeed: \u001b[32m220.09999\u001b[0m\n", " }\n", " }\n", " },\n", " owner: {\n", " firstName: \u001b[32m\"John\"\u001b[0m,\n", " lastName: \u001b[32m\"Doe\"\u001b[0m,\n", " age: \u001b[32m40.09999\u001b[0m\n", " }\n", "}\n" ] } ], "source": [ "complex_car = {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"car\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"make\": {\"type\": \"string\"},\n", " \"model\": {\"type\": \"string\"},\n", " \"year\": {\"type\": \"number\"},\n", " \"colors\": {\"type\": \"choice_probs\", \"enum\": [\"red\", \"green\", \"blue\", \"black\", \"white\"]},\n", " \"as_new\": {\"type\": \"choice_probs\", \"enum\": [\"true\", \"false\"]},\n", " \"rating\": {\"type\": \"choice_probs\", \"enum\": [\"1\", \"2\", \"3\", \"4\"]},\n", " \"features\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"audio\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"brand\": {\"type\": \"string\"},\n", " \"speakers\": {\"type\": \"number\"},\n", " \"hasBluetooth\": {\"type\": \"boolean\"},\n", " },\n", " },\n", " \"safety\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"airbags\": {\"type\": \"number\"},\n", " \"parkingSensors\": {\"type\": \"boolean\"},\n", " \"laneAssist\": {\"type\": \"boolean\"},\n", " },\n", " },\n", " \"performance\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"engine\": {\"type\": \"string\"},\n", " \"horsepower\": {\"type\": \"number\"},\n", " \"topSpeed\": {\"type\": \"number\"},\n", " },\n", " },\n", " },\n", " },\n", " },\n", " },\n", " \"owner\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"firstName\": {\"type\": \"string\"},\n", " \"lastName\": {\"type\": \"string\"},\n", " \"age\": {\"type\": \"number\"},\n", " },\n", " },\n", " },\n", "}\n", "builder = Jsonformer(\n", " model=model,\n", " tokenizer=tokenizer,\n", " json_schema=complex_car,\n", " prompt=\"generate an example Rolls Royce Phantom\",\n", ")\n", "\n", "print(\"Generating...\")\n", "output = builder()\n", "\n", "highlight_values(output)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.9.16" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }