Published 21 days ago
This workflow is a modification of the previous template on how to create an SQL agent with LangChain and SQLite.
The key difference β the agent has access only to the database schema, not to the actual data. To achieve this, SQL queries are made outside the AI Agent node, and the results are never passed back to the agent.
This approach allows the agent to generate SQL queries based on the structure of tables and their relationships, without having to access the actual data.
This makes the process more secure and efficient, especially in cases where data confidentiality is crucial.
To get started with this workflow, youβll need to set up a free MySQL server and import your database (check Step 1 and 2 in this tutorial).
Of course, you can switch MySQL to another SQL database such as PostgreSQL, the principle remains the same. The key is to download the schema once and save it locally to avoid repeated remote connections.
Run the top part of the workflow once to download and store the MySQL chinook database schema file on the server.
With this approach, we avoid the need to repeatedly connect to a remote db4free database and fetch the schema every time. As a result, we reach greater processing speed and efficiency.
Try these sample queries to see the schema-driven AI Agent in action:
Would you please list me all customers from Germany?
What are the music genres in the database?
What tables are available in the database?
Please describe the relationships between tables. - In this example, the AI Agent does not need to create the SQL query.
And if you prefer to keep the data private, you can manually execute the generated SQL query in your own environment using any database client or tool you trust ποΈ
π The AI Agent memory node does not store the actual data as we run SQL-queries outside the agent. It contains the database schema, user questions and the initial Agent reply. Actual SQL query results are passed to the chat window, but the values are not stored in the Agent memory.
Implement complex processes faster with n8n