/ examples / rag / rag_multi_agent.py
rag_multi_agent.py
  1  """
  2  RAG Multi-Agent Example
  3  
  4  This example demonstrates how multiple agents can work together
  5  on a shared knowledge base for collaborative RAG.
  6  
  7  Usage:
  8      python rag_multi_agent.py
  9  """
 10  
 11  from praisonaiagents import Agent, AgentTeam, Task
 12  
 13  
 14  # Shared knowledge base content
 15  SHARED_KNOWLEDGE = """
 16  # Climate Technology Report 2024
 17  
 18  ## Executive Summary
 19  Global investment in climate technology reached $150 billion in 2024, 
 20  a 25% increase from the previous year. Key areas of growth include 
 21  renewable energy, carbon capture, and sustainable transportation.
 22  
 23  ## Key Findings
 24  
 25  ### Renewable Energy
 26  - Solar capacity grew by 40% globally
 27  - Wind energy now provides 15% of global electricity
 28  - Battery storage costs decreased by 20%
 29  
 30  ### Carbon Capture
 31  - Direct air capture costs fell to $400/ton
 32  - 50 new carbon capture facilities announced
 33  - Total captured CO2 reached 50 million tons
 34  
 35  ### Sustainable Transportation
 36  - Electric vehicle sales reached 25 million units
 37  - Hydrogen fuel cell technology advancing rapidly
 38  - Sustainable aviation fuel production doubled
 39  
 40  ## Recommendations
 41  1. Increase investment in grid infrastructure
 42  2. Accelerate carbon capture deployment
 43  3. Support EV charging network expansion
 44  """
 45  
 46  
 47  def main():
 48      print("=" * 60)
 49      print("Multi-Agent RAG: Collaborative Analysis")
 50      print("=" * 60)
 51      
 52      # Create specialized agents with shared knowledge
 53      researcher = Agent(
 54          name="Researcher",
 55          role="Research Analyst",
 56          goal="Find and analyze relevant information",
 57          instructions=f"""You are a research analyst. Your job is to:
 58          1. Extract key facts and data points
 59          2. Provide detailed analysis
 60          3. Cite specific sections
 61          
 62          KNOWLEDGE BASE:
 63          {SHARED_KNOWLEDGE}""",
 64          output="silent"
 65      )
 66      
 67      summarizer = Agent(
 68          name="Summarizer",
 69          role="Content Summarizer",
 70          goal="Create concise summaries",
 71          instructions=f"""You are a content summarizer. Your job is to:
 72          1. Take research findings and create clear summaries
 73          2. Highlight the most important points
 74          3. Make complex information accessible
 75          
 76          KNOWLEDGE BASE:
 77          {SHARED_KNOWLEDGE}""",
 78          output="silent"
 79      )
 80      
 81      writer = Agent(
 82          name="Writer",
 83          role="Report Writer",
 84          goal="Create well-structured reports",
 85          instructions=f"""You are a report writer. Your job is to:
 86          1. Combine research and summaries into a cohesive report
 87          2. Ensure proper structure and flow
 88          3. Include key statistics
 89          
 90          KNOWLEDGE BASE:
 91          {SHARED_KNOWLEDGE}""",
 92          output="silent"
 93      )
 94      
 95      # Define tasks
 96      research_task = Task(
 97          description="Research the main investment trends and growth areas in climate technology",
 98          expected_output="Detailed research notes with key statistics",
 99          agent=researcher,
100      )
101      
102      summary_task = Task(
103          description="Summarize the research findings into 5 key bullet points",
104          expected_output="Bullet-point summary of main findings",
105          agent=summarizer,
106      )
107      
108      report_task = Task(
109          description="Write a brief executive briefing based on the research and summary",
110          expected_output="A 2-paragraph executive briefing",
111          agent=writer,
112      )
113      
114      # Run the multi-agent workflow
115      print("\nšŸ”„ Running multi-agent workflow...")
116      
117      agents = AgentTeam(
118          agents=[researcher, summarizer, writer],
119          tasks=[research_task, summary_task, report_task],
120          process="sequential",
121          output="silent"
122      )
123      
124      result = agents.start()
125      
126      print("\n" + "=" * 60)
127      print("Final Report")
128      print("=" * 60)
129      print(result)
130      
131      print("\nāœ… Multi-agent RAG example completed!")
132  
133  
134  if __name__ == "__main__":
135      main()