Views
No views yet
1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3from peft import PeftModel
4
5BASE_MODEL = "Qwen/Qwen2.5-1.5B-Instruct"
6LORA_MODEL = "Kamran-56/Qwen2.5-3B-PromptRefiner"
7
8tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
9
10base_model = AutoModelForCausalLM.from_pretrained(
11 BASE_MODEL,
12 torch_dtype=torch.bfloat16,
13 device_map="auto",
14 trust_remote_code=True
15)
16
17model = PeftModel.from_pretrained(base_model, LORA_MODEL)
18model.eval()
19
20def enhance_prompt(bad_prompt):
21 input_text = f"""<|im_start|>system
22You are a world-class prompt engineer with deep expertise across all domains
23including coding, writing, business, creativity, and research. Transform the
24given basic prompt into a highly specific, structured, and effective prompt.
25Every enhanced prompt MUST include a clear Role, Context, Task, Format, and
26Constraints. Return ONLY the enhanced prompt, nothing else.<|im_end|>
27<|im_start|>user
28{bad_prompt}<|im_end|>
29<|im_start|>assistant
30"""
31 inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
32 with torch.no_grad():
33 outputs = model.generate(
34 **inputs,
35 max_new_tokens=200,
36 do_sample=True,
37 temperature=0.7,
38 top_p=0.9,
39 repetition_penalty=1.2,
40 pad_token_id=tokenizer.eos_token_id
41 )
42 full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
43 return full_output.split("assistant")[-1].strip()
44
45# Example
46print(enhance_prompt("write a poem"))| Input Prompt | Quality Before | Quality After |
|---|---|---|
| "write a poem" | Vague, no structure | Role + Context + Format + Constraints added |
| "fix my code" | No context | Step-by-step debugging structure with format |
| "write an email" | Generic | Professional tone, structure, constraints defined |
1@model{qwen25_promptrefiner,
2 author = {Kamran},
3 title = {Qwen2.5-3B-PromptRefiner},
4 year = {2025},
5 publisher = {HuggingFace},
6 url = {https://huggingface.co/Kamran-56/Qwen2.5-3B-PromptRefiner}
7}