File size: 6,877 Bytes
e37209f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
---
license: mit
base_model: Qwen/Qwen2.5-1.5B-Instruct
language:
  - en
  - uz
  - ru
  - kk
  - kaa
tags:
  - queryshield
  - prompt-optimization
  - multilingual
  - instruction-tuning
  - lora
  - qlora
  - qwen2.5
  - uzbek
  - karakalpak
  - kazakh
  - central-asia
  - fine-tuned
pipeline_tag: text-generation
datasets:
- nickoo004/queryshield-multilingual
---

# QueryShield — Multilingual Prompt Optimizer

**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.

> Given a raw user question → outputs an expert-level optimized prompt telling a downstream LLM *how* to answer it.

---

## What it does

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.

```
User: "menga diabetni boshqarish uchun ovqat rejimi ayting"
         ↓  QueryShield
Optimized: "As a Medical Expert, the user is asking in Uzbek about dietary
            management for diabetes with high blood sugar. Provide a structured
            3-tier response covering: diabetes basics, dietary assessment, and
            an actionable meal plan. Respond entirely in Uzbek. Avoid jargon..."
         ↓  Downstream LLM
Final answer in Uzbek ✅
```

---

## Model Details

| Property | Value |
|---|---|
| **Base model** | Qwen/Qwen2.5-1.5B-Instruct |
| **Training data** | [QueryShield Multilingual Dataset](https://huggingface.co/datasets/nickoo004/queryshield-multilingual) |
| **Training rows** | 19,530 |
| **Epochs** | 3 |
| **Train loss** | 0.88 → 0.47 |
| **Eval loss** | 0.967 (best checkpoint) |
| **GPU** | NVIDIA RTX 3090 24GB |
| **Training time** | ~3.7 hours |
| **Parameters** | 1.5B total / 147M trainable (8.7%) |
| **Live demo** | [▶ Kaggle Notebook](https://www.kaggle.com/code/nursultankoshekbaev/queryshield-1-5b) |

---

## Languages

| Language | Code | Support |
|---|---|---|
| English | `en` | ✅ Full |
| Uzbek | `uz` | ✅ Full |
| Russian | `ru` | ✅ Full |
| Kazakh | `kk` | ✅ Full |
| Karakalpak | `kaa` | ✅ Good |

**Cross-lingual** scenarios supported — user can write in one language and request output in another (e.g., Uzbek input → Russian output).

---

## Quick Start

```python
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

model_id = "nickoo004/queryshield-1.5b"

tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.bfloat16,
    device_map="auto",
    trust_remote_code=True,
)

SYSTEM = (
    "You are QueryShield, a multilingual prompt optimizer. "
    "Given a raw user question, rewrite it into a detailed instruction "
    "prompt for a downstream LLM expert. "
    "User language: {in_lang}. Response language: {out_lang}. "
    "Expert role: {role}."
)

def optimize_prompt(user_question, input_language, output_language, role):
    messages = [
        {"role": "system", "content": SYSTEM.format(
            in_lang=input_language,
            out_lang=output_language,
            role=role,
        )},
        {"role": "user", "content": user_question},
    ]
    text = tokenizer.apply_chat_template(
        messages, tokenize=False, add_generation_prompt=True
    )
    inputs = tokenizer(text, return_tensors="pt").to(model.device)
    with torch.no_grad():
        output = model.generate(
            **inputs,
            max_new_tokens=512,
            temperature=0.7,
            do_sample=True,
            repetition_penalty=1.1,
            pad_token_id=tokenizer.eos_token_id,
        )
    new_tokens = output[0][inputs["input_ids"].shape[1]:]
    return tokenizer.decode(new_tokens, skip_special_tokens=True)


# Example 1 — Uzbek monolingual
result = optimize_prompt(
    user_question="menga diabetni boshqarish uchun eng yaxshi ovqatlanish rejimini ayting",
    input_language="Uzbek",
    output_language="Uzbek",
    role="Medical Expert",
)
print(result)

# Example 2 — Cross-lingual: Kazakh -> Uzbek
result = optimize_prompt(
    user_question="менің фермамда топырақ сапасы нашар, не істеуім керек?",
    input_language="Kazakh",
    output_language="Uzbek",
    role="Agricultural Scientist",
)
print(result)
```

---

## Live Demo

**[▶ Run on Kaggle](https://www.kaggle.com/code/nursultankoshekbaev/queryshield-1-5b)** — no setup needed, free GPU included.

Tests all 7 cases: English, Uzbek, Russian, Kazakh, Karakalpak + 2 cross-lingual pairs.

---

## Supported Domains (30 total)

| Domain | Expert Role |
|---|---|
| Software Engineering | Senior Software Engineer |
| Healthcare & Medicine | Medical Expert |
| Finance & Banking | Financial Analyst |
| Legal & Law | Legal Advisor |
| Data Science & AI | Data Scientist |
| Cybersecurity | Cybersecurity Specialist |
| Aviation & Aerospace | Aerospace Engineer |
| Agriculture | Agricultural Scientist |
| Education & Teaching | Experienced Educator |
| Automotive | Automotive Engineer |
| Pharmaceuticals | Pharmaceutical Researcher |
| Manufacturing | Manufacturing Expert |
| Civil / Mechanical / Electrical Engineering | Domain Engineer |
| Business & Marketing | Business Strategist |
| Creative Writing | Professional Writer |
| … and 15 more | … |

---

## Training Details

### Dataset
- **Source:** [nickoo004/queryshield-multilingual](https://huggingface.co/datasets/nickoo004/queryshield-multilingual)
- **19,530 rows** across 5 languages and 30 domains
- Generated by DeepSeek, Gemini, and Qwen2.5-14B

### Loss Curve
```
Epoch 1.0  ->  train: 1.023  |  eval: 0.997
Epoch 2.5  ->  train: 0.731  |  eval: 0.967  <- best checkpoint
```

---

## Limitations

- Karakalpak support is functional but may be less consistent than other languages due to limited training data for this low-resource language
- `optimized_prompt` output is always structured as an English instruction — this is by design
- Best results on domains covered in training data; novel domains may produce generic prompts
- Not suitable for harmful, illegal, or unethical query optimization

---

## Citation

```bibtex
@model{queryshield_1_5b_2026,
  author    = {nickoo004},
  title     = {QueryShield-1.5B: Multilingual Prompt Optimizer},
  year      = {2026},
  publisher = {Hugging Face},
  url       = {https://huggingface.co/nickoo004/queryshield-1.5b}
}
```

---

## License

This model is released under the **MIT License**.
Base model license: [Qwen License](https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct/blob/main/LICENSE)