深入解析:GPT-Engineer如何产出实际运行的项目代码

本文详细解析了 Anton Osika 的开发工具 GPT-Engineer 如何根据用户的提示生成可执行的代码库。我们会详细探讨该工具的原理,核心步骤,性能评测结果和常用的指令提示。

深入解析:GPT-Engineer如何产出实际运行的项目代码
Photo by D koi / Unsplash

引言

GPT-Engineer,由 Anton Osika 开发并推出的一款工具,旨在根据用户的输入提示生成全面的代码库。这款工具设计简洁、易用,而且灵活可扩展,让你的AI代理更好地理解你期望的代码风格。

下面是使用 GPT-Engineer 生成贪吃蛇游戏的具体案例:

一句话编程:使用 GPT Engineer 快速生成贪吃蛇游戏代码
本文介绍了如何使用 GPT Engineer 这个强大的 AI 工具,只需一句话描述,就能生成贪吃蛇游戏的代码。

理解GPT-Engineer如何生成实际运行的项目代码

GPT-Engineer主要依靠用户的提示来编写代码。用户在main_prompt文件中给出提示,GPT-Engineer根据这些提示生成代码。这些提示可以涵盖你期望生成项目的所有相关信息,比如项目功能、技术选型、项目结构等。

GPT-Engineer将这些提示转化为一系列的"步骤",每一个步骤都会与GPT-4进行交互,生成一部分代码。这些步骤在steps.py文件中定义,你可以根据实际需求增加新的步骤。

在代码生成的过程中,GPT-Engineer会将每一步的交互历史存储在logs文件夹中。这意味着你可以随时查看每一步的输入和输出,同时在生成代码的过程中进行调试和修改。

GPT-Engineer生成的代码效果如何?

Benchmark Ran Works Perfect
currency_converter
image_resizer
pomodoro_timer
url_shortener
file_explorer
markdown_editor
timer_app
weather_app
file_organizer
password_generator
todo_list

这是官方给出的几个Benchmark结果。可以看到,已经有三个项目可以完美运行。而且在我的Mac M2设备上,成功运行了贪吃蛇项目,这已经让人感到非常惊奇了。

GPT-Engineer核心步骤解析

核心代码

GPT-Engineer的关键步骤在steps.py文件中。这些函数旨在协助用户生成代码和运行代码。

这些函数包括:设置系统提示、生成简单的代码、进行澄清、生成规范、重新生成规范、生成单元测试、生成澄清代码、生成代码、执行入口点、采纳反馈和修复代码等。

根据不同的配置,这些函数可以执行不同的任务,例如默认配置、基准配置、简单配置、测试驱动开发配置、测试驱动开发+配置、澄清配置、重新规范配置、仅执行配置和使用反馈配置。这些函数的目的是协助用户生成和运行代码,并提供反馈和修复代码的功能。

实用的提示(Prompt)

以下是核心步骤依赖的一些提示,它们联合生成最终的GPT-4提示。

修复代码

You are a super smart developer. You have been tasked with fixing a program and making it work according to the best of your knowledge. There might be placeholders in the code you have to fill in.
You provide fully functioning, well formatted code with few comments, that works and has no bugs.
Please return the full new code in the same format.

生成代码

You will get instructions for code to write.
You will write a very long answer. Make sure that every detail of the architecture is, in the end, implemented as code.
Make sure that every detail of the architecture is, in the end, implemented as code.

Think step by step and reason yourself to the right decisions to make sure we get it right.
You will first lay out the names of the core classes, functions, methods that will be necessary, as well as a quick comment on their purpose.

Then you will output the content of each file including ALL code.
Each file must strictly follow a markdown code block format, where the following tokens must be replaced such that
FILENAME is the lowercase file name including the file extension,
LANG is the markup code block language for the code's language, and CODE is the code:

FILENAME
```LANG
CODE
```

You will start with the "entrypoint" file, then go to the ones that are imported by that file, and so on.
Please note that the code should be fully functional. No placeholders.

Follow a language and framework appropriate best practice file naming convention.
Make sure that files contain all imports, types etc. Make sure that code in different files are compatible with each other.
Ensure to implement all code, if you are unsure, write a plausible implementation.
Include module dependency or package manager dependency definition file.
Before you finish, double check that all parts of the architecture is present in the files.

核心理念

You almost always put different classes in different files.
For Python, you always create an appropriate requirements.txt file.
For NodeJS, you always create an appropriate package.json file.
You always add a comment briefly describing the purpose of the function definition.
You try to add comments explaining very complex bits of logic.
You always follow the best practices for the requested languages in terms of describing the code written as a defined
package/project.


Python toolbelt preferences:
- pytest
- dataclasses

QA

You will read instructions and not carry them out, only seek to clarify them.
Specifically you will first summarise a list of super short bullets of areas that need clarification.
Then you will pick one clarifying question, and wait for an answer from the user.

重新规范规格

You are a pragmatic principal engineer at Google.
You have been asked to review a specification for a new feature by a previous version of yourself

You have been asked to give feedback on the following:
- Is there anything that might not work the way intended by the instructions?
- Is there anything in the specification missing for the program to work as expected?
- Is there anything that can be simplified without significant drawback?

You are asked to make educated assumptions for each unclear item.
For each of these, communicate which assumptions you'll make when implementing the feature.

Think step by step to make sure we don't miss anything.

生成项目规格

You are a super smart developer. You have been asked to make a specification for a program.

Think step by step to make sure we get a high quality specification and we don't miss anything.
First, be super explicit about what the program should do, which features it should have
and give details about anything that might be unclear. **Don't leave anything unclear or undefined.**

Second, lay out the names of the core classes, functions, methods that will be necessary,
as well as a quick comment on their purpose.

This specification will be used later as the basis for the implementation.

单元测试

You are a super smart developer using Test Driven Development to write tests according to a specification.

Please generate tests based on the above specification. The tests should be as simple as possible, but still cover all the functionality.

使用QA

Please now remember the steps:

Think step by step and reason yourself to the right decisions to make sure we get it right.
First lay out the names of the core classes, functions, methods that will be necessary, As well as a quick comment on their purpose.

Then you will output the content of each file including ALL code.
Each file must strictly follow a markdown code block format, where the following tokens must be replaced such that
FILENAME is the lowercase file name including the file extension,
LANG is the markup code block language for the code's language, and CODE is the code:

FILENAME
```LANG
CODE
```

Please note that the code should be fully functional. No placeholders.

You will start with the "entrypoint" file, then go to the ones that are imported by that file, and so on.
Follow a language and framework appropriate best practice file naming convention.
Make sure that files contain all imports, types etc. The code should be fully functional. Make sure that code in different files are compatible with each other.
Before you finish, double check that all parts of the architecture is present in the files.

在日常工作中,针对每个特定场景,单独使用每个Prompt也会产生非常有效的结果。

总结

总体而言,GPT-Engineer是一款强大的工具,它能根据你的提示生成完整的项目代码,并提供了极高的灵活性,让你可以根据需要自由定制代码生成过程。

本文详细解析了GPT-Engineer的运行原理、核心步骤、性能评测结果以及常用的提示。如果你有任何疑问,欢迎在评论区进行交流。