How To Connect SQL Database To LangChain?

Asked 2 months ago
Answer 1
Viewed 166
1

Introduction

Lately, Huge Language Models (LLMs) have become dramatically well known because of their noteworthy capacities in creating lucid and logically significant text across a great many spaces. Despite the fact that LLMs are utilized for addressing questions, aiding examination, and assisting engineers in programming improvement, one of the significant shortcomings LLMs with having shown is their capacity to produce wrong or silly text, otherwise called "visualization." For instance, assuming you ask OpenAI's ChatGPT: "When did France gift Lithuania Vilnius television Pinnacle?", ChatGPT could answer: "France gifted Lithuania Vilnius television Pinnacle in 1980", which is authentically false since France didn't have anything to do with the development of the Vilnius television Pinnacle.

One justification for why LLMs can return such certain falsehoods is that LLMs will endeavor to conflate various wellsprings of data on the web to create a reaction that is wrong or deluding. One more justification for LLMs' visualization is that the wellspring of data isn't exact and that the LLM will utilize the data without approval. To assist with lessening LLM visualization for a particular space, we can endeavor to interface a LLM to a SQL data set which holds exact organized data to be questioned by the LLM. This will make the LLM center around a solitary hotspot for its data extraction, which permits the LLM to return the most reliable data conceivable given by the data set.

This article will exhibit how to utilize a LLM with a SQL information base by interfacing OpenAI's GPT-3.5 to a postgres data set. We will involve LangChain for our structure and will write in Python.

1. Getting started

Allow us to introduce the necessary bundles first, ensure you have previously introduced postgreSQL on your machine and have an OpenAI account too. Establish another python virtual climate if necessary:

pip install langchain 
pip install openai
pip install psycopg2

Create a file called main.py and import the following:

from langchain import OpenAI, SQLDatabase
from langchain.chains import SQLDatabaseSequentialChain

SQLDatabaseSequentialChain is a chain for questioning SQL information base that is a consecutive chain. Furthermore, as per the LangChain documentation, the chain is as per the following:

1. Based on the query, determine which tables to use.
2. Based on those tables, call the normal SQL database chain.

This is valuable for our situation since the quantity of tables in our data set is enormous.

For more modest information bases, you can simply utilize SQLDatabaseChain from LangChain.

2. Connect the database

Before we can interface the data set to our LLM, let us initially get a data set to interface with. Since LangChain utilizes SQLAlchemy to interface with SQL information bases, we can utilize any SQL vernacular upheld by SQLAlchemy, like MS SQL, MySQL, MariaDB, PostgreSQL, Prophet SQL, Databricks, or SQLite. Assuming you might want to find out about the prerequisites for interfacing with data sets, kindly allude to the SQLAlchemy documentation here. In my model I will utilize Dataherald's postgres real_estate data set. This data set contains 16 tables about lease, deals, stock, and different other land data for areas across the US in the two or three years. We will associate our LLM to this data set in endeavor to respond to land inquiries in the US.

The postgres data set association with psycopg2 seems to be the accompanying string:

Answered 2 months ago Wilman Kala