SQL Agent GitHub Repository

Explore the complete source code for the SQL Agent project. This repository contains all the necessary files and scripts to set up and run the SQL Agent using Composio.

1

Install the required packages

1pip install composio-langchain langchain-openai

Create a .env file and add your OpenAI API Key.

2

Import base packages

Next, we’ll import the essential libraries for our project.

1import os
2import dotenv
3
4from composio_langchain import App, ComposioToolSet
5from langchain import hub
6from langchain.agents import AgentExecutor, create_openai_functions_agent
7from langchain_openai import ChatOpenAI
8
9# Load environment variables
10dotenv.load_dotenv()
3

Initialize Language Model and Define Tools

Set up the language model and define the tools needed for SQL operations.

1# Initialize the language model
2llm = ChatOpenAI(model="gpt-4o")
3
4# Initialize the Composio ToolSet
5composio_toolset = ComposioToolSet()
6
7# Get tools for SQL and File operations
8sql_file_tools = composio_toolset.get_tools(apps=[App.SQLTOOL, App.FILETOOL])
9
10# Get tools for code interpreter (for plotting graphs)
11code_tools = composio_toolset.get_tools(apps=[App.CODEINTERPRETER])
12
13# Get all tools combined
14all_tools = composio_toolset.get_tools(apps=[App.SQLTOOL, App.FILETOOL, App.CODEINTERPRETER])
15
16# Pull the prompt template for the agent
17prompt = hub.pull("hwchase17/openai-functions-agent")
4

Set up the SQL Query Agent

Create an agent to execute SQL queries and save results.

1# Create the agent for SQL and File operations
2sql_agent = create_openai_functions_agent(llm, sql_file_tools, prompt)
3sql_executor = AgentExecutor(agent=sql_agent, tools=sql_file_tools, verbose=True)
4
5# Define the SQL query task
6query_task = (
7 "Connect to the SQLite database 'companydb' and execute the following query: "
8 "SELECT * FROM MOCK_DATA ORDER BY salary DESC LIMIT 10. "
9 "Save the results to a file named 'top_salaries.txt' and return the content."
10)
11
12# Execute the SQL query task
13sql_result = sql_executor.invoke({"input": query_task})
14print(f"SQL Query Result: {sql_result['output']}")
5

Set up the Data Visualization Agent

Create an agent to process data and generate visualizations.

1# Create the agent for data visualization
2visualization_agent = create_openai_functions_agent(llm, code_tools, prompt)
3visualization_executor = AgentExecutor(agent=visualization_agent, tools=code_tools, verbose=True)
4
5# Define the visualization task
6visualization_task = (
7 f"Using the following data from the top 10 highest salaries: {sql_result['output']}\n"
8 "Create a bar chart showing first_name on the x-axis and salary on the y-axis. "
9 "Save the visualization as 'salary_chart.png'."
10)
11
12# Execute the visualization task
13visualization_result = visualization_executor.invoke({"input": visualization_task})
14print(f"Visualization Result: {visualization_result['output']}")
6

Putting Everything Together

Execute the full workflow combining SQL queries and data visualization.

1# Execute the workflow
2def main():
3 # First, execute SQL query
4 sql_result = sql_executor.invoke({"input": query_task})
5 print(f"SQL Query Result: {sql_result['output']}")
6
7 # Then, create visualization
8 visualization_task = (
9 f"Using the following data from the top 10 highest salaries: {sql_result['output']}\n"
10 "Create a bar chart showing first_name on the x-axis and salary on the y-axis. "
11 "Save the visualization as 'salary_chart.png'."
12 )
13 visualization_result = visualization_executor.invoke({"input": visualization_task})
14 print(f"Visualization Result: {visualization_result['output']}")
15
16 return "Workflow completed successfully!"
17
18if __name__ == "__main__":
19 main()

Complete Code

1import os
2import dotenv
3
4from composio_langchain import App, ComposioToolSet
5from langchain import hub
6from langchain.agents import AgentExecutor, create_openai_functions_agent
7from langchain_openai import ChatOpenAI
8
9# Load environment variables
10dotenv.load_dotenv()
11
12# Initialize the language model
13llm = ChatOpenAI(model="gpt-4o")
14
15# Initialize the Composio ToolSet
16composio_toolset = ComposioToolSet()
17
18# Get tools for SQL and File operations
19sql_file_tools = composio_toolset.get_tools(apps=[App.SQLTOOL, App.FILETOOL])
20
21# Get tools for code interpreter (for plotting graphs)
22code_tools = composio_toolset.get_tools(apps=[App.CODEINTERPRETER])
23
24# Pull the prompt template for the agent
25prompt = hub.pull("hwchase17/openai-functions-agent")
26
27# Create the agent for SQL and File operations
28sql_agent = create_openai_functions_agent(llm, sql_file_tools, prompt)
29sql_executor = AgentExecutor(agent=sql_agent, tools=sql_file_tools, verbose=True)
30
31# Create the agent for data visualization
32visualization_agent = create_openai_functions_agent(llm, code_tools, prompt)
33visualization_executor = AgentExecutor(agent=visualization_agent, tools=code_tools, verbose=True)
34
35# Define the SQL query task
36query_task = (
37 "Connect to the SQLite database 'companydb' and execute the following query: "
38 "SELECT * FROM MOCK_DATA ORDER BY salary DESC LIMIT 10. "
39 "Save the results to a file named 'top_salaries.txt' and return the content."
40)
41
42def main():
43 # First, execute SQL query
44 sql_result = sql_executor.invoke({"input": query_task})
45 print(f"SQL Query Result: {sql_result['output']}")
46
47 # Then, create visualization
48 visualization_task = (
49 f"Using the following data from the top 10 highest salaries: {sql_result['output']}\n"
50 "Create a bar chart showing first_name on the x-axis and salary on the y-axis. "
51 "Save the visualization as 'salary_chart.png'."
52 )
53 visualization_result = visualization_executor.invoke({"input": visualization_task})
54 print(f"Visualization Result: {visualization_result['output']}")
55
56 return "Workflow completed successfully!"
57
58if __name__ == "__main__":
59 main()
Built with