Skip to content

Latest commit

 

History

History
191 lines (150 loc) · 6.84 KB

README.md

File metadata and controls

191 lines (150 loc) · 6.84 KB

Dealing With AI

Dealing with AI is a web application that uses OpenAI's GPT-3 API. The API endpoint allows users to make requests to the OpenAI API for various natural language processing tasks such as sentence correction, paraphrasing, report making, programming, math, writing, website optimization and many more.

Demo

image

image

Tech Stack

The tech stack used in this application includes:

- Node.js
- Express.js
- OpenAI API
- dotenv
- express-rate-limit

API Endpoint

The API endpoint for Dealing with AI is located at /v1/api/dealingWithAI. It accepts HTTP POST requests with a JSON body that includes the following properties:

- value: represents the user's input.
- option: represents an option chosen by the user (if applicable).
- language: represents the language used for the input.
- task: represents the task to be performed by the AI.

Upon receiving the request, the API performs rate limiting with the help of a middleware function called limiter. It then validates the input using another middleware function called validateInput. If the input is valid, the API generates a response using the AI and returns it in the following JSON format:

```json
{
  "data": "response"
}
```

Code

The application code is written in JavaScript and uses Node.js and Express.js to handle HTTP requests. The OpenAI API is used to generate responses to user input.

The stringClean function removes leading and trailing whitespace and multiple consecutive newline characters from a string.

The validateInput middleware function is used to validate the user input before it is processed. It checks if the value field is not empty and if the number of words in the input is within the limit specified by the MAX_CHARACTERS environment variable.

The application also uses the dotenv package to load environment variables from a .env file. The OPENAI_API_KEY, MAX_REQUESTS, MAX_REQUESTS_PER_MINUTE, MAX_CHARACTERS, and PORT variables are used to configure the OpenAI API key, rate limiting, and input validation.

Installation

To run Dealing with AI locally, you need to have Node.js and npm (Node Package Manager) installed on your machine. You also need an OpenAI API key to access the GPT-3 API.

Follow these steps to install the application and its dependencies:

  1. Clone the repository to your local machine. git clone https://github.com/sauravhathi/dealingWithAI.git
  2. Navigate to the project directory.
    cd dealingWithAI
  3. Install the dependencies using npm.
    npm i
  4. Create a .env file in the project directory with the following contents:
    OPENAI_API_KEY="your-openai-api-key"
    PORT=3000
    MAX_CHARACTERS = 400
    MAX_REQUESTS = 4
    MAX_REQUESTS_PER_MINUTE = 2
  5. Replace your-openai-api-key with your actual OpenAI API key. The MAX_REQUESTS, MAX_REQUESTS_PER_MINUTE, and MAX_CHARACTERS variables are used to configure rate limiting and input validation.

Usage

To start the application, run the following command in the project directory: npm start if you want to use nodemon npm run server

This will start the server on http://localhost:3000.

postman api testing

image

rate-limit

image

image

You can now send a POST request to the API endpoint /v1/api/dealingWithAI with a JSON body that includes a value field representing the user's input. Here is an example using curl:

```bash
curl --header "Content-Type: application/json" --request POST --data '{"value":"Hello, how are you?"}' http://localhost:3000/v1/api/dealingWithAI
```

The response will be a JSON object with a data field containing the generated response from the GPT-3 model:

```json
{"data":"I'm good, thank you. How can I assist you?"}
```

Postman Tests for all features

The implementation uses a switch statement to check the value of the option property in the request body, and modifies the prompt accordingly based on the value of option. If option is not present, the original prompt is used.

Some of the options have additional properties, such as language and task for the programming option, and task for the writing and website options. These properties are optional and can be null.

  1. Sentence Correction

    {
      "value": "Which following correct",
      "option": "sentence correction"
    }

    request and response

    image

  2. Paraphraser

    {
        "value": "hello everyone",
        "option": "paraphraser"
    }

    request and response

    image

  3. Report Making

    {
        "value": "5G tech",
        "option": "report making"
    }

    request and response

    image

  4. Programming

    {
        "value": "int* a;a=5;string str = \"hello\"",
        "option": "programming",
        "language": "c++",
        "task": "explain"
    }

    request

    image

    response

    image

  5. Math

    {
      "value": "5+9",
      "option": "math"
    }

    request

    image

    response

    image

  6. Writing

    {
      "value": "climate change",
      "option": "writing",
      "task": "essay"
    }

    request and response

    image

  7. Website

    {
      "value": "bollywood movie",
      "option": "website",
      "task": "keywords"
    }

    request and response

    image

Note that the input validation and rate limiting are applied to the API endpoint, so if the input is invalid or the rate limit is exceeded, an error response will be returned instead.