| from transformers import AutoTokenizer, AutoModelForCausalLM |
| import torch |
| import time |
|
|
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
| """ |
| tokenizer = AutoTokenizer.from_pretrained(".") |
| model = AutoModelForCausalLM.from_pretrained(".").cuda() |
| input_text = "#If I have a SQL table called people with columns 'name, date, count' generate a SQL query to get all peoples names" |
| inputs = tokenizer(input_text, return_tensors="pt").to(model.device) |
| outputs = model.generate(**inputs, max_length=128) |
| print(tokenizer.decode(outputs[0], skip_special_tokens=True)) |
| """ |
|
|
| tokenizer = AutoTokenizer.from_pretrained("./deepseek-coder-1.3b-instruct") |
| model = AutoModelForCausalLM.from_pretrained("./deepseek-coder-1.3b-instruct", torch_dtype=torch.bfloat16, device_map=device) |
| |
| |
|
|
| |
|
|
| input_text = """You are an AI assistant that generates SQL queries for an NBA database based on user questions. The database consists of two tables: |
| |
| 1. `team` - Stores information about NBA teams. |
| - `id`: Unique team identifier. |
| - `full_name`: Full team name (e.g., "Los Angeles Lakers"). |
| - `abbreviation`: 3-letter team code (e.g., "LAL"). |
| - `city`, `state`: Location of the team. |
| - `year_founded`: The year the team was founded. |
| |
| 2. `game` - Stores details of individual games. |
| - `game_date`: Date of the game. |
| - `team_id_home`, `team_id_away`: Unique IDs of home and away teams. |
| - `team_name_home`, `team_name_away`: Full names of the teams. |
| - `pts_home`, `pts_away`: Points scored by home and away teams. |
| - `wl_home`: "W" if the home team won, "L" if they lost. |
| - `reb_home`, `reb_away`: Total rebounds. |
| - `ast_home`, `ast_away`: Total assists. |
| - Other statistics include field goals (`fgm_home`, `fg_pct_home`), three-pointers (`fg3m_home`), free throws (`ftm_home`), and turnovers (`tov_home`). |
| |
| ### Instructions: |
| - Generate a valid SQL query to retrieve relevant data from the database. |
| - Use column names correctly based on the provided schema. |
| - Ensure the query is well-structured and avoids unnecessary joins. |
| - Format the query with proper indentation. |
| |
| ### Example Queries: |
| User: "What is the most points the Los Angeles Lakers have ever scored at home?" |
| SQL: |
| SELECT MAX(pts_home) |
| FROM game |
| WHERE team_name_home = 'Los Angeles Lakers'; |
| |
| User: "List all games where the Golden State Warriors scored more than 130 points." |
| SQL: |
| SELECT game_date, team_name_home, pts_home, team_name_away, pts_away |
| FROM game |
| WHERE (team_name_home = 'Golden State Warriors' AND pts_home > 130) |
| OR (team_name_away = 'Golden State Warriors' AND pts_away > 130); |
| |
| Now, generate a SQL query based on the following user request: """ |
|
|
| messages=[ |
| { 'role': 'user', 'content': input_text + "What is the most points ever scored by the New York Knicks at home?"} |
| |
| ] |
|
|
| start_time = time.time() |
| inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device) |
| |
| outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, top_k=50, top_p=0.95, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id) |
| end_time = time.time() |
|
|
| print(tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)) |
|
|
| print("Execution time:") |
| print(end_time - start_time) |