Skip to content

A basic API for sentiment analysis, leveraging the OpenAI API. Features include user authentication, customizable sentiment categories, rate limiting, daily usage limits, and Redis integration for efficient data management.

License

Notifications You must be signed in to change notification settings

carpodok/sentiment-analysis-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sentiment Analysis API

Frame 7

Overview

This repository provides a basic API for sentiment analysis, utilizing the OpenAI API for processing text inputs. The API categorizes sentiment as positive, negative, or neutral.

Key Features

  • Sentiment Analysis: Analyzes and categorizes text sentiment.
  • OpenAI API Integration: Uses OpenAI API for text analysis.
  • User Authentication: Users must be logged in to use the API.
  • Customizable Categories: Users can customize sentiment categories. Defaults are positive, negative, and neutral.
  • Rate Limiter: Limits usage to 100 requests per 15 minutes.
  • Daily Usage Limit: Each user has 150 requests per day, renewed nightly at 00:00.
  • Redis Integration: Utilizes Redis for efficient in-memory data storage and management.

Used Technologies

  • express: Web framework for Node.js.
  • mongoose: MongoDB object modeling tool.
  • jsonwebtoken: JSON Web Token implementation.
  • bcryptjs: Library to hash passwords.
  • express-validator: Middleware for validating and sanitizing user inputs
  • dotenv: Module to load environment variables.
  • express-rate-limit: Middleware to limit repeated requests.
  • node-cron:Task scheduler for Node.js.
  • nodemon: Utility to monitor for changes in Node.js applications.
  • openai: For sentiment analysis.
  • redis: In-memory data structure store.

Endpoints

  • POST /register

http://localhost:3000/register

Headers:

Content-Type: application/json

Request:

{
    "name": "ali",
    "email":"a@gmail.com",
    "password":"123456"
}

Response:

{
  "success": true,
  "token": ""
}

  • POST /login

http://localhost:3000/login

Headers:

Content-Type: application/json

Request:

{
    "email":"a@gmail.com",
    "password":"123456"
}

Response:

{
  "success": true,
  "token": ""
}

  • POST /analyze

http://localhost:3000/analyze

Headers:

Content-Type: application/json
Authorization: Bearer your_access_token_here

Request:

{
    "text": "The customer service was not good enough, and I am very disappointed with the experience."
}

Response:

{
  "success": true,
  "data": "Negative"
}

  • POST /custom-sentiment-categories

http://localhost:3000/custom-sentiment-categories

Headers:

Content-Type: application/json
Authorization: Bearer your_access_token_here

Request:

{
  "categories": ["Happy", "Sad", "Angry"]
}

Response:

{
  "success": true,
  "data": [
    "Happy",
    "Sad",
    "Angry"
  ]
}

  • GET /profile

http://localhost:3000/profile

Headers:

Content-Type: application/json
Authorization: Bearer your_access_token_here

Response:

{
  "success": true,
  "data": {
    "_id": "6666059d80df1c6f48e43c1f",
    "name": "ali",
    "email": "a@gmail.com",
    "customSentimentCategories": [
      "Happy",
      "Sad",
      "Angry"
    ],
    "requestCount": 1,
    "requestLimit": 2,
    "createdAt": "2024-06-09T19:42:21.608Z",
    "__v": 1
  }
}


Setup

1.Clone the Repo:

git clone https://github.com/carpodok/sentiment-analysis-api.git

2. Install Dependencies:

npm install

3. Configure Environment Variables

Create a .envfile in the root directory and add the following variables:

PORT=3000
OPENAI_API_KEY=your_api_key
MONGO_URI="your_mongo_uri"
JWT_SECRET=jwt_secret
REDIS_URL=redis://127.0.0.1:6379

4. Start the server:

nem start

For development purpose with live reload, use this:

npm run dev

License

This project is lisenced under the MIT License.

About

A basic API for sentiment analysis, leveraging the OpenAI API. Features include user authentication, customizable sentiment categories, rate limiting, daily usage limits, and Redis integration for efficient data management.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published