Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -57,9 +57,14 @@ def create_agent_app(db_path: str):
|
|
57 |
except Exception as e:
|
58 |
flash(f"[ERROR]: Failed to connect to DB: {e}", "error")
|
59 |
raise
|
60 |
-
|
61 |
@tool
|
62 |
def db_query_tool(query: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
63 |
try:
|
64 |
result = db_instance.run_no_throw(query)
|
65 |
return result or "Error: Query failed. Please rewrite your query and try again."
|
@@ -74,13 +79,54 @@ def create_agent_app(db_path: str):
|
|
74 |
messages: Annotated[list[AnyMessage], add_messages]
|
75 |
|
76 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
query_check = ChatPromptTemplate.from_messages([
|
78 |
-
("system",
|
79 |
("placeholder", "{messages}")
|
80 |
]) | llm.bind_tools([db_query_tool])
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
query_gen = ChatPromptTemplate.from_messages([
|
83 |
-
("system",
|
84 |
("placeholder", "{messages}")
|
85 |
]) | llm.bind_tools([SubmitFinalAnswer])
|
86 |
except Exception as e:
|
|
|
57 |
except Exception as e:
|
58 |
flash(f"[ERROR]: Failed to connect to DB: {e}", "error")
|
59 |
raise
|
60 |
+
|
61 |
@tool
|
62 |
def db_query_tool(query: str) -> str:
|
63 |
+
"""
|
64 |
+
Execute a SQL query against the database and return the result.
|
65 |
+
If the query is invalid or returns no result, an error message will be returned.
|
66 |
+
In case of an error, the user is advised to rewrite the query and try again.
|
67 |
+
"""
|
68 |
try:
|
69 |
result = db_instance.run_no_throw(query)
|
70 |
return result or "Error: Query failed. Please rewrite your query and try again."
|
|
|
79 |
messages: Annotated[list[AnyMessage], add_messages]
|
80 |
|
81 |
try:
|
82 |
+
query_check_system = """You are a SQL expert with a strong attention to detail.
|
83 |
+
Double check the SQLite query for common mistakes, including:
|
84 |
+
- Using NOT IN with NULL values
|
85 |
+
- Using UNION when UNION ALL should have been used
|
86 |
+
- Using BETWEEN for exclusive ranges
|
87 |
+
- Data type mismatch in predicates
|
88 |
+
- Properly quoting identifiers
|
89 |
+
- Using the correct number of arguments for functions
|
90 |
+
- Casting to the correct data type
|
91 |
+
- Using the proper columns for joins
|
92 |
+
|
93 |
+
If there are any of the above mistakes, rewrite the query. If there are no mistakes, just reproduce the original query.
|
94 |
+
|
95 |
+
You will call the appropriate tool to execute the query after running this check.
|
96 |
+
"""
|
97 |
+
|
98 |
query_check = ChatPromptTemplate.from_messages([
|
99 |
+
("system", query_check_system),
|
100 |
("placeholder", "{messages}")
|
101 |
]) | llm.bind_tools([db_query_tool])
|
102 |
+
|
103 |
+
query_gen_system = """You are a SQL expert with a strong attention to detail.
|
104 |
+
|
105 |
+
Given an input question, output a syntactically correct SQLite query to run, then look at the results of the query and return the answer.
|
106 |
+
|
107 |
+
DO NOT call any tool besides SubmitFinalAnswer to submit the final answer.
|
108 |
+
|
109 |
+
When generating the query:
|
110 |
+
|
111 |
+
Output the SQL query that answers the input question without a tool call.
|
112 |
+
|
113 |
+
Unless the user specifies a specific number of examples they wish to obtain, always limit your query to at most 5 results.
|
114 |
+
You can order the results by a relevant column to return the most interesting examples in the database.
|
115 |
+
Never query for all the columns from a specific table, only ask for the relevant columns given the question.
|
116 |
+
|
117 |
+
If you get an error while executing a query, rewrite the query and try again.
|
118 |
+
|
119 |
+
If you get an empty result set, you should try to rewrite the query to get a non-empty result set.
|
120 |
+
NEVER make stuff up if you don't have enough information to answer the query... just say you don't have enough information.
|
121 |
+
|
122 |
+
If you have enough information to answer the input question, simply invoke the appropriate tool to submit the final answer to the user.
|
123 |
+
|
124 |
+
DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database. Do not return any sql query except answer.
|
125 |
+
"""
|
126 |
+
|
127 |
+
|
128 |
query_gen = ChatPromptTemplate.from_messages([
|
129 |
+
("system", query_gen_system),
|
130 |
("placeholder", "{messages}")
|
131 |
]) | llm.bind_tools([SubmitFinalAnswer])
|
132 |
except Exception as e:
|