Harnessing GPT-Engineer for Streamlined Code Creation
Written on
Chapter 1: Introduction to GPT-Engineer
GPT-Engineer represents a revolutionary AI-driven coding assistant that transforms prompts into executable code. Notably, it poses pertinent questions to ensure it meets your coding requirements effectively. This tool is built on the renowned GPT-3/GPT-4 framework.
While many are acquainted with ChatGPT for code generation, GPT-Engineer distinguishes itself through several remarkable features:
- Comprehensive Code Generation: Unlike simple snippet generation, it can construct entire codebases based on your specifications, allowing you to download a complete project upon completion.
- Clarifying Queries: Before embarking on code creation, it engages in a dialogue similar to discussions developers have with product owners when exploring new features. Typical inquiries might include:
- What is the intended functionality of the feature?
- How should errors be managed?
- What are the valid input types?
- Code Enhancement: You can also utilize GPT-Engineer to refine existing code in your projects.
This article will guide you through my experiences with GPT-Engineer via a demonstration program, equipping you with the skills to create a full codebase in your preferred programming language.
Are you ready to jump in? Let’s get started!
Chapter 2: Installation Procedures
There are multiple options for installing GPT-Engineer, including using pip, running it as a Docker container, or testing it through GitHub's Codespaces in a browser. For comprehensive installation instructions, refer to the documentation.
For this demonstration, I will utilize Docker Compose.
Step 1: Clone the Repository
cd gpt-engineer
Step 2: Obtain an OpenAI API Key
To use GPT-Engineer, you must have an OPENAI_API_KEY. To generate a new key:
- Log in to your OpenAI account.
- Click on "Create a new secret key" and copy the generated value.
- Paste it into the docker-compose.yml file under the following section:
environment:
# - OPENAI_API_KEY=YOUR_API_KEY
Make sure to modify the path in ./projects/example to suit your project folder before building the image.
Step 3: Create a Prompt File
Generate a prompt file without an extension in your project directory and write your desired instructions. Then, build and run GPT-Engineer:
docker-compose -f docker-compose.yml build
docker-compose run --rm gpt-engineer
Demo Program
For this demo, I will create a basic BMI calculator. Here’s the content of my prompt:
Write a command line program in Python that asks the user for their age, weight, and height to calculate the user's BMI.
Queries from GPT-Engineer:
- What is the intended purpose of the BMI calculator?
- What inputs are necessary for the calculator?
- How is the BMI computed?
- What output does the calculator generate?
- Are there specific units for the inputs or outputs?
- Is any additional context required for the calculator?
Clarifications Provided:
- The user aims to determine if their BMI is healthy.
- Inputs: weight in kg, height in cm, age as a number, gender as either Female or Male.
- Use the standard BMI formula without extra context.
Following the clarifications, GPT-Engineer stated:
Nothing further needs clarification.
Based on the outlined requirements, it identified essential classes, functions, and methods for the BMI calculator:
- BMI Class:
- calculate_bmi(weight: float, height: float) -> float: Computes BMI using weight and height.
- interpret_bmi(bmi: float) -> str: Provides interpretation of the BMI value.
- User Class:
- __init__(name: str, age: int, gender: str, weight: float, height: float): Initializes a user object with specified attributes.
- get_bmi() -> float: Returns the user's BMI.
- get_bmi_interpretation() -> str: Returns an interpretation of the user's BMI.
Generated Code Files:
The tool produced the following files for the BMI calculator:
main.py
from bmi import BMI
from user import User
def main():
name = input("Enter your name: ")
age = int(input("Enter your age: "))
gender = input("Enter your gender (Male/Female): ")
weight = float(input("Enter your weight in kg: "))
height = float(input("Enter your height in cm: "))
user = User(name, age, gender, weight, height)
bmi = user.get_bmi()
print(f"Your BMI is: {bmi}")
interpretation = user.get_bmi_interpretation()
print(f"Interpretation: {interpretation}")
if __name__ == "__main__":
main()
bmi.py
class BMI:
@staticmethod
def calculate_bmi(weight: float, height: float) -> float:
height_in_meters = height / 100 # Convert cm to meters
bmi = weight / (height_in_meters ** 2)
return bmi
@staticmethod
def interpret_bmi(bmi: float) -> str:
if bmi < 18.5:
return "Underweight"elif bmi < 25:
return "Normal weight"elif bmi < 30:
return "Overweight"else:
return "Obese"
user.py
from bmi import BMI
class User:
def __init__(self, name: str, age: int, gender: str, weight: float, height: float):
self.name = name
self.age = age
self.gender = gender
self.weight = weight
self.height = height
def get_bmi(self) -> float:
return BMI.calculate_bmi(self.weight, self.height)
def get_bmi_interpretation(self) -> str:
bmi = self.get_bmi()
return BMI.interpret_bmi(bmi)
It looks promising at a glance, and you can find the generated codebase under projects/{example-project}/workspace.
Execution Instructions
To run the program, follow these steps:
- Install Python (if necessary):
sudo apt-get update
sudo apt-get install python3
- Create and activate a virtual environment:
python3 -m venv myenv
source myenv/bin/activate
- Install dependencies:
pip install -r requirements.txt
- Run the code:
python main.py
Ensure you're in the correct directory where the codebase is located before executing these commands.
Do you want to run this code? (y/n)
Upon executing, here’s the output:
$ python3 main.py
Enter your name: Kirshi
Enter your age: 33
Enter your gender (Male/Female): Female
Enter your weight in kg: 51
Enter your height in cm: 159
Your BMI is: 20.17325264032277
Interpretation: Normal weight
Conclusion
In this tutorial, you’ve learned how to set up and utilize GPT-Engineer for code generation based on user prompts. My personal experience has shown that it’s an excellent tool for developing simpler programs. Even if you don’t rely on it for entire code generation, the probing questions it poses can inspire deeper thought and facilitate code enhancement.
Keep in mind that the quality of the output is largely influenced by how well you articulate your instructions and respond to its inquiries. Although GPT-Engineer is a powerful tool, it’s important to recognize that for more intricate projects, it may not completely replace the expertise of seasoned developers—at least for now.
I hope you found this guide helpful! Thank you for reading, and happy coding!
In this video titled "GPT Engineer: The Future of Coding?", explore how GPT Engineer is shaping the future of programming and what it means for developers.
The second video, "GPT Engineer... Generate an entire codebase with one prompt," illustrates how GPT Engineer can streamline the coding process by generating complete codebases from a single prompt.