译者 | 李睿
审校 | 重楼
本文将介绍一个实验,展示多个人工智能代理如何通过群聊方式进行互动,并根据具体的业务需求协同工作,共同生成解决方案的架构。
本文介绍如何使用Databricks Community Edition (CE)(一个免费的基于云的平台)运行一些基本的人工智能Python代码。因为只处理开源库,所以这个实验可以很容易地在任何Python/PySpark环境中复现。
AutoGen和可对话代理简介
AutoGen是一个开源平台,使开发人员能够创建协作人工智能代理。这些代理通常通过聊天群组进行交互,并共同完成任务。可对话代理可以发送、接收和生成消息,也可以使用人工智能模型、工具和人工输入的组合来定制。可对话代理可以是以下任何一种:
- 用户代理,顾名思义,它是人类的代理,在用户输入与人工智能代理回复之间的某个环节发挥作用,并能执行代码。
- 一个或多个助理代理,它们是使用大型语言模型(LLM)但不需要人工输入或代码执行的人工智能助理。
以下将在用户代理(例如,架构主管)和三个助理代理(云计算架构师、开源架构师和首席架构师)之间创建一个聊天群组。其目标是基于业务需求列表提供解决方案架构,可以用以下的互动图来表示:
对话流程的工作原理如下:
- 业务需求提供给代理。
- 代理发起架构师之间的聊天。
- 云计算架构师将首先发言,为每个主要的云计算提供商:Azure、AWS和谷歌云提供建议。
- 下一位发言者。
- 开源架构师将提供在云计算领域之外使用开源框架的一个解决方案。
- 下一位(也是最后一位)发言者。
- 首席架构师将审查所有解决方案并提供最终提案。
提示工程
首先创建提示,从包含一些简单需求的公共部分(当前的任务)开始:
Plain Text
1 task = '''
2 **Task**: As an architect, you are required to design a solution for the
3 following business requirements:
4 - Data storage for massive amounts of IoT data
5 - Real-time data analytics and machine learning pipeline
6 - Scalability
7 - Cost Optimization
8 - Region pairs in Europe, for disaster recovery
9 - Tools for monitoring and observability
10 - Timeline: 6 months
11
12 Break down the problem using a Chain-of-Thought approach. Ensure that your
13 solution architecture is following best practices.
14 '''
云计算架构师提示:
Plain Text
1 cloud_prompt = '''
2 **Role**: You are an expert cloud architect. You need to develop architecture proposals
3 using either cloud-specific PaaS services, or cloud-agnostic ones.
4 The final proposal should consider all 3 main cloud providers: Azure, AWS and GCP, and provide
5 a data architecture for each. At the end, briefly state the advantages of cloud over on-premises
6 architectures, and summarize your solutions for each cloud provider using a table for clarity.
7 '''
8 cloud_prompt += task
开源架构师提示:
Plain Text
1 oss_prompt = '''
2 **Role**: You are an expert on-premises, open-source software architect. You need
3 to develop architecture proposals without considering cloud solutions.
4 Only use open-source frameworks that are popular and have lots of active contributors.
5 At the end, briefly state the advantages of open-source adoption, and summarize your
6 solutions using a table for clarity.
7 '''
8 oss_prompt += task
首席架构师提示:
Plain Text
1 lead_prompt = '''
2 **Role**: You are a lead Architect tasked with managing a conversation between
3 the cloud and the open-source Architects.
4 Each Architect will perform a task and respond with their resuls. You will critically
5 review those and also ask for, or pointo, the disadvantages of their soltuions.
6 You will review each result, and choose the best solution in accordance with the business
7 requirements and architecture best practices. You will use any number of summary tables to
8 communicate your decision.
9 '''
10 lead_prompt += task
在完成提示之后,可以开始创建可对话代理,并让它们在聊天设置中进行交互。
实现多代理聊天
由于使用的是Python,需要安装相应的pyautogen包:
Python
1 pip install pyautogen
从导入必要的库和一些配置开始。虽然可以使用多个模型,但在这个简单的示例中,选择仅使用GPT-4o。
Python
1 import autogen
2 from autogen import UserProxyAgent
3 from autogen import AssistantAgent
4
5
6 config_list = [{'model': 'gpt-4o', 'api_key': '---------'}]
7
8 gpt4o_config = {
9 "cache_seed": 42, # change the cache_seed for different trials
10 "temperature": 0,
11 "config_list": config_list,
12 "timeout": 120,
13 }
将温度设为0可以最大限度地减少模型的随机性,使其专注于最可能(即得分最高)的选择。现在,将创建具有特定提示的代理:
Python
1 user_proxy = autogen.UserProxyAgent(
2 name="supervisor",
3 system_message = "A Human Head of Architecture",
4 code_execution_config={
5 "last_n_messages": 2,
6 "work_dir": "groupchat",
7 "use_docker": False,
8 },
9 human_input_mode="NEVER",
10 )
11
12 cloud_agent = AssistantAgent(
13 name = "cloud",
14 system_message = cloud_prompt,
15 llm_config={"config_list": config_list}
16 )
17
18
oss_agent = AssistantAgent(
19 name = "oss",
20 system_message = oss_prompt,
21 llm_config={"config_list": config_list}
22 )
23
24 lead_agent = AssistantAgent(
25 name = "lead",
26 system_message = lead_prompt,
27 llm_config={"config_list": config_list}
28 )
上述代码应该清晰易懂。在开始聊天之前,需明确交互顺序,因为希望首席架构师在云计算架构师和开源架构师这两个助理提供解决方案之后再进行总结与决策。首席架构师需要审阅这些方案并作出决定。为了确保这一流程得以顺利执行,创建了一个状态转移函数,用于在聊天中选择发言者:
Python
1 def state_transition(last_speaker, groupchat):
2 messages = groupchat.messages
3
4 if last_speaker is user_proxy:
5 return cloud_agent
6 elif last_speaker is cloud_agent:
7 return oss_agent
8 elif last_speaker is oss_agent:
9 return lead_agent
10 elif last_speaker is lead_agent:
11 # lead -> end
12 return Non
这是一个有限状态机(FSM)的简单示例。现在,准备采用下面这段代码来触发聊天:
Python
1 groupchat = autogen.GroupChat(
2 agents=[user_proxy, cloud_agent, oss_agent, lead_agent],
3 messages=[],
4 max_round=6,
5 speaker_selection_method=state_transition,
6 )
7 manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)
8
9 user_proxy.initiate_chat(
10 manager, message="Provide your best architecture based on these business requirements."
11 )
解释输出
由于篇幅所限,在这里只展示输出的部分内容。该代码易于复制,对细节感兴趣的人可以直接在自己的环境中运行。
聊天从第一位发言者开始:
Plain Text
1 supervisor (to chat_manager):
2
3 Provide your best architecture based on these business requirements.
4
5--------------------------------------------------------------------------------
6
7 Next speaker: cloud
8
9 cloud (to chat_manager):
10
11 ### Chain-of-Thought Approach
12
13 #### 1. Understanding the Business Requirements
14 Based on the given requirements, we need:
15 - **High-capacity data storage** for IoT data.
16 ...
17 ### Advantages of Cloud over On-Premises Architectures
18 - **Scalability**: Easily scale up/down based on demand without large capital expenditure.
19 - **Cost Efficiency**: Pay-as-you-go pricing.
20 - **High Availability**: Built-in options for redundancy and failover.
21 - **Global Reach**: Deploy services in multiple regions across the world.
22 - **Ease of Management**: Managed services reduce operational burden.
23 - **Innovation Velocity**: Rapid deployment of new services and features.
24 ...
25
26 ### Summary Table
27
28| Criterion | AWS | Azure | GCP |
29|-------------------------------|-----------------------------------------|--------------------------------------------|-------------------------------------------------|
30| **Primary Data Storage** | Amazon S3, DynamoDB | Azure Blob Storage, Cosmos DB | Google Cloud Storage, Bigtable |
31| **Real-time Analytics** | Amazon Kinesis | Azure Stream Analytics | Google Cloud Dataflow |
32| **Machine Learning** | Amazon SageMaker | Azure Machine Learning | Google AI Platform
33 .....
然后,云计算架构师根据提供的需求列出每个云计算提供商的相关服务,解释云计算相对于内部部署的优势,并提供一个汇总表。
接下来是第二位发言者。开源架构师遵循大致相同的方法,列出相关服务,讨论开源的优点,并给出自己的解决方案。
Plain Text
1 Next speaker: oss
2
3 oss (to chat_manager):
4
5 ### Chain-of-Thought Approach
6
7 #### 1. Understanding the Business Requirements
8 ....
9 ### Advantages of Open-Source Adoption
10 - **Cost Savings**: No licensing fees.
11 - **Community Support**: Large active communities for troubleshooting and inquiries.
12- **Flexibility and Customization**: Ability to modify source code to meet unique business needs.
13- **Innovation**: Rapid adoption of cutting-edge technologies and features.
14 ...
15
16 ### Summary Table
17
18| Requirement | Solution Components |
19|------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
20| **Data Storage** | Apache Cassandra, HDFS |
21| **Real-time Analytics** | Apache Kafka, Apache Flink, Apache Spark |
22| **Machine Learning** | TensorFlow |
23| **Scalability** | Kubernetes, Docker |
24| **Disaster Recovery Regions** | Primary: Germany (Berlin), Secondary: Netherlands (Amsterdam) |
25| **Monitoring** | Prometheus, Grafana, ELK Stack (Elasticsearch, Logstash, Kibana)
26 ...
最后,首席架构师介入,对各种解决方案的优缺点进行分析,利用适用性矩阵等方法进行评估,并提出最终建议:
Plain Text
1 Next speaker: lead
2
3 lead (to chat_manager):
4
5 ### Evaluating Solutions Based on the Business Requirements and Best Practices
6
7 #### Analysis and Comparison of Proposed Architectures
8
9| **Criterion** | **AWS** | **Azure** | **GCP** | **Open-Source** |
10|-----------------------------------|----------------------------------------------------------------|----------------------------------------------------------------|----------------------------------------------------------------|---------------------------------------------------------------|
11| **Primary Data Storage** | Amazon S3, DynamoDB | Azure Blob Storage, Cosmos DB | Google Cloud Storage, Bigtable | Apache Cassandra, HDFS |
12| **Real-time Analytics** | Amazon Kinesis | Azure Stream Analytics | Google Cloud Dataflow | Apache Kafka, Apache Flink |
13| **Machine Learning** | Amazon SageMaker | Azure Machine Learning | Google AI Platform | TensorFlow |
14 ...
15
16 ### Advantages and Disadvantages
17 ...
18
19 #### Recommendation:
20
21 Based on the evaluation, here is a recommendation considering the timeline of 6 months, the requirements for scalability, disaster recovery, machine learning, and cost optimization:
22
23 - **Primary Recommendation**: **AWS**
24 - **Reasoning**: AWS provides a highly-integrated suite of services that are easy to deploy rapidly. They have strong support for both real-time analytics and machine learning. AWS offers robust monitoring tools and several options for cost optimization, which are fully managed, reducing the operational overhead and the complexity of the setup. They have a well-established global presence with region pairs suitable for disaster recovery.
25
26 - **Secondary Recommendation**: **Azure**
27 - **Reasoning**: If there is a heavier reliance on Microsoft ecosystems within the current infrastructure, Azure would be a suitable alternative. Azure provides strong support for machine learning, analytics, and disaster recovery and offers robust monitoring tools similar to AWS.
28
29 - **Alternative Consideration**: **GCP** for organizations already invested in Google technologies or with a requirement for industry-leading machine learning and data analytics capabilities.
30
31 - **Open-Source Option**: While it might be cost-effective and avoid vendor lock-in, the complexity, high maintena
需要注意的是,首席架构师表达的意见都是他自己的意见,并不一定反映本文作者的意见。
结语
这次实验使用简单的设置,创建了一个基本的架构团队组织,其中不同类型的参与者相互协作为业务提供解决方案。
架构师不仅仅是提供技术层面的建议,他们经常面对复杂且独特的挑战,这要求他们对业务需求有深刻的理解,并对长期影响进行批判性思考。而人工智能在处理某些无形因素方面还存在一些局限,这主要归因于它缺乏必要的人际交往能力和情商,至少目前是这样。
原文标题:Multi-Agent Conversation With AutoGen AI,作者:Tony Siciliani