@tool("list_tables")
def list_tables() -> str:
"""
List the available tables in the database.
This function returns a list of tables from the connected database.
Returns:
str: A string representation of the available tables in the database.
"""
db_tool = ListSQLDatabaseTool(db=db)
return db_tool.invoke("")
@tool("tables_schema")
def tables_schema(tables: str) -> str:
"""
Input is a comma-separated list of tables, output is the schema and sample rows
for those tables. Be sure that the tables actually exist by calling `list_tables` first!
Example Input: table1, table2, table3
"""
tool = InfoSQLDatabaseTool(db=db)
return tool.invoke(tables)
@tool("execute_sql")
def execute_sql(sql_query: str) -> str:
"""Execute a SQL query against the database. Returns the result"""
return QuerySQLDataBaseTool(db=db).invoke(sql_query)
sql_dev = Agent(
role="Senior Database Developer",
goal="Construct and execute SQL queries based on a request",
backstory=dedent(
"""
You are an experienced database engineer who is master at creating efficient and complex SQL queries.
You have a deep understanding of how different databases work and how to optimize queries.
Use the `list_tables` tool to find available tables.
Use the `tables_schema` tool to understand the metadata for the list of tables coming from the list_tables.
Use the `execute_sql` tool to execute queries against the database.
"""
),
llm=llm,
tools=[list_tables, tables_schema, execute_sql],
allow_delegation=False,
)
tables_schema tool is always failing giving wrong input tables:
I have only ‘salaries’ table.
Using tool: tables_schema
Tool Input:
“{"tables": "salaries, companies, employees, locations"}”
Tool Output:
Error: table_names {‘employees’, ‘locations’, ‘companies’} not found in database.