All writing

Building an AI-Powered Website Generator: A Deep Dive into Prompt Engineering & WebContainers

How I built Visio, an AI website generator, using clever prompt engineering to coax structured code from an LLM and WebContainers to run it live in the browser.

4 min read

What Inspired the Project

I was casually experimenting with a website called Bolt.new, a tool that allows users to generate websites by simply describing them in text. I found it fascinating that they were able to use a general-purpose large language model (LLM) to generate functional code for websites. The question that popped into my mind was: how does the website receive structured blocks of code based on user input? Did they create some sort of text parser that understands the LLM's output, or was there something else going on behind the scenes?

After digging into the API response, I realized that the key to making this work wasn't a fancy parser but rather clever prompt engineering. This insight sparked my curiosity, and I decided to build something similar to better understand the process. Here's how I approached my AI Website builder Visio.

How Prompt Engineering Drives the Website Generation Process

At the heart of this project is prompt engineering. When a user provides a website idea, the prompt used to query the LLM isn't just a simple string; it's carefully crafted to give the model all the context it needs to generate useful, structured output. The prompt is a combination of an initial system prompt and context prompts.

The trick lies in crafting these prompts in a way that the LLM can understand the structure and intent behind the request. By doing this, the LLM generates the response in a format that is consistent and easily interpretable.

For example, imagine that we define the prompts in such a way that they wrap the content in XML-like tags. These tags could indicate files, folders, commands, and the content inside those elements. With this structure, it's easy to extract the content we need, whether it's the code for a specific file or instructions for a folder. It essentially becomes a simple task of parsing the tags to identify the content.

Here's a simple breakdown of how that might look:

In this example, the <file> tag tells us the content is a file, and the <code> tag wraps the HTML code. By parsing these tags, I can easily generate the right content for each file or folder. This approach allows me to automate the creation of structured websites using an LLM.

Architecture and Flow of the Application

Once I understood how prompt engineering could structure the website generation, I needed to build the application architecture that could process the prompt and run the generated code. Here's where things get a bit more technical.

Before the user's prompt response arrives, there are certain default files and folders that need to be in place to run the generated website live. To do this, I created an API call that sets up these necessary files on the backend before the LLM's response is processed. This ensures that the required files (such as package.json, vite.config.js, etc.) are available to be populated with the generated content.

Once this initial setup is done, the user's prompt is processed, and the generated website code is returned. The next step is to run the code in a live environment. To achieve this, I used WebContainers—a technology that allows code to run directly in the browser. WebContainers provide an isolated environment where the code can be executed live, making it possible for users to see their websites in real time.

The flow looks something like this:

  1. The user submits their website idea.
  2. The API call prepares the necessary files and folders.
  3. The LLM processes the prompt and returns the generated website code.
  4. The website code is then run using WebContainers, and the live demo is displayed to the user.

The key part here is retaining context. As the user iterates and makes changes to their website idea, I append to the existing prompt chain. This allows me to pass along the context of previous responses, so the LLM can update its output accordingly. This dynamic, iterative approach ensures that the generated website is always up-to-date with the latest changes.

Building this AI-powered website generator taught me a lot about the power of prompt engineering and how it can be leveraged to automate complex tasks. By structuring prompts intelligently, I was able to guide the LLM to produce output that could be parsed and executed in a meaningful way. And with WebContainers, I was able to bring that output to life in real time, giving users an interactive experience.

This project was a fun experiment in AI automation, and I'm excited to continue improving it. I hope this deep dive gave you some useful insights into how prompt engineering and WebContainers can work together to build something powerful.