Created by author using Midjourney
Wouldn’t it be great to have a coding partner that never tires, works around the clock, and can generate an entire codebase from a single project description. This is the intent of a new project called GPT-Engineer, one of a number of emerging AI-powered coders. It’s an AI-powered application builder that uses the power of GPT to help you build applications. Developed by Anton Osika, GPT-Engineer is a game-changer in the world of AI-assisted development. You can check out the GPT-Engineer GitHub repository here to see it in action.
GPT-Engineer is not just another AI tool; it’s your coding assistant. Whether building a simple application or something more complex, such as a game, GPT-Engineer can help. It’s like having a junior developer by your side, asking clarifying questions to ensure it fully understands your project requirements.
Before you start coding with GPT-Engineer, you’ll need to install it. To do so, clone the GPT-Engineer repository from the project’s GitHub page. Next, create a new environment with a recent version of Python, and install the necessary modules. For a step-by-step guide, head over to the GPT-Engineer GitHub page.
GPT-Engineer prompting is based on feeding it design documents. But how does one draft a useful design document? Why not use GPT-4 to help create them?!?
Let’s use GPT-4 to generate detailed project design descriptions, which can then be fed into GPT-Engineer. This way, you can ensure that your AI coding buddy has all the information it needs to build your application.
As an example, let’s create a design document with GPT-4 to prompt GPT-Engineer with. It really doesn’t take much to get ChatGPT to produce a useful design document — though they can be more complex, as you will see below. Here is our prompt:
I want to implement a game of snake in Python, using the game’s classic rules and gameplay. It can use external libraries to accomplish this. Generate a design document for this project.
Here are the first several lines of the generated design document, which I can assure you, in total, does a good job of outlining what needs to be done.
The purpose of this design document is to outline the implementation details for creating a classic Snake game in Python. The game will adhere to the traditional rules and gameplay, where the player controls a snake that grows in length by eating food while avoiding collisions with itself and the game boundaries. The game will be developed using external libraries to facilitate graphics and user input.
The Snake game will consist of the following components:
a) Snake: A movable object that grows in length when it eats food and loses the game if it collides with itself or the game boundaries.
b) Food: A randomly placed object that the snake can eat to increase its length and score.
Now that we have a design document prompt, we can use it with GPT-Engineer.
One of the great things about GPT-Engineer is its versatility. It can implement a simple snake game, just as it can help you build a wide range of applications, including a simple key-value database using only Python’s standard library modules. Just describe the project in the
main.prompt file, and GPT-Engineer will ask you for any necessary clarifications. Once you’ve provided the details, sit back and watch as GPT-Engineer generates your codebase.
First, let’s generate a design document with ChatGPT-4. Remember, you don’t need to use ChatGPT to accomplish this. If you have a detailed description of the software you are looking to build you are able to use it. But for a comprehensive conversion of your design notes to something that is easily ingestible by GPT-Engineer, this is a logical route to take.
Here is the prompt the author used with ChatGPT-4 to generate a design document:
Generate a high quality design document that can be used by our project team to design the software described below. It should be complete and usable as the sole design documentation to get our team to complete our project.
Simple key-value database engine written in Python using only standard library modules (except those mentioned below).
The basic interaction with the database engine when it is running should be via imported Python module with a clear and concise library API.
There should also be a separate console that can be run to act as a command line REPL for interacting with the database engine and databases.
Additional methods of interaction should include an API created with Fast API (external library).
A key-value database is a type of NoSQL database that stores data as a collection of key-value pairs. Unlike traditional relational databases, key-value databases do not enforce a rigid schema, providing flexibility and scalability for certain use cases. Designing a key-value database involves considering several important specifications.
Commands that should be accessible:
– connect to database
– delete database
– get key
– insert data
– update data
– delete data
– search for existence of both keys and values
Data Model: Each record consists of a unique key and an associated value. The value can be a simple scalar value or a complex data structure (such as JSON or XML).
Key Structure: The key serves as the primary identifier for each data entry and must be unique within the database. Keys can be alphanumeric strings, integers, or even composite keys (multiple values combined to form a single key).
Value Structure: The value associated with each key can be of any data type. It can range from basic data types (strings, numbers, booleans) to more complex structures like nested objects or arrays.
Performance and Caching: In-memory caching techniques should be employed to speed up read operations and reduce latency.
And here is the design document that ChatGPT-4 produced:
The main goal of this project is to design and develop a lightweight key-value database engine written in Python, using only standard library modules, and FastAPI for the HTTP interface. The application will provide the essential features of a key-value database, including connecting to the database, data insertion, update, deletion, and retrieval, along with search functionality for keys and values.
The database will be interfaced via three methods:
1. A Python module that can be imported by other Python programs.
2. A command line REPL console for manual interaction.
3. An API created with FastAPI for remote interactions.
The database engine will have the following main components:
1. **Data Store**: The actual storage for the key-value pairs. As a simple Python implementation, this could be a dictionary where keys are unique identifiers and values can be of any datatype. The data store should have efficient methods for accessing, adding, updating, and deleting data.
2. **Database Engine**: The engine that provides the methods to interact with the data store. This will include methods to connect to a database, insert, update, and delete data, as well as fetch a value by key. It should also include a method to check the existence of a particular key or value in the data store.
3. **In-Memory Cache**: A cache layer that holds frequently accessed data to improve read speed and reduce latency. This can be implemented using Python’s `functools.lru_cache` decorator or a custom caching solution.
4. **CLI**: A command-line interface for manual interaction with the database engine. This would use the `cmd` or `argparse` module from the Python standard library to create a REPL interface.
5. **HTTP API**: A FastAPI application that exposes the database engine methods as a HTTP API. This will allow remote interaction with the database.
## Data Model
The database engine will follow a simple key-value data model. Each record in the database will consist of a unique key and an associated value.
### Key Structure
Keys can be alphanumeric strings, integers, or composite keys. They must be unique within the database and will serve as the primary identifier for each data entry.
### Value Structure
The value associated with each key can be of any Python data type. It can range from basic data types (strings, numbers, booleans) to more complex structures like nested objects or arrays.
## API Design
The Python library API, CLI, and FastAPI should provide the following functions:
– `connect(database_name: str)`: Connect to a database. Creates the database if it does not exist.
– `delete_database(database_name: str)`: Deletes a database.
– `insert(key: str, value: Any)`: Inserts a new key-value pair into the database.
– `update(key: str, value: Any)`: Updates the value of an existing key.
– `delete(key: str)`: Deletes a key-value pair from the database.
– `get(key: str)`: Fetches the value of a key from the database.
– `exists_key(key: str)`: Checks if a key exists in the database.
– `exists_value(value: Any)`: Checks if a value exists in the database.
## Performance and Caching
In-memory caching techniques will be employed to speed up read operations and reduce latency. The `functools.lru_cache` decorator or a custom caching solution could be used for this purpose. The cache should have a maximum size to prevent excessive memory usage.
This document provides a high-level design for a simple key-value database engine using Python and FastAPI. By adhering to this design, we can ensure that the database will be flexible and scalable, meeting the needs of various applications.
And that’s it. The only thing left to do was run GPT-Engineer and direct it to these criteria.
So what now? How do we fix the issues that were present in the generated code? Well, recently and up to a few days ago, the author was using GPT-Engineer in conjunction with Aider, another capable GPT-backed software generating project to try and tweak the results. However, with the recent public availability of the ChatGPT Code Generator plugin, it seems that trying it out for both generating and fixing code would be a good idea.
In sum it up, GPT-Engineer is more than just a tool, it’s a potential revolution in how we build applications. It’s a testament to the power of AI, LLMs, and perhaps a glimpse into the future of coding. Regardless of whether you are an experienced developer or just starting out, GPT-Engineer is definitely worth a look.