Views
No views yet
| Metric | Base Model | After SFT |
|---|---|---|
| JSON Validity Rate | 100% | 100% |
| Exact Match Accuracy | 39% | 100% |
| Field Coverage Rate | 80.2% | 100% |
Input:
"Please add Alice Johnson to the mailing list.
City: Austin, Age: 32, Email: alice@example.com."
Output:
{"name": "Alice Johnson", "age": 32,
"email": "alice@example.com", "city": "Austin"}1from transformers import AutoTokenizer, AutoModelForCausalLM
2from peft import PeftModel
3import torch
4
5base = AutoModelForCausalLM.from_pretrained(
6 "Qwen/Qwen2.5-7B-Instruct",
7 torch_dtype=torch.bfloat16,
8 device_map="auto"
9)
10model = PeftModel.from_pretrained(
11 base,
12 "vishaalsai29/qwen2.5-7b-json-extraction-sft"
13)
14tokenizer = AutoTokenizer.from_pretrained(
15 "vishaalsai29/qwen2.5-7b-json-extraction-sft"
16)
17
18SYSTEM = """You are a precise JSON extraction assistant.
19Given unstructured text, extract the requested fields and
20return ONLY valid JSON. No explanation, no markdown."""
21
22messages = [
23 {"role": "system", "content": SYSTEM},
24 {"role": "user", "content": "Extract name, age, email, city.\n\nText: Alice Johnson is 32, based in Austin. Email: alice@example.com"}
25]
26
27text = tokenizer.apply_chat_template(
28 messages, tokenize=False, add_generation_prompt=True
29)
30inputs = tokenizer(text, return_tensors="pt").to(model.device)
31
32with torch.no_grad():
33 out = model.generate(**inputs, max_new_tokens=128, temperature=0.1, do_sample=True)
34
35print(tokenizer.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))
36# {"name": "Alice Johnson", "age": 32, "email": "alice@example.com", "city": "Austin"}