PEFT
Safetensors
English

Improve language tag

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