kkhushisaid commited on
Commit
d8cbcf4
·
verified ·
1 Parent(s): 7634430

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -21
app.py CHANGED
@@ -95,28 +95,24 @@ def find_best_fit(embeddings, model, user_query, metadata_list):
95
  return metadata_list[best_match_index]
96
 
97
  def create_prompt(user_query, table_metadata):
98
- """Generates a prompt for the AI model."""
99
- system_prompt = """
100
- You are a SQL query generator specialized in generating SQL queries for a single table at a time.
101
- Your task is to accurately convert natural language queries into SQL statements based on the user's intent and the provided table metadata.
102
-
103
- Rules:
104
- - Assume all queries relate to a single table provided in the metadata. Ignore references to other tables.
105
- - Ensure the generated query matches the table name, columns, and data types in the metadata.
106
- - Capture filters, sorting, or aggregations as per user intent.
107
- - Use standard SQL syntax.
108
-
109
- Input:
110
- User Query: {user_query}
111
- Table Metadata: {table_metadata}
112
-
113
- Output:
114
- - Provide only the SQL query in a single line. No extra words.
115
  """
116
  return system_prompt
117
 
 
118
  def generate_sql_query(system_prompt):
119
- """Uses Groq API to generate an SQL query and prints debugging info."""
120
  try:
121
  client = Groq(api_key=groq_api_key)
122
  chat_completion = client.chat.completions.create(
@@ -124,11 +120,14 @@ def generate_sql_query(system_prompt):
124
  model="llama3-70b-8192"
125
  )
126
 
127
- # Extract response
 
 
 
128
  result = chat_completion.choices[0].message.content.strip()
129
  print(f"✅ AI Response: {result}") # Debugging
130
 
131
- # Check if result is a valid SQL query
132
  if result.lower().startswith("select"):
133
  return result
134
  else:
@@ -139,6 +138,7 @@ def generate_sql_query(system_prompt):
139
  print(f"❌ API Error: {e}")
140
  return "⚠️ API failed. Check logs."
141
 
 
142
  def response(user_query, dataset_folder):
143
  """Processes the user query and returns an SQL query."""
144
  dataframes, metadata_list = load_dataset_metadata(dataset_folder)
@@ -163,4 +163,4 @@ iface = gr.Interface(
163
 
164
  # Run Gradio app
165
  if __name__ == "__main__":
166
- iface.launch()
 
95
  return metadata_list[best_match_index]
96
 
97
  def create_prompt(user_query, table_metadata):
98
+ """Generates a direct and structured SQL prompt."""
99
+ system_prompt = f"""
100
+ You are an AI SQL query generator. Your job is to convert natural language questions into precise SQL queries.
101
+
102
+ **Table Information:**
103
+ {table_metadata}
104
+
105
+ **User Question:**
106
+ {user_query}
107
+
108
+ **Output:**
109
+ Provide ONLY the SQL query. Do not add any explanation, comments, or extra text.
 
 
 
 
 
110
  """
111
  return system_prompt
112
 
113
+
114
  def generate_sql_query(system_prompt):
115
+ """Uses Groq API to generate an SQL query with better debugging."""
116
  try:
117
  client = Groq(api_key=groq_api_key)
118
  chat_completion = client.chat.completions.create(
 
120
  model="llama3-70b-8192"
121
  )
122
 
123
+ # Debug: Print entire response
124
+ print("🔍 Full API Response:", chat_completion)
125
+
126
+ # Extract AI response
127
  result = chat_completion.choices[0].message.content.strip()
128
  print(f"✅ AI Response: {result}") # Debugging
129
 
130
+ # Check if the response starts with "SELECT"
131
  if result.lower().startswith("select"):
132
  return result
133
  else:
 
138
  print(f"❌ API Error: {e}")
139
  return "⚠️ API failed. Check logs."
140
 
141
+
142
  def response(user_query, dataset_folder):
143
  """Processes the user query and returns an SQL query."""
144
  dataframes, metadata_list = load_dataset_metadata(dataset_folder)
 
163
 
164
  # Run Gradio app
165
  if __name__ == "__main__":
166
+ iface.launch()