File size: 9,428 Bytes
ec050d8 2c21b41 48054f0 2c21b41 fd7845c 2c21b41 fd7845c 2c21b41 fd7845c 2c21b41 fd7845c 2c21b41 |
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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
---
license: apache-2.0
---
# Description
This is a LoRA-finetuned `codellama/CodeLlama-7b-hf` text2SQL model that generates SQLite queries. This is a relatively small model that was fine-tuned on 8 x A10Gs with a total GPU memory of 192GB for over 4 days for 3 epochs. For databases with different SQL syntaxes that do not adhere to SQLite's syntax, we plan to launch other models specifically catered to them.
# Usage
## Huggingface Transformers Library
```py
from transformers import AutoTokenizer, AutoModelForCausalLM
model_name = 'unSQLv1-7b-generic-lora'
device = 'cuda'
model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
tokenizer = AutoTokenizer.from_pretrained(model_name)
example_prompt = '''
### Schema and the Natural Language Query:
CREATE TABLE stadium (
stadium_id number,
location text,
name text,
capacity number,
highest number,
lowest number,
average number
)
CREATE TABLE singer (
singer_id number,
name text,
country text,
song_name text,
song_release_year text,
age number,
is_male others
)
CREATE TABLE concert (
concert_id number,
concert_name text,
theme text,
stadium_id text,
year text
)
CREATE TABLE singer_in_concert (
concert_id number,
singer_id text
)
-- Using valid SQLite, answer the following questions for the tables provided above.
-- What is the maximum, the average, and the minimum capacity of stadiums ?
'''
inputs = tokenizer.encode(example_prompt, return_tensors="pt").to(device)
outputs = model.generate(inputs, max_length=512)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```
## Sagemaker Endpoint I/O Example
```py
payload = {
"inputs": "### Schema and the Natural Language Query:\nCREATE TABLE stadium (\n stadium_id number,\n location text,\n name text,\n capacity number,\n highest number,\n lowest number,\n average number\n)\n\nCREATE TABLE singer (\n singer_id number,\n name text,\n country text,\n song_name text,\n song_release_year text,\n age number,\n is_male others\n)\n\nCREATE TABLE concert (\n concert_id number,\n concert_name text,\n theme text,\n stadium_id text,\n year text\n)\n\nCREATE TABLE singer_in_concert (\n concert_id number,\n singer_id text\n)\n\n-- Using valid SQLite, answer the following questions for the tables provided above.\n\n-- What is the maximum, the average, and the minimum capacity of stadiums ?",
"parameters": {
"maxNewTokens": 512,
"topP": 0.9,
"temperature": 0.2
}
}
client = boto3.client('runtime.sagemaker')
endpoint_name = 'deployed_model_name'
response = client.invoke_endpoint(
EndpointName=endpoint_name,
ContentType='application/json',
Body=json.dumps(payload).encode('utf-8'),
)
response = response["Body"].read().decode("utf8")
response = json.loads(response)
print(response[0]['generated_text'])
```
```json
{
"body": [
{
"generated_text": "\n\n\n### Response:\nSELECT MAX(capacity), AVG(capacity), MIN(capacity) FROM stadium",
"details": {
"finish_reason": "eos_token",
"generated_tokens": 30,
"seed": 14524408611356330000,
"prefill": [],
"tokens": [
{
"id": 13,
"text": "\n",
"logprob": 0,
"special": false
},
{
"id": 13,
"text": "\n",
"logprob": 0,
"special": false
},
{
"id": 13,
"text": "\n",
"logprob": 0,
"special": false
},
{
"id": 2277,
"text": "##",
"logprob": 0,
"special": false
},
{
"id": 29937,
"text": "#",
"logprob": 0,
"special": false
},
{
"id": 13291,
"text": " Response",
"logprob": 0,
"special": false
},
{
"id": 29901,
"text": ":",
"logprob": 0,
"special": false
},
{
"id": 13,
"text": "\n",
"logprob": 0,
"special": false
},
{
"id": 6404,
"text": "SELECT",
"logprob": 0,
"special": false
},
{
"id": 18134,
"text": " MAX",
"logprob": 0,
"special": false
},
{
"id": 29898,
"text": "(",
"logprob": 0,
"special": false
},
{
"id": 5030,
"text": "cap",
"logprob": 0,
"special": false
},
{
"id": 5946,
"text": "acity",
"logprob": 0,
"special": false
},
{
"id": 511,
"text": "),",
"logprob": 0,
"special": false
},
{
"id": 16884,
"text": " AV",
"logprob": 0,
"special": false
},
{
"id": 29954,
"text": "G",
"logprob": 0,
"special": false
},
{
"id": 29898,
"text": "(",
"logprob": 0,
"special": false
},
{
"id": 5030,
"text": "cap",
"logprob": 0,
"special": false
},
{
"id": 5946,
"text": "acity",
"logprob": 0,
"special": false
},
{
"id": 511,
"text": "),",
"logprob": 0,
"special": false
},
{
"id": 341,
"text": " M",
"logprob": 0,
"special": false
},
{
"id": 1177,
"text": "IN",
"logprob": 0,
"special": false
},
{
"id": 29898,
"text": "(",
"logprob": 0,
"special": false
},
{
"id": 5030,
"text": "cap",
"logprob": 0,
"special": false
},
{
"id": 5946,
"text": "acity",
"logprob": 0,
"special": false
},
{
"id": 29897,
"text": ")",
"logprob": 0,
"special": false
},
{
"id": 3895,
"text": " FROM",
"logprob": 0,
"special": false
},
{
"id": 10728,
"text": " stad",
"logprob": 0,
"special": false
},
{
"id": 1974,
"text": "ium",
"logprob": 0,
"special": false
},
{
"id": 2,
"text": "</s>",
"logprob": 0,
"special": true
}
]
}
}
],
"contentType": "application/json",
"invokedProductionVariant": "AllTraffic"
}
``` |