← Writing
AI & Generative AI

Hierarchical Multi-Agent Systems

Data Mastery Series — Episode 59: การสร้างระบบ AI ทีมงานด้วย Supervisor Agent กับทีมย่อย

2 May 202513 min readLangChainLangGraphRAGAI AgentDashboard
Multi-Agent Systems · Part 1 of 4

Hierarchical Multi-Agent Systems

Data Mastery Series — Episode 59: การสร้างระบบ AI ทีมงานด้วย Supervisor Agent กับทีมย่อย

📌 Connect with me and follow our journey: Linkedin, Facebook

ในซีรีส์ Data Mastery เราได้สำรวจศักยภาพของ LangGraph ผ่านหลายตอนที่ผ่านมา ทั้ง LangGraph Introduction, Agentic RAG, Adaptive RAG, Corrective RAG, Self RAG, SQL RAG, Prompt Template Agent, Network Multi-Agent Systems, และ Supervisor Multi-Agent Systems

สำหรับ EP.59 นี้ เราจะพาท่านไปรู้จักกับ Hierarchical Multi-Agent Systems ซึ่งเป็นการออกแบบระบบ AI ที่มีโครงสร้างเป็นลำดับชั้น โดยใช้ Top-Level Supervisor ในการกำกับดูแล Sub-Team หลายทีม ที่แต่ละทีมประกอบด้วย agent ที่ทำงานร่วมกันอย่างเป็นระบบ

🧩 แนวคิดของ Hierarchical Multi-Agent Systems

ในระบบนี้ เราแบ่ง Agent ออกเป็น 2 ระดับ:

  1. Top-Level Supervisor: ทำหน้าที่ตัดสินใจว่าจะส่งงานไปยังทีมย่อยใด
  2. Sub-Team Supervisor: ในตัวอย่างของเรามีการแบ่งเป็น 2 ทีมย่อย:
    - Research Team Supervisor ซึ่งกำกับดูแล Agent 2 ตัว: search_agent และ web_scraper_agent - Writing Team Supervisor ซึ่งกำกับดูแล Agent 3 ตัว:note_taking_agent, doc_writer_agent, chart_generating_agent

✨ กรณีศึกษานี้อ้างอิงจากเอกสารทางการของ LangGraph ในหัวข้อ Multi-Agent Systems → Hierarchical Teams: Orchestrate nested teams of agents to solve problems

🔁 Workflow Graph & Function ที่เกี่ยวข้อง

ในระบบนี้ เราออกแบบเครือข่าย Agent และ Team แบบแบ่งหน้าที่อย่างชัดเจน โดยใช้ LangGraph เป็นแกนหลักในการจัดลำดับขั้นตอนและควบคุมการทำงาน โดยมี Top-Level Supervisor ควบคุม 2 ทีมย่อย ได้แก่ Research Team และ Writing Team

**teams_supervisor_node(state)**

Figure: Team Supervisor Node Workflow

หน้าที่:

  • ควบคุมลำดับการทำงานของทีมย่อยทั้งหมดในระบบ
  • วิเคราะห์ข้อความจากผู้ใช้ และตัดสินใจว่าจะส่งงานต่อไปยัง research_team, writing_team หรือ FINISH
  • ใช้กลยุทธ์แบบ round-robin: เมื่อทีมหนึ่งทำงานเสร็จ จะส่งต่อให้อีกทีมหนึ่ง

🛠 ใช้เครื่องมือ:

  • ChatOpenAI(model="gpt-4o")
  • system_prompt สำหรับการตัดสินใจเลือกทีมย่อย
  • เชื่อมต่อกับ LangGraph ของแต่ละทีมผ่าน call_research_team(...) และ call_paper_writing_team(...)

**call_research_team(state)**

Figure: Research Node Workflow

หน้าที่:

  • เรียกใช้งานกราฟย่อยของ Research Team เพื่อค้นหาข้อมูล
  • รับข้อความจากผู้ใช้แล้วส่งต่อเข้าสู่ LangGraph ของทีม Research
  • รวบรวมข้อมูลผลลัพธ์ทั้งหมดจากSearch Agent และ Web Scraper Agent เพื่อเก็บไว้ใน state

Agent ภายในทีม Research:

  • Search Agent
    - ทำหน้าที่ค้นข้อมูลเบื้องต้นจากแหล่งออนไลน์
    - เครื่องมือที่ใช้: tavily_tool
    - สร้างด้วย: create_react_agent(...)
  • Web Scraper Agent
    - ดึงเนื้อหาละเอียดจากเว็บไซต์โดยตรง หลังจากได้รับ URL
    - เครื่องมือที่ใช้: WebBaseLoader (ผ่าน LangChain loader)
    - สร้างด้วย: create_react_agent(...)

**call_paper_writing_team(state)**

Figure: Writing Node Workflow

หน้าที่:

  • เรียกใช้งานกราฟย่อยของทีม Document Writing
  • รับ state ที่อัปเดตแล้วจากทีม Research และส่งต่อเข้าไปในกราฟของทีมเขียนเอกสาร
  • รับผลลัพธ์จากการทำงานของ Document Writer, Note Taker และ Chart Generator

Agent ภายในทีม Writing:

  • Note-Taking Agent
    - อ่านข้อมูลต้นฉบับแล้วสรุปประเด็นหลักเพื่อเตรียมโครงร่างเอกสาร
    - เครื่องมือที่ใช้: read_document, create_outline
    - สร้างด้วย: create_react_agent(...)
  • Document Writer Agent
    - เขียนเนื้อหาลงในเอกสารตามโครงร่างที่ได้รับ
    - เครื่องมือที่ใช้: write_document, edit_document, read_document
    - สร้างด้วย: create_react_agent(...)
  • Chart Generator Agent
    - ทำการคำนวณและแสดงผลลัพธ์ในรูปแบบแผนภูมิหรือข้อมูลตัวเลข
    - เครื่องมือที่ใช้: read_document, python_repl_tool
    - สร้างด้วย: create_react_agent(...)

ถ้าให้เข้าใจง่ายๆ ก็จะคล้ายกับการเขียน Supervisor Multi-Agent Systems ใน EP ที่แล้วเพียงแต่ว่า Hierarchical Multi-Agent Systems จะแยกเขียน LangGraph ของ Top-Level Supervisor กับ Sub-Team ออกจากกัน

Figure: ลองเขียน Diagram ในแบบ Supervisor Multi-Agent Systems

🔍 ตัวอย่างผลลัพธ์

"""
Summary

  • User question: Tell me about Taylor Swift
  • AI Answer:

##########################
--- STEP 1: Raw Search_Result ---
##########################

1st Search Result

  • Title: Taylor Swift - Wikipedia
  • URL: https://en.wikipedia.org/wiki/Taylor_Swift
  • Content: From Wikipedia, the free encyclopedia American singer-songwriter (born 1989) For other uses, see Taylor Swift (disambiguation). | Taylor Swift | | --- | | Swift in 2023 | | Born | Taylor Alison Swift (1989-12-13) December 13, 1989 (age 35) West Reading, Pennsylvania, US | | Occupations | Sing ...
  • Score: 0.81665546

2st Search Result

  • Title: Taylor Swift: Biography, Musician, 2025 Grammy Nominee
  • URL: https://www.biography.com/musicians/taylor-swift
  • Content: Swift’s successes in 2023 also include: the release of the 1989 (Taylor’s Version) album, the record-setting concert film Taylor Swift: The Eras Tour, two more No. 1 songs on the Billboard Hot 100, tying with Drake as the artist with the most Billboard Music Awards in history, dethroning Bad Bunny a ...
  • Score: 0.78334314

3st Search Result

  • Title: Taylor Swift | Songs, Albums, Eras Tour, Travis Kelce, & Facts
  • URL: https://www.britannica.com/biography/Taylor-Swift
  • Content: In December 2023, Swift was honored as Time magazine’s “Person of the Year.” Finalists also included Barbie, Vladimir Putin, and Sam Altman. The honor came shortly after the music streaming platform Spotify deemed her its most-played artist. According to a Bloomberg analysis, Swift is now a billiona ...
  • Score: 0.7244226

4st Search Result

##########################
--- STEP 2: Final Result From Search ---
##########################

aylor Swift is a highly accomplished American singer-songwriter and musician, born on December 13, 1989, in West Reading, Pennsylvania. Her career, which began in 2003, has spanned various roles including singer-songwriter, record producer, actress, and director. By 2023, she gained further acclaim with numerous achievements and recognitions:

  1. Musical Releases and Achievements: In 2023, Swift released "1989 (Taylor's Version)" and the record-setting concert film "The Eras Tour". She achieved two more No. 1 songs on the Billboard Hot 100, tied with Drake as the artist with the most Billboard Music Awards in history, and became Spotify's and Apple Music's most-streamed artist of the year.

  2. Financial Milestone: Swift achieved billionaire status, with an estimated net worth of around $1.1 billion by Bloomberg's analysis.

  3. Recognitions and Awards: Taylor Swift was named Time's "Person of the Year" in 2023, becoming the fifth woman to earn this title. She was also recognized by Apple Music as their 2023 Artist of the Year and placed fifth on Forbes' list of the most powerful women of 2023.

  4. Personal Life: Swift began dating NFL player Travis Kelce in 2023 after a public encounter during her Eras Tour.

  5. Cultural Impact: Swift is recognized as a powerful cultural and musical figure, often referred to as the "Empress of the Zeitgeist".

For further reading, you can check out her detailed biography on Wikipedia or Biography.com.

##########################
--- STEP 3: Web_Scraper_Result ---
##########################

Main pageContentsCurrent eventsRandom articleAbout WikipediaContact us
Contribute

HelpLearn to editCommunity portalRecent changesUpload fileSpecial pages
Search
Search
Appearance
Donate
Create account
Log in
Personal tools
Donate Create account Log in
Pages for logged out editors learn more
ContributionsTalk
Contents
move to sidebar
hide
(Top)
1
Early life
2
Career
Toggle Career subsection
2.1
2004–2008: Career beginnings and first album
2.2
2008–2010: Fearless
2.3
2010–2014: Speak Now and Red
2.4
2014–2018: 1989 and Reputation
2.5
2018–2021: Lover, Folklore, and Evermore
2.6
2021–2023: Re-recordings and Midnights
2.7
2023

##########################
--- STEP 4: Writing Team Result---
##########################

  • Supervisor chose: doc_writer

    Sure, I can provide some details about Taylor Swift. Here’s an overview:

Early Life

  • Birth: Taylor Alison Swift was born on December 13, 1989, in Reading, Pennsylvania, USA.
  • Early Interest in Music: She developed an interest in musical theater at a young age and began performing in local talent shows and events.

Music Career

  • Debut Album: She released her self-titled debut album, "Taylor Swift," in 2006, which brought her instant fame with hits like "Tim McGraw" and "Our Song."
  • Subsequent Albums: Swift continued to release successful albums, including "Fearless" (2008), "Speak Now" (2010), "Red" (2012), "1989" (2014), "Reputation" (2017), "Lover" (2019), "Folklore" (2020), and "Evermore" (2020).
  • Musical Evolution: Known initially for her country music roots, she successfully transitioned into pop and indie/folk, showcasing her versatility as an artist.

Achievements

  • Awards: Taylor Swift has received multiple awards, including Grammy Awards, American Music Awards, and MTV Video Music Awards.
  • Record Sales: She is one of the best-selling music artists, with millions of albums sold worldwide.

Personal Life

  • Public Image: Known for her narrative songwriting, which often centers around her personal life, relationships, and experiences.
  • Philanthropy: Taylor Swift actively engages in charitable work, including disaster relief and education initiatives.

Impact and Legacy

  • Taylor Swift is considered an influential figure in the music industry, known for her songwriting prowess and ability to connect with a broad audience through her music.

If you would like more detailed information about any specific part of her life or career, let me know!

  • Supervisor chose: note_taker

Outline Points:

  1. Introduction

    • Brief overview of who Taylor Swift is.
  2. Early Life

    • Birth and family background.
    • Early interest and involvement in music.
    • Early influences and inspirations.
  3. Music Career

    • Debut album and rise to fame.
    • Transition from country to pop and indie/folk.
    • Discography and notable works.
  4. Achievements

    • Awards and recognitions.
    • Milestones in her music career.
  5. Personal Life

    • Overview of her relationships and personal experiences.
    • Role as a public figure.
    • Philanthropy and charitable efforts.
  6. Impact and Legacy

    • Influence on the music industry.
    • Contributions to music and culture.
    • Longevity and evolution as an artist.
  7. Conclusion

    • Summary of her achievements and ongoing influence.
  • Supervisor chose: chart_generator
    Successfully executed:
    ```python
    import matplotlib.pyplot as plt

Data for the chart

labels = ['Grammy Awards', 'American Music Awards', 'Billboard Music Awards', 'MTV Video Music Awards', 'Country Music Association Awards']
awards_count = [12, 32, 29, 11, 11]

Create the bar chart

plt.figure(figsize=(10, 6))
plt.bar(labels, awards_count, color=['gold', 'silver', 'pink', 'purple', 'green'])
plt.title('Awards Won by Taylor Swift')
plt.xlabel('Award Shows')
plt.ylabel('Number of Awards')
plt.xticks(rotation=45, ha='right')
plt.tight_layout()

Show the chart

plt.show()
```
Stdout:

##################################################

  • Route: start --> supervisor --> research_team --> search --> search --> search --> search --> research_team --> web_scraper --> research_team --> research_team --> supervisor --> writing_team --> doc_writer --> doc_writer --> writing_team --> note_taker --> note_taker --> note_taker --> note_taker --> writing_team --> chart_generator --> chart_generator --> chart_generator --> chart_generator --> writing_team --> writing_team --> supervisor --> end

##################################################
"""

จุดเด่นของ Hierarchical Multi-Agent Systems

  • แบ่งทีมตามความเชี่ยวชาญ: ทำให้ระบบมีความเป็น modular และ reusable สูง
  • การควบคุมโดย Supervisor: ระดับบนสามารถควบคุมลำดับการทำงานโดยไม่จำเป็นต้อง hard-code workflow ไว้ล่วงหน้า
  • ความเชี่ยวชาญเฉพาะทาง: แต่ละ agent มีหน้าที่เฉพาะ ทำให้สามารถเพิ่ม/ลดฟีเจอร์ได้อย่างยืดหยุ่น
  • การประยุกต์ใช้งานจริง: เหมาะสำหรับงานที่มีความซับซ้อนสูง เช่น การสรุปเนื้อหา การเขียนเอกสารวิชาการ หรือการจัดทำรายงานข่าว

ในตอนถัดไป เราจะกล่าวถึง Planning Agents ในหัวข้อ Plan-and-Execute: Implement a basic planning and execution agent ซึ่งเหมาะกับงานที่ต้องการ_วางลำดับก่อน-หลังของ task_ โดยไม่จำเป็นต้องกำหนด workflow ไว้ล่วงหน้า

📌 ติดตามกันต่อใน Data Mastery Series ตอนหน้าเร็ว ๆ นี้!


Data Science Explore the world of data science with Donato_Story

Dashboard Discover the power of data visualization with Donato_Story

Donato_Journey Join me on my journey (Thai version)

Course_Review Discover the training courses with Donato_Story (Thai version)

Let’s Connect!

Your thoughts and feedback are invaluable. Feel free to share them in the comments or connect with me on

Originally published on Medium

Related