Improve language tag
#1
by
lbourdois
- opened
README.md
CHANGED
|
@@ -1,214 +1,226 @@
|
|
| 1 |
-
---
|
| 2 |
-
base_model: Qwen/Qwen2.5-7B
|
| 3 |
-
library_name: peft
|
| 4 |
-
language:
|
| 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 |
This model builds upon the Qwen 2.5 7B model and uses techniques from the PEFT library. Special thanks to the teams behind these projects.
|
|
|
|
| 1 |
+
---
|
| 2 |
+
base_model: Qwen/Qwen2.5-7B
|
| 3 |
+
library_name: peft
|
| 4 |
+
language:
|
| 5 |
+
- zho
|
| 6 |
+
- eng
|
| 7 |
+
- fra
|
| 8 |
+
- spa
|
| 9 |
+
- por
|
| 10 |
+
- deu
|
| 11 |
+
- ita
|
| 12 |
+
- rus
|
| 13 |
+
- jpn
|
| 14 |
+
- kor
|
| 15 |
+
- vie
|
| 16 |
+
- tha
|
| 17 |
+
- ara
|
| 18 |
+
license: agpl-3.0
|
| 19 |
+
datasets:
|
| 20 |
+
- OramaSearch/nlp-to-query-small
|
| 21 |
+
---
|
| 22 |
+
|
| 23 |
+
# Query Translator Mini
|
| 24 |
+
|
| 25 |
+
This repository contains a fine-tuned version of Qwen 2.5 7B model specialized in translating natural language queries into structured Orama search queries.
|
| 26 |
+
|
| 27 |
+
The model uses PEFT with LoRA to maintain efficiency while achieving high performance.
|
| 28 |
+
|
| 29 |
+
## Model Details
|
| 30 |
+
|
| 31 |
+
### Model Description
|
| 32 |
+
|
| 33 |
+
The Query Translator Mini model is designed to convert natural language queries into structured JSON queries compatible with the Orama search engine.
|
| 34 |
+
|
| 35 |
+
It understands various data types and query operators, making it versatile for different search scenarios.
|
| 36 |
+
|
| 37 |
+
### Key Features
|
| 38 |
+
|
| 39 |
+
- Translates natural language to structured Orama queries
|
| 40 |
+
- Supports multiple field types: string, number, boolean, enum, and arrays
|
| 41 |
+
- Handles complex query operators: `gt`, `gte`, `lt`, `lte`, `eq`, `between`, `containsAll`
|
| 42 |
+
- Supports nested properties with dot notation
|
| 43 |
+
- Works with both full-text search and filtered queries
|
| 44 |
+
|
| 45 |
+
## Usage
|
| 46 |
+
|
| 47 |
+
```python
|
| 48 |
+
import json, torch
|
| 49 |
+
from peft import PeftModel
|
| 50 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 51 |
+
|
| 52 |
+
SYSTEM_PROMPT = """
|
| 53 |
+
You are a tool used to generate synthetic data of Orama queries. Orama is a full-text, vector, and hybrid search engine.
|
| 54 |
+
|
| 55 |
+
Let me show you what you need to do with some examples.
|
| 56 |
+
|
| 57 |
+
Example:
|
| 58 |
+
- Query: `"What are the red wines that cost less than 20 dollars?"`
|
| 59 |
+
- Schema: `{ "name": "string", "content": "string", "price": "number", "tags": "enum[]" }`
|
| 60 |
+
- Generated query: `{ "term": "", "where": { "tags": { "containsAll": ["red", "wine"] }, "price": { "lt": 20 } } }`
|
| 61 |
+
|
| 62 |
+
Another example:
|
| 63 |
+
- Query: `"Show me 5 prosecco wines good for aperitif"`
|
| 64 |
+
- Schema: `{ "name": "string", "content": "string", "price": "number", "tags": "enum[]" }`
|
| 65 |
+
- Generated query: `{ "term": "prosecco aperitif", "limit": 5 }`
|
| 66 |
+
|
| 67 |
+
One last example:
|
| 68 |
+
- Query: `"Show me some wine reviews with a score greater than 4.5 and less than 5.0."`
|
| 69 |
+
- Schema: `{ "title": "string", "content": "string", "reviews": { "score": "number", "text": "string" } }]`
|
| 70 |
+
- Generated query: `{ "term": "", "where": { "reviews.score": { "between": [4.5, 5.0] } } }`
|
| 71 |
+
|
| 72 |
+
The rules to generate the query are:
|
| 73 |
+
|
| 74 |
+
- Never use an "embedding" field in the schema.
|
| 75 |
+
- Every query has a "term" field that is a string. It represents the full-text search terms. Can be empty (will match all documents).
|
| 76 |
+
- You can use a "where" field that is an object. It represents the filters to apply to the documents. Its keys and values depend on the schema of the database:
|
| 77 |
+
- If the field is a "string", you should not use operators. Example: `{ "where": { "title": "champagne" } }`.
|
| 78 |
+
- If the field is a "number", you can use the following operators: "gt", "gte", "lt", "lte", "eq", "between". Example: `{ "where": { "price": { "between": [20, 100] } } }`. Another example: `{ "where": { "price": { "lt": 20 } } }`.
|
| 79 |
+
- If the field is an "enum", you can use the following operators: "eq", "in", "nin". Example: `{ "where": { "tags": { "containsAll": ["red", "wine"] } } }`.
|
| 80 |
+
- If the field is an "string[]", it's gonna be just like the "string" field, but you can use an array of values. Example: `{ "where": { "title": ["champagne", "montagne"] } }`.
|
| 81 |
+
- If the field is a "boolean", you can use the following operators: "eq". Example: `{ "where": { "isAvailable": true } }`. Another example: `{ "where": { "isAvailable": false } }`.
|
| 82 |
+
- If the field is a "enum[]", you can use the following operators: "containsAll". Example: `{ "where": { "tags": { "containsAll": ["red", "wine"] } } }`.
|
| 83 |
+
- Nested properties are supported. Just translate them into dot notation. Example: `{ "where": { "author.name": "John" } }`.
|
| 84 |
+
- Array of numbers are not supported.
|
| 85 |
+
- Array of booleans are not supported.
|
| 86 |
+
|
| 87 |
+
Return just a JSON object, nothing more.
|
| 88 |
+
"""
|
| 89 |
+
|
| 90 |
+
QUERY = "Show me some wine reviews with a score greater than 4.5 and less than 5.0."
|
| 91 |
+
|
| 92 |
+
SCHEMA = {
|
| 93 |
+
"title": "string",
|
| 94 |
+
"description": "string",
|
| 95 |
+
"price": "number",
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
base_model_name = "Qwen/Qwen2.5-7B"
|
| 99 |
+
adapter_path = "OramaSearch/query-translator-mini"
|
| 100 |
+
|
| 101 |
+
print("Loading tokenizer...")
|
| 102 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
| 103 |
+
|
| 104 |
+
print("Loading base model...")
|
| 105 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 106 |
+
base_model_name,
|
| 107 |
+
torch_dtype=torch.float16,
|
| 108 |
+
device_map="auto",
|
| 109 |
+
trust_remote_code=True,
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
print("Loading fine-tuned adapter...")
|
| 113 |
+
model = PeftModel.from_pretrained(model, adapter_path)
|
| 114 |
+
|
| 115 |
+
if torch.cuda.is_available():
|
| 116 |
+
model = model.cuda()
|
| 117 |
+
print(f"GPU memory after loading: {torch.cuda.memory_allocated(0) / 1024**2:.2f} MB")
|
| 118 |
+
|
| 119 |
+
messages = [
|
| 120 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 121 |
+
{"role": "user", "content": f"Query: {QUERY}\nSchema: {json.dumps(SCHEMA)}"},
|
| 122 |
+
]
|
| 123 |
+
|
| 124 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 125 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 126 |
+
outputs = model.generate(
|
| 127 |
+
**inputs,
|
| 128 |
+
max_new_tokens=512,
|
| 129 |
+
do_sample=True,
|
| 130 |
+
temperature=0.1,
|
| 131 |
+
top_p=0.9,
|
| 132 |
+
num_return_sequences=1,
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 136 |
+
print(response)
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
## Training Details
|
| 140 |
+
|
| 141 |
+
The model was trained on a NVIDIA H100 SXM using the following configuration:
|
| 142 |
+
|
| 143 |
+
- Base Model: Qwen 2.5 7B
|
| 144 |
+
- Training Method: LoRA
|
| 145 |
+
- Quantization: 4-bit quantization using bitsandbytes
|
| 146 |
+
- LoRA Configuration:
|
| 147 |
+
- Rank: 16
|
| 148 |
+
- Alpha: 32
|
| 149 |
+
- Dropout: 0.1
|
| 150 |
+
- Target Modules: Attention layers and MLP
|
| 151 |
+
|
| 152 |
+
- Training Arguments:
|
| 153 |
+
- Epochs: 3
|
| 154 |
+
- Batch Size: 2
|
| 155 |
+
- Learning Rate: 5e-5
|
| 156 |
+
- Gradient Accumulation Steps: 8
|
| 157 |
+
- FP16 Training: Enabled
|
| 158 |
+
- Gradient Checkpointing: Enabled
|
| 159 |
+
|
| 160 |
+
## Supported Query Types
|
| 161 |
+
|
| 162 |
+
The model can handle various types of queries including:
|
| 163 |
+
|
| 164 |
+
1. Simple text search:
|
| 165 |
+
|
| 166 |
+
```json
|
| 167 |
+
{
|
| 168 |
+
"term": "prosecco aperitif",
|
| 169 |
+
"limit": 5
|
| 170 |
+
}
|
| 171 |
+
```
|
| 172 |
+
|
| 173 |
+
2. Numeric range queries:
|
| 174 |
+
|
| 175 |
+
```json
|
| 176 |
+
{
|
| 177 |
+
"term": "",
|
| 178 |
+
"where": {
|
| 179 |
+
"price": {
|
| 180 |
+
"between": [20, 100]
|
| 181 |
+
}
|
| 182 |
+
}
|
| 183 |
+
}
|
| 184 |
+
```
|
| 185 |
+
|
| 186 |
+
3. Tag-based filtering:
|
| 187 |
+
|
| 188 |
+
```json
|
| 189 |
+
{
|
| 190 |
+
"term": "",
|
| 191 |
+
"where": {
|
| 192 |
+
"tags": {
|
| 193 |
+
"containsAll": ["red", "wine"]
|
| 194 |
+
}
|
| 195 |
+
}
|
| 196 |
+
}
|
| 197 |
+
```
|
| 198 |
+
|
| 199 |
+
## Limitations
|
| 200 |
+
|
| 201 |
+
- Does not support array of numbers or booleans
|
| 202 |
+
- Maximum input length is 1024 tokens
|
| 203 |
+
- Embedding fields are not supported in the schema
|
| 204 |
+
|
| 205 |
+
## Citation
|
| 206 |
+
|
| 207 |
+
If you use this model in your research, please cite:
|
| 208 |
+
|
| 209 |
+
```
|
| 210 |
+
@misc{query-translator-mini,
|
| 211 |
+
author = {OramaSearch Inc.},
|
| 212 |
+
title = {Query Translator Mini: Natural Language to Orama Query Translation},
|
| 213 |
+
year = {2024},
|
| 214 |
+
publisher = {HuggingFace},
|
| 215 |
+
journal = {HuggingFace Repository},
|
| 216 |
+
howpublished = {\url{https://huggingface.co/OramaSearch/query-translator-mini}}
|
| 217 |
+
}
|
| 218 |
+
```
|
| 219 |
+
|
| 220 |
+
## License
|
| 221 |
+
|
| 222 |
+
AGPLv3
|
| 223 |
+
|
| 224 |
+
## Acknowledgments
|
| 225 |
+
|
| 226 |
This model builds upon the Qwen 2.5 7B model and uses techniques from the PEFT library. Special thanks to the teams behind these projects.
|