This project is a Node.js application that uses the OpenAI API to interact with users. It also stores user messages and responses in a MongoDB database. The project is structured to separate concerns into different modules for better maintainability and scalability. The project uses Express.js for handling HTTP requests, and includes CORS support for cross-origin requests.
project-directory/ │ ├── src/ │ ├── index.js │ ├── openaiClient.js │ ├── assistantService.js │ ├── routes.js │ ├── utils.js │ └── db.js ├── node_modules/ ├── package.json ├── package-lock.json └── README.md
The entry point of the application. It sets up the Express server, enables CORS, and uses the routes defined in src/routes.js.
Initializes the OpenAI client with the provided API key and exports it for use in other modules.
Contains functions for interacting with the OpenAI API, such as retrieving the run status and getting assistant instructions. It also saves the assistant's responses to MongoDB.
Defines the API endpoints and their handlers. It handles the POST requests to /api/message, processes the user input, communicates with the OpenAI API, and saves the messages to MongoDB.
Contains utility functions such as sleep, which is used to introduce delays.
Contains the MongoDB client setup and a function to connect to the database.
- Node.js installed on your machine
- An OpenAI API key
- MongoDB installed and running locally
-
Clone the repository:
git clone <repository-url> cd project-directory
-
Install dependencies:
npm install
-
Environment Variables: Update your OpenAI API key in
src/openaiClient.js:const apiKey = 'your--api-key';