Views
No views yet
TinyLlama/TinyLlama-1.1B-Chat-v1.0 that has been specifically trained to act as an e-commerce intent detection model. Given a catalog of products and a user's request, it outputs a structured JSON object representing the user's intent (add or remove), the product name, and the quantity.Catalog and the User request.optimum and auto-gptq to run this 4-bit GPTQ model.pip install -q optimum auto-gptq transformers1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
4# Model repository on the Hugging Face Hub
5model_id = "jtlicardo/tinyllama-ecommerce-intent-gptq"
6
7# Load the tokenizer and the 4-bit quantized model
8tokenizer = AutoTokenizer.from_pretrained(model_id)
9model = AutoModelForCausalLM.from_pretrained(
10 model_id,
11 device_map="auto",
12 torch_dtype=torch.float16 # Recommended for inference
13)
14
15# --- Define the prompt ---
16catalog = """Catalog:
17Shampoo (400ml bottle)
18Hand Soap (250ml dispenser)
19Peanut Butter (340g jar)
20Headphones
21Green Tea (25 tea bags)"""
22
23user_query = "Could you please take off 4 pairs of headphons from my cart?"
24
25# --- Format the prompt using the model's chat template ---
26# The model was trained to see this structure.
27prompt = f"<|user|>\n{catalog}\n\nUser:\n{user_query}\n<|assistant|>\n"
28
29# --- Generate the output ---
30pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
31outputs = pipe(
32 prompt,
33 max_new_tokens=50, # Max length of the JSON output
34 do_sample=False, # Use deterministic output
35 temperature=None, # Not needed for do_sample=False
36 top_p=None, # Not needed for do_sample=False
37 return_full_text=False # Only return the generated part
38)
39
40# The output will be a clean JSON string
41generated_json = outputs[0]['generated_text'].strip()
42print(generated_json)
43# Expected output:
44# {"action": "remove", "product": "Headphones", "quantity": 4}trl library's SFTTrainer.prompt/completion pairs.completion_only_loss=True was used to ensure the model only learned to generate the assistant's JSON response.