Skip to content

Commit d3b4175

Browse files
authored
redis storage docs (#206)
- redis storage docs for agent, team and workflow
1 parent 3dd6649 commit d3b4175

File tree

7 files changed

+343
-4
lines changed

7 files changed

+343
-4
lines changed

_snippets/storage-redis-params.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
| Parameter | Type | Description | Default |
2+
| --- | --- | --- | --- |
3+
| `prefix` | `str` | Prefix for Redis keys to namespace the sessions | Required |
4+
| `host` | `str` | Redis host address | `"localhost"` |
5+
| `port` | `int` | Redis port number | `6379` |
6+
| `db` | `int` | Redis database number | `0` |
7+
| `password` | `Optional[str]` | Redis password if authentication is required | `None` |
8+
| `mode` | `Optional[Literal["agent", "team", "workflow"]]` | Storage mode | `"agent"` |
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: Redis Agent Storage
3+
sidebarTitle: Redis
4+
---
5+
6+
Agno supports using Redis as a storage backend for Agents 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_agent.py
19+
from agno.agent import Agent
20+
from agno.storage.redis import RedisStorage
21+
from agno.tools.duckduckgo import DuckDuckGoTools
22+
23+
# Initialize Redis storage with default local connection
24+
storage = RedisStorage(
25+
# Prefix for Redis keys to namespace the sessions
26+
prefix="agno_test",
27+
# Redis host address
28+
host="localhost",
29+
# Redis port number
30+
port=6379,
31+
)
32+
33+
# Create agent with Redis storage
34+
agent = Agent(
35+
storage=storage,
36+
)
37+
```
38+
39+
## Params
40+
41+
<Snippet file="storage-redis-params.mdx" />
42+
43+
## Developer Resources
44+
45+
- View [Cookbook](https://github.com/agno-agi/agno/blob/main/cookbook/storage/redis_storage/redis_storage_for_agent.py)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: Redis Team Storage
3+
sidebarTitle: Redis
4+
---
5+
6+
Agno supports using Redis as a storage backend for Teams 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_team.py
19+
"""
20+
Run: `pip install openai duckduckgo-search newspaper4k lxml_html_clean agno redis` to install the dependencies
21+
"""
22+
23+
from typing import List
24+
25+
from agno.agent import Agent
26+
from agno.models.openai import OpenAIChat
27+
from agno.storage.redis import RedisStorage
28+
from agno.team import Team
29+
from agno.tools.duckduckgo import DuckDuckGoTools
30+
from agno.tools.hackernews import HackerNewsTools
31+
from pydantic import BaseModel
32+
33+
# Initialize Redis storage with default local connection
34+
storage = RedisStorage(
35+
# Prefix for Redis keys to namespace the sessions
36+
prefix="agno_test",
37+
# Redis host address
38+
host="localhost",
39+
# Redis port number
40+
port=6379,
41+
)
42+
43+
44+
class Article(BaseModel):
45+
title: str
46+
summary: str
47+
reference_links: List[str]
48+
49+
50+
hn_researcher = Agent(
51+
name="HackerNews Researcher",
52+
model=OpenAIChat("gpt-4o"),
53+
role="Gets top stories from hackernews.",
54+
tools=[HackerNewsTools()],
55+
)
56+
57+
web_searcher = Agent(
58+
name="Web Searcher",
59+
model=OpenAIChat("gpt-4o"),
60+
role="Searches the web for information on a topic",
61+
tools=[DuckDuckGoTools()],
62+
add_datetime_to_instructions=True,
63+
)
64+
65+
66+
hn_team = Team(
67+
name="HackerNews Team",
68+
mode="coordinate",
69+
model=OpenAIChat("gpt-4o"),
70+
members=[hn_researcher, web_searcher],
71+
storage=storage,
72+
instructions=[
73+
"First, search hackernews for what the user is asking about.",
74+
"Then, ask the web searcher to search for each story to get more information.",
75+
"Finally, provide a thoughtful and engaging summary.",
76+
],
77+
response_model=Article,
78+
show_tool_calls=True,
79+
markdown=True,
80+
debug_mode=True,
81+
show_members_responses=True,
82+
)
83+
84+
hn_team.print_response("Write an article about the top 2 stories on hackernews")
85+
```
86+
87+
## Params
88+
89+
<Snippet file="storage-redis-params.mdx" />
90+
91+
## Developer Resources
92+
93+
- View [Cookbook](https://github.com/agno-agi/agno/blob/main/cookbook/storage/redis_storage/redis_storage_for_team.py)
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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)

mint.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@
317317
"storage/postgres",
318318
"storage/sqlite",
319319
"storage/yaml",
320-
"storage/singlestore"
320+
"storage/singlestore",
321+
"storage/redis"
321322
]
322323
},
323324
{
@@ -565,7 +566,8 @@
565566
"examples/concepts/storage/agent_storage/dynamodb",
566567
"examples/concepts/storage/agent_storage/mongodb",
567568
"examples/concepts/storage/agent_storage/json",
568-
"examples/concepts/storage/agent_storage/yaml"
569+
"examples/concepts/storage/agent_storage/yaml",
570+
"examples/concepts/storage/agent_storage/redis"
569571
]
570572
},
571573
{
@@ -577,7 +579,8 @@
577579
"examples/concepts/storage/workflow_storage/dynamodb",
578580
"examples/concepts/storage/workflow_storage/mongodb",
579581
"examples/concepts/storage/workflow_storage/json",
580-
"examples/concepts/storage/workflow_storage/yaml"
582+
"examples/concepts/storage/workflow_storage/yaml",
583+
"examples/concepts/storage/workflow_storage/redis"
581584
]
582585
},
583586
{
@@ -589,7 +592,8 @@
589592
"examples/concepts/storage/team_storage/dynamodb",
590593
"examples/concepts/storage/team_storage/mongodb",
591594
"examples/concepts/storage/team_storage/json",
592-
"examples/concepts/storage/team_storage/yaml"
595+
"examples/concepts/storage/team_storage/yaml",
596+
"examples/concepts/storage/team_storage/redis"
593597
]
594598
}
595599
]

storage/introduction.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@ The following databases are supported as a storage backend:
6060
- [MongoDB](/storage/mongodb)
6161
- [YAML](/storage/yaml)
6262
- [JSON](/storage/json)
63+
- [Redis](/storage/redis)
6364

6465
Check detailed [examples](/examples/concepts/storage) for each storage

storage/redis.mdx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: Redis Storage
3+
sidebarTitle: Redis
4+
---
5+
6+
Agno supports using Redis as a storage backend for Agents 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_agent.py
19+
from agno.agent import Agent
20+
from agno.storage.redis import RedisStorage
21+
from agno.tools.duckduckgo import DuckDuckGoTools
22+
23+
# Initialize Redis storage with default local connection
24+
storage = RedisStorage(
25+
prefix="agno_test", # Prefix for Redis keys to namespace the sessions
26+
host="localhost", # Redis host address
27+
port=6379, # Redis port number
28+
)
29+
30+
# Create agent with Redis storage
31+
agent = Agent(
32+
storage=storage,
33+
tools=[DuckDuckGoTools()],
34+
add_history_to_messages=True,
35+
)
36+
37+
agent.print_response("How many people live in Canada?")
38+
39+
agent.print_response("What is their national anthem called?")
40+
41+
# Verify storage contents
42+
print("\nVerifying storage contents...")
43+
all_sessions = storage.get_all_sessions()
44+
print(f"Total sessions in Redis: {len(all_sessions)}")
45+
46+
if all_sessions:
47+
print("\nSession details:")
48+
session = all_sessions[0]
49+
print(f"Session ID: {session.session_id}")
50+
print(f"Messages count: {len(session.memory['messages'])}")
51+
```
52+
53+
## Params
54+
55+
<Snippet file="storage-redis-params.mdx" />
56+
57+
## Developer Resources
58+
59+
- View [Cookbook](https://github.com/agno-agi/agno/blob/main/cookbook/storage/redis_storage/redis_storage_for_agent.py)

0 commit comments

Comments
 (0)