
In this third blog, I am not leaving the same concept, leveraging existing LLMs and (for the moment) avoiding the pre-training topic. I firmly believe that enterprises will have their own LLMs using one of the existing models as a base. That's out of discussion, but the evolution of the LLMs is so intense that it will probably take some time to get to that point. Pre-training models is not an easy thing and is not cheap either. In this series of blogs, I am introducing a way to interact with existing LLMs with techniques that do not require pre-training the models on our own datasets. Then, in this blog, I present what I believe is an exciting topic: While building an end-to-end solution, where a conversation will end by accessing live data from another application. |
Natural language querying allows users to interact with databases; leveraging the power of LangChain, SQL Agents, and Large Language Models (LLMs), we can create applications that enable users to query databases using natural language.
LangChain is a framework designed for building applications powered by language models. It provides a standard chain interface, integrates with various tools, and offers end-to-end application chains. The two main features of LangChain are data-awareness and agentic behavior.
Data awareness enables the language model to connect to other data sources, while agentic behavior allows it to interact with its environment. Using agents, LangChain can dynamically decide which tools to call based on user input. This makes agents extremely powerful when used correctly.
SQLDatabaseChain
.Why use Agents on top of Chains The main advantages of using the SQL Agent are:
|
This blog uses Langchain to connect the application with LLM and External Data Sources, such as an Oracle DB for querying.
The SQL Database Agent from LangChain is designed to interact with any database, allowing users to ask questions in natural language and receive answers.
from langchain import Cohere, SQLDatabase, SQLDatabaseChain
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.agents.agent_types import AgentType
from langchain.agents import create_sql_agent
import cx_Oracle
import os
import cohere
import os
COHERE_API_KEY="Your Cohere API Key"
os.environ["COHERE_API_KEY"] = COHERE_API_KEY
from sqlalchemy import create_engine
engine=create_engine(url, echo=True)
db = SQLDatabase(engine)
lib_dir = os.path.join(os.environ.get("HOME"), "Development", "instantclient_19_8")
cx_Oracle.init_oracle_client(lib_dir=lib_dir)
hostname='localhost'
port='...'
service_name='...'
username='<...>'
password='<...>'
# cx_Oracle.init_oracle_client(lib_dir=lib_dir)
oracle_connection_string_fmt = (
'oracle+cx_oracle://{username}:{password}@' +
cx_Oracle.makedsn('{hostname}', '{port}', service_name='{service_name}')
)
url = oracle_connection_string_fmt.format(
username=username, password=password,
hostname=hostname, port=port,
service_name=service_name,
)
agent_executor = create_sql_agent(
llm=Cohere(temperature=0),
toolkit=SQLDatabaseToolkit(db=db, llm=Cohere(temperature=0)),
verbose=True,
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
)
With LlamaIndex, thanks to its data connectors, you can effortlessly incorporate data from diverse sources such as APIs, databases, and PDFs. This data is structured into optimized intermediate formats suitable for LLMs. LlamaIndex enables seamless interaction and conversation with your data through query engines, chat interfaces, and LLM-powered data agents, all in natural language.
In this blog, we introduced the concept and some details of using LangChain’s SQL Database Chain and Agents with large language models to perform natural language queries (NLQ) of any Phyton SQLAlchemy database. I wanted to emphasize Agents, a fundamental piece in all modern frameworks; LangChain uses Agents, LlamaIndex uses Agents, and Bedrock just introduced Agents.
Using LangChain’s SQL Database Chain and SQL Database Agent, we can leverage large language models (LLMs) to ask questions of multiple types of databases using natural language without building the query ourselves. Questions will be converted into SQL queries and executed against the database. Assuming the generated SQL query is well-formed, the query results will be converted into a textual explanation. For example, we ask questions like, “How many customers have purchased this Material in the last 12 months?” or “What were the total purchases we had in August for this company code?” These will be converted into SQL SELECT
statements. The answer is then composed into textual explanations as a response to our application.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
3 | |
2 | |
1 | |
1 | |
1 | |
1 |