This project is a Node.js application that interacts with the OpenAI GPT-4 API to perform sentiment and self-harm risk analysis on user messages. It stores analysis results in MongoDB, manages user authentication, and sends email alerts when potential self-harm content is detected. The application provides a chat interface for users and can be deployed locally or to a cloud environment.
- Self-Harm Risk Detection: Utilizes OpenAI's Moderation API to detect self-harm content in user messages.
- Email Notifications: Sends email alerts to designated recipients when potential self-harm risk is detected.
- MongoDB Integration: Stores risk analysis results and user data in a MongoDB database using Mongoose.
- Secure User Authentication: Implements JWT for session management and bcrypt for password hashing.
- Real-time Chat Interface: Users can communicate with the AI in real-time through a web-based chat interface.
- User Registration and Login: Allows users to create accounts and securely log in before accessing the chat.
- Installation
- Configuration
- Running the Application
- API Endpoints
- Email Notifications
- Deployment
- Security and Privacy Considerations
- Contributing
- License
- Acknowledgments
- Node.js: Ensure you have Node.js installed.
- MongoDB Atlas Account: Sign up for a free MongoDB Atlas account.
- OpenAI API Key: Obtain an API key from OpenAI.
- Email Service Account: Set up an account with an email service provider like SendGrid.
- Git: For cloning the repository.
-
Clone the Repository:
git clone https://github.com/carson-evans/GPT-API.git
-
Navigate to the Project Directory:
cd path/to/project-directory
-
Install Dependencies:
npm install
-
Set Up Environment Variables:
-
Rename
sample_env.js
toenv.js
and update it with your actual keys and configurations.// env.js export const process = { env: { OPENAI_API_KEY: "your-openai-api-key", MONGODB_URI: "your-mongodb-connection-uri", EMAIL_SERVICE_API_KEY: "your-email-service-api-key", ALERT_EMAIL_RECIPIENT: "alerts@example.com", JWT_SECRET: "your-jwt-secret-key", }, };
-
Note: Ensure
env.js
is included in your.gitignore
file to prevent sensitive information from being committed to version control.
-
- Obtain your API key from the OpenAI dashboard.
- Update the
OPENAI_API_KEY
in yourenv.js
.
- Create a MongoDB Atlas cluster.
- Set up a database user and whitelist your IP address.
- Obtain your connection URI and update the
MONGODB_URI
in yourenv.js
.
- Create an account with SendGrid or another email service provider.
- Generate an API key and update the
EMAIL_SERVICE_API_KEY
in yourenv.js
. - Verify your sender identity as per your email service's instructions.
- Set the
ALERT_EMAIL_RECIPIENT
in yourenv.js
to the email address where alerts should be sent. - Set the
from: 'no-reply@yourdomain.com'
inindex.js
to the verified sender email.
- Generate a secure secret key for signing JWT tokens.
- Update the
JWT_SECRET
in yourenv.js
.
npm start
- The server will run at
http://localhost:3000
.
- Open your browser and navigate to
http://localhost:3000
. - Register a new account and log in to access the chat interface.
- Registers a new user.
{
"username": "your-username",
"password": "your-password"
}
{
"message": "Registration successful"
}
- Authenticates a user and returns a JWT token.
{
"username": "your-username",
"password": "your-password"
}
{
"message": "Login successful",
"token": "your-jwt-token"
}
- Handles chat messages and performs risk analysis.
- Authorization:
Bearer your-jwt-token
{
"messages": [
{
"role": "user",
"content": "Your message here"
}
]
}
{
"response": "Assistant's response"
}
- When potential self-harm content is detected in a user's message, the application sends an email alert to the address specified in
ALERT_EMAIL_RECIPIENT
. - Email Content:
- Subject:
Urgent: Self-Harm Risk Detected for User ID [userId]
- Body: Includes the user's message and the self-harm score.
- Subject:
- Ensure all environment variables are set securely in your hosting environment.
- Common platforms include Heroku, AWS Elastic Beanstalk, and Azure App Service.
- Use HTTPS to secure data in transit.
- Obtain SSL certificates through services like Let's Encrypt.
- User Consent: Inform users that their messages are analyzed for safety purposes.
- Data Protection: Implement measures to protect user data, including encryption and secure storage practices.
- Access Control: Limit access to sensitive data and ensure only authorized personnel can view alerts.
- Compliance: Adhere to relevant laws and regulations, such as GDPR or HIPAA.
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch:
git checkout -b feature/your-feature-name
-
Commit your changes:
git commit -m "Add your descriptive commit message"
-
Push to the branch:
git push origin feature/your-feature-name
-
Open a pull request.
This project is licensed under the MIT License.
- OpenAI for providing the GPT-4 and Moderation APIs.
- MongoDB for database solutions.
- SendGrid for email services.