Skip to main content
This project involves setting up and running a system of agents to conduct SQL queries, write the output to a file, and plot graphs based on the data. We use Composio to set up the tools and OpenAI GPT-4o to power the agents. Follow this guide to set up and run the project. Note: This is a Python only example. JS version coming soon!

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 system using Langchain and Composio.
1

Imports and Environment Setup

In your Python script, import the necessary libraries and set up your environment variables.
import os
import dotenv
from composio_langchain import App, ComposioToolSet
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_openai import ChatOpenAI

# Load environment variables
dotenv.load_dotenv()
2

Initialize Language Model and Define Tools

Initialize the language model with OpenAI API key and model name, and set up the necessary tools for the agents.
llm = ChatOpenAI(model="gpt-4o")

# Initialize the Composio ToolSet
composio_toolset = ComposioToolSet()

# Get tools for SQL and File operations
sql_file_tool = composio_toolset.get_tools(apps=[App.SQLTOOL, App.FILETOOL])

# Get tools for SQL, File, and Code Interpreter operations
tools = composio_toolset.get_tools(apps=[App.SQLTOOL, App.FILETOOL, App.CODEINTERPRETER])
3

Define the Task and Execute SQL Query

Define the task to execute a SQL query and write the output to a file.
# Pull the prompt template for the agent
prompt = hub.pull("hwchase17/openai-functions-agent")

# Define the task to execute
query_task = (
    "Write sqlite query to get top 10 rows from the only table MOCK_DATA "
    "and database companydb using sqltool, write the output in a file called log.txt and return the output"
)

# Create the agent for SQL and File operations and execute the task
query_agent = create_openai_functions_agent(llm, sql_file_tool, prompt)
agent_executor = AgentExecutor(agent=query_agent, tools=sql_file_tool, verbose=True)
res = agent_executor.invoke({"input": query_task})
4

Define and Execute Code Interpreter Task

Create and execute the agent for Code Interpreter operations to plot a graph based on the extracted data.
# Create the agent for Code Interpreter operations
code_tool = composio_toolset.get_tools(apps=[App.CODEINTERPRETER])
code_agent = create_openai_functions_agent(llm, code_tool, prompt)
agent_executor = AgentExecutor(agent=code_agent, tools=code_tool, verbose=True)

# Define the task for plotting graphs
plot_task = (
    "Using the following extracted information, plot the graph between first name and salary: "
    + res["output"]
)

# Execute the plotting task
final_res = agent_executor.invoke({"input": plot_task})

Putting it All Together

import os
import dotenv
from composio_langchain import App, ComposioToolSet
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_openai import ChatOpenAI

# Load environment variables
dotenv.load_dotenv()

# Initialize the LLM with the OpenAI GPT-4o model and API key
llm = ChatOpenAI(model="gpt-4o")

# Initialize the Composio ToolSet
composio_toolset = ComposioToolSet()

# Get tools for SQL and File operations
sql_file_tool = composio_toolset.get_tools(apps=[App.SQLTOOL, App.FILETOOL])

# Get tools for SQL, File, and Code Interpreter operations
tools = composio_toolset.get_tools(apps=[App.SQLTOOL, App.FILETOOL, App.CODEINTERPRETER])

# Pull the prompt template for the agent
prompt = hub.pull("hwchase17/openai-functions-agent")

# Define the task to execute
query_task = (
    "Write sqlite query to get top 10 rows from the only table MOCK_DATA "
    "and database companydb using sqltool, write the output in a file called log.txt and return the output"
)

# Create the agent for SQL and File operations and execute the task
query_agent = create_openai_functions_agent(llm, sql_file_tool, prompt)
agent_executor = AgentExecutor(agent=query_agent, tools=sql_file_tool, verbose=True)
res = agent_executor.invoke({"input": query_task})

# Create the agent for Code Interpreter operations
code_tool = composio_toolset.get_tools(apps=[App.CODEINTERPRETER])
code_agent = create_openai_functions_agent(llm, code_tool, prompt)
agent_executor = AgentExecutor(agent=code_agent, tools=code_tool, verbose=True)

# Define the task for plotting graphs
plot_task = (
    "Using the following extracted information, plot the graph between first name and salary: "
    + res["output"]
)

# Execute the plotting task
final_res = agent_executor.invoke({"input": plot_task})

# Print the final result
print(final_res)