nickoo004 commited on
Commit
a037af3
·
verified ·
1 Parent(s): 9594aaa

Add model card

Browse files
Files changed (1) hide show
  1. README.md +219 -0
README.md ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ base_model: Qwen/Qwen2.5-1.5B-Instruct
4
+ language:
5
+ - en
6
+ - uz
7
+ - ru
8
+ - kk
9
+ - kaa
10
+ tags:
11
+ - queryshield
12
+ - prompt-optimization
13
+ - multilingual
14
+ - instruction-tuning
15
+ - lora
16
+ - qlora
17
+ - qwen2.5
18
+ - uzbek
19
+ - karakalpak
20
+ - kazakh
21
+ - central-asia
22
+ - fine-tuned
23
+ pipeline_tag: text-generation
24
+ datasets:
25
+ - nickoo004/queryshield-multilingual
26
+ ---
27
+
28
+ # QueryShield — Multilingual Prompt Optimizer
29
+
30
+ **QueryShield-1.5B** is a fine-tuned version of [Qwen2.5-1.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct) trained to rewrite raw, messy user queries into detailed, structured instruction prompts for downstream LLMs — across 5 languages and 30 professional domains.
31
+
32
+ > Given a raw user question → outputs an expert-level optimized prompt telling a downstream LLM *how* to answer it.
33
+
34
+ ---
35
+
36
+ ## What it does
37
+
38
+ Most LLMs perform significantly better when given structured, detailed prompts rather than raw user input. QueryShield sits **between the user and the LLM** — it takes the raw query and rewrites it into a high-quality instruction prompt automatically.
39
+
40
+ ```
41
+ User: "menga diabetni boshqarish uchun ovqat rejimi ayting"
42
+ ↓ QueryShield
43
+ Optimized: "As a Medical Expert, the user is asking in Uzbek about dietary
44
+ management for diabetes with high blood sugar. Provide a structured
45
+ 3-tier response covering: diabetes basics, dietary assessment, and
46
+ an actionable meal plan. Respond entirely in Uzbek. Avoid jargon..."
47
+ ↓ Downstream LLM
48
+ Final answer in Uzbek ✅
49
+ ```
50
+
51
+ ---
52
+
53
+ ## Model Details
54
+
55
+ | Property | Value |
56
+ |---|---|
57
+ | **Base model** | Qwen/Qwen2.5-1.5B-Instruct |
58
+ | **Training data** | [QueryShield Multilingual Dataset](https://huggingface.co/datasets/nickoo004/queryshield-multilingual) |
59
+ | **Training rows** | 19,530 |
60
+ | **Epochs** | 3 |
61
+ | **Train loss** | 0.88 → 0.47 |
62
+ | **Eval loss** | 0.967 |
63
+ | **GPU** | NVIDIA RTX 3090 24GB |
64
+ | **Training time** | ~3.7 hours |
65
+ | **Parameters** | 1.5B total / 147M trainable (8.7%) |
66
+
67
+ ---
68
+
69
+ ## Languages
70
+
71
+ | Language | Code | Support |
72
+ |---|---|---|
73
+ | English | `en` | ✅ Full |
74
+ | Uzbek | `uz` | ✅ Full |
75
+ | Russian | `ru` | ✅ Full |
76
+ | Kazakh | `kk` | ✅ Full |
77
+ | Karakalpak | `kaa` | ✅ Good |
78
+
79
+ **Cross-lingual** scenarios supported — user can write in one language and request output in another (e.g., Uzbek input → Russian output).
80
+
81
+ ---
82
+
83
+ ## Quick Start
84
+
85
+ ```python
86
+ from transformers import AutoTokenizer, AutoModelForCausalLM
87
+ import torch
88
+
89
+ model_id = "nickoo004/queryshield-1.5b"
90
+
91
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
92
+ model = AutoModelForCausalLM.from_pretrained(
93
+ model_id,
94
+ torch_dtype=torch.bfloat16,
95
+ device_map="auto",
96
+ trust_remote_code=True,
97
+ )
98
+
99
+ SYSTEM = (
100
+ "You are QueryShield, a multilingual prompt optimizer. "
101
+ "Given a raw user question, rewrite it into a detailed instruction "
102
+ "prompt for a downstream LLM expert. "
103
+ "User language: {in_lang}. Response language: {out_lang}. "
104
+ "Expert role: {role}."
105
+ )
106
+
107
+ def optimize_prompt(user_question, input_language, output_language, role):
108
+ messages = [
109
+ {"role": "system", "content": SYSTEM.format(
110
+ in_lang=input_language,
111
+ out_lang=output_language,
112
+ role=role,
113
+ )},
114
+ {"role": "user", "content": user_question},
115
+ ]
116
+ text = tokenizer.apply_chat_template(
117
+ messages, tokenize=False, add_generation_prompt=True
118
+ )
119
+ inputs = tokenizer(text, return_tensors="pt").to(model.device)
120
+ with torch.no_grad():
121
+ output = model.generate(
122
+ **inputs,
123
+ max_new_tokens=512,
124
+ temperature=0.7,
125
+ do_sample=True,
126
+ repetition_penalty=1.1,
127
+ pad_token_id=tokenizer.eos_token_id,
128
+ )
129
+ new_tokens = output[0][inputs["input_ids"].shape[1]:]
130
+ return tokenizer.decode(new_tokens, skip_special_tokens=True)
131
+
132
+
133
+ # Example 1 — Uzbek monolingual
134
+ result = optimize_prompt(
135
+ user_question="menga diabetni boshqarish uchun eng yaxshi ovqatlanish rejimini ayting",
136
+ input_language="Uzbek",
137
+ output_language="Uzbek",
138
+ role="Medical Expert",
139
+ )
140
+ print(result)
141
+
142
+ # Example 2 — Cross-lingual: Kazakh → Uzbek
143
+ result = optimize_prompt(
144
+ user_question="менің фермамда топырақ сапасы нашар, не істеуім керек?",
145
+ input_language="Kazakh",
146
+ output_language="Uzbek",
147
+ role="Agricultural Scientist",
148
+ )
149
+ print(result)
150
+ ```
151
+
152
+ ---
153
+
154
+ ## Supported Domains (30 total)
155
+
156
+ | Domain | Expert Role |
157
+ |---|---|
158
+ | Software Engineering | Senior Software Engineer |
159
+ | Healthcare & Medicine | Medical Expert |
160
+ | Finance & Banking | Financial Analyst |
161
+ | Legal & Law | Legal Advisor |
162
+ | Data Science & AI | Data Scientist |
163
+ | Cybersecurity | Cybersecurity Specialist |
164
+ | Aviation & Aerospace | Aerospace Engineer |
165
+ | Agriculture | Agricultural Scientist |
166
+ | Education & Teaching | Experienced Educator |
167
+ | Automotive | Automotive Engineer |
168
+ | Pharmaceuticals | Pharmaceutical Researcher |
169
+ | Manufacturing | Manufacturing Expert |
170
+ | Civil / Mechanical / Electrical Engineering | Domain Engineer |
171
+ | Business & Marketing | Business Strategist |
172
+ | Creative Writing | Professional Writer |
173
+ | … and 15 more | … |
174
+
175
+ ---
176
+
177
+ ## Training Details
178
+
179
+ ### Dataset
180
+ - **Source:** [nickoo004/queryshield-multilingual](https://huggingface.co/datasets/nickoo004/queryshield-multilingual)
181
+ - **19,530 rows** across 5 languages and 30 domains
182
+ - Generated by DeepSeek, Gemini, and Qwen2.5-14B
183
+
184
+
185
+ ### Loss Curve
186
+ ```
187
+ Epoch 1.0 → train: 1.023 | eval: 0.997
188
+ Epoch 2.5 → train: 0.731 | eval: 0.967
189
+ ```
190
+
191
+ ---
192
+
193
+ ## Limitations
194
+
195
+ - Karakalpak support is functional but may be less consistent than other languages due to limited training data for this low-resource language
196
+ - `optimized_prompt` output is always structured as an English instruction — this is by design
197
+ - Best results on domains covered in training data; novel domains may produce generic prompts
198
+ - Not suitable for harmful, illegal, or unethical query optimization
199
+
200
+ ---
201
+
202
+ ## Citation
203
+
204
+ ```bibtex
205
+ @model{queryshield_1_5b_2025,
206
+ author = {nickoo004},
207
+ title = {QueryShield-1.5B: Multilingual Prompt Optimizer},
208
+ year = {2025},
209
+ publisher = {Hugging Face},
210
+ url = {https://huggingface.co/nickoo004/queryshield-1.5b}
211
+ }
212
+ ```
213
+
214
+ ---
215
+
216
+ ## License
217
+
218
+ This model is released under the **MIT License**.
219
+ Base model license: [Qwen License](https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct/blob/main/LICENSE)