This project is a Next.js application that allows users to query a PostgreSQL database using natural language and visualize the results. It's powered by the AI SDK by Vercel and uses OpenAI's GPT-4o model to translate natural language queries into SQL.
- Natural Language to SQL: Users can input queries in plain English, which are then converted to SQL using AI.
- Data Visualization: Results are displayed in both table and chart formats, with the chart type automatically selected based on the data.
- Query Explanation: Users can view the full SQL query and get an AI-generated explanation of each part of the query.
- Next.js for the frontend and API routes
- AI SDK by Vercel for AI integration
- OpenAI's GPT-4o for natural language processing
- PostgreSQL for data storage
- Vercel Postgres for database hosting
- Framer Motion for animations
- ShadowUI for UI components
- Tailwind CSS for styling
- Recharts for data visualization
- The user enters a natural language query about unicorn companies.
- The application uses GPT-4 to generate an appropriate SQL query.
- The SQL query is executed against the PostgreSQL database.
- Results are displayed in a table format.
- An AI-generated chart configuration is created based on the data.
- The results are visualized using the generated chart configuration.
- Users can toggle between table and chart views.
- Users can request an explanation of the SQL query, which is also generated by AI.
The database contains information about unicorn companies, including:
- Company name
- Valuation
- Date joined (unicorn status)
- Country
- City
- Industry
- Select investors
This data is based on CB Insights' list of unicorn companies.
To get the project up and running, follow these steps:
-
Install dependencies:
pnpm install
-
Copy the example environment file:
cp .env.example .env
-
Add your OpenAI API key and PostgreSQL connection string to the
.env
file:OPENAI_API_KEY=your_api_key_here POSTGRES_URL="..." POSTGRES_PRISMA_URL="..." POSTGRES_URL_NO_SSL="..." POSTGRES_URL_NON_POOLING="..." POSTGRES_USER="..." POSTGRES_HOST="..." POSTGRES_PASSWORD="..." POSTGRES_DATABASE="..."
-
Download the dataset:
- Go to https://www.cbinsights.com/research-unicorn-companies
- Download the unicorn companies dataset
- Save the file as
unicorns.csv
in the root of your project
-
Seed the database:
pnpm run seed
-
Start the development server:
pnpm run dev
Your project should now be running on http://localhost:3000.
The project is set up for easy deployment on Vercel. Use the "Deploy with Vercel" button in the repository to create your own instance of the application.
To learn more about the technologies used in this project, check out the following resources:
- Next.js Documentation
- AI SDK
- OpenAI
- Vercel Postgres powered by Neon
- Framer Motion
- ShadcnUI
- Tailwind CSS
- Recharts
Step 1: Install Prisma and Initialize Run the following commands in your terminal:
npm install @prisma/client
npx prisma init
Step 2: Define the Prisma Schema Edit the prisma/schema.prisma file to define the unicorns table:
NOTE: this table name must match the one in the prompt!!!
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model unicorns {
id Int @id @default(autoincrement())
company String @unique
valuation Float
date_joined DateTime?
country String
city String
industry String
select_investors String
}
Step 3: Set Up Environment Variables Make sure your .env file contains the connection string to your local PostgreSQL database:
A new bi
database using public
schema.
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/bi?schema=public"
Step 4: Run Prisma Migrate Run the following commands to apply the schema to your database:
npx prisma migrate dev --name init
Step 5: Seed the database with CSV file
pnpm run seed
Step 6: Run the app
pnpm run dev