-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathllm.py
More file actions
144 lines (118 loc) · 5.3 KB
/
llm.py
File metadata and controls
144 lines (118 loc) · 5.3 KB
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
import google.generativeai as genai
import os
from dotenv import load_dotenv
import sqlite3
import pandas as pd
import re
import yaml
import logging
import time # Import time module for tracking
from prompts import Prompts
class LLM:
def __init__(self, model_name="gemini-1.5-pro-latest"):
load_dotenv()
self.api_key = os.getenv("GEMINI_API_KEY")
if not self.api_key:
raise ValueError("GEMINI_API_KEY not found in environment variables")
genai.configure(api_key=self.api_key)
self.model = genai.GenerativeModel(model_name)
# Use Prompts class instead of loading from YAML
self.prompts = Prompts()
def invoke(self, prompt: str) -> str:
invoke_start = time.time()
model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content(prompt)
invoke_end = time.time()
print(f"Time to invoke model and generate response: {invoke_end - invoke_start:.4f} seconds")
return response.text
def generate_sql_query(self, natural_language_prompt: str, original_prompt: str) -> str:
start_time = time.time()
# Access the prompt directly from the Prompts class
sql_generation_prompt = self.prompts.sql_generation_agent_prompt
# Include the original prompt for context
formatted_prompt = f"{sql_generation_prompt}\nOriginal Prompt: {original_prompt}\n{natural_language_prompt}"
logging.info("Formatted prompt prepared.")
# Invoke the model
invoke_start = time.time()
try:
response = self.invoke(formatted_prompt)
invoke_end = time.time()
logging.info(f"Time for invoke in SQL generation: {invoke_end - invoke_start:.4f} seconds")
except Exception as e:
logging.error(f"Error invoking the model: {e}")
return ""
# Clean SQL query
clean_start = time.time()
try:
formatted_response = self.clean_sql_query(response)
clean_end = time.time()
logging.info(f"Time to clean SQL query: {clean_end - clean_start:.4f} seconds")
except Exception as e:
logging.error(f"Error cleaning SQL query: {e}")
return ""
total_time = time.time() - start_time
logging.info(f"Total time for SQL generation: {total_time:.4f} seconds")
return formatted_response
def execute_sql_query(self, sql_query: str, csv_file: str):
execution_start = time.time()
# Read CSV
read_start = time.time()
df = pd.read_csv(csv_file)
read_end = time.time()
print(f"Time to read CSV file: {read_end - read_start:.4f} seconds")
# Execute SQL query in SQLite
with sqlite3.connect(':memory:') as conn:
df.to_sql('transactions', conn, index=False, if_exists='replace')
try:
sql_start = time.time()
results = pd.read_sql_query(sql_query, conn)
sql_end = time.time()
print(f"Time to execute SQL query: {sql_end - sql_start:.4f} seconds")
print(results)
return results
except Exception as e:
print(f"Query Execution Error: {e}")
return pd.DataFrame() # Return an empty DataFrame
execution_end = time.time()
print(f"Total time for SQL query execution: {execution_end - execution_start:.4f} seconds")
@staticmethod
def clean_sql_query(query: str) -> str:
"""
Cleans the LLM response to extract only the SQL query.
Removes backticks and extraneous characters.
"""
query = re.sub(r'[`]', '', query)
query = query.strip()
sql_start = re.search(r"(SELECT|INSERT|UPDATE|DELETE)\b", query, re.IGNORECASE)
if sql_start:
query = query[sql_start.start():]
sql_end = query.find(';')
if sql_end != -1:
query = query[:sql_end + 1]
return query
if __name__ == "__main__":
llm = LLM()
raw_query = """
Retrieve all transactions of type "TRANSFER" and "CASH_OUT" where the transaction amount is above 100.
Include columns for the step, type, amount, origin account balance before (oldbalanceOrg) and after (newbalanceOrig) the transaction,
and the destination account balance before and after the transaction.
Sort the results by transaction amount in descending order.
"""
start_time = time.time()
print("Starting SQL query generation and execution pipeline.")
# Generate SQL Query
query_generation_start = time.time()
query = llm.generate_sql_query(raw_query, raw_query)
query_generation_end = time.time()
print(f"Time to generate SQL query: {query_generation_end - query_generation_start:.4f} seconds")
# Execute SQL Query
execution_start = time.time()
csv_file_path = "datasets/samples.csv"
result = llm.execute_sql_query(query, csv_file_path)
execution_end = time.time()
print(f"Time to execute SQL query and process results: {execution_end - execution_start:.4f} seconds")
# Print Results
print(result.to_dict(orient="records"))
print(result)
end_time = time.time()
print(f"Total time for script execution: {end_time - start_time:.4f} seconds")