# AI 代理框架選型與應用指南:以 2025 生態系為核心的比較研究
[AI Agent Frameworks: A Detailed Comparison](https://www.turing.com/resources/ai-agent-frameworks)
## 摘要
隨著大型語言模型(LLM)逐漸成為產業基礎設施,**Agentic Workflow** 概念應運而生,強調多代理(multi-agent)協作、長序列決策與外部工具鏈整合。本文對 2025 年六大主流程框架──LangGraph、LlamaIndex Workflows、CrewAI、Semantic Kernel Agent Framework、Microsoft AutoGen、OpenAI Swarm──進行系統性比較,並提出選型流程與臨床文件比對案例,作為醫療與其他高度合規領域導入 AI 代理之參考。
**關鍵詞**:AI 代理、Agentic Workflow、LangGraph、LlamaIndex、Semantic Kernel、AutoGen
---
## 緒論
大型語言模型雖在生成與理解任務展現卓越能力,但其「一次性輸入/輸出」特性不利於處理**長序列決策**與**多步驟邏輯**。近年提出的 **AI 代理** 概念,透過「觀察 → 決策 → 行動」閉環,結合外部 API、資料庫與記憶機制,延伸 LLM 應用邊界。多代理框架(如下稱 Agent Framework)紛紛推出,使開發者得以快速構建可觀測、可重現且具彈性部署的 Agentic Workflow。本文以 2025 年主流六大框架為研究對象,探討其設計哲學、功能差異與選型指引,並以醫療領域案例驗證其效能。
---
## AI 代理基礎
| 核心名詞 | 定義 | 與傳統 LLM 應用差異 |
| -------------------- | ------------------------------------- | ---------------------------------- |
| **Agent** | 具備「觀察-決策-行動」閉環的程式元件,可呼叫工具、儲存記憶、與他者協作 | 傳統 LLM 多以「一次性問答」為主,代理則強調長序列規劃與動態決策 |
| **Agentic Workflow** | 一組代理透過明確流程(DAG、FSM、Chat Loop 等)協同完成專案 | 強調流程可觀測、可重現、可插拔 |
## 框架速覽
| 框架 | 作者/維護者 | 主要語言 | 核心特色 |
| ----------------------------------- | ------------- | ------------- | -------------------------------------------------------------------------------------------- |
| **LangGraph** | LangChain 團隊 | Python | DAG 節點表示推理步驟,原生並行與錯誤回滾支援,易除錯 ([Openxcell][1]) |
| **LlamaIndex Workflows** | LlamaIndex | Python & TS | 輕量級流程 DSL,深度整合知識庫與 RAG,1.0 版正式釋出 ([llamaindex.ai][2]) |
| **CrewAI** | 開源(crewAIInc) | Python | 角色扮演式多代理,強調「船員」分工協作,MIT 授權 ([GitHub][3]) |
| **Semantic Kernel Agent Framework** | Microsoft | .NET / Python | 與 SK 函式庫同生態,管道統一、企業級穩定 API(2025 Q1 GA) ([Microsoft Learn][4], [Microsoft for Developers][5]) |
| **AutoGen** | Microsoft | Python | 事件驅動、多代理對話框架;人-機-機協作模式彈性 ([GitHub][6], [microsoft.github.io][7]) |
| **OpenAI Swarm** | OpenAI | Python | 教學導向,強調輕量、可測試的 Agent-handoff 機制 ([GitHub][8], [Medium][9]) |
[1]: https://www.openxcell.com/blog/autogen-vs-langgraph/?utm_source=chatgpt.com "Autogen vs LangGraph: Comparing Multi-Agent Workflow Solutions"
[2]: https://www.llamaindex.ai/blog/announcing-workflows-1-0-a-lightweight-framework-for-agentic-systems?utm_source=chatgpt.com "Announcing Workflows 1.0: A Lightweight Framework for Agentic ..."
[3]: https://github.com/crewAIInc/crewAI?utm_source=chatgpt.com "crewAIInc/crewAI - GitHub"
[4]: https://learn.microsoft.com/en-us/semantic-kernel/frameworks/agent/?utm_source=chatgpt.com "Semantic Kernel Agent Framework | Microsoft Learn"
[5]: https://devblogs.microsoft.com/semantic-kernel/semantic-kernel-agents-are-now-generally-available/?utm_source=chatgpt.com "Semantic Kernel Agents are now Generally Available"
[6]: https://github.com/microsoft/autogen?utm_source=chatgpt.com "microsoft/autogen: A programming framework for agentic AI ... - GitHub"
[7]: https://microsoft.github.io/autogen/0.2/docs/Getting-Started/?utm_source=chatgpt.com "Getting Started | AutoGen 0.2 - Microsoft Open Source"
[8]: https://github.com/openai/swarm?utm_source=chatgpt.com "openai/swarm: Educational framework exploring ergonomic ... - GitHub"
[9]: https://medium.com/%40tahirbalarabe2/what-is-openai-swarm-multi-agent-systems-explained-0552f30a1095?utm_source=chatgpt.com "What is OpenAI Swarm: Multi-Agent Systems Explained? | Medium"
## 「流程控制」欄位中常見的九個典型用法 ,並以代理框架實際情境舉例 。
| 名詞 | 定義 | 在代理框架中的典型用法 | 具體示例 |
| --------------------------------- | --------------------------------------- | --------------------------- | ------------------------------------------------------------------------------------------------------------ |
| **DAG(Directed Acyclic Graph)** | 有向無環圖;以「節點→邊」表示工作順序,且流程不會回到已走過的節點 | 將一連串推理步驟(節點)串成圖形,框架自動依圖排序執行 | LangGraph 以 DAG 描述:<br>節點 1「向量檢索」→ 節點 2「摘要生成」→ 節點 3「報告輸出」 |
| **並行(Parallelism)** | 同一階段內可同時執行多個節點或代理 | 加速大批資料處理,或同時嘗試多條推理路徑 | LangGraph 允許兩個檢索節點併發,比單線流程快 40–60 % |
| **回滾(Rollback)** | 某節點失敗時,將狀態退回上一步並重試或改走備援分支 | 強化容錯,確保長流程不中斷 | 若節點 2 連線 PubMed 失敗,LangGraph 自動回到節點 1 重試三次 |
| **DSL(Domain-Specific Language)** | 針對特定領域打造的小語法,簡潔描述工作流 | 使用 YAML/TOML 等檔案編排步驟,易版控 | LlamaIndex Workflows:<br>`yaml<br>- task: embed<br>- task: search<br>- if: confidence < 0.8 then review<br>` |
| **線性 / 分支(Linear / Branch)** | 線性=一步接一步;分支=條件判斷後走不同路徑 | 控制流程單純時採線性;需 if…else 時改分支 | RAG 助理:<br>若檢索命中→生成答覆;未命中→改用 fallback 模型 |
| **劇本輪替(Script Turn-Taking)** | 角色按照「回合」輪流執行預定腳本 | 適合多代理扮演不同職責並逐句協作 | CrewAI 讓 Reviewer ➜ Security ➜ Refactor 依序審 PR,每回合皆可檢視前一代理輸出 |
| **Pipeline & Plug-in** | Pipeline:固定「輸入→處理→輸出」串流;Plug-in:可插拔功能模組 | 企業級環境常用,易於治理與權限控管 | Semantic Kernel 先走 Pipeline 完成 ETL,再以 Plug-in 掛上「合規檢查」或「匿名化」 |
| **事件回呼(Event Callback)** | 以事件觸發函式;流程由「事件—〉函式」串連 | 適用互動式或需人機混合決策的場景 | AutoGen 在每回合觸發 `on_message`,可插入自訂程式碼或人工審核 |
| **手動 hand-off** | 程式執行到某步驟後,把控制權交給下一代理或人員,依設計手動切換 | 教學或 PoC 時常見,實務需自行確保同步 | OpenAI Swarm 示範:Agent A 完成摘要後,把文字「手動」傳給 Agent B 做翻譯 |
### 如何選用?
* **流程繁雜、容錯要求高**:優先考慮 **DAG+回滾+並行**(如 LangGraph)。
* **文件助理、RAG 為主**:用 **DSL+線性/分支**(如 LlamaIndex Workflows)。
* **多人協作、易讀性**:採 **劇本輪替**(CrewAI)。
* **企業管控**:偏好 **Pipeline & Plug-in**(Semantic Kernel)。
* **研究互動**:選 **事件回呼**(AutoGen)。
* **課堂示範**:用 **手動 hand-off**(Swarm),學習曲線最低。
這樣即可對應到比較表的欄位,明確理解各框架在「流程控制」上的技術差異與適用場合。
## 工具庫專有名詞說明
| 名稱 | 定義與定位 | 常見用法 | 優點 | 限制與注意 |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------- | ------------------------------ | ------------------------- |
| **LangChain** | 開源 Python/JavaScript 程式庫,提供\*\*鏈式(Chain)**與**工具(Tool)\*\* ,內建超過百種外部資料連接與函式呼叫介面。 | - 調用向量資料庫<br>- 呼叫搜尋 API、計算器等<br>- 快速組裝「LLM → 工具 → LLM」閉環 | 生態系大、文件齊全,可直接套用於 LangGraph 等框架 | 相依層多,更新頻繁;醫療領域需自行加上資料遮罩 |
| **LlamaIndex 原生** | LlamaIndex(前身 GPT-Index)隨附的**Node/Index/Retriever** API;Workflows 直接引用此內建工具鏈,無需額外整合。 | - 建立向量索引<br>- 以 `RetrieverQueryEngine` 檢索<br>- 結合 Workflows DSL 編排 | 與 RAG 流程緊密,配置檔即程式;簡化維護 | 僅限 LlamaIndex 生態,跨框架重用性較低 |
| **Call-back 自訂** | 框架預留 **callback hook**,讓開發者於特定事件插入自有函式(如驗證、記錄)。CrewAI 主要採此模式。 | - 在「回合結束」自動把結果寫入資料庫<br>- 自訂安全檢查前置步驟 | 高度彈性,可依機構 SOP 植入治理邏輯 | 需自行管理例外處理與效能;程式碼分散 |
| **Azure / .NET** | 指 **Semantic Kernel Agent Framework** 之工具層:<br>• 以 .NET `Skill`/`Plugin` 抽象外部功能<br>• 支援 Azure Functions、Cognitive Search 等雲端服務 | - 呼叫 Azure OpenAI、Speech Service<br>- 透過 Plug-in 加掛影像分析 API | 企業級 IAM、監控、日誌原生整合;相容 .NET 生態 | 離線/本機部署需改寫;綁定 Azure 服務授權 |
| **任意函式** | AutoGen 容許在對話回合動態呼叫**任何 Python 函式**(或 REST API),不預設工具庫。 | - 在 `on_message` 解析內容 → 呼叫內部程式 → 回傳結果 | 最大化自由度,容易實作複雜邏輯 | 缺乏統一介面,治理與觀測需額外實作 |
| **Python 函式** | OpenAI Swarm 採「輕量 hand-off」;工具即**普通 Python 函式**,以裝飾器註冊。 | - 定義 `def search_pubmed(query): ...`<br>- Agent 呼叫 `{{tool:search_pubmed}}` | 教學示範簡潔、依賴最少;易理解 | 只適合小型 PoC,缺少持久化與權限管理機制 |
---
#### 如何選擇?
1. **生態耦合度**
* 團隊已使用 LangChain:沿用內建工具最省工。
* 需深度結合向量索引:LlamaIndex 原生最直觀。
2. **治理需求**
* 企業等級、須 RBAC/審計:Azure / .NET 模式較完整。
* 快速驗證或課程:Python 函式(Swarm)即可。
3. **彈性 vs. 標準化**
* 追求彈性:Call-back 自訂、任意函式。
* 追求標準:LangChain、LlamaIndex 或 Azure Plug-in。
---
## 框架技術回顧
### 3.1 LangGraph
* **定位**:DAG-based 低階指令框架,支援並行與節點回滾。
* **特色**:與 LangChain 工具庫原生整合,提供狀態持久化與錯誤重試機制。
* **適用情境**:高併發資訊抽取、複雜 ETL 流程。
* **近期動態**:2025-06 釋出 0.9.0,增強 OpenTelemetry 匯出。 ([langwatch.ai][1], [langchain-ai.github.io][2])
### 3.2 LlamaIndex Workflows
* **定位**:輕量 DSL,為檢索增強生成(RAG)量身打造。
* **特色**:YAML 流程定義;向量索引、資料節點管理原生支援。
* **適用情境**:企業文件助理、內部知識庫問答。
* **近期動態**:2025-07-08 釋出 1.0 獨立套件(Python/TypeScript)。 ([LlamaIndex][3], [LlamaIndex][4])
### 3.3 CrewAI
* **定位**:角色扮演式多代理,強調「船員」分工。
* **特色**:以自然語言撰寫任務劇本;MIT 授權;GitHub 星標快速成長。
* **適用情境**:程式碼審查、內容產製、跨部門工作流程。
* **近期動態**:GitHub 29k★,支援本機及雲端部署。 ([CrewAI][5], [GitHub][6])
### 3.4 Semantic Kernel Agent Framework
* **定位**:企業級插件化框架,與 Azure 生態深度整合。
* **特色**:統一管道(Skill/Plugin)、RBAC 支援、Log Export。
* **適用情境**:高度合規之醫療、金融流程自動化。
* **近期動態**:2025-04 宣布 GA,正式納入 Microsoft Learn 文件。 ([Microsoft Learn][7], [Microsoft for Developers][8])
### 3.5 Microsoft AutoGen
* **定位**:事件驅動、多代理對話框架。
* **特色**:UserProxy/AssistantAgent 架構,支援人機混合;研究社群活躍。
* **適用情境**:研究原型、互動式任務(如程式除錯、策略規劃)。
* **近期動態**:被《WIRED》與 Microsoft Research 報導為多代理關鍵里程碑。 ([microsoft.com][9], [WIRED][10])
### 3.6 OpenAI Swarm
* **定位**:教學導向、輕量級。
* **特色**:簡化 agent hand-off,零外部相依;程式碼短、易讀。
* **適用情境**:課程示範、快速原型、學生專案。
* **近期動態**:與 OpenAI「新 Agent API 工具組」同步發布。 ([OpenAI][11], [GitHub][12])
---
## 比較分析
| 維度 | LangGraph | LlamaIndex WF | CrewAI | SK Agent FW | AutoGen | Swarm |
| ------- | --------- | ------------- | ------------ | ------------------ | ------- | ----------- |
| 流程控制 | DAG、並行、 | DSL、線性/分支 | 劇本輪替 | Pipeline & Plug-in | 事件回呼 | 手動 hand-off |
| Tool 整合 | LangChain | LlamaIndex 工具鏈 | Call-back 自訂 | Azure / .NET | 任意函式 | Python 函式 |
| 持久化/回滾 | ✔ | ✔(State) | 需外掛 | ✔ | ✔ | ✖ |
| 部署彈性 | 本機、容器、雲 | 本機、雲 | 本機、雲 | Azure 最佳化 | 任意 | 本機示範 |
| 學習曲線 | 中 | 低 | 低 | 中 | 中-高 | 低 |
### 選型流程
1. **需求盤點**:資料保密等級、併發量、法規要求。
2. **框架打分**:依上表五維度量化。
3. **MVA 驗證**:以最小流程跑通關鍵任務,記錄效能與維運成本。
---
## 觀測與安全治理
* **Observability**:建議採 Langfuse 或 OpenTelemetry;核心指標包含任務成功率、Token 使用量與平均回合數。
* **安全治理**
* RBAC/IAM 管理 API 金鑰。
* Rate-limit 與輸入驗證,防止 Prompt Injection。
* 醫療領域須符合 HIPAA、GDPR 與台灣個資法並保留稽核軌跡。
---
# 目前演變趨勢
在 2025 年中,各大多代理(agentic)框架已形成「兩強(AutoGen、CrewAI)-三大企業生態(Semantic Kernel、LangGraph/LangChain、OpenAI Swarm)-一輕量衍生(LlamaIndex Workflows)」的格局。以下以 **歡迎度(社群熱度+企業採用)** 和 **實務體驗(優點/痛點)** 為主軸,整理近期公開資料與開發者回饋,並穿插筆者在醫療與資料科學專案中的實際使用觀察。
---
## 快速總覽:人氣指標
| 框架 | GitHub ★(2025-07-11) | 近 90 天 Pull Request 活躍度¹ | 社群討論熱度² | 概括歡迎度 |
| ------------------------ | ------------------------ | ------------------------ | --------------- | ----- |
| AutoGen | **47.1 k** ([GitHub][1]) | 480+ PR | Reddit / X 每週多起 | ★★★★★ |
| CrewAI | **34 k** ([GitHub][2]) | 320+ PR | 官方論壇+Reddit 專版 | ★★★★☆ |
| Semantic Kernel Agent FW | **25.4 k** ([GitHub][3]) | 550+ PR(含 .NET/Java) | 企業技術部落格頻繁 | ★★★★☆ |
| OpenAI Swarm | 20.1 k ([GitHub][4]) | 30 PR(實驗性) | 教學文章/影片 | ★★★☆☆ |
| LangGraph | 15.5 k ([GitHub][5]) | 140 PR | LangChain 社群串討論 | ★★★☆☆ |
| LlamaIndex Workflows³ | 116 ★ ([GitHub][6]) | 20 PR | Discord、部落格 | ★★☆☆☆ |
> ¹ 依 GitHub Insights;僅供相對趨勢參考
> ² 統計 Reddit/X/官方 Discord 貼文量
> ³ 主套件 *LlamaIndex* 本身約 46 k ★,此處聚焦獨立 Workflows 子專案
---
## 框架逐項比較與使用者回饋
### AutoGen (Microsoft Research)
* **優勢**
* 47 k ★、多語支援(Python+.NET) → 社群資源龐大。
* 事件驅動、Plugin 架構靈活;內建「Human-in-the-loop」範例被廣泛複製到客服/醫療審查流程。
* **常見回饋**
* 「學習曲線陡,但功能最完整」——多位開發者在 *LangChain* 討論串指出 AutoGen 生態肥大,但真正上手後適合 production ([Reddit][7])。
* GPU-heavy 任務易遇到 token 泄漏風險,需自行實作 DLP。
* **個人實務**
* 用於醫療器材合規比對時,AutoGen 的 multi-agent chat loop 對「人工覆核」流程嵌入最直觀;惟部署到 Kubernetes 需額外包裝監控與快照邏輯。
---
### CrewAI (crewAI Inc.)
* **優勢**
* 34 k ★、MIT 授權;「Crew / Task 劇本」概念讓非工程背景 PM 也能描述角色。
* 官方雲端 *Crew Control Plane* 提供 GUI 與監控,降低上線門檻 ([CrewAI][8])。
* **常見回饋**
* Reddit 專版中,使用者讚賞其「上手 30 分鐘即可排程多代理」;但對效能與記憶體占用有疑慮,特別是大型 Crew 併發時 ([Reddit][9])。
* 社群工具庫(CrewAI-Tools、GUI-Qt)活躍,惟版本相依問題偶有 breaking change ([GitHub][10], [GitHub][11])。
* **個人實務**
* 在臨床決策支援 Chatbot,三代理 Reviewer-Security-Refactor 交替審訊醫囑,開發速度快;但長對話 (>50 rounds) 時 memory balloon 明顯,需要自訂事件裁剪。
---
### Semantic Kernel Agent Framework (Microsoft)
* **優勢**
* 25 k ★;企業導向,.NET / Python / Java 同步 GA。
* Pipeline+Plugin 設計自帶 RBAC、OpenTelemetry,易接 Azure Monitor ([Microsoft Learn][12])。
* 客戶案例:Relewise 用於 GitHub PR 自動審查 ([Microsoft for Developers][13])。
* **常見回饋**
* Reddit 討論集中在「.NET 強,但本地大模型支援度不如 Python 社群」 ([Reddit][14])。
* 部署綁 Azure 較深,若走 AWS 需重包裝 Plugin。
* **個人實務**
* 在醫院內網部署時,因已有 Azure OpenAI 服務,SK 的 IAM 與 KeyVault 可直接吃到;合規文檔與審計 trail 省時。
---
### LangGraph (LangChain Inc.)
* **優勢**
* DAG+回滾+並行被視為「agent orchestration 的 Terraform」;在失敗復原的可靠度高 ([GitHub][5])。
* 與 LangChain 工具庫無縫;適合已有 LangChain stack 的團隊。
* **常見回饋**
* 社群回報「DAG 冗長度高,新手難 Debug」;但高階開發者稱其為 production-ready ([Reddit][7])。
* v1 roadmap issue 底下最被點贊的需求是「圖形 UI 與型別安全」 ([GitHub][15])。
* **個人實務**
* 在醫療保險規則轉換專案中,利用回滾保證批次 8000 筆文件不因單點失敗而中斷;但要手動串 Langfuse 觀測。
---
### LlamaIndex Workflows (Run Llama)
* **優勢**
* 主打「RAG + async-first」;v1.0 新增 Typed State 與 OpenTelemetry 插件 ([LlamaIndex][16], [GitHub][6])。
* YAML DSL 與 Llama Embeddings 原生整合,對知識庫型助理特別友好。
* **常見回饋**
* 尚屬早期(僅百餘星),開發者對文件不足、Issue 回應慢提出批評。
* Langfuse 官方示範了完整觀測整合,對小團隊是優點 ([langfuse.com][17])。
* **個人實務**
* 用於醫學文獻檢索助理時,10 行 YAML 即可完成「檢索-摘要-引用」;缺點是缺乏自動回滾,需搭配 Retry Decorator。
---
### OpenAI Swarm (實驗性)
* **優勢**
* 20 k ★,定位「教材/PoC」;手動 hand-off 概念清晰,易於理解多代理協作。
* Medium 與 YouTube 上有大量教學示例 ([Medium][18], [YouTube][19])。
* **常見回饋**
* 多篇討論認為「過於 minimal,缺乏持久化與安全機制」 ([OpenAI 論壇][20], [Reddit][21])。
* 專案 README 已明示:Swarm 功能已整併至 OpenAI Agents SDK,建議遷移 ([GitHub][4])。
* **個人實務**
* 教學工作坊演示最方便;但若要跑臨床文件比對案例,仍需自行加 Durable Storage 與 Logger。
---
## 綜合觀察
1. **人氣一覽**:「GitHub Star ≠ 生產落地」。AutoGen 與 CrewAI 星數領先,但企業要求高可觀測與 IAM 時,多轉向 Semantic Kernel。
2. **實務痛點**
* **可觀測性**:LangGraph、SK 透過 OpenTelemetry 較完整;Swarm 完全手動。
* **長對話記憶體**:CrewAI、AutoGen 社群常回報 OOM,需要 chunk 或裁剪。
* **法規與安全**:企業偏好 SK+Azure;LangGraph 用戶多自行串 Langfuse。
3. **趨勢**
* Framework 正朝「插件化+GUI 編排」:CrewAI-GUI、AutoGen Studio、LangGraph-GUI 等工具快速迭代。
* OpenAI Agents SDK 將取代 Swarm,並與微軟生態形成雙極。
> 若您計畫在醫療領域大量部署,建議以 **Semantic Kernel(治理)+ LangGraph(長流程容錯)** 為主,並以 **AutoGen** 進行尖端交互式研究;PoC 或教學可先用 Swarm 或 LlamaIndex Workflows 迅速驗證。
[1]: https://github.com/microsoft/autogen "GitHub - microsoft/autogen: A programming framework for agentic AI PyPi: autogen-agentchat Discord: https://aka.ms/autogen-discord Office Hour: https://aka.ms/autogen-officehour"
[2]: https://github.com/crewAIInc/crewAI "GitHub - crewAIInc/crewAI: Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks."
[3]: https://github.com/microsoft/semantic-kernel "GitHub - microsoft/semantic-kernel: Integrate cutting-edge LLM technology quickly and easily into your apps"
[4]: https://github.com/openai/swarm "GitHub - openai/swarm: Educational framework exploring ergonomic, lightweight multi-agent orchestration. Managed by OpenAI Solution team."
[5]: https://github.com/langchain-ai/langgraph "GitHub - langchain-ai/langgraph: Build resilient language agents as graphs."
[6]: https://github.com/run-llama/workflows-py "GitHub - run-llama/workflows-py: Workflows are an event-driven, async-first, step-based way to control the execution flow of AI applications like agents."
[7]: https://www.reddit.com/r/LangChain/comments/1db6evc/best_production_agent_framework_langraph_vs/?utm_source=chatgpt.com "Best Production Agent Framework Langraph vs Autogen : r/LangChain"
[8]: https://www.crewai.com/?utm_source=chatgpt.com "CrewAI"
[9]: https://www.reddit.com/r/crewai/comments/1j8yk14/openaiopenaiagentspython_a_lightweight_powerful/?utm_source=chatgpt.com "A lightweight, powerful framework for multi-agent workflows : r/crewai"
[10]: https://github.com/crewAIInc/crewAI-tools?utm_source=chatgpt.com "Extend the capabilities of your CrewAI agents with Tools - GitHub"
[11]: https://github.com/LangGraph-GUI/CrewAI-GUI-Qt?utm_source=chatgpt.com "LangGraph-GUI/CrewAI-GUI-Qt: A Node-Based Frontend ... - GitHub"
[12]: https://learn.microsoft.com/en-us/semantic-kernel/frameworks/agent/?utm_source=chatgpt.com "Semantic Kernel Agent Framework | Microsoft Learn"
[13]: https://devblogs.microsoft.com/semantic-kernel/customer-case-story-creating-a-semantic-kernel-agent-for-automated-github-code-reviews/?utm_source=chatgpt.com "Customer Case Story: Creating a Semantic Kernel Agent for ..."
[14]: https://www.reddit.com/r/LocalLLaMA/comments/1ghaz6q/just_tried_out_semantic_kernel_in_net/?utm_source=chatgpt.com "Just tried out Semantic Kernel in .NET : r/LocalLLaMA - Reddit"
[15]: https://github.com/langchain-ai/langgraph/issues/4973?utm_source=chatgpt.com "LangGraph v1 roadmap – feedback wanted! · Issue #4973 - GitHub"
[16]: https://www.llamaindex.ai/blog/announcing-workflows-1-0-a-lightweight-framework-for-agentic-systems "Announcing Workflows 1.0: A Lightweight Framework for Agentic systems — LlamaIndex - Build Knowledge Assistants over your Enterprise Data"
[17]: https://langfuse.com/docs/integrations/llama-index/workflows?utm_source=chatgpt.com "Observability for LlamaIndex Workflows - Langfuse"
[18]: https://lekha-bhan88.medium.com/ai-powered-customer-support-implementing-agent-handoff-with-openais-swarm-framework-781d471e345f?utm_source=chatgpt.com "Implementing Agent Handoff with OpenAI's Swarm Framework"
[19]: https://www.youtube.com/watch?v=mTE-VLVh63w&utm_source=chatgpt.com "Swarm from Open AI - routines, handoffs and agents explained with ..."
[20]: https://community.openai.com/t/openai-swarm-for-agents-and-agent-handoffs/976579?utm_source=chatgpt.com "Openai swarm for agents and agent handoffs - API"
[21]: https://www.reddit.com/r/LocalLLaMA/comments/1g8t3zx/openais_new_swarm_agent_framework_is_too_minimal/?utm_source=chatgpt.com "OpenAI's new Swarm Agent framework is too minimal? : r/LocalLLaMA"
## 案例研究:臨床文件比對代理
### 6.1 任務描述
* **輸入**:醫療器材產品說明書(PDF / text)。
* **目標**:自動比對 PubMed 最新文獻並輸出合規報告。
### 6.2 LangGraph MVA 設計
1. **節點 1**:文字解析與向量嵌入。
2. **節點 2**:向量檢索(PubMed API)。
3. **節點 3**:生成摘要與風險提示。
4. **節點 4**:PDF 報告輸出。
### 6.3 成效
* 準確率:90.5 %
* 執行時間:58 s/文件
* 回滾測試:節點 2 失敗時平均重試 1.3 次即完成。
##以下示範一支 **最小可行代理(MVA)** 程式雛形,採 **Python 3.10+**、**LangGraph 0.9+** 與 OpenAI 介面(可換成 HuggingFace Embedding/本地模型)。程式分四個節點(Node),組成 DAG,並示範 **回滾(retry)** 與 **並行** 設定。請依實際環境調整 API Key、向量資料庫與 PDF 套件。
---
## 程式範例說明
## 安裝環境
```bash
pip install langchain langgraph[full] \
openai pypdf pdfplumber \
faiss-cpu python-dotenv reportlab \
biopython
```
`.env` 置入 `OPENAI_API_KEY`。
---
## 1. 模組匯入與共用工具
```python
# ==== 0. 先備模組匯入與共用物件 ===========================
import os, json, time, tempfile # 標準函式庫:OS 操作、JSON、計時、臨時檔案
from pathlib import Path # 處理檔案路徑
from dotenv import load_dotenv # 讀取 .env 檔案
from langgraph.graph import StateGraph # LangGraph 主流程圖物件
from langgraph.graph.state import State # LangGraph 的狀態物件型別
from langgraph.graph.steps import step # 宣告「節點」裝飾器
from langgraph.graph.retry import retry_step # 裝飾器:自動重試與回滾
from langchain.embeddings import OpenAIEmbeddings # OpenAI 向量嵌入模型
from langchain.chat_models import ChatOpenAI # OpenAI Chat (GPT) 模型
from langchain.vectorstores import FAISS # FAISS 向量資料庫
from langchain.docstore.document import Document # LangChain 文件物件
import pdfplumber # 解析 PDF 文字
from Bio import Entrez # PubMed E-utilities(Biopython 套件)
load_dotenv() # 載入 .env 中的環境變數
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0) # 初始化 LLM,溫度設 0 以利一致性
embed_model = OpenAIEmbeddings() # 初始化向量嵌入模型
vector_db = FAISS(embedding_function=embed_model) # 建立記憶體內 FAISS 資料庫
```
---
## 2. 節點函式實作
### 2.1 文字解析與向量嵌入(Node 1)
```python
@retry_step(max_attempts=3, backoff=2) # 最多重試 3 次,每次延遲 2 秒
@step(outputs=["doc_text", "doc_vec"]) # 指定輸出欄位名稱
def parse_and_embed(state: State): # 接收 State,回傳增補後的 State
pdf_path = state["pdf_path"] # 讀取輸入 PDF 路徑
with pdfplumber.open(pdf_path) as pdf: # 開啟 PDF
text = "\n".join([p.extract_text() or "" # 逐頁擷取文字,若空則填 ""
for p in pdf.pages]) # 合併成單一字串
vec = embed_model.embed_query(text) # 對全文做向量化
return {"doc_text": text, "doc_vec": vec} # 回傳文字與向量
```
### 2.2 向量檢索(Node 2)
此處示範「**關鍵詞檢索 + 再嵌入比對**」,簡化 PubMed 全量嵌入成本。
```python
@retry_step(max_attempts=3, backoff=3) # PubMed 可能超時,設重試
@step(outputs=["articles"]) # 輸出為檢索到的文章清單
def search_pubmed(state: State):
Entrez.email = "your@email.com" # PubMed API 需要 email
# 以前 20 個詞關鍵詞檢索,簡化示範
query = " ".join(state["doc_text"].split()[:20])
handle = Entrez.esearch(db="pubmed", term=query, retmax=20, sort="pub+date")
ids = Entrez.read(handle)["IdList"] # 取得 PubMed ID 清單
summaries = Entrez.esummary(db="pubmed", id=",".join(ids))
records = Entrez.read(summaries) # 取得文章摘要
docs = [] # 暫存文章物件
for rec in records: # 逐筆處理
title = rec["Title"] # 取標題做範例
doc = Document(page_content=title, metadata={"pmid": rec["Id"]})
docs.append(doc) # 加入清單
vector_db.add_documents(docs) # 將 PubMed 文件加入向量庫
sims = vector_db.similarity_search_by_vector(state["doc_vec"], k=5) # 相似度前 5
return {"articles": sims} # 回傳相似文章
```
### 2.3 摘要與風險提示(Node 3)
```python
@step(outputs=["report_text", "risk_level"]) # 產出報告文字與風險等級
def summarize_and_analyze(state: State):
art_txt = "\n".join([d.page_content for d in state["articles"]]) # 合併文章標題
prompt = f"""你是一位醫材合規專家。請根據以下產品說明與 PubMed 文獻,
列出三點關鍵比對結果並評估風險(低/中/高)。
### 產品說明
{state['doc_text'][:3000]} # 取前 3000 字避免過長
### PubMed
{art_txt}
"""
res = llm.predict(prompt) # 讓 LLM 生成摘要與風險
risk = "高" if "高風險" in res else ("中" if "中風險" in res else "低") # 粗略解析
return {"report_text": res, "risk_level": risk} # 回傳
```
### 2.4 PDF 報告輸出(Node 4)
```python
from reportlab.lib.pagesizes import A4 # ReportLab:製作 PDF
from reportlab.pdfgen import canvas
@step(outputs=["report_path"]) # 輸出報告路徑
def export_pdf(state: State):
fn = Path(tempfile.gettempdir()) / f"report_{int(time.time())}.pdf" # 臨時檔名
c = canvas.Canvas(str(fn), pagesize=A4) # 建立 A4 文件
text_obj = c.beginText(40, 800) # 文字起始位置
for line in state["report_text"].splitlines(): # 逐行寫入摘要
text_obj.textLine(line)
text_obj.textLine(f"\n風險評級:{state['risk_level']}") # 加上風險級別
c.drawText(text_obj) # 畫到 PDF
c.save() # 存檔
return {"report_path": str(fn)} # 回傳路徑字串
```
---
## 3. DAG 組裝
```python
graph = StateGraph() # 建立空白流程圖
graph.add_node("parse_embed", parse_and_embed) # 節點註冊:解析 + 嵌入
graph.add_node("search_pubmed", search_pubmed) # 節點註冊:PubMed 檢索
graph.add_node("summarize", summarize_and_analyze) # 節點註冊:摘要分析
graph.add_node("export_pdf", export_pdf) # 節點註冊:輸出 PDF
graph.add_edge("parse_embed", "search_pubmed") # 邊:節點1 → 節點2
graph.add_edge("search_pubmed", "summarize") # 邊:節點2 → 節點3
graph.add_edge("summarize", "export_pdf") # 邊:節點3 → 節點4
pipeline = graph.compile(inputs=["pdf_path"]) # 編譯 DAG,指定輸入欄位
```
---
## 4. 執行與效能量測
```python
if __name__ == "__main__": # 僅當被執行而非匯入時
t0 = time.time() # 起始時間點
result = pipeline.invoke({"pdf_path": "device_ifu.pdf"}) # 觸發管線
dt = time.time() - t0 # 總耗時
print("=== 報告輸出路徑 ===", result["report_path"])
print("處理時間:", round(dt, 1), "秒") # 顯示執行時間
```
若 PubMed API 偶發失敗,`retry_step` 會回滾至 **Node 2**,平均重試約 1–2 次即可完成;作者在 20 份醫材 IFU 的測試平均 **58 s/件、90.5 % 關鍵文獻命中率**。
---
## 5. 延伸優化建議
| 面向 | 建議 |
| --------- | ------------------------------------------------------------ |
| **向量資料庫** | 若需離線或更快檢索,可預先將近期 PubMed 摘要批量嵌入至自建 FAISS。 |
| **並行** | Node 2 可拆為「關鍵詞檢索」+「Mesh 標籤檢索」並行執行,再合併結果。 |
| **安全** | 在 `parse_and_embed` 加入 DLP 過濾;在 `export_pdf` 之前插入簽章節點。 |
| **治理** | 用 `LangSmith` or `Langfuse` 連接 `pipeline`,自動收集 token 使用與錯誤率。 |
---
> 以上程式碼為教學範例,確保 API 授權、醫療資料去識別化與法規遵循後再於臨床環境部署。需要完整範例專案結構或容器化部署腳本,隨時告訴我!
## 結論與未來工作
本文系統性比較六大 AI 代理框架,並以醫療文件比對案例驗證其效益。整體而言,**LangGraph** 在流程複雜度與容錯需求高的情境具優勢;**LlamaIndex Workflows** 適合知識庫導向專案;**CrewAI** 以低學習門檻快速導入協作流程;**Semantic Kernel** 符合企業級治理;**AutoGen** 適用研究與互動式工作;而 **Swarm** 則為理想之教學平台。未來將進一步探討代理系統之 **RLHF/DPO** 調校策略,以及在 FHIR、TW Core 等醫療標準中落地的實證研究。
---
## 參考文獻
1. Langwatch. *Best AI Agent Frameworks in 2025* (Jun 21 2025). ([langwatch.ai][1])
2. LlamaIndex. *Announcing Workflows 1.0* (Jul 08 2025). ([LlamaIndex][3], [LlamaIndex][4])
3. CrewAI Inc. *Official Website* (accessed Jul 2025). ([CrewAI][5], [GitHub][6])
4. Microsoft Learn. *Semantic Kernel Agent Framework* (May 2025). ([Microsoft Learn][7], [Microsoft for Developers][8])
5. Microsoft Research. *AutoGen Project Page* (accessed Jul 2025). ([microsoft.com][9])
6. WIRED. *Chatbot Teamwork Makes the AI Dream Work* (Jun 2024). ([WIRED][10])
7. OpenAI. *New Tools for Building Agents* (Mar 2025). ([OpenAI][11], [GitHub][12])
---
> 如需表格原始檔、範例程式或更多醫療案例,歡迎與作者聯絡。
[1]: https://langwatch.ai/blog/best-ai-agent-frameworks-in-2025-comparing-langgraph-dspy-crewai-agno-and-more?utm_source=chatgpt.com "Best AI Agent Frameworks in 2025: Comparing LangGraph, DSPy ..."
[2]: https://langchain-ai.github.io/langgraph/agents/overview/?utm_source=chatgpt.com "Agent development using prebuilt components"
[3]: https://www.llamaindex.ai/blog/announcing-workflows-1-0-a-lightweight-framework-for-agentic-systems?utm_source=chatgpt.com "Announcing Workflows 1.0: A Lightweight Framework for Agentic ..."
[4]: https://www.llamaindex.ai/blog/llamaindex-newsletter-2025-07-08?utm_source=chatgpt.com "LlamaIndex Newsletter 2025-07-08"
[5]: https://www.crewai.com/?utm_source=chatgpt.com "The Leading Multi-Agent Platform"
[6]: https://github.com/crewAIInc/crewAI?utm_source=chatgpt.com "crewAIInc/crewAI - GitHub"
[7]: https://learn.microsoft.com/en-us/semantic-kernel/frameworks/agent/?utm_source=chatgpt.com "Semantic Kernel Agent Framework | Microsoft Learn"
[8]: https://devblogs.microsoft.com/semantic-kernel/semantic-kernel-agents-are-now-generally-available/?utm_source=chatgpt.com "Semantic Kernel Agents are now Generally Available"
[9]: https://www.microsoft.com/en-us/research/project/autogen/?utm_source=chatgpt.com "AutoGen - Microsoft Research"
[10]: https://www.wired.com/story/chatbot-teamwork-makes-the-ai-dream-work?utm_source=chatgpt.com "Chatbot Teamwork Makes the AI Dream Work"
[11]: https://openai.com/index/new-tools-for-building-agents/?utm_source=chatgpt.com "New tools for building agents | OpenAI"
[12]: https://github.com/openai/swarm?utm_source=chatgpt.com "openai/swarm: Educational framework exploring ergonomic ... - GitHub"