跳转到主要内容
Chinese, Simplified

最近几周,OpenAI对ChatGPT产生了很多兴趣,出现了各种有趣的用例。

在许多方面,这是与建筑师白板相当的AI,但它有很多用途,而不仅仅是绘制线条和方框。在这篇文章中,我想使用这项创新技术来演示启动软件架构过程的另一个用例。

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

ChatGPT的一个警告是它仍在学习,所以不要期望一致性。在另一天问同样的问题可能会产生不同的结果。

设置ChatGPT帐户本身很简单,可以按照以下地址的说明进行操作:https://openai.com/blog/chatgpt/

用户故事

因此,让我们先让ChatGPT对用户故事做出回应。

正如你所看到的,这个用户故事是我所在行业特有的。ChatGPT拥有庞大的知识库,因此需要进行实验来了解ChatGPT的知识深度——它知道什么?

第一个问题的关键是非常明确关键细节,以获得最佳结果。通过这种方式,ChatGPT可以得到一组不错的结果,例如:

如果您的结果与上述类似,那么您知道ChatGPT已经找到了线索,并将基于您提出的进一步问题。要记住的关键是,以下所有结果都将基于这个初始上下文,因此,为什么值得花时间好好定义第一次交互。

所以现在我们可以做一些非常了不起的事情。建立一个数据库!

让我们制作一个数据库

…这就是ChatGPT的响应方式:

但我们不要就此止步。让我们继续…

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完全熟悉美人鱼脚本,因此它可以生成这种脚本语言所允许的图表。

编程

正如我们已经建立的那样,ChatGPT已经学会了编码,因此您可以继续要求ChatGPT编写JavaScript、CSS和HTML,通过简单的请求将一个工作的原型放在一起。ChatGPT经过培训,可以用许多流行的编程语言生成代码。

如果有足够的时间,这是一个熟练的人可以做的事情。但这不是生产就绪代码;要使代码达到这样的质量和实用性,需要做更多的工作。但ChatGPT没有理由在做这件事时袖手旁观。

因此,让我们尝试使用ChatGPT提高代码质量:

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做的下一个任务是为基于REST的API创建Swagger。ChatGPT再次做出了值得尊敬的努力,足以开始开发。

结论

ChatGPT可以开始开发,因为它具有广泛的功能,但尚未显示其深度。每次转弯时,我都必须让ChatGPT来完成任务;那里没有主动权,我已经开始并推动下一步行动。

然而,开发一组你知道的标准问题会促使ChatGPT提供一个好的结果,这可能很简单。在sprint计划中,这可能足以以多种方式开始开发。

这项技术现在就在这里,我们不妨完全接受它并继续实验,这样我们就可以进一步了解这种先进的自动化可以继续提供什么价值。

原文地址
https://betterprogramming.pub/chatgpt-and-software-architecture-308b6e0cc25a
本文地址
Article

微信

知识星球

微信公众号

视频号