Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gather agents example #1633

Merged
merged 3 commits into from
Dec 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions cookbook/async/gather_agents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import asyncio
from rich.pretty import pprint
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.duckduckgo import DuckDuckGo

providers = ["openai", "anthropic", "ollama", "cohere", "google"]
instructions = [
"Your task is to write a well researched report on AI providers.",
"The report should be unbiased and factual.",
]


async def get_reports():
tasks = []
for provider in providers:
agent = Agent(
model=OpenAIChat(id="gpt-4"),
instructions=instructions,
tools=[DuckDuckGo()],
)
tasks.append(agent.arun(f"Write a report on the following AI provider: {provider}"))

results = await asyncio.gather(*tasks)
return results


async def main():
results = await get_reports()
for result in results:
print("************")
pprint(result.content)
print("************")
print("\n")


if __name__ == "__main__":
asyncio.run(main())
Loading