|
| 1 | +--- |
| 2 | +title: Redis Workflow Storage |
| 3 | +sidebarTitle: Redis |
| 4 | +--- |
| 5 | + |
| 6 | +Agno supports using Redis as a storage backend for Workflows using the `RedisStorage` class. |
| 7 | + |
| 8 | +## Usage |
| 9 | + |
| 10 | +### Run Redis |
| 11 | + |
| 12 | +Install [docker desktop](https://docs.docker.com/desktop/install/mac-install/) and run **Redis** on port **6379** using: |
| 13 | + |
| 14 | +```bash |
| 15 | +docker run --name my-redis -p 6379:6379 -d redis |
| 16 | +``` |
| 17 | + |
| 18 | +```python redis_storage_for_workflow.py |
| 19 | +""" |
| 20 | +Run: `pip install openai httpx newspaper4k redis agno` to install the dependencies |
| 21 | +""" |
| 22 | + |
| 23 | +import json |
| 24 | +from typing import Iterator |
| 25 | + |
| 26 | +import httpx |
| 27 | +from agno.agent import Agent |
| 28 | +from agno.run.response import RunResponse |
| 29 | +from agno.storage.redis import RedisStorage |
| 30 | +from agno.tools.newspaper4k import Newspaper4kTools |
| 31 | +from agno.utils.log import logger |
| 32 | +from agno.utils.pprint import pprint_run_response |
| 33 | +from agno.workflow import Workflow |
| 34 | + |
| 35 | +# Initialize Redis storage with default local connection |
| 36 | +storage = RedisStorage( |
| 37 | + # Prefix for Redis keys to namespace the sessions |
| 38 | + prefix="agno_test", |
| 39 | + # Redis host address |
| 40 | + host="localhost", |
| 41 | + # Redis port number |
| 42 | + port=6379, |
| 43 | +) |
| 44 | + |
| 45 | + |
| 46 | +class HackerNewsReporter(Workflow): |
| 47 | + description: str = ( |
| 48 | + "Get the top stories from Hacker News and write a report on them." |
| 49 | + ) |
| 50 | + |
| 51 | + hn_agent: Agent = Agent( |
| 52 | + description="Get the top stories from hackernews. " |
| 53 | + "Share all possible information, including url, score, title and summary if available.", |
| 54 | + show_tool_calls=True, |
| 55 | + ) |
| 56 | + |
| 57 | + writer: Agent = Agent( |
| 58 | + tools=[Newspaper4kTools()], |
| 59 | + description="Write an engaging report on the top stories from hackernews.", |
| 60 | + instructions=[ |
| 61 | + "You will be provided with top stories and their links.", |
| 62 | + "Carefully read each article and think about the contents", |
| 63 | + "Then generate a final New York Times worthy article", |
| 64 | + "Break the article into sections and provide key takeaways at the end.", |
| 65 | + "Make sure the title is catchy and engaging.", |
| 66 | + "Share score, title, url and summary of every article.", |
| 67 | + "Give the section relevant titles and provide details/facts/processes in each section." |
| 68 | + "Ignore articles that you cannot read or understand.", |
| 69 | + "REMEMBER: you are writing for the New York Times, so the quality of the article is important.", |
| 70 | + ], |
| 71 | + ) |
| 72 | + |
| 73 | + def get_top_hackernews_stories(self, num_stories: int = 10) -> str: |
| 74 | + """Use this function to get top stories from Hacker News. |
| 75 | +
|
| 76 | + Args: |
| 77 | + num_stories (int): Number of stories to return. Defaults to 10. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + str: JSON string of top stories. |
| 81 | + """ |
| 82 | + |
| 83 | + # Fetch top story IDs |
| 84 | + response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json") |
| 85 | + story_ids = response.json() |
| 86 | + |
| 87 | + # Fetch story details |
| 88 | + stories = [] |
| 89 | + for story_id in story_ids[:num_stories]: |
| 90 | + story_response = httpx.get( |
| 91 | + f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json" |
| 92 | + ) |
| 93 | + story = story_response.json() |
| 94 | + story["username"] = story["by"] |
| 95 | + stories.append(story) |
| 96 | + return json.dumps(stories) |
| 97 | + |
| 98 | + def run(self, num_stories: int = 5) -> Iterator[RunResponse]: |
| 99 | + # Set the tools for hn_agent here to avoid circular reference |
| 100 | + self.hn_agent.tools = [self.get_top_hackernews_stories] |
| 101 | + |
| 102 | + logger.info(f"Getting top {num_stories} stories from HackerNews.") |
| 103 | + top_stories: RunResponse = self.hn_agent.run(num_stories=num_stories) |
| 104 | + if top_stories is None or not top_stories.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(top_stories.content, stream=True) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + # Run workflow |
| 116 | + report: Iterator[RunResponse] = HackerNewsReporter( |
| 117 | + storage=storage, debug_mode=False |
| 118 | + ).run(num_stories=5) |
| 119 | + # Print the report |
| 120 | + pprint_run_response(report, markdown=True, show_time=True) |
| 121 | +``` |
| 122 | + |
| 123 | +## Params |
| 124 | + |
| 125 | +<Snippet file="storage-redis-params.mdx" /> |
| 126 | + |
| 127 | +## Developer Resources |
| 128 | + |
| 129 | +- View [Cookbook](https://github.com/agno-agi/agno/blob/main/cookbook/storage/redis_storage/redis_storage_for_workflow.py) |
0 commit comments