|
| 1 | +--- |
| 2 | +title: Team Workflow |
| 3 | +--- |
| 4 | +**TeamWorkflow** generates summarised reports on top reddit and hackernews posts. |
| 5 | +This example demonstrates the usage of teams as nodes of a workflow. |
| 6 | + |
| 7 | +Create a file `team_worklfow.py` with the following code: |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +```python team_worklfow.py |
| 12 | + |
| 13 | +from textwrap import dedent |
| 14 | +from typing import Iterator |
| 15 | + |
| 16 | +from agno.agent import Agent, RunResponse |
| 17 | +from agno.models.openai import OpenAIChat |
| 18 | +from agno.team.team import Team |
| 19 | +from agno.tools.exa import ExaTools |
| 20 | +from agno.tools.hackernews import HackerNewsTools |
| 21 | +from agno.tools.newspaper4k import Newspaper4kTools |
| 22 | +from agno.utils.log import logger |
| 23 | +from agno.utils.pprint import pprint_run_response |
| 24 | +from agno.workflow import Workflow |
| 25 | + |
| 26 | + |
| 27 | +class TeamWorkflow(Workflow): |
| 28 | + description: str = ( |
| 29 | + "Get the top stories from Hacker News and Reddit and write a report on them." |
| 30 | + ) |
| 31 | + |
| 32 | + reddit_researcher = Agent( |
| 33 | + name="Reddit Researcher", |
| 34 | + role="Research a topic on Reddit", |
| 35 | + model=OpenAIChat(id="gpt-4o"), |
| 36 | + tools=[ExaTools()], |
| 37 | + add_name_to_instructions=True, |
| 38 | + instructions=dedent(""" |
| 39 | + You are a Reddit researcher. |
| 40 | + You will be given a topic to research on Reddit. |
| 41 | + You will need to find the most relevant posts on Reddit. |
| 42 | + """), |
| 43 | + ) |
| 44 | + |
| 45 | + hackernews_researcher = Agent( |
| 46 | + name="HackerNews Researcher", |
| 47 | + model=OpenAIChat("gpt-4o"), |
| 48 | + role="Research a topic on HackerNews.", |
| 49 | + tools=[HackerNewsTools()], |
| 50 | + add_name_to_instructions=True, |
| 51 | + instructions=dedent(""" |
| 52 | + You are a HackerNews researcher. |
| 53 | + You will be given a topic to research on HackerNews. |
| 54 | + You will need to find the most relevant posts on HackerNews. |
| 55 | + """), |
| 56 | + ) |
| 57 | + |
| 58 | + agent_team = Team( |
| 59 | + name="Discussion Team", |
| 60 | + mode="collaborate", |
| 61 | + model=OpenAIChat("gpt-4o"), |
| 62 | + members=[ |
| 63 | + reddit_researcher, |
| 64 | + hackernews_researcher, |
| 65 | + ], |
| 66 | + instructions=[ |
| 67 | + "You are a discussion coordinator.", |
| 68 | + "Your primary role is to facilitate the research process.", |
| 69 | + "Once both team members have provided their research results with links to top stories from their respective platforms (Reddit and HackerNews), you should stop the discussion.", |
| 70 | + "Do not continue the discussion after receiving the links - your goal is to collect the research results, not to reach a consensus on content.", |
| 71 | + "Ensure each member provides relevant links with brief descriptions before concluding.", |
| 72 | + ], |
| 73 | + success_criteria="The team has reached a consensus.", |
| 74 | + enable_agentic_context=True, |
| 75 | + show_tool_calls=True, |
| 76 | + markdown=True, |
| 77 | + debug_mode=True, |
| 78 | + show_members_responses=True, |
| 79 | + ) |
| 80 | + |
| 81 | + writer: Agent = Agent( |
| 82 | + tools=[Newspaper4kTools(), ExaTools()], |
| 83 | + description="Write an engaging report on the top stories from various sources.", |
| 84 | + instructions=[ |
| 85 | + "You will receive links to top stories from Reddit and HackerNews from the agent team.", |
| 86 | + "Your task is to access these links and thoroughly read each article.", |
| 87 | + "Extract key information, insights, and notable points from each source.", |
| 88 | + "Write a comprehensive, well-structured report that synthesizes the information.", |
| 89 | + "Create a catchy and engaging title for your report.", |
| 90 | + "Organize the content into relevant sections with descriptive headings.", |
| 91 | + "For each article, include its source, title, URL, and a brief summary.", |
| 92 | + "Provide detailed analysis and context for the most important stories.", |
| 93 | + "End with key takeaways that summarize the main insights.", |
| 94 | + "Maintain a professional tone similar to New York Times reporting.", |
| 95 | + "If you cannot access or understand certain articles, note this and focus on the ones you can analyze.", |
| 96 | + ], |
| 97 | + ) |
| 98 | + |
| 99 | + def run(self) -> Iterator[RunResponse]: |
| 100 | + logger.info(f"Getting top stories from HackerNews.") |
| 101 | + discussion: RunResponse = self.agent_team.run( |
| 102 | + "Getting 2 top stories from HackerNews and reddit and write a brief report on them" |
| 103 | + ) |
| 104 | + if discussion is None or not discussion.content: |
| 105 | + yield RunResponse( |
| 106 | + run_id=self.run_id, content="Sorry, could not get the top stories." |
| 107 | + ) |
| 108 | + return |
| 109 | + |
| 110 | + logger.info("Reading each story and writing a report.") |
| 111 | + yield from self.writer.run(discussion.content, stream=True) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + # Run workflow |
| 116 | + report: Iterator[RunResponse] = TeamWorkflow(debug_mode=False).run() |
| 117 | + # Print the report |
| 118 | + pprint_run_response(report, markdown=True, show_time=True) |
| 119 | +``` |
| 120 | + |
| 121 | +## Usage |
| 122 | + |
| 123 | +<Steps> |
| 124 | + <Snippet file="create-venv-step.mdx" /> |
| 125 | + |
| 126 | + <Step title="Install libraries"> |
| 127 | + ```bash |
| 128 | + pip install openai newspaper4k exa_py agno |
| 129 | + ``` |
| 130 | + </Step> |
| 131 | + |
| 132 | + <Step title="Run the workflow"> |
| 133 | + ```bash |
| 134 | + python team_worklfow.py |
| 135 | + ``` |
| 136 | + </Step> |
| 137 | + |
| 138 | +</Steps> |
0 commit comments