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.
Frontend👉 https://dealing-with-ai.vercel.app/
The tech stack used in this application includes:
- Node.js
- Express.js
- OpenAI API
- dotenv
- express-rate-limit
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"
}
```
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.
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:
- Clone the repository to your local machine.
git clone https://github.com/sauravhathi/dealingWithAI.git
- Navigate to the project directory.
cd dealingWithAI
- Install the dependencies using npm.
npm i
- 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
- Replace
your-openai-api-key
with your actual OpenAI API key. TheMAX_REQUESTS
,MAX_REQUESTS_PER_MINUTE
, andMAX_CHARACTERS
variables are used to configure rate limiting and input validation.
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
.
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?"}
```
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.
-
Sentence Correction
{ "value": "Which following correct", "option": "sentence correction" }
-
Paraphraser
{ "value": "hello everyone", "option": "paraphraser" }
-
Report Making
{ "value": "5G tech", "option": "report making" }
-
Programming
{ "value": "int* a;a=5;string str = \"hello\"", "option": "programming", "language": "c++", "task": "explain" }
-
Math
{ "value": "5+9", "option": "math" }
-
Writing
{ "value": "climate change", "option": "writing", "task": "essay" }
-
Website
{ "value": "bollywood movie", "option": "website", "task": "keywords" }
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.