File size: 3,289 Bytes
cc15fc6
cd62380
cc15fc6
cd62380
 
 
 
 
 
 
 
 
cc15fc6
cd62380
cc15fc6
cd62380
cc15fc6
cd62380
 
 
cc15fc6
cd62380
cc15fc6
cd62380
cc15fc6
cd62380
cc15fc6
cd62380
 
 
 
 
cc15fc6
cd62380
cc15fc6
cd62380
cc15fc6
cd62380
f867b2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc15fc6
 
cd62380
 
cc15fc6
cd62380
 
 
 
 
 
 
cc15fc6
cd62380
cc15fc6
cd62380
 
cc15fc6
cd62380
 
 
 
cc15fc6
cd62380
cc15fc6
cd62380
cc15fc6
cd62380
 
 
cc15fc6
cd62380
cc15fc6
cd62380
cc15fc6
cd62380
 
 
cc15fc6
cd62380
cc15fc6
cd62380
 
cc15fc6
cd62380
cc15fc6
cd62380
cc15fc6
cd62380
 
 
 
cc15fc6
cd62380
cc15fc6
cd62380
cc15fc6
cd62380
 
 
cc15fc6
cd62380
cc15fc6
cd62380
cc15fc6
cd62380
 
cc15fc6
cd62380
cc15fc6
cd62380
cc15fc6
cd62380
 
cc15fc6
cd62380
cc15fc6
cd62380
cc15fc6
cd62380
 
 
02012e9
cd62380
02012e9
cd62380
 
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
---
base_model: unsloth/gemma-2b-it
library_name: peft
tags:
- text-to-mongodb
- LoRA
- instruction-tuning
- mongodb
- gemma
license: mit
language:
- en
---
# 🧠 Gemma 2B - MongoDB Query Generator (LoRA)

This is a LoRA fine-tuned version of `unsloth/gemma-2b-it` that converts natural language instructions into **MongoDB query strings** like:

```js
db.users.find({ "isActive": true, "age": { "$gt": 30 } })
```

The model is instruction-tuned to support a text-to-query use case for MongoDB across typical collections like `users`, `orders`, and `products`.

---

## ✨ Model Details

- **Base model**: [`unsloth/gemma-2b-it`](https://huggingface.co/unsloth/gemma-2b-it)
- **Fine-tuned with**: LoRA (4-bit quantized)
- **Framework**: [Unsloth](https://github.com/unslothai/unsloth) + PEFT
- **Dataset**: Synthetic instructions paired with MongoDB queries (300+ examples)
- **Use case**: Text-to-MongoDB query generation

---

## πŸ“¦ How to Use

```python
from unsloth import FastLanguageModel

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/gemma-2b-it",
    max_seq_length = 1024,
    dtype = torch.float16,
    load_in_4bit = True,
)

# Load LoRA adapter
model = FastLanguageModel.get_peft_model(
    model,
    r=16,
    lora_alpha=32,
    target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
    lora_dropout=0.05,
    bias="none",
)

# Load parameter
model.load_adapter("kihyun1998/gemma-2b-it-mongodb-lora", adapter_name="default")


prompt = """### Instruction:
Convert to MongoDB query string.

### Input:
Collection: users
Fields:
- name (string)
- age (int)
- isActive (boolean)
- country (string)

Question: Show all active users from Korea older than 30.

### Response:
"""

inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
output = model.generate(**inputs, max_new_tokens=100)
print(tokenizer.decode(output[0], skip_special_tokens=True))
```

---

## πŸ’‘ Example Output

```js
db.users.find({ "isActive": true, "country": "Korea", "age": { "$gt": 30 } })
```

---

## πŸ“š Intended Use

- Converting business-friendly questions into executable MongoDB queries
- Powering internal dashboards, query builders, or no-code tools
- Works best on structured fields and simple query logic

### Out-of-scope:

- Complex joins or aggregation pipelines
- Nested or dynamic schema reasoning

---

## πŸ“Š Training Details

- LoRA rank: 16
- Epochs: 3
- Dataset: 300+ synthetic natural language β†’ MongoDB query pairs
- Training hardware: Google Colab (T4 GPU)

---

## 🚧 Limitations

- Model assumes collection and fields are already known (RAG context required)
- May hallucinate field names not present in context
- Limited handling of advanced MongoDB features like `$lookup`, `$aggregate`

---

## 🧾 License

The base model is under [Gemma license](https://ai.google.dev/gemma#license).  
This LoRA adapter inherits the same conditions.

---

## πŸ§‘β€πŸ’» Author

- 🐱 [@kihyun1998](https://huggingface.co/kihyun1998)
- πŸ’¬ Questions? Open an issue or contact via Hugging Face.

---

## 🏁 Citation

```bibtex
@misc{kihyun2025mongodb,
  title={Gemma 2B MongoDB Query Generator (LoRA)},
  author={Kihyun Park},
  year={2025},
  howpublished={\\url{https://huggingface.co/kihyun1998/gemma-2b-it-mongodb-lora}}
}
```