SQL Agent
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
1 pip 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.
1 import os 2 import dotenv 3 4 from composio_langchain import App, ComposioToolSet 5 from langchain import hub 6 from langchain.agents import AgentExecutor, create_openai_functions_agent 7 from langchain_openai import ChatOpenAI 8 9 # Load environment variables 10 dotenv.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 2 llm = ChatOpenAI(model="gpt-4o") 3 4 # Initialize the Composio ToolSet 5 composio_toolset = ComposioToolSet() 6 7 # Get tools for SQL and File operations 8 sql_file_tools = composio_toolset.get_tools(apps=[App.SQLTOOL, App.FILETOOL]) 9 10 # Get tools for code interpreter (for plotting graphs) 11 code_tools = composio_toolset.get_tools(apps=[App.CODEINTERPRETER]) 12 13 # Get all tools combined 14 all_tools = composio_toolset.get_tools(apps=[App.SQLTOOL, App.FILETOOL, App.CODEINTERPRETER]) 15 16 # Pull the prompt template for the agent 17 prompt = 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 2 sql_agent = create_openai_functions_agent(llm, sql_file_tools, prompt) 3 sql_executor = AgentExecutor(agent=sql_agent, tools=sql_file_tools, verbose=True) 4 5 # Define the SQL query task 6 query_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 13 sql_result = sql_executor.invoke({"input": query_task}) 14 print(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 2 visualization_agent = create_openai_functions_agent(llm, code_tools, prompt) 3 visualization_executor = AgentExecutor(agent=visualization_agent, tools=code_tools, verbose=True) 4 5 # Define the visualization task 6 visualization_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 13 visualization_result = visualization_executor.invoke({"input": visualization_task}) 14 print(f"Visualization Result: {visualization_result['output']}")
6
Putting Everything Together
Execute the full workflow combining SQL queries and data visualization.
1 # Execute the workflow 2 def 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 18 if __name__ == "__main__": 19 main()
Complete Code
1 import os 2 import dotenv 3 4 from composio_langchain import App, ComposioToolSet 5 from langchain import hub 6 from langchain.agents import AgentExecutor, create_openai_functions_agent 7 from langchain_openai import ChatOpenAI 8 9 # Load environment variables 10 dotenv.load_dotenv() 11 12 # Initialize the language model 13 llm = ChatOpenAI(model="gpt-4o") 14 15 # Initialize the Composio ToolSet 16 composio_toolset = ComposioToolSet() 17 18 # Get tools for SQL and File operations 19 sql_file_tools = composio_toolset.get_tools(apps=[App.SQLTOOL, App.FILETOOL]) 20 21 # Get tools for code interpreter (for plotting graphs) 22 code_tools = composio_toolset.get_tools(apps=[App.CODEINTERPRETER]) 23 24 # Pull the prompt template for the agent 25 prompt = hub.pull("hwchase17/openai-functions-agent") 26 27 # Create the agent for SQL and File operations 28 sql_agent = create_openai_functions_agent(llm, sql_file_tools, prompt) 29 sql_executor = AgentExecutor(agent=sql_agent, tools=sql_file_tools, verbose=True) 30 31 # Create the agent for data visualization 32 visualization_agent = create_openai_functions_agent(llm, code_tools, prompt) 33 visualization_executor = AgentExecutor(agent=visualization_agent, tools=code_tools, verbose=True) 34 35 # Define the SQL query task 36 query_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 42 def 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 58 if __name__ == "__main__": 59 main()