NL2SQL
Beyond using RAG for unstructured data retrieval, we can also use agents to analyze structured data sources. For this purpose, we can build an agent capable of querying a database and providing answers to user queries based on the results.
To work with the structured data generated previously, we’ll create an SQLite database that can be used in your local environment. However, it’s worth noting that any other structured data source could be used, such as Cloud SQL, by employing the Database
interface offered within langchain_community
. To proceed with this example, we’ll need to install the langchain_community
library:
pip install langchain_community
The following code creates a single table within the database named ITEMS
. The columns in this table align with the structure of the data generated earlier:
from langchain_community.utilities import SQLDatabase db = SQLDatabase.from_uri("sqlite:///database.sqlite") db.run( &...