
LANGCHAIN — How to Safely Query Enterprise Data with Langchain Agents SQL OpenAI Gretel
Software is like entropy: It is difficult to grasp, weighs nothing, and obeys the Second Law of Thermodynamics; i.e., it always increases. — Norman Augustine.
In this tutorial, I will guide you through the process of safely querying enterprise data using LangChain Agents, SQL, and OpenAI Gretel. We’ll cover the key technologies involved, explain the concept of LangChain agents, generating synthetic tabular data, and querying the data. We will also demonstrate the importance of privacy and how using synthetic data can protect sensitive information.
Key Technologies
Before we begin, let’s understand the key technologies involved in this process:
- LLM Chains: Frameworks such as LangChain for developing applications powered by language models by chaining them together.
- Agents: These use an LLM to decide what actions to take and the order to take them in, making future decisions by iteratively observing the outcome of prior actions.
- Function Aware LLMs: Certain LLMs have been fine-tuned to detect when a function should be called and respond with the inputs that should be passed to the function.
- Synthetic Data: An artificial version of real-world data created by data-aware generative models that offer strong privacy guarantees.
- SQL Databases: The backbone holding the data you’ll be querying. For this tutorial, we’ll use a SQLite database.
What is an Agent in LangChain?
The LangChain Agent uses a MRKL-based approach to query the database schema and example rows, and uses these to generate SQL queries, which it then executes to pull back the results you’re asking for.
Generating Synthetic Tabular Data
Before diving into the example, let’s talk about synthetic data. With Gretel’s models, you can create an artificial but statistically similar version of your sensitive data. This synthetic data is safe to use, thanks to privacy features like differential privacy.
To get started with Gretel’s synthetic data, you can use the IBM HR Employee Attrition dataset and an API key from Gretel’s console. You can then run Gretel’s quickstart notebook to create a synthetic version of the data.
Initializing the LangChain Agent
First, you need to install the necessary dependencies. Run the following command to install the required packages:
!pip install -Uqq langchain openai gretel-client
!pip install -Uqq smart_open tabulateNext, initialize the LangChain Agent and connect it to your SQL database using the following code snippet:
from langchain.agents import AgentExecutor, create_sql_agent
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.agents.agent_types import AgentType
from langchain.chat_models import ChatOpenAI
from langchain.llms.openai import OpenAI
from langchain.sql_database import SQLDatabase
def create_agent(db_uri, agent_type=AgentType.OPENAI_FUNCTIONS, verbose=VERBOSE_LANGCHAIN, temperature=0, model="gpt-3.5-turbo-0613"):
db = SQLDatabase.from_uri(db_uri)
toolkit = SQLDatabaseToolkit(db=db, llm=OpenAI(temperature=temperature))
return create_sql_agent(llm=ChatOpenAI(temperature=temperature, model=model), toolkit=toolkit, verbose=verbose, agent_type=agent_type)Querying the Data
After initializing the LangChain Agent, you can run queries on the data. Here’s a sample function to compare the outputs of real data and synthetic data:
def run_and_compare_queries(synthetic, real, query: str):
query_template = f"{query} Execute all necessary queries, and always return results to the query, no explanations or apologies please. Word wrap output every 50 characters."
result1 = synthetic.run(query_template)
result2 = real.run(query_template)
print("=== Comparing Results for Query ===")
print(f"Query: {query}")
table_data = [{"From Agent on Synthetic DB": result1, "From Agent on Real DB": result2}]
print(tabulate(table_data, headers="keys", tablefmt="pretty"))Sample Queries
Here are a few sample queries you can run using the LangChain Agent:
- Which three departments have the highest attrition rates?
prompt = "Which 3 departments have the highest attrition rates? Return a list please."
run_and_compare_queries(synthetic=agent_synthetic_db, real=agent_real_db, query=prompt)




