Mygpt is a simple web application built using React, CSS, and a Node.js backend. It demonstrates the basic structure and functionality of a chat application using GPT-3.5-turbo from OpenAI. This README file will help you understand the project and its codebase.
MyGPT.mp4
The main goal of the project is to showcase a chat application that interacts with the GPT-3.5-turbo AI model provided by OpenAI. The application allows users to send messages and receive responses from the AI model.
To set up the project locally, follow these steps:
- Clone the repository:
git clone https://github.com/yourusername/Mygpt.git- Install the required dependencies:
cd Mygpt
npm install- Set up your
.envfile with your OpenAI API key:
echo "API_KEY=your_openai_api_key" > .env- Start the development server:
npm startOnce the server is running, navigate to http://localhost:8005 in your web browser to interact with the chat application.
App.js is the main React component that contains the chat functionality and logic. It includes the following key features:
- State management using the
useStateanduseEffecthooks. createNewChatfunction: Resets the current chat state and prepares for a new chat session.handleClickfunction: Changes the active chat based on the unique title.getMessagesfunction: Sends a POST request to the backend server to fetch the AI-generated response.handleSubmitfunction: Handles the form submission event to trigger thegetMessagesfunction.- Rendering of the chat history and user interface for sending messages.
server.js is the Node.js backend server that handles API requests to OpenAI. It includes the following key features:
- Express.js app setup with CORS middleware.
- Loading of the OpenAI API key from the
.envfile usingdotenv. /completionsroute: Handles the POST request from the frontend to fetch AI-generated responses. It sends the user message as input to the OpenAI API and receives the AI-generated message as output. The response is then sent back to the frontend.
The main files of the project include:
index.js: The entry point for the React application.App.js: The main React component that contains the chat functionality and logic.index.css: The stylesheet for the application.server.js: The Node.js backend server that handles API requests to OpenAI.
This project is licensed under the MIT License.