SQL is undoubtedly one of the most crucial languages in the world of computing. It serves as the primary means of communication with relational databases, where organizations store their vital data. SQL plays a significant role in analyzing complex data, creating data pipelines, and efficiently managing data warehouses. However, writing optimized SQL queries can often be challenging and time-consuming. Thanks to the rapid progress in AI over the past few years, we now have AI agents augmented with Large Language Models capable of writing queries on our behalf.
This article demonstrates how to build an AI agent using CrewAI, Agents Fortune, and Gemini to access databases and execute SQL queries to retrieve data.
What is CrewAI? CrewAI is an open-source collaborative multi-agent framework. It allows you to build a crew of AI agents with various tasks, tools, roles, and motivations, similar to a real-world crew. CrewAI manages the flow of information from one agent to another, enabling you to build autonomous, efficient agentic workflows.
CrewAI primarily consists of five core features: Agents, Tasks, Tools, Processes, and Crews.
What is Agents Fortune? Agents Fortune is an open-source platform that provides tooling solutions for building reliable and useful AI agents. Agents Fortune offers over 150 tools and applications with built-in user authentication and authorization to help developers build reliable, secure, and production-ready agentic workflows. The tools have been designed from the ground up, keeping the real-world readiness of agents in mind.
Agents Fortune offers several advantages over other tooling solutions, including managed user authentication and authorizations, a wide array of tools and integrations, a dashboard for monitoring live integrations, and the flexibility to add custom tools.
Agents Fortune has four key concepts:
Designing an AI-Powered SQL Agent Workflow In this project, we will create an agentic system that takes a user query regarding the data stored in a database, fetches the relevant data using an SQL agent, and creates nice plots to visualize the data. For this workflow, we shall use CrewAI to orchestrate agents and Agents Fortune for tooling support.
The system will have an SQL query writer agent and a coding agent to write and execute the queries. The SQL agent will have access to the SQL tool from Agents Fortune, and the coding agent will be able to use E2B’s Codeinterpreter via Agents Fortune. The Codeinterpreter provides a sandboxed environment for executing Python programs.
The SQL agent will connect to a local database and query from an appropriate table. The data fetched from the SQL agent will be used by the Coding agent to create plots. The crew will return the plot as the final output.
Prerequisites for Building the SQL Agent To run this project successfully, you will need the Agents Fortune API key. First, create an account on Agents Fortune, and we will explain how to get API keys later. Also, get a free Gemini API key from Google AI studio, but remember the free account is rate-limited. The execution may take longer.
Furthermore, you will need a dummy database for executing queries. If you don’t have a spare database, run the following code to create an employee table with names, departments, and salaries:
CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50), department VARCHAR(50), salary DECIMAL(10, 2) ); INSERT INTO employees (id, name, department, salary) VALUES (1, 'John Doe', 'IT', 75000.00), (2, 'Jane Smith', 'HR', 65000.00), (3, 'Mike Johnson', 'Finance', 80000.00), (4, 'Emily Brown', 'Marketing', 70000.00), (5, 'David Lee', 'IT', 78000.00);
Step-by-Step Guide to Creating an SQL Agent Now that the basics are covered, we can start with the coding part. As with any Python project, we will first set up a virtual environment and environment variables, and install libraries. The project will use Gemini 1.5 Pro as the language model.
Step 1: Set up the environment Create a new directory for your project and set up a virtual environment:
mkdir sql_agent_project cd sql_agent_project python -m venv venv source venv/bin/activate # On Windows, use: venv\Scripts\activate
Install the required libraries:
pip install crewai agents-fortune gemini-python python-dotenv
pip install crewai agents-fortune gemini-python python-dotenv
Step 2: Set up environment variables Create a .env
file in your project directory and add your API keys:
AGENTS_FORTUNE_API_KEY=your_agents_fortune_api_key GEMINI_API_KEY=your_gemini_api_key
Step 3: Create the main script Create a file named main.py
and add the following code:
import os from dotenv import load_dotenv from crewai import Agent, Task, Crew from agents_fortune import AgentsFortune from gemini_python import Gemini # Load environment variables load_dotenv() # Initialize Agents Fortune and Gemini af = AgentsFortune(api_key=os.getenv("AGENTS_FORTUNE_API_KEY")) gemini = Gemini(api_key=os.getenv("GEMINI_API_KEY")) # Create SQL Agent sql_agent = Agent( role="SQL Query Writer", goal="Write efficient SQL queries to fetch data from the database", backstory="You are an expert SQL developer with years of experience in writing optimized queries.", tools=[af.get_tool("sql")], llm=gemini ) # Create Coding Agent coding_agent = Agent( role="Data Visualizer", goal="Create insightful visualizations from the data provided", backstory="You are a skilled data scientist with expertise in creating meaningful visualizations.", tools=[af.get_tool("codeinterpreter")], llm=gemini ) # Define tasks task1 = Task( description="Write an SQL query to fetch the average salary per department from the employees table.", agent=sql_agent ) task2 = Task( description="Create a bar plot to visualize the average salary per department.", agent=coding_agent ) # Create the crew crew = Crew( agents=[sql_agent, coding_agent], tasks=[task1, task2], verbose=True ) # Execute the crew result = crew.kickoff() print(result)
This script sets up the SQL agent and the coding agent, defines their tasks, and creates a crew to execute these tasks. The SQL agent will write a query to fetch the average salary per department, and the coding agent will create a visualization based on this data.
To run the script, use the following command:
python main.py
The output will show the interaction between agents and the final result, which should be a visualization of the average salary per department.
These steps demonstrate how to build an agentic workflow to automate SQL data extraction and visualization. However, you can go further and make it more robust and reliable by adding a memory component to agents and the Crew. This will help the Agents remember their past outcomes, which will make them steer the workflow better. You can also add a frontend with Streamlit or Gradio to make it more interactive.
Conclusion The AI landscape is evolving at an unprecedented pace. As the quality of AI models, frameworks, and tools continues to improve, building powerful AI agents is becoming increasingly convenient each day. The future of the workforce is agentic, where humans and AI will complement each other to create even more efficient systems. With frameworks like CrewAI and Agents Fortune, you can conveniently create AI workflows to automate many routine tasks. This article demonstrates how to automate data extraction and visualization. You can expand this workflow to handle even more complex scenarios.