-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from theyashwanthsai/improvements/29
Improvements/29. Core Concepts page and Tools page have been changed.
- Loading branch information
Showing
17 changed files
with
347 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
--- | ||
title: "SQL Agent" | ||
sidebarTitle: "SQL Agent" | ||
icon: "database" | ||
description: "This guide provides detailed steps to create an agent that leverages | ||
Composio to perform SQL queries and file operations to perform sql queries and | ||
File operations and plot insightful graphs on the data in the db" | ||
--- | ||
|
||
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. | ||
|
||
<Steps> | ||
<Step title="Setup Environment and Install Dependencies"> | ||
> Ensure you have a `.env` file with your OpenAI and Composio API keys. | ||
```bash | ||
# Install the required packages | ||
pip install dotenv composio_langchain langchain_openai | ||
``` | ||
|
||
Fill in the `.env` file with your API keys: | ||
``` | ||
OPENAI_API_KEY=your_openai_api_key | ||
COMPOSIO_API_KEY=your_composio_api_key | ||
``` | ||
</Step> | ||
|
||
<Step title="Imports and Environment Setup"> | ||
In your Python script, import the necessary libraries and set up your environment variables. | ||
<CodeGroup> | ||
```python | ||
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() | ||
``` | ||
</CodeGroup> | ||
</Step> | ||
|
||
<Step title="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. | ||
<CodeGroup> | ||
```python | ||
# Initialize the LLM with the OpenAI GPT-4o model and API key | ||
llm = ChatOpenAI(model="gpt-4o", openai_api_key=os.getenv("OPENAI_API_KEY")) | ||
# Initialize the Composio ToolSet with the API key | ||
composio_toolset = ComposioToolSet(api_key=os.getenv("COMPOSIO_API_KEY")) | ||
# 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]) | ||
``` | ||
</CodeGroup> | ||
</Step> | ||
|
||
<Step title="Define the Task and Execute SQL Query"> | ||
Define the task to execute a SQL query and write the output to a file. | ||
<CodeGroup> | ||
```python | ||
# 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 company.db 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}) | ||
``` | ||
</CodeGroup> | ||
</Step> | ||
|
||
<Step title="Define and Execute Code Interpreter Task"> | ||
Create and execute the agent for Code Interpreter operations to plot a graph based on the extracted data. | ||
<CodeGroup> | ||
```python | ||
# 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}) | ||
``` | ||
</CodeGroup> | ||
</Step> | ||
|
||
</Steps> | ||
|
||
## Putting it All Together | ||
<CodeGroup> | ||
```python | ||
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", openai_api_key=os.getenv("OPENAI_API_KEY")) | ||
# Initialize the Composio ToolSet with the API key | ||
composio_toolset = ComposioToolSet(api_key=os.getenv("COMPOSIO_API_KEY")) | ||
# 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 company.db 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) | ||
``` | ||
|
||
</CodeGroup> | ||
|
||
## Link to the Source Code | ||
- [GitHub]() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
introduction/foundations/components/integrations/custom-integration.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.