Skip to content

Natural language querying allows users to interact with databases more intuitively and efficiently. By leveraging the power of LangChain, SQL Agents, and OpenAI’s Large Language Models (LLMs) like GPT, we have created an application that enable users to query databases using NLP

License

Notifications You must be signed in to change notification settings

Syed007Hassan/Chat-To-Your-Database

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chat To Your Database

image

Demo.mp4

Table Of Contents

Introduction

Natural language querying allows users to interact with databases more intuitively and efficiently. By leveraging the power of LangChain, SQL Agents, and OpenAI’s Large Language Models (LLMs) like ChatGPT, we have created an application that enables users to query databases using NLP. All it needs a SQL-based schema, and it can perform any read and write action to that schema.

This is an experimental app to test the abilities of LLMs to query SQL databases using SQL Agents provided by Langchain. To use it, you should have an OPENAI_API_KEY.

Features

  • Auto-generate SQL queries from the natural language descriptions
  • Translate SQL queries from one dialect to another
  • Optimize SQL queries to make them run faster
  • Analyze SQL queries to identify potential problems
  • Optimize the SQL queries that are used to fetch data from the SQL database
  • Analyze the SQL queries that are used in your NextJS application to identify potential problems
  • Displays all the data in the form of a table after executing the query on a schema
  • Stores the history prompts and the queries generated in No-SQL

Examples

Sample database SQLite

Sample northwind database

See it in action

Langchain-SQL.Agent.mp4

Sample database PostgreSQL

DVDRental

See it in action

pg res

Tech Stack

  • Frontend: NextJS/TailwindCSS/Flowbite
  • Backend: ExpressJS/NestJS
  • Databases: Sqlite/PostgreSQL/MongoDB
  • LLM: Langchain SQL Agent with Open AI LLM

Getting Started

Add Env file

After cloning the repo, change the directory to nest_api, and add a .env file.

OPENAI_API_KEY=sk-xxx
# Docker environment variables
DB_TYPE=postgres
PG_HOST=postgres
PG_USER=postgres
PG_PASSWORD=postgres
PG_DB=postgres
PG_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=postgres
[email protected]
PGADMIN_DEFAULT_PASSWORD=admin
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=pass12345
MONGO_URL=mongodb://root:pass12345@mongodb:27017/chatwithdb?authSource=admin
PORT=5000

Running the APP using Docker Compose with your own Database (PostgreSQL)

  • Build the image using docker-compose build --no-cache

  • Run Container with the build image docker-compose up

    image

  • Open PgAdmin to upload your Database (I will use *.tar file to upload the whole database, you could use any method, and just remember to name the database as mentioned in .env. In my case it is postgres)

  • On localhost:5050, login to PgAdmin

    image

  • Create a server group

    image

  • Then register server

    image

    image

  • Now restore the database to upload the file

    image

    image

  • After restoring, all the tables will be shown

  • Run the frontend after changing the directory to the client and then using npm run dev, available at localhost:3000

  • Register and then Sign-in, the application will be redirected towards /chat URL

  • According to your schema write any prompt to generate an SQL query and the results database contains

    chatpath

Contribution Guidelines

Contributions to this project are highly encouraged and appreciated. To help us maintain a high level of quality, please follow these contribution guidelines:

  • Fork the repository to your GitHub account.
  • Make changes and improvements in your forked repository.
  • Test your changes thoroughly.
  • Create a well-explained pull request detailing the changes you made and the problems they solve.
  • Ensure your pull requests comply with the coding standards and styles followed in the project.
  • Be responsive to feedback and iterate on your contributions, if necessary.
  • Upon review, your pull request will go through a validation process by the project maintainers. Once approved, your changes will be merged into the main branch, and you will become a contributor to the NextJs Langchain Agents SQL project. We appreciate your efforts in making this project better and more valuable.

Please follow the contribution guidelines outlined in the Contributing.md file in this repository, to make sure that your contributions align with the project standards.

License

This project is licensed under the MIT License.

About

Natural language querying allows users to interact with databases more intuitively and efficiently. By leveraging the power of LangChain, SQL Agents, and OpenAI’s Large Language Models (LLMs) like GPT, we have created an application that enable users to query databases using NLP

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published