* This blog post is a summary of this video.

Build Blazing Fast Web Apps with Next.js Edge Functions and OpenAI

Author: Hassan El MghariTime: 2024-01-30 21:15:02

Table of Contents

Introduction to Building Fast Web Apps with Next.js and Vercel Edge Functions

In this post, we'll learn how to build fast and responsive web applications using Next.js and Vercel Edge functions. We'll use a side project called Twitterbio.com as an example to showcase techniques for building your own applications powered by large language models like GPT-3.

Twitterbio.com allows users to enter their current Twitter bio and select a 'vibe'. It then uses GPT-3 to generate two updated bios. We'll do a code walkthrough of this Next.js app, explain how Edge functions improve speed and UX, and show how to integrate OpenAI for natural language generation.

Overview of Project Example - Twitterbio.com

Let's take a look at the Twitterbio.com application we'll be working with. You copy in your current Twitter bio, select your 'vibe', and it uses GPT-3 to generate two updated bios. In the code, we have a standard Next.js app. The index page contains UI components like the textarea to collect the user's bio, a dropdown for their vibe, and a 'Generate Your Bio' button. This calls the generateBio() function. Below, we display the generated bios after getting the results back from OpenAI. The key pieces of state track the user's bio, chosen vibe, and the generated bios.

Code Walkthrough of Next.js App

Inside generateBio(), we call a serverless function passing the prompt text. This constructs a prompt asking GPT-3 to generate two twitter bios based on the user's current bio and selected vibe. The API function calls OpenAI with the prompt, gets the results, and returns them to display on the page. So this works, but we can do even better using Edge functions...

Generate Bio Function Calls OpenAI API

Our generate API function is standard - it gets the prompt text from the request body then constructs a payload to call the OpenAI API. We await the response, then return the results to display on the front-end. While this works, there's an even better way to build this using Edge functions for increased speed and a better UX...

Leveraging Edge Functions for Speed and UX

The better way to build this app is using Edge functions. Edge functions are similar to serverless functions but smaller, faster, and can run on the edge. This allows for streaming data and a better UX.

Why use Edge functions given their limitations on node.js libraries? Two key reasons:

  • Speed and performance - No cold starts, significantly faster than serverless functions

  • User experience - Streaming data allows seeing results right away vs waiting with a spinner

Comparing Serverless Functions vs Edge Functions

Edge functions have limitations like not supporting some libraries, smaller code size limits, and shorter timeouts. However, they have big advantages for speed and UX. Being smaller and running on the edge means no cold starts and much faster responses than traditional serverless functions. Also, streaming data is only possible with Edge functions.

Seeing Edge Functions in Action

Let's see the difference in action. Here we have the serverless function version on the right, and the Edge function version on the left. When we click 'Generate Bio' on both, notice how much faster results appear on the left. The Edge function streams back data immediately as it becomes available. This allows rendering results right away instead of waiting with a spinner. Much better UX!

Code Changes to Enable Edge Functions

The code to enable Edge functions only requires a few changes:

  • Specify runtime: 'edge' in next.config.js
  • Create an OpenAI stream in the API function to return data chunks
  • In generateBio(), loop through stream data to display results as they come in That's all it takes to unlock faster speed and a better UX with Edge functions in Next.js!

OpenAI Integration for Natural Language Generation

For natural language generation, we integrated the OpenAI API using their GPT-3 model. This powers the bio generation capabilities of our example app.

Our API function calls GPT-3 by passing a prompt with instructions to generate two twitter bios based on the user's current bio and selected vibe. We await the results and return them to display on the front-end.

By using Edge functions and streaming, we can show initial results immediately as OpenAI generates them instead of waiting for the full response.

Conclusion and Key Takeaways

In this post, we learned how Next.js and Vercel Edge functions can be used to build fast, responsive web apps.

Key takeaways:

  • Edge functions provide speed and performance benefits over serverless functions

  • Streaming data with Edge functions enables a better UX by showing results immediately

  • OpenAI's API can be leveraged for natural language generation capabilities

By combining these technologies, you can rapidly build your own applications powered by large language models with great user experiences.


Q: How do Edge functions improve speed and UX?
A: Edge functions have no cold starts, faster response times, and built-in streaming to show results as they come in. This massively improves speed and UX.

Q: What limitations do Edge functions have?
A: Edge functions don't support some advanced Node.js libraries like Prisma. They also have shorter timeout limits than traditional functions.

Q: What is the example project showcased?
A: The example project is twitterbio.com, which generates AI-powered Twitter bios using OpenAI's GPT-3.

Q: How does streaming work with Edge functions?
A: The OpenAI library streams back text gradually as it is generated. The front-end loops through and displays results without waiting for the full response.