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!

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay