Spaces:
Sleeping
Sleeping
Database Connection Fix
Browse files- pyproject.toml +5 -0
- requirements.txt +8 -1
- src/graph.py +57 -40
- uv.lock +0 -0
pyproject.toml
CHANGED
|
@@ -21,4 +21,9 @@ dependencies = [
|
|
| 21 |
"sqlalchemy>=2.0.50",
|
| 22 |
"streamlit>=1.58.0",
|
| 23 |
"pymysql",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
]
|
|
|
|
| 21 |
"sqlalchemy>=2.0.50",
|
| 22 |
"streamlit>=1.58.0",
|
| 23 |
"pymysql",
|
| 24 |
+
"psycopg2-binary>=2.9.12",
|
| 25 |
+
"pyodbc>=5.3.0",
|
| 26 |
+
"oracledb>=4.0.1",
|
| 27 |
+
"snowflake-sqlalchemy>=1.10.0",
|
| 28 |
+
"sqlalchemy-bigquery>=1.17.0",
|
| 29 |
]
|
requirements.txt
CHANGED
|
@@ -17,4 +17,11 @@ streamlit
|
|
| 17 |
requests
|
| 18 |
psycopg-pool
|
| 19 |
langsmith
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
requests
|
| 18 |
psycopg-pool
|
| 19 |
langsmith
|
| 20 |
+
|
| 21 |
+
# --- Database Drivers ---
|
| 22 |
+
psycopg2-binary
|
| 23 |
+
pymysql
|
| 24 |
+
pyodbc
|
| 25 |
+
oracledb
|
| 26 |
+
snowflake-sqlalchemy
|
| 27 |
+
sqlalchemy-bigquery
|
src/graph.py
CHANGED
|
@@ -81,37 +81,50 @@ INSTRUCTION: Analyze the error message and the schema carefully. Fix the syntax,
|
|
| 81 |
else :
|
| 82 |
error_context = ""
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
| 86 |
|
| 87 |
=== DATABASE SCHEMA & DIALECT ===
|
| 88 |
-
Look at the metadata below to identify the targeted database engine dialect and table layout:
|
| 89 |
{scheme}
|
| 90 |
|
| 91 |
=== CONVERSATION HISTORY ===
|
| 92 |
-
Use this previous context to resolve ambiguous terms (e.g., if the user says "filter those by...", look here to see what "those" refers to):
|
| 93 |
{history_text}
|
|
|
|
|
|
|
| 94 |
{error_context}
|
| 95 |
|
| 96 |
=== CRITICAL RULES ===
|
| 97 |
-
1.
|
| 98 |
-
2.
|
| 99 |
-
3.
|
| 100 |
-
4.
|
| 101 |
-
5.
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
final_msg = [
|
| 117 |
system_prompt,
|
|
@@ -149,6 +162,7 @@ def routing(state : State) :
|
|
| 149 |
def answer_node(state : State) :
|
| 150 |
messages = state.get("messages")
|
| 151 |
query_result = state.get("query_result" , "No records found.")
|
|
|
|
| 152 |
error = state.get("error")
|
| 153 |
|
| 154 |
history_messages = messages[:-1]
|
|
@@ -162,29 +176,32 @@ def answer_node(state : State) :
|
|
| 162 |
else:
|
| 163 |
history_text = "This is the first user request. No history exists."
|
| 164 |
|
| 165 |
-
system_prompt = f"""
|
|
|
|
| 166 |
|
| 167 |
=== CONVERSATION HISTORY ===
|
| 168 |
-
Use this to maintain the context and tone of the conversation:
|
| 169 |
{history_text}
|
| 170 |
|
| 171 |
-
=== EXECUTION CONTEXT ===
|
|
|
|
|
|
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
|
|
|
| 188 |
|
| 189 |
final_msg = [
|
| 190 |
SystemMessage(content=system_prompt),
|
|
|
|
| 81 |
else :
|
| 82 |
error_context = ""
|
| 83 |
|
| 84 |
+
system_prompt = SystemMessage(content=f"""
|
| 85 |
+
You are an expert Data Analyst and SQL Engineer.
|
| 86 |
+
|
| 87 |
+
Your task is to generate ONE valid SELECT query for the latest user request.
|
| 88 |
|
| 89 |
=== DATABASE SCHEMA & DIALECT ===
|
|
|
|
| 90 |
{scheme}
|
| 91 |
|
| 92 |
=== CONVERSATION HISTORY ===
|
|
|
|
| 93 |
{history_text}
|
| 94 |
+
|
| 95 |
+
=== ERROR CORRECTION MODE ===
|
| 96 |
{error_context}
|
| 97 |
|
| 98 |
=== CRITICAL RULES ===
|
| 99 |
+
1. Use ONLY tables and columns that exist in the schema.
|
| 100 |
+
2. Never hallucinate columns, joins, or tables.
|
| 101 |
+
3. Generate only SELECT queries. No INSERT, UPDATE, DELETE, DROP, TRUNCATE, ALTER.
|
| 102 |
+
4. Use the exact SQL dialect implied by the schema metadata.
|
| 103 |
+
5. For any output columns, ALWAYS use clear aliases.
|
| 104 |
+
Example:
|
| 105 |
+
- customer_id AS customer_id
|
| 106 |
+
- customer_name AS customer_name
|
| 107 |
+
- SUM(amount) AS total_amount
|
| 108 |
+
6. When the user asks for a person/customer/company/product/entity, return BOTH:
|
| 109 |
+
- the readable name field if it exists
|
| 110 |
+
- the matching ID field
|
| 111 |
+
7. If a name exists in another table, join to fetch it.
|
| 112 |
+
8. If no readable name exists, return the best human-readable identifier available, and the ID.
|
| 113 |
+
9. For aggregate queries, include a label column when possible so the answer layer can explain the result.
|
| 114 |
+
10. If fixing an error, preserve the original user intent and correct only the broken parts.
|
| 115 |
+
|
| 116 |
+
=== PRIORITY RULE FOR ID VS NAME ===
|
| 117 |
+
- Priority 1: name + id together, if possible
|
| 118 |
+
- Priority 2: name only, if name exists but id cannot be included
|
| 119 |
+
- Priority 3: id only, only if no readable name exists
|
| 120 |
+
|
| 121 |
+
=== OUTPUT FORMAT REQUIREMENT ===
|
| 122 |
+
Return a SQL query whose selected columns are self-explanatory.
|
| 123 |
+
Do not rely on positional meaning like column 1, column 2 without aliases.
|
| 124 |
+
|
| 125 |
+
=== CURRENT DATE ===
|
| 126 |
+
Today's date is {current_date}.
|
| 127 |
+
""")
|
| 128 |
|
| 129 |
final_msg = [
|
| 130 |
system_prompt,
|
|
|
|
| 162 |
def answer_node(state : State) :
|
| 163 |
messages = state.get("messages")
|
| 164 |
query_result = state.get("query_result" , "No records found.")
|
| 165 |
+
sql_query = state.get("sql_query", "")
|
| 166 |
error = state.get("error")
|
| 167 |
|
| 168 |
history_messages = messages[:-1]
|
|
|
|
| 176 |
else:
|
| 177 |
history_text = "This is the first user request. No history exists."
|
| 178 |
|
| 179 |
+
system_prompt = f"""
|
| 180 |
+
You are a helpful Data Analyst communicating directly with a user.
|
| 181 |
|
| 182 |
=== CONVERSATION HISTORY ===
|
|
|
|
| 183 |
{history_text}
|
| 184 |
|
| 185 |
+
=== EXECUTION CONTEXT ===
|
| 186 |
+
SQL QUERY USED:
|
| 187 |
+
{sql_query}
|
| 188 |
|
| 189 |
+
RAW DATABASE RESULT:
|
| 190 |
+
{query_result}
|
| 191 |
+
|
| 192 |
+
=== INSTRUCTIONS ===
|
| 193 |
+
1. Use ONLY the returned data.
|
| 194 |
+
2. Interpret the result using the SQL query and its selected aliases.
|
| 195 |
+
3. If the query selected columns like customer_id, customer_name, total_amount, use those exact labels in the final response.
|
| 196 |
+
4. If the result is positional, map values to the SQL SELECT order.
|
| 197 |
+
5. Never invent a name or ID.
|
| 198 |
+
6. For who/which questions:
|
| 199 |
+
- prefer name + id
|
| 200 |
+
- if name is missing, give the id and clearly say no readable name was returned
|
| 201 |
+
7. If the result contains an ID and a value like total_amount, explain them clearly.
|
| 202 |
+
8. Do not mention SQL or the database in the final answer.
|
| 203 |
+
9. Give a clean, professional response.
|
| 204 |
+
"""
|
| 205 |
|
| 206 |
final_msg = [
|
| 207 |
SystemMessage(content=system_prompt),
|
uv.lock
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|