Views
No views yet
pip install torch transformers peft accelerate1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3from peft import PeftModel
4
5# 1. Define the System Prompt (CRITICAL)
6system_prompt = """[SEE ORIGINAL MODEL CARD]"""
7
8# 2. Load Base Model
9base_model_id = "Qwen/Qwen3-1.7B"
10model = AutoModelForCausalLM.from_pretrained(
11 base_model_id,
12 torch_dtype=torch.float16,
13 device_map="auto"
14)
15tokenizer = AutoTokenizer.from_pretrained(base_model_id)
16
17# 3. Load LoRA Adapter
18adapter_id = "dzur658/ping-device-id-LoRA-001-HF" # Replace with your repo
19model = PeftModel.from_pretrained(model, adapter_id)
20
21# 4. Prepare Input
22# ---
23# NOTE: We fake messages to load database context into the model
24
25# replace with a real device from the Ping Knowledge Base
26device_str = "OnePlus 7"
27
28fake_user_prompt = "[System Command]: Load reference for {device_str}"
29
30# replace with the accompanying update guide from the Ping Knowledge Base
31knowledge_base_doc = "[REPLACE ME]"
32
33# first assistant turn should be formatted like this
34fake_assistant = f"<think>\nTrigger: System Command received (\"Load reference for {device_str}\").\nAction: Retrieve \"{device_str} Update Guide\" from database.\nPlan: Output the full update instructions so the user has the context available immediately.\n</think>" + knowledge_base_doc
35
36# ---
37
38# question to the model regarding the device
39prompt = "Wait, so if my OnePlus 7 is no longer receiving security updates does that mean I need to upgrade immediately?"
40
41messages = [
42 {"role": "system", "content": system_prompt},
43 {"role": "user", "content": fake_user_prompt},
44 {"role": "assistant", "content": fake_assistant},
45 {"role": "user", "content": prompt}
46]
47
48# 5. Generate
49text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
50inputs = tokenizer(text, return_tensors="pt").to(model.device)
51
52outputs = model.generate(
53 **inputs,
54 max_new_tokens=8192, # longer context for fitting knowledge base doc and reasoning tokens
55 temperature=0.0, # Greedy decoding for logic
56 do_sample=False
57)
58
59print(tokenizer.decode(outputs[0], skip_special_tokens=True))