DEV Community

Laurentiu Raducu
Laurentiu Raducu

Posted on

My Journey Creating an API to Check Terms and Conditions Using ChatGPT

Hey there, fellow developers! Today, I want to talk about my latest project called terms-and-conditions-verifier. The aim of this API is to check any terms and conditions sent by a client app for any irregularities or fishy stuff. To achieve this, I have integrated ChatGPT, which is an advanced language model that can understand natural language, into the API.

To give you a brief idea of how the code works, let's take a look at the following file:

import dotenv from "dotenv-safe";
dotenv.config();
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import { ChatGPTAPIBrowser } from "chatgpt";
import { oraPromise } from "ora";
import config from "./config.js";

const app = express().use(cors()).use(bodyParser.json());

const gptApi = new ChatGPTAPIBrowser({
  email: process.env.OPENAI_EMAIL,
  password: process.env.OPENAI_PASSWORD,
});

await gptApi.initSession();

const Config = configure(config);

class Conversation {
  //...
}

const conversation = new Conversation();

app.post("/eula-summary", async (req, res) => {
  //...
});

const EnsureAuth = new Promise((resolve, reject) => {
  //...
});

async function start() {
  //...
}

function configure({ plugins, ...opts }) {
  //...
}

start();

Enter fullscreen mode Exit fullscreen mode

The first few lines of code import necessary modules such as dotenv, express, body-parser, cors, ChatGPTAPIBrowser and oraPromise. dotenv is used to read environment variables from a .env file, express is used to create an instance of the express application, body-parser is used to parse incoming request bodies, cors is used to enable Cross-Origin Resource Sharing (CORS), ChatGPTAPIBrowser is used to interact with the ChatGPT API via the browser, and oraPromise is used to create loading spinners for various functions.

After importing the modules, the code initializes the ChatGPT API by passing the credentials to the ChatGPTAPIBrowser constructor. It then creates a new Conversation instance and configures the API by calling the configure function.

The configure function collects rules and parsers from all plugins and trains the ChatGPT model with all the rules. It then runs the ChatGPT response through all plugin parsers.

The start function connects to ChatGPT and trains the model with the rules collected from all plugins. It then starts the express application, and the app.post method listens for incoming requests. Once a request is received, the API sends the terms and conditions to the Conversation instance for processing.

This project can prove to be a powerful tool that can help you detect any irregularities or fishy stuff in terms and conditions. With the help of ChatGPT, you can easily process natural language and provide quick responses to your clients. I hope this post has given you a better understanding of the code behind the terms-and-conditions-verifier API. Happy coding!

Top comments (0)