Update README.md
Browse files
README.md
CHANGED
@@ -4,199 +4,190 @@ base_model: microsoft/phi-2
|
|
4 |
---
|
5 |
|
6 |
# Model Card for Model ID
|
7 |
-
|
8 |
-
<!-- Provide a quick summary of what the model is/does. -->
|
9 |
-
|
10 |
|
11 |
|
12 |
## Model Details
|
|
|
13 |
|
14 |
### Model Description
|
15 |
|
16 |
<!-- Provide a longer summary of what this model is. -->
|
17 |
|
18 |
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
- **License:** [More Information Needed]
|
26 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
27 |
-
|
28 |
-
### Model Sources [optional]
|
29 |
-
|
30 |
-
<!-- Provide the basic links for the model. -->
|
31 |
-
|
32 |
-
- **Repository:** [More Information Needed]
|
33 |
-
- **Paper [optional]:** [More Information Needed]
|
34 |
-
- **Demo [optional]:** [More Information Needed]
|
35 |
-
|
36 |
-
## Uses
|
37 |
-
|
38 |
-
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
39 |
-
|
40 |
-
### Direct Use
|
41 |
-
|
42 |
-
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
43 |
-
|
44 |
-
[More Information Needed]
|
45 |
-
|
46 |
-
### Downstream Use [optional]
|
47 |
|
48 |
-
|
|
|
49 |
|
50 |
-
|
|
|
51 |
|
52 |
-
###
|
|
|
|
|
53 |
|
54 |
-
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
55 |
-
|
56 |
-
[More Information Needed]
|
57 |
-
|
58 |
-
## Bias, Risks, and Limitations
|
59 |
-
|
60 |
-
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
61 |
-
|
62 |
-
[More Information Needed]
|
63 |
-
|
64 |
-
### Recommendations
|
65 |
-
|
66 |
-
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
67 |
-
|
68 |
-
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
69 |
|
70 |
## How to Get Started with the Model
|
71 |
|
72 |
-
Use the code
|
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 |
- PEFT 0.10.0
|
|
|
4 |
---
|
5 |
|
6 |
# Model Card for Model ID
|
7 |
+
phi-2-mongodb is a fine-tuned version of microsoft/phi-2 to generate MongoDB pipeline queries. It was fine-tuned on a custom curated natural language to MongoDB queries dataset, I'll be releasing that next week.
|
|
|
|
|
8 |
|
9 |
|
10 |
## Model Details
|
11 |
+
Further details about fine-tuned model can be found at : https://github.com/Chirayu-Tripathi/nl2query. It can also be used via nl2query library.
|
12 |
|
13 |
### Model Description
|
14 |
|
15 |
<!-- Provide a longer summary of what this model is. -->
|
16 |
|
17 |
|
18 |
+
- **Fine-tuned by:** [`Chirayu Tripathi`](http://www.linkedin.com/in/chirayu-tripathi)
|
19 |
+
- **Developed by:** [`Microsoft`]
|
20 |
+
- **Language(s) (NLP):** English
|
21 |
+
- **License:** MIT
|
22 |
+
- **Finetuned from model:** [`microsoft/phi-2`](https://huggingface.co/microsoft/phi-2)
|
23 |
|
24 |
+
### Prompt Template
|
25 |
+
```
|
26 |
+
prompt_template = f"""<s>
|
27 |
+
Task Description:
|
28 |
+
Your task is to create a MongoDB query that accurately fulfills the provided Instruct while strictly adhering to the given MongoDB schema. Ensure that the query solely relies on keys and columns present in the schema. Minimize the usage of lookup operations wherever feasible to enhance query efficiency.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
MongoDB Schema:
|
31 |
+
{db_schema}
|
32 |
|
33 |
+
### Instruct:
|
34 |
+
{text}
|
35 |
|
36 |
+
### Output:
|
37 |
+
"""
|
38 |
+
```
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
## How to Get Started with the Model
|
42 |
|
43 |
+
Use the code sample provided in the original post to interact with the model.
|
44 |
+
```python
|
45 |
+
from transformers import (
|
46 |
+
AutoTokenizer,
|
47 |
+
AutoModelForCausalLM,
|
48 |
+
BitsAndBytesConfig,
|
49 |
+
)
|
50 |
+
import torch
|
51 |
+
from peft import PeftModel
|
52 |
+
|
53 |
+
db_schema = '''{
|
54 |
+
"collections": [
|
55 |
+
{
|
56 |
+
"name": "shipwrecks",
|
57 |
+
"indexes": [
|
58 |
+
{
|
59 |
+
"key": {
|
60 |
+
"_id": 1
|
61 |
+
}
|
62 |
+
},
|
63 |
+
{
|
64 |
+
"key": {
|
65 |
+
"feature_type": 1
|
66 |
+
}
|
67 |
+
},
|
68 |
+
{
|
69 |
+
"key": {
|
70 |
+
"chart": 1
|
71 |
+
}
|
72 |
+
},
|
73 |
+
{
|
74 |
+
"key": {
|
75 |
+
"latdec": 1,
|
76 |
+
"londec": 1
|
77 |
+
}
|
78 |
+
}
|
79 |
+
],
|
80 |
+
"uniqueIndexes": [],
|
81 |
+
"document": {
|
82 |
+
"properties": {
|
83 |
+
"_id": {
|
84 |
+
"bsonType": "string"
|
85 |
+
},
|
86 |
+
"recrd": {
|
87 |
+
"bsonType": "string"
|
88 |
+
},
|
89 |
+
"vesslterms": {
|
90 |
+
"bsonType": "string"
|
91 |
+
},
|
92 |
+
"feature_type": {
|
93 |
+
"bsonType": "string"
|
94 |
+
},
|
95 |
+
"chart": {
|
96 |
+
"bsonType": "string"
|
97 |
+
},
|
98 |
+
"latdec": {
|
99 |
+
"bsonType": "double"
|
100 |
+
},
|
101 |
+
"londec": {
|
102 |
+
"bsonType": "double"
|
103 |
+
},
|
104 |
+
"gp_quality": {
|
105 |
+
"bsonType": "string"
|
106 |
+
},
|
107 |
+
"depth": {
|
108 |
+
"bsonType": "string"
|
109 |
+
},
|
110 |
+
"sounding_type": {
|
111 |
+
"bsonType": "string"
|
112 |
+
},
|
113 |
+
"history": {
|
114 |
+
"bsonType": "string"
|
115 |
+
},
|
116 |
+
"quasou": {
|
117 |
+
"bsonType": "string"
|
118 |
+
},
|
119 |
+
"watlev": {
|
120 |
+
"bsonType": "string"
|
121 |
+
},
|
122 |
+
"coordinates": {
|
123 |
+
"bsonType": "array",
|
124 |
+
"items": {
|
125 |
+
"bsonType": "double"
|
126 |
+
}
|
127 |
+
}
|
128 |
+
}
|
129 |
+
}
|
130 |
+
}
|
131 |
+
],
|
132 |
+
"version": 1
|
133 |
+
}'''
|
134 |
+
|
135 |
+
text = ''''Find the count of shipwrecks for each unique combination of "latdec" and "longdec"'''
|
136 |
+
prompt = f"""<s>
|
137 |
+
Task Description:
|
138 |
+
Your task is to create a MongoDB query that accurately fulfills the provided Instruct while strictly adhering to the given MongoDB schema. Ensure that the query solely relies on keys and columns present in the schema. Minimize the usage of lookup operations wherever feasible to enhance query efficiency.
|
139 |
+
|
140 |
+
MongoDB Schema:
|
141 |
+
{db_schema}
|
142 |
+
|
143 |
+
### Instruct:
|
144 |
+
{text}
|
145 |
+
|
146 |
+
### Output:
|
147 |
+
"""
|
148 |
+
|
149 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
150 |
+
base_model_id = "microsoft/phi-2"
|
151 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_id, use_fast=True)
|
152 |
+
compute_dtype = getattr(torch, "float16")
|
153 |
+
bnb_config = BitsAndBytesConfig(
|
154 |
+
load_in_4bit=True,
|
155 |
+
bnb_4bit_quant_type="nf4",
|
156 |
+
bnb_4bit_compute_dtype=compute_dtype,
|
157 |
+
bnb_4bit_use_double_quant=True,
|
158 |
+
)
|
159 |
+
model = AutoModelForCausalLM.from_pretrained(
|
160 |
+
base_model_id,
|
161 |
+
trust_remote_code=True,
|
162 |
+
quantization_config=bnb_config,
|
163 |
+
revision="refs/pr/23",
|
164 |
+
device_map={"": 0},
|
165 |
+
torch_dtype="auto",
|
166 |
+
flash_attn=True,
|
167 |
+
flash_rotary=True,
|
168 |
+
fused_dense=True,
|
169 |
+
)
|
170 |
+
adapter = 'Chirayu/phi-2-mongodb'
|
171 |
+
|
172 |
+
model = PeftModel.from_pretrained(model, adapter).to(device)
|
173 |
+
model_inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
174 |
+
output = model.generate(
|
175 |
+
**model_inputs,
|
176 |
+
max_length=1024,
|
177 |
+
no_repeat_ngram_size=10,
|
178 |
+
repetition_penalty=1.02,
|
179 |
+
pad_token_id=tokenizer.eos_token_id,
|
180 |
+
eos_token_id=tokenizer.eos_token_id,
|
181 |
+
)[0]
|
182 |
+
|
183 |
+
prompt_length = model_inputs['input_ids'].shape[1]
|
184 |
+
query = tokenizer.decode(output[prompt_length:], skip_special_tokens=False)
|
185 |
+
try:
|
186 |
+
stop_idx = query.index("</s>")
|
187 |
+
except Exception as e:
|
188 |
+
print(e)
|
189 |
+
stop_idx = len(query)
|
190 |
+
print(query[: stop_idx].strip())
|
191 |
+
```
|
192 |
|
193 |
- PEFT 0.10.0
|