Views
No views yet
pip install transformers torch accelerate1<|begin_of_text|><|start_header_id|>system<|end_header_id|>
2
3[System context and database schema]
4
5<|eot_id|><|start_header_id|>user<|end_header_id|>
6
7[User query]
8
9<|eot_id|><|start_header_id|>assistant<|end_header_id|>1from transformers import pipeline
2import torch
3
4# Initialize the pipeline
5generator = pipeline(
6 "text-generation",
7 model="XeAI/LLaMa_3.2_3B_Instruct_Text2SQL", # Replace with your model ID
8 torch_dtype=torch.float16,
9 device_map="auto"
10)
11
12def generate_sql_query(context, question):
13 # Format the prompt according to the training template
14 prompt = f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
15
16Cutting Knowledge Date: December 2023
17Today Date: 07 Nov 2024
18
19You are a specialized SQL query generator focused solely on the provided RAG database. Your tasks are:
201. Generate SQL queries based on user requests that are related to querying the RAG database.
212. Only output the SQL query itself, without any additional explanation or commentary.
223. Use the context provided from the RAG database to craft accurate queries.
23
24Context: {context}
25<|eot_id|><|start_header_id|>user<|end_header_id|>
26
27{question}<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
28
29 response = generator(
30 prompt,
31 max_length=500,
32 num_return_sequences=1,
33 temperature=0.1,
34 do_sample=True,
35 pad_token_id=generator.tokenizer.eos_token_id
36 )
37
38 return response[0]['generated_text']
39
40# Example usage
41context = """CREATE TABLE upgrades (id INT, cost FLOAT, type TEXT);
42INSERT INTO upgrades (id, cost, type) VALUES
43(1, 500, 'Insulation'),
44(2, 1000, 'HVAC'),
45(3, 1500, 'Lighting');"""
46
47questions = [
48 "Find the energy efficiency upgrades with the highest cost and their types.",
49 "Show me all upgrades costing less than 1000 dollars.",
50 "Calculate the average cost of all upgrades."
51]
52
53for question in questions:
54 sql = generate_sql_query(context, question)
55 print(f"\nQuestion: {question}")
56 print(f"Generated SQL: {sql}\n")1def generate_sql_with_custom_prompt(context, question, custom_system_prompt=""):
2 base_prompt = """<|begin_of_text|><|start_header_id|>system<|end_header_id|>
3
4Cutting Knowledge Date: December 2023
5Today Date: 07 Nov 2024
6
7You are a specialized SQL query generator focused solely on the provided RAG database."""
8
9 full_prompt = f"""{base_prompt}
10{custom_system_prompt}
11
12Context: {context}
13<|eot_id|><|start_header_id|>user<|end_header_id|>
14
15{question}<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
16
17 response = generator(
18 full_prompt,
19 max_length=500,
20 num_return_sequences=1,
21 temperature=0.1,
22 do_sample=True,
23 pad_token_id=generator.tokenizer.eos_token_id
24 )
25
26 return response[0]['generated_text']