|
| 1 | +"""Use Async MySQL as the database for a team. |
| 2 | +Run `pip install openai duckduckgo-search newspaper4k lxml_html_clean agno sqlalchemy asyncmy` to install the dependencies |
| 3 | +""" |
| 4 | + |
| 5 | +import asyncio |
| 6 | +import uuid |
| 7 | +from typing import List |
| 8 | + |
| 9 | +from agno.agent import Agent |
| 10 | +from agno.db.base import SessionType |
| 11 | +from agno.db.mysql import AsyncMySQLDb |
| 12 | +from agno.team import Team |
| 13 | +from agno.tools.duckduckgo import DuckDuckGoTools |
| 14 | +from agno.tools.hackernews import HackerNewsTools |
| 15 | +from pydantic import BaseModel |
| 16 | + |
| 17 | +db_url = "mysql+asyncmy://ai:ai@localhost:3306/ai" |
| 18 | +db = AsyncMySQLDb(db_url=db_url) |
| 19 | + |
| 20 | + |
| 21 | +class Article(BaseModel): |
| 22 | + title: str |
| 23 | + summary: str |
| 24 | + reference_links: List[str] |
| 25 | + |
| 26 | + |
| 27 | +hn_researcher = Agent( |
| 28 | + name="HackerNews Researcher", |
| 29 | + role="Gets top stories from hackernews.", |
| 30 | + tools=[HackerNewsTools()], |
| 31 | +) |
| 32 | + |
| 33 | +web_searcher = Agent( |
| 34 | + name="Web Searcher", |
| 35 | + role="Searches the web for information on a topic", |
| 36 | + tools=[DuckDuckGoTools()], |
| 37 | + add_datetime_to_context=True, |
| 38 | +) |
| 39 | + |
| 40 | + |
| 41 | +hn_team = Team( |
| 42 | + name="HackerNews Team", |
| 43 | + members=[hn_researcher, web_searcher], |
| 44 | + db=db, |
| 45 | + instructions=[ |
| 46 | + "First, search hackernews for what the user is asking about.", |
| 47 | + "Then, ask the web searcher to search for each story to get more information.", |
| 48 | + "Finally, provide a thoughtful and engaging summary.", |
| 49 | + ], |
| 50 | + output_schema=Article, |
| 51 | + markdown=True, |
| 52 | + show_members_responses=True, |
| 53 | +) |
| 54 | + |
| 55 | + |
| 56 | +async def main(): |
| 57 | + """Run the agent queries in the same event loop""" |
| 58 | + session_id = str(uuid.uuid4()) |
| 59 | + await hn_team.aprint_response( |
| 60 | + "Write an article about the top 2 stories on hackernews", session_id=session_id |
| 61 | + ) |
| 62 | + session_data = await db.get_session( |
| 63 | + session_id=session_id, session_type=SessionType.TEAM |
| 64 | + ) |
| 65 | + print("\n=== SESSION DATA ===") |
| 66 | + print(session_data.to_dict()) |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + asyncio.run(main()) |
0 commit comments