如何使用AutoGen AI技術(shù)實(shí)現(xiàn)多代理對(duì)話 原創(chuàng)
本文將介紹一個(gè)實(shí)驗(yàn),展示多個(gè)人工智能代理如何通過群聊方式進(jìn)行互動(dòng),并根據(jù)具體的業(yè)務(wù)需求協(xié)同工作,共同生成解決方案的架構(gòu)。
本文介紹如何使用Databricks Community Edition (CE)(一個(gè)免費(fèi)的基于云的平臺(tái))運(yùn)行一些基本的人工智能Python代碼。因?yàn)橹惶幚黹_源庫,所以這個(gè)實(shí)驗(yàn)可以很容易地在任何Python/PySpark環(huán)境中復(fù)現(xiàn)。
AutoGen和可對(duì)話代理簡介
??AutoGen???是一個(gè)開源平臺(tái),使開發(fā)人員能夠創(chuàng)建協(xié)作人工智能代理。這些代理通常通過聊天群組進(jìn)行交互,并共同完成任務(wù)。??可對(duì)話代理???可以發(fā)送、接收和生成消息,也可以使用人工智能模型、工具和人工輸入的組合來定制??蓪?duì)話代理可以是以下任何一種:
- 用戶代理,顧名思義,它是人類的代理,在用戶輸入與人工智能代理回復(fù)之間的某個(gè)環(huán)節(jié)發(fā)揮作用,并能執(zhí)行代碼。
- 一個(gè)或多個(gè)助理代理,它們是使用大型語言模型(LLM)但不需要人工輸入或代碼執(zhí)行的人工智能助理。
以下將在用戶代理(例如,架構(gòu)主管)和三個(gè)助理代理(云計(jì)算架構(gòu)師、開源架構(gòu)師和首席架構(gòu)師)之間創(chuàng)建一個(gè)聊天群組。其目標(biāo)是基于業(yè)務(wù)需求列表提供解決方案架構(gòu),可以用以下的互動(dòng)圖來表示:
對(duì)話流程的工作原理如下:
- 業(yè)務(wù)需求提供給代理。
- 代理發(fā)起架構(gòu)師之間的聊天。
- 云計(jì)算架構(gòu)師將首先發(fā)言,為每個(gè)主要的云計(jì)算提供商:Azure、AWS和谷歌云提供建議。
- 下一位發(fā)言者。
- 開源架構(gòu)師將提供在云計(jì)算領(lǐng)域之外使用開源框架的一個(gè)解決方案。
- 下一位(也是最后一位)發(fā)言者。
- 首席架構(gòu)師將審查所有解決方案并提供最終提案。
提示工程
首先創(chuàng)建提示,從包含一些簡單需求的公共部分(當(dāng)前的任務(wù))開始:
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 '''
云計(jì)算架構(gòu)師提示:
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
開源架構(gòu)師提示:
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
首席架構(gòu)師提示:
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
在完成提示之后,可以開始創(chuàng)建可對(duì)話代理,并讓它們在聊天設(shè)置中進(jìn)行交互。
實(shí)現(xiàn)多代理聊天
由于使用的是Python,需要安裝相應(yīng)的pyautogen包:
Python
1 pip install pyautogen
從導(dǎo)入必要的庫和一些配置開始。雖然可以使用多個(gè)模型,但在這個(gè)簡單的示例中,選擇僅使用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 }
將溫度設(shè)為0可以最大限度地減少模型的隨機(jī)性,使其專注于最可能(即得分最高)的選擇?,F(xiàn)在,將創(chuàng)建具有特定提示的代理:
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 )
上述代碼應(yīng)該清晰易懂。在開始聊天之前,需明確交互順序,因?yàn)橄M紫軜?gòu)師在云計(jì)算架構(gòu)師和開源架構(gòu)師這兩個(gè)助理提供解決方案之后再進(jìn)行總結(jié)與決策。首席架構(gòu)師需要審閱這些方案并作出決定。為了確保這一流程得以順利執(zhí)行,創(chuàng)建了一個(gè)狀態(tài)轉(zhuǎn)移函數(shù),用于在聊天中選擇發(fā)言者:
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
這是一個(gè)有限狀態(tài)機(jī)(FSM)的簡單示例?,F(xiàn)在,準(zhǔn)備采用下面這段代碼來觸發(fā)聊天:
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 )
解釋輸出
由于篇幅所限,在這里只展示輸出的部分內(nèi)容。該代碼易于復(fù)制,對(duì)細(xì)節(jié)感興趣的人可以直接在自己的環(huán)境中運(yùn)行。
聊天從第一位發(fā)言者開始:
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 .....
然后,云計(jì)算架構(gòu)師根據(jù)提供的需求列出每個(gè)云計(jì)算提供商的相關(guān)服務(wù),解釋云計(jì)算相對(duì)于內(nèi)部部署的優(yōu)勢,并提供一個(gè)匯總表。
接下來是第二位發(fā)言者。開源架構(gòu)師遵循大致相同的方法,列出相關(guān)服務(wù),討論開源的優(yōu)點(diǎn),并給出自己的解決方案。
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 ...
最后,首席架構(gòu)師介入,對(duì)各種解決方案的優(yōu)缺點(diǎn)進(jìn)行分析,利用適用性矩陣等方法進(jìn)行評(píng)估,并提出最終建議:
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
需要注意的是,首席架構(gòu)師表達(dá)的意見都是他自己的意見,并不一定反映本文作者的意見。
結(jié)語
這次實(shí)驗(yàn)使用簡單的設(shè)置,創(chuàng)建了一個(gè)基本的架構(gòu)團(tuán)隊(duì)組織,其中不同類型的參與者相互協(xié)作為業(yè)務(wù)提供解決方案。
架構(gòu)師不僅僅是提供技術(shù)層面的建議,他們經(jīng)常面對(duì)復(fù)雜且獨(dú)特的挑戰(zhàn),這要求他們對(duì)業(yè)務(wù)需求有深刻的理解,并對(duì)長期影響進(jìn)行批判性思考。而人工智能在處理某些無形因素方面還存在一些局限,這主要?dú)w因于它缺乏必要的人際交往能力和情商,至少目前是這樣。
原文標(biāo)題:??Multi-Agent Conversation With AutoGen AI???,作者:Tony Siciliani
