软件架构中的生成AI:先别替换你的架构师!

ChatGPT中文站

此文章是系列之一,我们探索如何利用生成式人工智能来完成软件开发生命周期中的各个阶段。您可以阅读这篇关于实验方法和工具的文章。

软件架构建设非常重要,它确保开发出的软件符合业务需求,并使技术团队取得成功。本文章探讨了生成式人工智能,特别是 GPT,如何协助软件架构。

软件架构通常由不同职称的工程师处理,如软件架构师,首席工程师,工程师,技术主管或资深工程师,这取决于组织和团队结构。对于本文,我们将集体地称这些人为“软件架构师”。

软件架构师的主要职责

  • 技术栈选择:选择技术栈,包括编程语言、框架、平台和数据库。
  • 决策:为设计模式、编码规范和部署制定战略技术决策。
  • 系统设计:设计软件系统的总体结构,以符合业务目标。
  • 跨职能需求:将安全性、可扩展性、性能和可靠性等跨职能需求整合到软件设计中。
  • 风险管理:识别和减轻系统中的潜在风险。
  • 团队指导:指导和辅导开发团队,确保遵循定义的架构和编码标准。
  • 利益相关者沟通:作为利益相关者之间的桥梁,传达系统的体系结构设计并解释技术方面。
  • 文档:维护重要的技术文档,如设计规范和架构图。

生成式人工智能如何帮助软件架构师?

现在让我们探索一下生成式人工智能如何支持建筑师在他们的职责方面。

技术栈选择:

生成式人工智能可以在这个阶段发挥建设性作用,根据定义的要求推荐潜在的技术和可行的替代方案。

例如,考虑以下提示用于列举全面的技术栈,包括前端、后端、基础设施、安全工具和CI/CD管道。

我们使用Python借助OpenAI的聊天API,而不是直接使用ChatGPT进行更高效的提示。要了解更多关于这种方法的信息,请查看开发者的ChatGPT提示工程课程。

import openai
import os

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
openai.api_key = os.getenv('OPENAI_API_KEY')

def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0
)
return response.choices[0].message["content"]
tech_stack_prompt = """
Your role is to act as a Software Architect assistant.

Requirements that needs to be met:
- Ecosystem: Node.js
- Cloud Provider: AWS
- Use a typed language
- Backend should be a RESTful API
- Use managed services provided by the cloud provider
- Use managed relational database provided by the cloud provider
- Prefer open source tools
- Do not suggest serverless functions such as AWS Lambda
- Prefer npm over yarn

Please suggest the below:
- Frontend Tech stack
- Programming language
- Framework
- Build tool
- Code coverage tool
- Dependency management
- Dependency vulnerability scanning
- Static code analysis
- Unit testing
- UI testing
- Backend API Tech stack
- Programming language
- Framework
- Build tool
- Code coverage tool
- Static code analysis
- Dependency management
- Dependency vulnerability scanning
- Unit testing
- API testing framework
- API documentation
- Database
- Secret management
- Security
- Container scanning tool
- Static Application Security Testing (SAST) tool
- Dynamic Application Security Testing (DAST) tool
- Penetration testing tool
- Infrastructure
- Infrastructure as code
- Security compliance scanning
- Logging
- Monitoring
- CI/CD tool


For each of the above, please provide in the below format:
<tool name>
Description: <short description>
Reason to choose: <reason> Pls note <reason> must not include generic words like popular or widely used or large community
Alternatives: <alternatives>. <provide the reasons to not choose them. Must not contain the reason that it lack some of the advanced features>.


Output must be in markdown syntax with proper newlines and indentation that can be directly copied to a markdown file.
"""

response = get_completion(tech_stack_prompt)
print(response)

GPT提供了技术桟的详细建议(以下为部分输出)。该方法的优点是这些提示可以在整个组织中重复使用,这加快了架构师的工作。如上所示,技术桟是针对在AWS上托管的Node.js环境进行定制的。但是,此提示可以根据所需的生态系统和云服务提供商进行定制。

## Frontend Tech stack

### Programming language
TypeScript
**Description:** TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.
**Reason to choose:** TypeScript provides type checking, which helps catch errors at compile time rather than runtime. This leads to more stable and maintainable code.
**Alternatives:** JavaScript. JavaScript lacks type checking, which can lead to errors at runtime.

### Framework
React
**Description:** React is a popular JavaScript library for building user interfaces.
**Reason to choose:** React has a large and active community, which means there are many resources and plugins available. It also has a virtual DOM, which makes it fast and efficient.
**Alternatives:** Vue.js, Angular. Vue.js is a newer framework with a smaller community. Angular is a more complex framework that can be difficult to learn.

### Build tool
Webpack
**Description:** Webpack is a module bundler for JavaScript applications.
**Reason to choose:** Webpack is highly configurable and can handle a wide range of tasks, such as bundling, minification, and code splitting. It also has a large and active community.
**Alternatives:** Parcel, Rollup. Parcel is a simpler build tool that is easier to set up, but it lacks some of the advanced features of Webpack. Rollup is a more specialized build tool that is designed for libraries rather than applications.

### Code coverage tool
Istanbul
**Description:** Istanbul is a code coverage tool for JavaScript applications.
**Reason to choose:** Istanbul is widely used and has good integration with other tools, such as Jest. It also has a large and active community.
**Alternatives:** nyc. nyc is a similar tool to Istanbul, but it is less widely used.

...
...

技术决策:

生成式人工智能可以通过呈现多种选项来支持建筑师。例如,在我们的实验中,我们使用这个提示来识别在 AWS 上为 React.js 前端使用各种部署策略。

deployment_options_prompt = """
What are the different ways to deploy the frontend react js application to AWS
- Suggest at least 3 ways
- For each way, provide the pros and cons

Suggest the best way among the 3 for the below requirements
- For a small team of 3 developers
- For faster deployment using AWS CloudFormation
- Lower cost
- Used by up to 1000 users
- Around 100 concurrent users

Output should be in markdown syntax with proper newlines and indentation that can be directly copied to a markdown file.
"""

response = get_completion(deployment_options_prompt)
print(response)

GPT提供了相应的选项及其利弊和推荐的方法。

## Deploying React JS Application to AWS

There are several ways to deploy a React JS application to AWS. Here are three ways with their pros and cons:

### 1. Deploying to Amazon S3

#### Pros:
- Easy to set up and deploy
- Low cost
- Good for static websites

#### Cons:
- Limited functionality
- Not suitable for dynamic websites

### 2. Deploying to AWS Elastic Beanstalk

#### Pros:
- Easy to deploy and manage
- Supports multiple languages and frameworks
- Auto-scaling and load balancing

#### Cons:
- Higher cost than S3
- Limited control over infrastructure

### 3. Deploying to AWS EC2

#### Pros:
- Full control over infrastructure
- Can handle complex applications
- Can be cost-effective for larger applications

#### Cons:
- More complex to set up and manage
- Requires more technical expertise
- Higher cost than S3 and Elastic Beanstalk

### Best Way for the Given Requirements
Based on the requirements, the best way to deploy the React JS application to AWS would be to use AWS Elastic Beanstalk. It is easy to deploy and manage, supports auto-scaling and load balancing, and is cost-effective for a small team of 3 developers. It can handle up to 1000 users and around 100 concurrent users. Additionally, it can be accessed from anywhere in the world, making it suitable for a team working from different locations. Finally, AWS CloudFormation can be used to automate the deployment process, making it faster and more efficient.

系统设计:

像 GPT 这样的大型语言模型是根据无数的建筑实践进行训练的。因此,它们可以针对特定项目的要求提出适当的建筑设计建议。这不仅极大地缩短了技术解决方案设计所需的时间,还为建筑师提供了一个基础蓝图,以启动他们的设计。

例如,生成式AI可以通过正确的提示加速创建部署图的过程。在我们的实验中,我们使用下面的提示来使用DiagramGPT生成部署图。尽管生成的设计可能不是最好或最准确的,但它提供了一个快速的起点。

Please provide deployment diagram with all the below items

- React application deployed to s3 and exposed via cloudfront
- Backend nodejs service deployed using Elastic Beanstalk and communicate with AWS relation database
- Backend REST API exposed via API Gateway
- Cloudfront routing to s3 or api gateway based on path
- Route 53 in front of cloudfront
- Show the users accessing the route 53 from internet
- All Components should be inside the AWS Cloud
- Arrows to represent the network flow between the components
ChatGPT中文站
Auto Generated Deployment Diagram using Diagram GPT

跨职能要求:

生成式人工智能能够通过提供洞察和建议来帮助建筑师应对跨职能需求。值得注意的是,跨职能需求经常被忽视,特别是对于缺乏经验的或新手系统设计师。

在这种情况下,使用下面展示的提示作为指南可以帮助他们在建筑设计过程中导航这个关键方面,确保这些要求得到充分考虑。

cross_functional_prompt = """
List all the cross-functional requirements (At least 15) that need to be followed in the overall system architecture

Information about the System architecture:
- Nodejs backend deployed in AWS Elastic Beanstalk
- React frontend deployed in AWS S3
- AWS RDS MySQL database

Each cross-functional requirement should be in the below format:
<requirement name>:
Description: <Description of the requirement in detail>
Best Practices: List of practices to follow to meet the requirement
Example: List multiple examples of how the requirement is met
Tools: List the tools that can be used to meet the requirement

The output must be in markdown syntax with proper new lines and indentation that can be directly copied to a markdown file
"""

response = get_completion(cross_functional_prompt)
print(response)

GPT 能够使用工具和示例提供关于各种跨部门需求的详细建议。部分输出如下:

## Cross-Functional Requirements
-----------------
### Scalability:
Description: The system should be able to handle an increase in load without affecting performance.
Best Practices:
- Use horizontal scaling by adding more instances of the application.
- Use load balancers to distribute traffic evenly.
- Use caching to reduce the load on the database.

Example:
- Use AWS Elastic Beanstalk to automatically scale the application based on traffic.
- Use AWS Auto Scaling to add or remove instances based on CPU utilization.

Tools:
- AWS Elastic Beanstalk
- AWS Auto Scaling
-----------------
### Security:
Description: The system should be secure and protect against unauthorized access.
Best Practices:
- Use HTTPS for all communication.
- Use encryption for sensitive data.
- Use strong passwords and two-factor authentication.

Example:
- Use AWS Certificate Manager to obtain SSL/TLS certificates for HTTPS.
- Use AWS Key Management Service to encrypt and decrypt sensitive data.

Tools:
- AWS Certificate Manager
- AWS Key Management Service
-----------------
### Performance:
Description: The system should be performant and respond quickly to user requests.
Best Practices:
- Use caching to reduce the load on the database.
- Use CDNs to serve static assets.
- Use compression to reduce the size of responses.

Example:
- Use AWS ElastiCache to cache frequently accessed data.
- Use AWS CloudFront to serve static assets.

Tools:
- AWS ElastiCache
- AWS CloudFront
-----------------
### Monitoring:
...
### Availability:
...
### Reliability:
...
### Maintainability:
...
### Cost:
...
### Compliance:
...
### Disaster Recovery:
...
### Collaboration:
...
### Documentation:
...
### Performance Optimization:
...
### Accessibility:
...

生成式人工智能能否充当完整的建筑师?

如果生成式人工智能被用作一个完整的软件架构师,将会涉及到许多相关考虑。

设计细节的限制。

生成式人工智能目前只能提供基本的高级设计蓝图,但需要做出更微妙的建筑决策所需的详细说明。

例如,在我们的实验项目中,当面临业务需求和设计系统的任务时,GPT只能生成如下非常基本的大纲。这强调了对每个技术设计要素进行仔细提示以实现全面和定制的架构解决方案的需求。

ChatGPT中文站

复杂度过载:

当提供更多细节时,人工智能开始将更多的复杂性添加到解决方案中。这种过度复杂化可能使开发过程比必须的更加困难。例如,它建议使用具有无服务器功能的复杂分布式架构,而可以使用简单的单片设计更有效地处理它。

潜在的安全风险:

使用生成式人工智能在软件架构中可能存在某些安全挑战。人工智能可能会忽略关键方面,如安全API端点, 执行数据加密协议,或忽略关键的网络安全措施,例如防火墙或入侵检测系统。这些例子突显了仅依赖生成式人工智能进行软件架构可能存在的安全盲点。

受限创造力

AI的创新潜力受限于它所训练的数据的多样性和程度。如果让AI设计最先进技术的软件解决方案,它可能会发现提供创新性的解决方案具有挑战性。原因是,它可能没有被训练足够多的与该领域相关的数据。

忽视团队和文化因素:

生成式人工智能在其架构建议中可能会忽略团队动态和组织文化。例如,它可能会提出一个复杂的解决方案,而不考虑团队的能力或需要的技能的开发人员的可用性。它也可能建议一个从技术上运行但不符合组织更广泛目标的解决方案。

一种供建筑师使用的工具,而非替代品

最终,虽然生成式AI可能是一种宝贵的工具,但它还没有准备好取代人类建筑师。机器做决策的能力只有在训练其数据良好的情况下才能达到最佳水平。它无法理解复杂的商业背景,预见未预料的挑战,也无法带来人类建筑师能够提供的创造力和创新精神。

归根结底,机器无法完全取代经验丰富的建筑师的决策能力。软件架构决策往往涉及主观判断,商业直觉和个人责任 - 这些是人工智能目前缺乏的特征。

因此,在可预见的未来,虽然AI将成为建筑师工具箱中越来越重要的工具,但要让你的建筑师保持亲密。让他们引导AI以最佳方式服务于你的软件架构需求。正是通过这种合作,我们将看到最有效和创新的解决方案。

2023-10-20 17:04:45 AI中文站翻译自原文