mirror of
https://github.com/wassname/prob_jsonformer.git
synced 2026-09-07 17:00:24 +08:00
5.4 KiB
5.4 KiB
In [1]:
from transformers import AutoModelForCausalLM, AutoTokenizer
print("Loading model and tokenizer...")
model_name = "databricks/dolly-v2-12b"
model = AutoModelForCausalLM.from_pretrained(model_name, use_cache=True)
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True, use_cache=True)
print("Loaded model and tokenizer")/home/ubuntu/jsonllm/.venv/lib/python3.10/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 from .autonotebook import tqdm as notebook_tqdm
Loading model and tokenizer... Loaded model and tokenizer
In [3]:
from jsonformer.format import highlight_values
from jsonformer.main import Jsonformer
weather_schema = {
"type": "object",
"properties": {
"temperature": {"type": "number"},
"humidity": {
"type": "number",
},
"wind_speed": {
"type": "object",
"properties": {
"value": {"type": "number"},
"unit": {"type": "string"},
},
},
},
}
builder = Jsonformer(
model=model,
tokenizer=tokenizer,
json_schema=weather_schema,
prompt="generate the weather",
)
print("Generating...")
output = builder()
highlight_values(output)
Generating...
{
temperature: [32m2.2225[0m,
humidity: [32m1.0[0m,
wind_speed: {
value: [32m0.0[0m,
unit: [32m"value"[0m
}
}
In [8]:
car = {
"type": "object",
"properties": {
"make": {"type": "string"},
"model": {"type": "string"},
"year": {"type": "number"},
"colors": {
"type": "array",
"items": {"type": "string"},
}
},
}
builder = Jsonformer(
model=model,
tokenizer=tokenizer,
json_schema=car,
prompt="generate an example car",
)
print("Generating...")
output = builder()
highlight_values(output)
Generating...
{
make: [32m"Ford"[0m,
model: [32m"Mustang"[0m,
year: [32m10.0[0m,
colors: [
[32m"red"[0m,
[32m"white"[0m,
[32m"blue"[0m,
[32m"black"[0m,
[32m"yellow"[0m,
[32m"orange"[0m,
[32m"green"[0m,
[32m"pink"[0m,
[32m"purple"[0m,
[32m"violet"[0m
]
}
In [9]:
test = {
"temperature": 24,
"humidity": 48.0,
"wind_speed": {
"value": 12,
"unit": "mph"
}
}
highlight_values(test)
{
temperature: [32m24[0m,
humidity: [32m48.0[0m,
wind_speed: {
value: [32m12[0m,
unit: [32m"mph"[0m
}
}
In [ ]:
In [ ]: