Views
No views yet
"Hi, I want a refund because my wireless earbuds are defective. Order id: ORD-39256"
1{
2 "intent": "refund",
3 "priority": "high",
4 "entities": {
5 "order_id": "ORD-39256",
6 "product": "wireless earbuds"
7 },
8 "needs_clarification": false,
9 "clarifying_question": null
10}1{
2 "intent": "shipping",
3 "priority": "medium",
4 "entities": {
5 "order_id": null,
6 "product": null
7 },
8 "needs_clarification": true,
9 "clarifying_question": "Can you share your order ID and the delivery address ZIP code so I can check the shipment status?"
10}| Field | Type | Description |
|---|---|---|
intent | string | One of: refund, cancel, shipping, exchange, complaint, inquiry |
priority | string | low, medium, or high |
entities.order_id | string | null | Extracted order ID if present |
entities.product | string | null | Extracted product name if present |
needs_clarification | boolean | Whether the model needs more info to proceed |
clarifying_question | string | null | Follow-up question if clarification is needed |
1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3from peft import PeftModel
4
5# Load base model in 4-bit
6bnb_config = BitsAndBytesConfig(
7 load_in_4bit=True,
8 bnb_4bit_quant_type="nf4",
9 bnb_4bit_compute_dtype=torch.float16,
10 bnb_4bit_use_double_quant=True,
11)
12
13base_model = AutoModelForCausalLM.from_pretrained(
14 "mistralai/Mistral-7B-Instruct-v0.3",
15 quantization_config=bnb_config,
16 device_map="auto",
17)
18
19tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3")
20tokenizer.pad_token = tokenizer.eos_token
21
22# Load LoRA adapter
23model = PeftModel.from_pretrained(base_model, "aashnakunk/mistral-7b-json-support")
24model.eval()
25
26# Build prompt
27system = """You are a support automation assistant.
28Return ONLY a single JSON object that matches this schema exactly, with these keys in this order:
291) intent
302) priority
313) entities (with keys: order_id, product)
324) needs_clarification
335) clarifying_question"""
34
35user_message = "Hi, I want a refund because my wireless earbuds are defective. Order id: ORD-39256"
36
37prompt = f"<s>[INST] {system}\n\n{user_message} [/INST]"
38
39inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
40
41with torch.no_grad():
42 outputs = model.generate(
43 **inputs,
44 max_new_tokens=256,
45 do_sample=False,
46 repetition_penalty=1.2,
47 pad_token_id=tokenizer.eos_token_id,
48 )
49
50response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
51print(response)model.config.use_cache = True for generation (it's disabled during training)model.eval() to disable dropoutdo_sample=False for deterministic JSON outputrepetition_penalty=1.2 helps prevent degenerate repetition| Parameter | Value |
|---|---|
| Base model | mistralai/Mistral-7B-Instruct-v0.3 |
| Method | QLoRA (4-bit quantization + LoRA adapters) |
| LoRA rank (r) | 16 |
| LoRA alpha | 32 |
| LoRA dropout | 0.05 |
| Target modules | q_proj, k_proj, v_proj, o_proj |
| Trainable parameters | 13.6M / 3.77B (0.36%) |
| Training examples | 6,000 |
| Epochs | 1 |
| Batch size | 1 (with gradient accumulation = 8, effective batch = 8) |
| Learning rate | 2e-4 |
| Optimizer | paged_adamw_8bit |
| Precision | fp16 mixed precision |
| Hardware | NVIDIA Tesla T4 (15 GB) |
| Training time | ~2.75 hours |
| Final training loss | 0.109 |
order_id and product fields