如何使用AutoGen AI技术实现多代理对话

译者 | 李睿审校 | 重楼本文将介绍一个实验,展示多个人工智能代理如何通过群聊方式进行互动,并根据具体的业务需求协同工作,共同生成解决方案的架构。 本文介绍如何使用Databricks Community Edition (CE)(一个免费的基于云的平台)运行一些基本的人工智能Python代码。 因为只处理开源库,所以这个实验可以很容易地在任何Python/PySpark环境中复现。

如何使用AutoGen AI技术实现多代理对话

译者 | 李睿

审校 | 重楼

本文将介绍一个实验,展示多个人工智能代理如何通过群聊方式进行互动,并根据具体的业务需求协同工作,共同生成解决方案的架构。

本文介绍如何使用Databricks Community Edition (CE)(一个免费的基于云的平台)运行一些基本的人工智能Python代码。因为只处理开源库,所以这个实验可以很容易地在任何Python/PySpark环境中复现。

AutoGen和可对话代理简介

AutoGen是一个开源平台,使开发人员能够创建协作人工智能代理。这些代理通常通过聊天群组进行交互,并共同完成任务。可对话代理可以发送、接收和生成消息,也可以使用人工智能模型、工具和人工输入的组合来定制。可对话代理可以是以下任何一种:

  • 用户代理,顾名思义,它是人类的代理,在用户输入与人工智能代理回复之间的某个环节发挥作用,并能执行代码。
  • 一个或多个助理代理,它们是使用大型语言模型(LLM)但不需要人工输入或代码执行的人工智能助理。

以下将在用户代理(例如,架构主管)和三个助理代理(云计算架构师、开源架构师和首席架构师)之间创建一个聊天群组。其目标是基于业务需求列表提供解决方案架构,可以用以下的互动图来表示:

如何使用AutoGen AI技术实现多代理对话

对话流程的工作原理如下:

  • 业务需求提供给代理。
  • 代理发起架构师之间的聊天。
  • 云计算架构师将首先发言,为每个主要的云计算提供商:Azure、AWS和谷歌云提供建议。
  • 下一位发言者。
  • 开源架构师将提供在云计算领域之外使用开源框架的一个解决方案。
  • 下一位(也是最后一位)发言者。
  • 首席架构师将审查所有解决方案并提供最终提案。

提示工程

首先创建提示,从包含一些简单需求的公共部分(当前的任务)开始:

  1. Plain Text
  2. 1 task = '''
  3. 2 **Task**: As an architect, you are required to design a solution for the
  4. 3 following business requirements:
  5. 4 - Data storage for massive amounts of IoT data
  6. 5 - Real-time data analytics and machine learning pipeline
  7. 6 - Scalability
  8. 7 - Cost Optimization
  9. 8 - Region pairs in Europe, for disaster recovery
  10. 9 - Tools for monitoring and observability
  11. 10 - Timeline: 6 months
  12. 11
  13. 12 Break down the problem using a Chain-of-Thought approach. Ensure that your
  14. 13 solution architecture is following best practices.
  15. 14 '''

云计算架构师提示:

  1. Plain Text
  2. 1 cloud_prompt = '''
  3. 2 **Role**: You are an expert cloud architect. You need to develop architecture proposals
  4. 3 using either cloud-specific PaaS services, or cloud-agnostic ones.
  5. 4 The final proposal should consider all 3 main cloud providers: Azure, AWS and GCP, and provide
  6. 5 a data architecture for each. At the end, briefly state the advantages of cloud over on-premises
  7. 6 architectures, and summarize your solutions for each cloud provider using a table for clarity.
  8. 7 '''
  9. 8 cloud_prompt += task

开源架构师提示:

  1. Plain Text
  2. 1 oss_prompt = '''
  3. 2 **Role**: You are an expert on-premises, open-source software architect. You need
  4. 3 to develop architecture proposals without considering cloud solutions.
  5. 4 Only use open-source frameworks that are popular and have lots of active contributors.
  6. 5 At the end, briefly state the advantages of open-source adoption, and summarize your
  7. 6 solutions using a table for clarity.
  8. 7 '''
  9. 8 oss_prompt += task

首席架构师提示:

  1. Plain Text
  2. 1 lead_prompt = '''
  3. 2 **Role**: You are a lead Architect tasked with managing a conversation between
  4. 3 the cloud and the open-source Architects.
  5. 4 Each Architect will perform a task and respond with their resuls. You will critically
  6. 5 review those and also ask for, or pointo, the disadvantages of their soltuions.
  7. 6 You will review each result, and choose the best solution in accordance with the business
  8. 7 requirements and architecture best practices. You will use any number of summary tables to
  9. 8 communicate your decision.
  10. 9 '''
  11. 10 lead_prompt += task

在完成提示之后,可以开始创建可对话代理,并让它们在聊天设置中进行交互。

实现多代理聊天

由于使用的是Python,需要安装相应的pyautogen包:

  1. Python
  2. 1 pip install pyautogen

从导入必要的库和一些配置开始。虽然可以使用多个模型,但在这个简单的示例中,选择仅使用GPT-4o。

  1. Python
  2. 1 import autogen
  3. 2 from autogen import UserProxyAgent
  4. 3 from autogen import AssistantAgent
  5. 4
  6. 5
  7. 6 config_list = [{'model': 'gpt-4o', 'api_key': '---------'}]
  8. 7
  9. 8 gpt4o_config = {
  10. 9 "cache_seed": 42, # change the cache_seed for different trials
  11. 10 "temperature": 0,
  12. 11 "config_list": config_list,
  13. 12 "timeout": 120,
  14. 13 }

将温度设为0可以最大限度地减少模型的随机性,使其专注于最可能(即得分最高)的选择。现在,将创建具有特定提示的代理:

  1. Python
  2. 1 user_proxy = autogen.UserProxyAgent(
  3. 2 name="supervisor",
  4. 3 system_message = "A Human Head of Architecture",
  5. 4 code_execution_config={
  6. 5 "last_n_messages": 2,
  7. 6 "work_dir": "groupchat",
  8. 7 "use_docker": False,
  9. 8 },
  10. 9 human_input_mode="NEVER",
  11. 10 )
  12. 11
  13. 12 cloud_agent = AssistantAgent(
  14. 13 name = "cloud",
  15. 14 system_message = cloud_prompt,
  16. 15 llm_config={"config_list": config_list}
  17. 16 )
  18. 17
  19. 18
  20. oss_agent = AssistantAgent(
  21. 19 name = "oss",
  22. 20 system_message = oss_prompt,
  23. 21 llm_config={"config_list": config_list}
  24. 22 )
  25. 23
  26. 24 lead_agent = AssistantAgent(
  27. 25 name = "lead",
  28. 26 system_message = lead_prompt,
  29. 27 llm_config={"config_list": config_list}
  30. 28 )

上述代码应该清晰易懂。在开始聊天之前,需明确交互顺序,因为希望首席架构师在云计算架构师和开源架构师这两个助理提供解决方案之后再进行总结与决策。首席架构师需要审阅这些方案并作出决定。为了确保这一流程得以顺利执行,创建了一个状态转移函数,用于在聊天中选择发言者:

  1. Python
  2. 1 def state_transition(last_speaker, groupchat):
  3. 2 messages = groupchat.messages
  4. 3
  5. 4 if last_speaker is user_proxy:
  6. 5 return cloud_agent
  7. 6 elif last_speaker is cloud_agent:
  8. 7 return oss_agent
  9. 8 elif last_speaker is oss_agent:
  10. 9 return lead_agent
  11. 10 elif last_speaker is lead_agent:
  12. 11 # lead -> end
  13. 12 return Non

这是一个有限状态机(FSM)的简单示例。现在,准备采用下面这段代码来触发聊天:

  1. Python
  2. 1 groupchat = autogen.GroupChat(
  3. 2 agents=[user_proxy, cloud_agent, oss_agent, lead_agent],
  4. 3 messages=[],
  5. 4 max_round=6,
  6. 5 speaker_selection_method=state_transition,
  7. 6 )
  8. 7 manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)
  9. 8
  10. 9 user_proxy.initiate_chat(
  11. 10 manager, message="Provide your best architecture based on these business requirements."
  12. 11 )

解释输出

由于篇幅所限,在这里只展示输出的部分内容。该代码易于复制,对细节感兴趣的人可以直接在自己的环境中运行。

聊天从第一位发言者开始:

  1. Plain Text
  2. 1 supervisor (to chat_manager):
  3. 2
  4. 3 Provide your best architecture based on these business requirements.
  5. 4
  6. 5--------------------------------------------------------------------------------
  7. 6
  8. 7 Next speaker: cloud
  9. 8
  10. 9 cloud (to chat_manager):
  11. 10
  12. 11 ### Chain-of-Thought Approach
  13. 12
  14. 13 #### 1. Understanding the Business Requirements
  15. 14 Based on the given requirements, we need:
  16. 15 - **High-capacity data storage** for IoT data.
  17. 16 ...
  18. 17 ### Advantages of Cloud over On-Premises Architectures
  19. 18 - **Scalability**: Easily scale up/down based on demand without large capital expenditure.
  20. 19 - **Cost Efficiency**: Pay-as-you-go pricing.
  21. 20 - **High Availability**: Built-in options for redundancy and failover.
  22. 21 - **Global Reach**: Deploy services in multiple regions across the world.
  23. 22 - **Ease of Management**: Managed services reduce operational burden.
  24. 23 - **Innovation Velocity**: Rapid deployment of new services and features.
  25. 24 ...
  26. 25
  27. 26 ### Summary Table
  28. 27
  29. 28| Criterion | AWS | Azure | GCP |
  30. 29|-------------------------------|-----------------------------------------|--------------------------------------------|-------------------------------------------------|
  31. 30| **Primary Data Storage** | Amazon S3, DynamoDB | Azure Blob Storage, Cosmos DB | Google Cloud Storage, Bigtable |
  32. 31| **Real-time Analytics** | Amazon Kinesis | Azure Stream Analytics | Google Cloud Dataflow |
  33. 32| **Machine Learning** | Amazon SageMaker | Azure Machine Learning | Google AI Platform
  34. 33 .....

然后,云计算架构师根据提供的需求列出每个云计算提供商的相关服务,解释云计算相对于内部部署的优势,并提供一个汇总表。

接下来是第二位发言者。开源架构师遵循大致相同的方法,列出相关服务,讨论开源的优点,并给出自己的解决方案。

  1. Plain Text
  2. 1 Next speaker: oss
  3. 2
  4. 3 oss (to chat_manager):
  5. 4
  6. 5 ### Chain-of-Thought Approach
  7. 6
  8. 7 #### 1. Understanding the Business Requirements
  9. 8 ....
  10. 9 ### Advantages of Open-Source Adoption
  11. 10 - **Cost Savings**: No licensing fees.
  12. 11 - **Community Support**: Large active communities for troubleshooting and inquiries.
  13. 12- **Flexibility and Customization**: Ability to modify source code to meet unique business needs.
  14. 13- **Innovation**: Rapid adoption of cutting-edge technologies and features.
  15. 14 ...
  16. 15
  17. 16 ### Summary Table
  18. 17
  19. 18| Requirement | Solution Components |
  20. 19|------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
  21. 20| **Data Storage** | Apache Cassandra, HDFS |
  22. 21| **Real-time Analytics** | Apache Kafka, Apache Flink, Apache Spark |
  23. 22| **Machine Learning** | TensorFlow |
  24. 23| **Scalability** | Kubernetes, Docker |
  25. 24| **Disaster Recovery Regions** | Primary: Germany (Berlin), Secondary: Netherlands (Amsterdam) |
  26. 25| **Monitoring** | Prometheus, Grafana, ELK Stack (Elasticsearch, Logstash, Kibana)
  27. 26 ...

最后,首席架构师介入,对各种解决方案的优缺点进行分析,利用适用性矩阵等方法进行评估,并提出最终建议:

  1. Plain Text
  2. 1 Next speaker: lead
  3. 2
  4. 3 lead (to chat_manager):
  5. 4
  6. 5 ### Evaluating Solutions Based on the Business Requirements and Best Practices
  7. 6
  8. 7 #### Analysis and Comparison of Proposed Architectures
  9. 8
  10. 9| **Criterion** | **AWS** | **Azure** | **GCP** | **Open-Source** |
  11. 10|-----------------------------------|----------------------------------------------------------------|----------------------------------------------------------------|----------------------------------------------------------------|---------------------------------------------------------------|
  12. 11| **Primary Data Storage** | Amazon S3, DynamoDB | Azure Blob Storage, Cosmos DB | Google Cloud Storage, Bigtable | Apache Cassandra, HDFS |
  13. 12| **Real-time Analytics** | Amazon Kinesis | Azure Stream Analytics | Google Cloud Dataflow | Apache Kafka, Apache Flink |
  14. 13| **Machine Learning** | Amazon SageMaker | Azure Machine Learning | Google AI Platform | TensorFlow |
  15. 14 ...
  16. 15
  17. 16 ### Advantages and Disadvantages
  18. 17 ...
  19. 18
  20. 19 #### Recommendation:
  21. 20
  22. 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:
  23. 22
  24. 23 - **Primary Recommendation**: **AWS**
  25. 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.
  26. 25
  27. 26 - **Secondary Recommendation**: **Azure**
  28. 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.
  29. 28
  30. 29 - **Alternative Consideration**: **GCP** for organizations already invested in Google technologies or with a requirement for industry-leading machine learning and data analytics capabilities.
  31. 30
  32. 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

理论

提升 YOLO 模型:使用 Albumentations 进行高级数据增强

2024-12-4 16:50:35

理论

刚刚,DeepMind最强「基础世界模型」诞生!单图生1分钟游戏世界,解锁下一代智能体

2024-12-5 8:42:39

0 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
    暂无讨论,说说你的看法吧
个人中心
今日签到
搜索