SQLAgent
SQLAgent lets you turn a language model into a smart SQL helper.
You give it a database connection, a model, and optional extra tools, and it builds an agent that can understand natural‑language questions and turn them into SQL queries, run them, and return the results.
How it Works
When you add SQLAgent to your dashboard, it first pulls the database URI from the credentials you supplied.
It then creates a SQLDatabase object that knows how to talk to that database.
A SQLDatabaseToolkit is built around that database, giving the agent a set of ready‑made tools like “Run a query” or “Get table schema.”
The component then combines the toolkit with your chosen language model and any extra tools you added to create an AgentExecutor.
When you run the agent, it reads the user’s natural‑language input, decides which tool to use, generates the SQL, executes it, and returns the result.
Inputs
Input Fields
- Database Info: Provide the database connection details that the agent will use to run queries. This includes credentials and connection string.
- Extra Tools: Add any additional tools that the agent can use besides the built‑in SQL tools. These can be custom actions or utilities.
- Model: Select the language model that the agent will use to understand and generate natural language queries.
- Handle Parse Errors: Choose how the agent should react when it cannot parse a query or encounters an error. Options include retrying, skipping, or reporting.
- Input: The natural language question or command that you want the agent to process.
- Max Iterations: Set the maximum number of times the agent can loop while trying to answer a query before giving up.
- Verbose: Toggle detailed logging for debugging and monitoring the agent’s behavior.
- Tool Name: The name that will appear to the agent when it selects this component as a tool.
- Tool Description: A clear description of what the tool does, helping the agent decide when to use it.
- Tools arguments metadata: Define the metadata for the arguments that the tool expects, such as data types and descriptions.
Outputs
- Agent: The fully configured AgentExecutor that can be run to process queries.
- Response: The textual answer or result returned by the agent after processing the input.
- Tool: A Tool object that can be added to other agents or workflows.
Usage Example
- Add SQLAgent to your workflow.
- Connect Database Info – paste the connection string or select a stored credential.
- Select a Model – choose the language model you want to use (e.g., GPT‑4).
- (Optional) Add Extra Tools – e.g., a custom “Send Email” tool if you want the agent to notify someone after a query.
- Set Max Iterations to 5 and Verbose to true for debugging.
- Run the agent with an input like:
What were the top 5 sales in the last month?
- The agent will generate the SQL, execute it, and return the results in the Response output.
- You can then feed that response into another component, such as a chart or email sender.
Related Components
- LCAgentComponent – Base component that provides common agent functionality.
- SQLDatabaseToolkit – Supplies ready‑made SQL tools for the agent.
- AgentAsyncHandler – Handles asynchronous callbacks and logging.
- SQLDatabase – Utility for connecting to SQL databases.
Tips and Best Practices
- Keep your database credentials secure; use Nappai’s credential manager.
- Limit Max Iterations to avoid runaway loops.
- Enable Verbose only when troubleshooting; it can produce large logs.
- Test the agent with simple queries first before moving to complex reports.
- If you add custom tools, provide clear descriptions so the agent knows when to use them.
Security Considerations
- Store database credentials in encrypted vaults and grant the agent only the permissions it needs.
- Use parameterized queries to prevent SQL injection; the toolkit handles this automatically.
- Monitor the agent’s logs for unexpected queries or errors.
- If the agent can send emails or other external actions, ensure those tools are also secured and audited.