Views
No views yet
1
2from transformers import AutoTokenizer, AutoModelForCausalLM
3import torch
4
5model_id = "ByteForge/Llama_3_8b_Instruct_Text2Sql_FullPrecision_Finetuned"
6
7tokenizer = AutoTokenizer.from_pretrained(model_id)
8
9model = AutoModelForCausalLM.from_pretrained(
10 model_id,
11 torch_dtype=torch.bfloat16,
12 device_map="auto",
13)
14
15prompt="""
16CREATE TABLE stadium (
17 stadium_id number,
18 location text,
19 name text,
20 capacity number,
21 highest number,
22 lowest number,
23 average number
24)
25
26CREATE TABLE singer (
27 singer_id number,
28 name text,
29 country text,
30 song_name text,
31 song_release_year text,
32 age number,
33 is_male others
34)
35
36CREATE TABLE concert (
37 concert_id number,
38 concert_name text,
39 theme text,
40 stadium_id text,
41 year text
42)
43
44CREATE TABLE singer_in_concert (
45 concert_id number,
46 singer_id text
47)
48
49-- Using valid SQLite, answer the following questions for the tables provided above.
50
51-- What is the maximum, the average, and the minimum capacity of stadiums ? (Generate 1 Sql query. No explaination needed)
52
53answer:
54"""
55
56messages = [
57 {"role": "system", "content": "You are an text to SQL query translator. Users will ask you questions in English and you will generate a SQL query based on the provided SCHEMA."},
58 {"role": "user", "content": prompt},
59]
60
61input_ids = tokenizer.apply_chat_template(
62 messages,
63 add_generation_prompt=True,
64 return_tensors="pt"
65).to(model.device)
66
67terminators = [
68 tokenizer.eos_token_id,
69 tokenizer.convert_tokens_to_ids("<|eot_id|>")
70]
71
72outputs = model.generate(
73 input_ids,
74 max_new_tokens=256,
75 eos_token_id=terminators,
76 do_sample=True,
77 temperature=0.6,
78 top_p=0.9,
79)
80response = outputs[0]
81print(tokenizer.decode(response, skip_special_tokens=True))
82
83#
84#system
85#You are an text to SQL query translator. Users will ask you questions in English and you will generate a SQL query based on the provided SCHEMA.
86#SCHEMA:
87#CREATE TABLE match_season (College VARCHAR, POSITION VARCHAR)
88#user
89#Which college have both players with position midfielder and players with position defender?
90#assistant
91#SELECT College FROM match_season WHERE POSITION = "Midfielder" INTERSECT SELECT College FROM match_season WHERE POSITION = "Defender"
92#