ChatGPT与软件架构(1) - 快速原型

通过ChatGPT生成设计和原型代码,可以帮助团队快速启动项目,验证想法,提高效率。原文: ChatGPT and Software Architecture

OpenAI的ChatGPT现在越来越火,出现了各种有趣用例。

从许多方面来看,ChatGPT都可以看作是AI赋能的架构师白板,除了画画线条和框框,还可以有许多用途。我在本文中将演示如何基于ChatGPT启动软件架构流程。

就像在白板上画画一样,过程会有点混乱,各种因素相互作用会导致不得不通过不断修改来找到最佳答案。本文旨在演示成功使用ChatGPT的一些提问技巧,这些技巧产生了真正有价值的结果。

需要注意,ChatGPT仍在学习中,所以不要对一致性有太大期望,多次问同样的问题可能会得到不同的结果。

设置ChatGPT帐户很简单,可以按照官方文档完成: https://openai.com/blog/chatgpt/

用户故事

我们从要求ChatGPT写用户故事开始。

写一个用户故事,测量员登录到系统,输入地理空间坐标、建筑、建筑类型和特征信息,这是他们一天中要进行的许多调查中的一个

如你所见,这是个特定行业的用户故事。ChatGPT拥有庞大的知识库,需要通过实验来了解ChatGPT的知识深度,即它知道些什么?

第一个问题的关键是要对关键细节非常明确,以获得最佳结果。这样,ChatGPT可以响应一组体面的结果,如下所示:

如果获得的结果与上述类似,那就意味着ChatGPT已经找到了线索,并将根据进一步提出的问题进行构建。要记住的关键是,以下所有结果都将基于这个初始上下文,因此值得花时间定义第一个交互。

现在可以做些非常了不起的事情,构建数据库!

构建数据库

在markdown中以表格格式详细生成数据模型

ChatGPT是这样回应的:

不止如此,我们继续…

生成SQL
CREATE TABLE Surveyors (
 id INT NOT NULL AUTO_INCREMENT,
 name VARCHAR(255) NOT NULL,
 email VARCHAR(255) NOT NULL,
 password VARCHAR(255) NOT NULL,
 PRIMARY KEY (id)
);
CREATE TABLE Buildings (
 id INT NOT NULL AUTO_INCREMENT,
 type_id INT NOT NULL,
 coordinates VARCHAR(255) NOT NULL,
 notes TEXT,
 surveyor_id INT NOT NULL,
 PRIMARY KEY (id),
 FOREIGN KEY (type_id) REFERENCES BuildingTypes(id),
 FOREIGN KEY (surveyor_id) REFERENCES Surveyors(id)
);
CREATE TABLE BuildingTypes (
 id INT NOT NULL AUTO_INCREMENT,
 type VARCHAR(255) NOT NULL,
 PRIMARY KEY (id)
);

ChatGPT输出了完全可用的SQL,可以顺利创建SQL数据库。不止如此……

创建类图

<script src="https://cdn.jsdelivr.net/npm/mermaid@8.6.0/dist/mermaid.min.js"></script>
<script>mermaid.initialize({startOnLoad:true});</script>
<div class="mermaid">
 classDiagram
 class Surveyors {
 +id: int
 +name: string
 +email: string
 +password: string
 }
 class Buildings {
 +id: int
 +type_id: int
 +coordinates: string
 +notes: string
 +surveyor_id: int
 }
 class BuildingTypes {
 +id: int
 +type: string
 }
 Surveyors --> Buildings
 Buildings --> BuildingTypes
</div>

ChatGPT生成的类图如下所示…

ChatGPT真是让人惊喜:

ChatGPT没法显示图表,但可以生成能够创建图表的Mermaid代码。ChatGPT非常熟悉Mermaid脚本,可以生成这种脚本语言支持的图表。

由此可见,ChatGPT已经学会了编码,可以继续要求ChatGPT编写JavaScript、CSS和HTML,通过简单的请求将原型组合在一起。ChatGPT经过训练,可以用许多流行的编程语言生成代码。

如果有足够的时间,每个有经验的人都可以做到,只不过速度要慢一些。但这不是生产代码,要使代码达到生产质量和可用性水平,还需要做更多工作。不过ChatGPT已经很让人惊艳了。

我们尝试用ChatGPT来提高代码质量:

生成遵循C#实践原则的类设计,包含错误处理和数据库访问层

ChatGPT的响应:

public class Surveyor
{
 private readonly string _name;
 private readonly string _email;
 private readonly List<ISurvey> _surveys;
 private readonly IDatabaseAccess _databaseAccess;
 public Surveyor(string name, string email, IDatabaseAccess databaseAccess)
 {
 _name = name;
 _email = email;
 _databaseAccess = databaseAccess;
 _surveys = new List<ISurvey>();
 }
 public void AddSurvey(ISurvey survey)
 {
 try
 {
 _surveys.Add(survey);
 _databaseAccess.SaveSurvey(survey);
 }
 catch (Exception ex)
 {
 // Log the error
 }
 }
 public void RemoveSurvey(ISurvey survey)
 {
 try
 {
 _surveys.Remove(survey);
 _databaseAccess.DeleteSurvey(survey);
 }
 catch (Exception ex)
 {
 // Log the error
 }
 }
 public List<ISurvey> GetSurveys()
 {
 try
 {
 return _databaseAccess.GetSurveys();
 }
 catch (Exception ex)
 {
 // Log the error
 return new List<ISurvey>();
 }
 }
}
public interface IDatabaseAccess
{
 void SaveSurvey(ISurvey survey);
 void DeleteSurvey(ISurvey survey);
 List<ISurvey> GetSurveys();
}
public class DatabaseAccess : IDatabaseAccess
{
 public void SaveSurvey(ISurvey survey)
 {
 // Implementation for saving the survey to a database
 }
 public void DeleteSurvey(ISurvey survey)
 {
 // Implementation for deleting the survey from the database
 }
 public List<ISurvey> GetSurveys()
 {
 // Implementation for retrieving a list of surveys from the database
 }
}
public interface ISurvey
{
 decimal Latitude { get; set; }
 decimal Longitude { get; set; }
 string BuildingType { get; set; }
 Dictionary<string, string> BuildingCharacteristics { get; set; }
}
public class Survey : ISurvey
{
 public decimal Latitude { get; set; }
 public decimal Longitude { get; set; }
 public string BuildingType { get; set; }
 public Dictionary<string, string> BuildingCharacteristics { get; set; }
 public Survey(decimal latitude, decimal longitude, string buildingType, Dictionary<string, string> buildingCharacteristics)
 {
 Latitude = latitude;
 Longitude = longitude;
 BuildingType = buildingType;
 BuildingCharacteristics = buildingCharacteristics;
 }
}

作为一组脚手架类,非常不错了。

接下来让ChatGPT为RESP API创建Swagger,ChatGPT再次返回了足以启动开发的不错成果。

结论

ChatGPT功能广泛,可以帮助启动开发,但还无法实现比较深度的工作。每个回合都必须靠人来激发ChatGPT完成任务,缺乏主动性,需要有人发起并推动接下来的步骤。

不过可以开发一组标准问题,促使ChatGPT提供好的结果,也许足以帮助团队以多种方式开始开发。


你好,我是俞凡,在Motorola做过研发,现在在Mavenir做技术工作,对通信、网络、后端架构、云原生、DevOps、CICD、区块链、AI等技术始终保持着浓厚的兴趣,平时喜欢阅读、思考,相信持续学习、终身成长,欢迎一起交流学习。微信公众号:DeepNoMind

本文由mdnice多平台发布

作者:俞凡原文地址:https://segmentfault.com/a/1190000043852859

%s 个评论

要回复文章请先登录注册