This project demonstrates how to create a chat interface using OpenAI's GPT API, with a React frontend and Flask backend. The backend streams completions from the GPT API, and the frontend displays these in a chat interface.
This project contains two main directories:
frontend
: This contains the React application that renders the chat interface.run.py
: This is the Flask server file that handles the backend operations.
The frontend is structured as follows:
public
: This directory contains static files and theindex.html
file where the React app is hooked.src
: This directory contains all the React JavaScript files, components, and CSS files.
You can run the following commands in this directory:
npm start
: Runs the app in development mode.npm test
: Runs the test watcher in an interactive mode.npm run build
: Builds the app for production to thebuild
folder.npm run eject
: Removes this tool and copies build dependencies, configuration files, and scripts into the app directory.
The backend server is a Flask application that communicates with the OpenAI API. It serves two main routes:
/
: This route simply returns a "Hello World!" message./api/prompt
: This is the main API route that takes aprompt
as a POST request and streams the completions from the GPT API.
The server runs on port 4444, and CORS is handled globally.
To set up this project, follow these steps:
- Clone the repository to your local machine.
- Install the necessary packages for both the frontend and backend.
- Replace
YOUR_API_KEY
inrun.py
with your actual OpenAI API key. - Start the Flask server by running
python run.py
in the main directory. - Start the React application by running
npm start
in thefrontend
directory.
Please note that the Flask server must be running in order for the React app to function properly.