Build a custom ChatGPT web app in no time with this open source ready-for-production React + AWS Serverless scaffolding. This completely free, public GitHub repository includes Vite, Terraform, AWS API Gateway and AWS Lambda.

Summary

I actually run this code in production as part of my portfolio. See openai.lawrencemcdaniel.com, so follow the link if you want to see this very code in action, and don’t forget to take a look at the “About” page while you’re there. Also, you’ll find the source code here https://github.com/FullStackWithLawrence/aws-openai. The repo contains excellent Getting Started instructions and technical documentation, so I’m going to keep this article high level, focusing more on why you should be using this repo to jumpstart your next ChatGPT single-page web app project.

The OpenAI API documentation includes 30 example applications as a means of presenting the seemingly limitless amazing things that you can do with their API, providing just a few lines of natural language text to guide OpenAI’s various Large Language Chat Generative Pre-trained Transformers (ie chatGPT models). But ironically, that is the easy part. You still need a polished front end app that’s on parr with what today’s users are accustomed. And, for a variety of reasons which I’ll further explain below, you also need a back end to host your custom REST API. One quick spoiler: at a minimum you’ll need to setup some kind of proxy REST API service just to protect your 😳 OpenAI API key 😳 from the world’s least sophisticated hackers.

OpenAI Code Samples

Using this code base, creating your own ChatGPT app is as simple as configuring a JSON dict of this form, plus creating a small Terraform module of the form shown below.

Sarcastic Chat source code
Sarcastic Chat backend source code

React App

Building a good chat app is not easy, which is why so many programmers are employed at platforms like WhatsApp, SnapChat, Telegram, Vine, Signal, Terminal, Dust and so on. So keep that in mind when setting out to attempt to achieve the same level of quality as a one-person dev team. The React app included in this repository is quite good. And if you’re at least modestly familiar with React and TypeScript then it’s fairly easy to understand. Where possible it leverages popular NPM packages which helps this part of the project to self-maintain itself over the long term, and it minimizes your learning curve.

Key features

  • Robust, highly customizable chat features
  • A component model for implementing your own highly personalized OpenAI apps
  • Skinnable UI for each app
  • Includes default assets for each app
  • Small compact code base
  • Robust error handling for non-200 response codes from the custom REST API
  • Handles direct text input as well as file attachments
  • Info link to the OpenAI API official code sample
  • Build-deploy managed with Vite

REST API

Lest I forget to mention it, a couple of months ago I wrote this entire deep dive technical article about this backend, “OpenAI API With AWS Lambda” which covers the subtleties and nuances of programmatically interacting with the OpenAI API using AWS Python Lambda.

By any measure, this is a production-ready back end service. It’s a 1-click build, using Infrastructure-as-Code Terraform for every AWS cloud resource in the project, including for example, the Python and NodeJS Lambdas. If you’re new to Terraform then this is an excellent project for getting started. The documentation is excellent, and it strictly follows current best practices. It incidentally also touches a broad ranges of AWS services including IAM, CloudWatch, API Gateway, Lambda and Route53. So it turns out to also be a pretty good templating tool for your future projects.

This back end is intended to achieve multiple strategic objectives. In no particular order, this is what I’ve attempted to do for you:

  • Provide a fully automated and fast build-deploy workflow
  • Provide an easy to follow road map for creating your own custom API endpoints that interact not only with chatGPT models but also the growing list of other kinds of models that OpenAI has published like DALL·EWhisperEmbeddings, and Moderation.
  • Provide good scaffolding for creating real OpenAI projects for paying customers
  • Use entirely serverless architecture that carries zero running cost while idle
  • Keep your sensitive data safe. This is harder to do than it might seem.
  • Provide copious documentation. In this repo you’ll find well documented code plus supplemental documentation resources, and detailed documentation on each URL endpoint.

Key features

  • Robust, performant and infinitely scalable
  • Fast build time; usually less than 60 seconds to fully implement
  • Includes both Python and Node.js Lambda examples
  • Deploy https to a custom domain
  • Preconfigured Postman files for testing
  • includes AWS API Gateway usage policy and api key
  • Full CORS configuration

How It Works

AWS API Gateway REST API workflow
  1. a JSON object and custom headers are added to an HTTP request and sent to the API as a ‘PUT’ method.
  2. API Gateway uses a Request Mapping Template in a non-proxy Lambda integration request to combine user request text with your OpenAPI application definition, and then forward the combined data as a custom JSON object to a Lambda function.
  3. Lambda parses and validates the custom JSON object and then invokes the OpenAI API, passing your api key which is stored as a Lambda environment variable.
  4. OpenAI API results are returned as JSON objects.
  5. Lambda creates a custom JSON response containing the http response body as well as system information for API Gateway.
  6. API Gateway passes through the http response to the client.

You’ll find a detailed narrative explanation of the design strategy in this article, OpenAI API With AWS Lambda

Services and Technologies Used

  • OpenAI: a PyPi package thata provides convenient access to the OpenAI API from applications written in the Python language. It includes a pre-defined set of classes for API resources that initialize themselves dynamically from API responses which makes it compatible with a wide range of versions of the OpenAI API.
  • API Gateway: an AWS service for creating, publishing, maintaining, monitoring, and securing REST, HTTP, and WebSocket APIs at any scale.
  • IAM: a web service that helps you securely control access to AWS resources. With IAM, you can centrally manage permissions that control which AWS resources users can access. You use IAM to control who is authenticated (signed in) and authorized (has permissions) to use resources.
  • Lambda: an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services. It is a computing service that runs code in response to events and automatically manages the computing resources required by that code. It was introduced on November 13, 2014.
  • CloudWatch: CloudWatch enables you to monitor your complete stack (applications, infrastructure, network, and services) and use alarms, logs, and events data to take automated actions and reduce mean time to resolution (MTTR).
  • Route53: (OPTIONAL). a scalable and highly available Domain Name System service. Released on December 5, 2010.
  • Certificate Manager: (OPTIONAL). handles the complexity of creating, storing, and renewing public and private SSL/TLS X.509 certificates and keys that protect your AWS websites and applications.

CORS

CORS is always a tedious topics with regard to REST API’s. Please note the following special considerations in this API project:

  • CORS preflight is implemented with a Node.js Lambda – openai_cors_preflight_handler
  • There are a total of 5 response types which require including CORS headers. These are
    • the hoped-for 200 response status that is returned by Lambda
    • the less hoped-for 400 and 500 response statuses returned by Lambda
    • and the even less hoped-for 400 and 500 response statuses that can be returned by API Gateway itself in certain cases such as a.) Lambda timeout, b.) invalid api key credentials, amongst other possibilities.
  • For audit and trouble shooting purposes, Cloudwatch logs exist for API Gateway as well as the two Lambas, openai_text and openai_cors_preflight_handler

In each case this project attempts to compile an http response that is as verbose as technically possible given the nature and origin of the response data.

CORS Setup

YouTube – Build a ChatGPT chatbot with React and AWS Serverless

I hope you found this helpful. My YouTube channel might also interest you. Contributors are welcome. My contact information is on my web site. Please help me improve this article by leaving a comment below. Thank you!