打造AI協作未來:淺談MCP、A2A與ACP的角色與應用 與ACP實戰
打造AI協作未來:淺談MCP、A2A與ACP的角色與應用 與ACP實戰¶
內容主要來自Deep Learning AI 課程,一開始介紹了三種AI協議:MCP、A2A與ACP,並說明它們之間的差異與應用場景:
- MCP(Model Context Protocol)模型上下文協議:由 Anthropic 提出,旨在讓 AI 模型能安全且標準化地存取外部資料來源與工具。可比喻為一位聰明學生原本只能依靠課本(訓練資料),但透過 MCP,便能連接圖書館、資料庫、甚至網路搜尋引擎。MCP 擴展了 AI 模型「看見」並「使用」外部世界資訊與功能的能力。
- A2A(Agent-to-Agent Protocol)代理間協議:由 Google 推出,重點在於讓不同功能的 AI 代理能夠無縫溝通與合作。例如,一個擅長語音處理的 AI 可以與文字摘要或圖像生成的 AI 合作,藉由 A2A 協議,這些代理能彼此串接、協同作業。
- ACP(Agent Communication Protocol)代理通訊協議:由 IBM 推動,透過其 BeeAI 框架發展。ACP 是一種輕量級、基於 HTTP 的開放協議,強調代理之間的通用性與模組化,無論使用哪種 AI 架構都能實現整合與重複使用,讓溝通變得更容易,也便於跨平台協作。
實作架構亮點:¶
課程的實作部分聚焦於多代理溝通與工具整合,展示如何活用這些協議建構一個靈活的 AI 協作系統:
1.建構 RAG AI 並透過 ACP 部署至 Server,作為基本服務框架。
%%writefile ../my_acp_project/crew_agent_server.py #輸出文件...#要餵進去RAG的文檔rag_tool.add("../data/gold-hospital-and-premium-extras.pdf", data_type="pdf_file")@server.agent()async def policy_agent(input: list[Message]) -> AsyncGenerator[RunYield, RunYieldResume]: "This is an agent for questions around policy coverage, it uses a RAG pattern to find answers based on policy documentation. Use it to help answer questions on coverage and waiting periods." insurance_agent = Agent( role="Senior Insurance Coverage Assistant", goal="Determine whether something is covered or not", backstory="You are an expert insurance agent designed to assist with coverage queries", verbose=True, allow_delegation=False, llm=llm, tools=[rag_tool], max_retry_limit=5 ) task1 = Task( description=input[0].parts[0].content, expected_output = "A comprehensive response as to the users question", agent=insurance_agent ) crew = Crew(agents=[insurance_agent], tasks=[task1], verbose=True) task_output = await crew.kickoff_async() yield Message(parts=[MessagePart(content=str(task_output))])if __name__ == "__main__": server.run(port=8001)
2.設計一隻具不同邏輯的 AI(health_agent),專門處理健康問題,展現 ACP 協議在異構 AI 系統整合上的彈性。
%%writefile ../my_acp_project/smolagents_server.py...#建立另一隻不同架構的CodeAgent Agent @server.agent()async def health_agent(input: list[Message], context: Context) -> AsyncGenerator[RunYield, RunYieldResume]: "This is a CodeAgent which supports the hospital to handle health based questions for patients. Current or prospective patients can use it to find answers about their health and hospital treatments." agent = CodeAgent(tools=[DuckDuckGoSearchTool(), VisitWebpageTool()], model=model) prompt = input[0].parts[0].content response = agent.run(prompt) yield Message(parts=[MessagePart(content=str(response))])if __name__ == "__main__": server.run(port=8000)
3.透過 ACP 實現 AI 代理的順序性調用,適合用於需依序處理的工作流程。
#當兩隻ACP Agent都架設好後from acp_sdk.client import Clientimport asynciofrom colorama import Fore #可以手動的去呼叫兩隻ACP agent,適用於需要遵循特定流程的應用async def run_hospital_workflow() -> None: async with Client(base_url="http://localhost:8001") as insurer, Client(base_url="http://localhost:8000") as hospital: run1 = await hospital.run_sync( agent="health_agent", input="Do I need rehabilitation after a shoulder reconstruction?" ) content = run1.output[0].parts[0].content print(Fore.LIGHTMAGENTA_EX+ content + Fore.RESET) run2 = await insurer.run_sync( agent="policy_agent", input=f"Context: {content} What is the waiting period for rehabilitation?" ) print(Fore.YELLOW + run2.output[0].parts[0].content + Fore.RESET)asyncio.run(run_hospital_workflow())
4.讓 AI 自主決策應該呼叫哪一隻代理服務,提升智慧調度能力。
#或是如下,把agents 做成一個Jason放進acp_agents,再透過acpagent去決定拿到的task應該呼叫哪個Agentasync def run_hospital_workflow() -> None: async with Client(base_url="http://localhost:8001") as insurer, Client(base_url="http://localhost:8000") as hospital: # agents discovery agent_collection = await AgentCollection.from_acp(insurer, hospital) acp_agents = {agent.name: {'agent':agent, 'client':client} for client, agent in agent_collection.agents} print(acp_agents) # passing the agents as tools to ACPCallingAgent acpagent = ACPCallingAgent(acp_agents=acp_agents, model=model) # running the agent with a user query result = await acpagent.run("do i need rehabilitation after a shoulder reconstruction and what is the waiting period from my insurance?") print(Fore.YELLOW + f"Final result: {result}" + Fore.RESET)
5.於 AI 服務中導入 MCP,使代理不僅能推理,也能操作特定工具,進一步強化其實用性。
整體而言,ACP 的概念也許較新,但與 MCP 和 A2A 一樣,皆致力於建構一個安全、可靠且使用者友善的 AI 溝通與協作環境。這些協議在設計上具有高度相容性,能促進跨專案、跨系統的整合,幫助開發者更專注於商業邏輯與實際應用需求的實現。
Comments
Loading comments…
Leave a Comment