Code Monkey home page Code Monkey logo

gpt2pg's Introduction

gpt2pg

This chrome extension allows developers to quickly generate and test SQL queries on a Postgres database using ChatGPT, a powerful language model developed by OpenAI.

Features

Extract SQL queries generated by ChatGPT and send them to a Node.js API Connect the API to a global database cache service (PolyScale) to reduce the risk of query abuse and improve performance Display query results in a table on the ChatGPT page

Prerequisites

  • A Neon account
  • A PolyScale account (optional)
  • Vercel CLI

Installation

  1. Clone the repository to your local machine
  2. Run npm install to install the necessary dependencies
  3. Run npm run build to build the extension
  4. Go to chrome://extensions/ in your chrome browser
  5. Turn on developer mode
  6. Click on "Load unpacked"
  7. Select the "dist" folder in your local repository

Usage

  • Open chat.openai.com
  • Generate SQL queries using ChatGPT
  • Click on the "Run SQL" button below the query to send the query to the API
  • Query results will be displayed in a table on the ChatGPT page

API

The API is a simple Node.js function deployed to Vercel. The following packages are required:

  • pg
  • dotenv Create a new folder and Node.js app by running the following commands on your terminal:
npm init
npm install pg dotenv

Create a api/handler.js and paste the following code:

const { Pool } = require('pg');
require('dotenv').config();

const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
});

exports.handler = async (event) => {
  const { sql, chatId } = JSON.parse(event.body);
  const schema = `chat_${chatId}`;
  const client = await pool.connect();
  try {
    await client.query(`SET search_path TO ${schema}`);
    const { rows } = await client.query(sql);
    return {
      statusCode: 200,
      body: JSON.stringify(rows),
    };
  } catch (err) {
    return {
      statusCode: 500,
      body: JSON.stringify({ error: err.message }),
    };
  } finally {
    client.release();
  }
};

Note that the DATABASE_URL is the one provided by PolyScale when you connect it to your Neon database.

gpt2pg's People

Contributors

raoufchebri avatar

Stargazers

Roman avatar

Forkers

downpat19

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.