Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -137,78 +137,78 @@ DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the databa
|
|
| 137 |
query_gen_prompt = ChatPromptTemplate.from_messages([("system", query_gen_system), ("placeholder", "{messages}")])
|
| 138 |
query_gen = query_gen_prompt | llm.bind_tools([SubmitFinalAnswer])
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
)
|
| 169 |
)
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
| 212 |
|
| 213 |
###############################################################################
|
| 214 |
# Application Factory: create_app()
|
|
|
|
| 137 |
query_gen_prompt = ChatPromptTemplate.from_messages([("system", query_gen_system), ("placeholder", "{messages}")])
|
| 138 |
query_gen = query_gen_prompt | llm.bind_tools([SubmitFinalAnswer])
|
| 139 |
|
| 140 |
+
# Define workflow nodes and fallback functions
|
| 141 |
+
def first_tool_call(state: State) -> dict[str, list[AIMessage]]:
|
| 142 |
+
return {"messages": [AIMessage(content="", tool_calls=[{"name": "sql_db_list_tables", "args": {}, "id": "tool_abcd123"}])]}
|
| 143 |
+
|
| 144 |
+
def handle_tool_error(state: State) -> dict:
|
| 145 |
+
error = state.get("error")
|
| 146 |
+
tool_calls = state["messages"][-1].tool_calls
|
| 147 |
+
return {
|
| 148 |
+
"messages": [
|
| 149 |
+
ToolMessage(content=f"Error: {repr(error)}\n please fix your mistakes.", tool_call_id=tc["id"])
|
| 150 |
+
for tc in tool_calls
|
| 151 |
+
]
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
def create_tool_node_with_fallback(tools_list: list) -> RunnableWithFallbacks[Any, dict]:
|
| 155 |
+
return ToolNode(tools_list).with_fallbacks([RunnableLambda(handle_tool_error)], exception_key="error")
|
| 156 |
+
|
| 157 |
+
def query_gen_node(state: State):
|
| 158 |
+
message = query_gen.invoke(state)
|
| 159 |
+
# Check for incorrect tool calls
|
| 160 |
+
tool_messages = []
|
| 161 |
+
if message.tool_calls:
|
| 162 |
+
for tc in message.tool_calls:
|
| 163 |
+
if tc["name"] != "SubmitFinalAnswer":
|
| 164 |
+
tool_messages.append(
|
| 165 |
+
ToolMessage(
|
| 166 |
+
content=f"Error: The wrong tool was called: {tc['name']}. Please fix your mistakes. Remember to only call SubmitFinalAnswer to submit the final answer. Generated queries should be outputted WITHOUT a tool call.",
|
| 167 |
+
tool_call_id=tc["id"],
|
|
|
|
| 168 |
)
|
| 169 |
+
)
|
| 170 |
+
return {"messages": [message] + tool_messages}
|
| 171 |
+
|
| 172 |
+
def should_continue(state: State) -> Literal[END, "correct_query", "query_gen"]:
|
| 173 |
+
messages = state["messages"]
|
| 174 |
+
last_message = messages[-1]
|
| 175 |
+
if getattr(last_message, "tool_calls", None):
|
| 176 |
+
return END
|
| 177 |
+
if last_message.content.startswith("Error:"):
|
| 178 |
+
return "query_gen"
|
| 179 |
+
else:
|
| 180 |
+
return "correct_query"
|
| 181 |
+
|
| 182 |
+
def model_check_query(state: State) -> dict[str, list[AIMessage]]:
|
| 183 |
+
"""Double-check if the query is correct before executing it."""
|
| 184 |
+
return {"messages": [query_check.invoke({"messages": [state["messages"][-1]]})]}
|
| 185 |
+
|
| 186 |
+
# Get tools for listing tables and fetching schema
|
| 187 |
+
list_tables_tool = next((tool for tool in tools_instance if tool.name == "sql_db_list_tables"), None)
|
| 188 |
+
get_schema_tool = next((tool for tool in tools_instance if tool.name == "sql_db_schema"), None)
|
| 189 |
+
|
| 190 |
+
# Define the workflow (state graph)
|
| 191 |
+
workflow = StateGraph(State)
|
| 192 |
+
workflow.add_node("first_tool_call", first_tool_call)
|
| 193 |
+
workflow.add_node("list_tables_tool", create_tool_node_with_fallback([list_tables_tool]))
|
| 194 |
+
workflow.add_node("get_schema_tool", create_tool_node_with_fallback([get_schema_tool]))
|
| 195 |
+
model_get_schema = llm.bind_tools([get_schema_tool])
|
| 196 |
+
workflow.add_node("model_get_schema", lambda state: {"messages": [model_get_schema.invoke(state["messages"])],})
|
| 197 |
+
workflow.add_node("query_gen", query_gen_node)
|
| 198 |
+
workflow.add_node("correct_query", model_check_query)
|
| 199 |
+
workflow.add_node("execute_query", create_tool_node_with_fallback([db_query_tool]))
|
| 200 |
+
|
| 201 |
+
workflow.add_edge(START, "first_tool_call")
|
| 202 |
+
workflow.add_edge("first_tool_call", "list_tables_tool")
|
| 203 |
+
workflow.add_edge("list_tables_tool", "model_get_schema")
|
| 204 |
+
workflow.add_edge("model_get_schema", "get_schema_tool")
|
| 205 |
+
workflow.add_edge("get_schema_tool", "query_gen")
|
| 206 |
+
workflow.add_conditional_edges("query_gen", should_continue)
|
| 207 |
+
workflow.add_edge("correct_query", "execute_query")
|
| 208 |
+
workflow.add_edge("execute_query", "query_gen")
|
| 209 |
+
|
| 210 |
+
# Compile and return the agent application workflow
|
| 211 |
+
return workflow.compile()
|
| 212 |
|
| 213 |
###############################################################################
|
| 214 |
# Application Factory: create_app()
|