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

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

Surfing Croyde Bay @Unsplash
Surfing Croyde Bay @Unsplash

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

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

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

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

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

用户故事

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

alt

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

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

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

alt

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

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

构建数据库
alt

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

ChatGPT是这样回应的:

alt

不止如此,我们继续…

alt

生成SQL

CREATE TABLE Surveyors (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(255NOT NULL,
    email VARCHAR(255NOT NULL,
    password VARCHAR(255NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE Buildings (
    id INT NOT NULL AUTO_INCREMENT,
    type_id INT NOT NULL,
    coordinates VARCHAR(255NOT 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(255NOT NULL,
    PRIMARY KEY (id)
);

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

创建类图
alt
<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生成的类图如下所示…

alt

ChatGPT真是让人惊喜:

alt
alt

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

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

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

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

alt

生成遵循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 { getset; }
    decimal Longitude { getset; }
    string BuildingType { getset; }
    Dictionary<stringstring> BuildingCharacteristics { getset; }
}

public class Survey : ISurvey
{
    public decimal Latitude { getset; }
    public decimal Longitude { getset; }
    public string BuildingType { getset; }
    public Dictionary<stringstring> BuildingCharacteristics { getset; }

    public Survey(decimal latitude, decimal longitude, string buildingType, Dictionary<stringstring> buildingCharacteristics)
    {
        Latitude = latitude;
        Longitude = longitude;
        BuildingType = buildingType;
        BuildingCharacteristics = buildingCharacteristics;
    }
}

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

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

alt
结论

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

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


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

- END -

本文由 mdnice 多平台发布

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

俞凡 DeepNoMind

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值