jacoblee93 / langchain-chat-sql

Home Page:https://langchain-chat-sql.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

🦜️🔗 Chat over SQL

This app demonstrates how to query a SQL database using natural language.

It queries a Neon DB-hosted instance of the popular demo Chinook database using LangChain.js and OpenAI's gpt-3.5-turbo model to generate SQL queries.

You can watch the accompanying Neon DB Developer Days talk here to learn more about the prompt and chain, or check out a live Vercel deployment here.

Setup

First, you'll need to create an empty Neon DB instance.

Using your credentials, run the following commands to bootstrap the DB and a readonly user that the LLM will use to run generated queries:

psql postgres://<USERNAME>:<PASSWORD>@<HOSTNAME>/<DB_NAME> -f bootstrap.sql

psql postgres://<USERNAME>:<PASSWORD>@<HOSTNAME>/<DB_NAME> -c "CREATE ROLE llm WITH LOGIN PASSWORD '<YOUR_PASSWORD>';"

psql postgres://<USERNAME>:<PASSWORD>@<HOSTNAME>/<DB_NAME> -c "GRANT CONNECT ON DATABASE <DB_NAME> TO llm;"

psql postgres://<USERNAME>:<PASSWORD>@<HOSTNAME>/<DB_NAME> -c "GRANT SELECT ON ALL TABLES IN SCHEMA public TO llm;"

You can modify the last step to only give read access to certain tables, as well as allow insert/update access to specific tables, if desired.

Next, copy the .env.example file to .env.local and populate your OPENAI_API_KEY and Neon connection strings. To enable tracing, you can enter your LangSmith keys as well.

Finally, run yarn to install the required dependencies.

Usage

Run yarn dev to start this app locally.

The database simulates a media store and contains information on employees, customers, records, and transactions. You can learn more about it in the GitHub repo.

Here are some traces for example queries you can ask:

  • Who works here?

https://smith.langchain.com/public/6d9e466d-a272-4dae-89b6-56e0686fc652/r

  • How many customers are from Germany?

https://smith.langchain.com/public/b9c83439-e12c-4ca1-97b1-7a71ba6a8539/r

Thank you!

Check out the following links for more:

About

https://langchain-chat-sql.vercel.app

License:MIT License


Languages

Language:TypeScript 95.0%Language:JavaScript 3.5%Language:CSS 1.5%