|
5 | 5 | This example demonstrates how to use the Redis context provider to persist |
6 | 6 | conversational details. Pass it as a constructor argument to create_agent. |
7 | 7 |
|
| 8 | +Note: For session history persistence, see RedisHistoryProvider in the |
| 9 | +conversations/redis_history_provider.py sample. RedisContextProvider is for |
| 10 | +AI context (RAG, memories), while RedisHistoryProvider stores message history. |
| 11 | +
|
8 | 12 | Requirements: |
9 | 13 | - A Redis instance with RediSearch enabled (e.g., Redis Stack) |
10 | 14 | - agent-framework with the Redis extra installed: pip install "agent-framework-redis" |
|
17 | 21 | import asyncio |
18 | 22 | import os |
19 | 23 |
|
| 24 | +from agent_framework import AgentSession |
20 | 25 | from agent_framework.azure import AzureOpenAIResponsesClient |
21 | 26 | from agent_framework.redis import RedisContextProvider |
22 | 27 | from azure.identity import AzureCliCredential |
@@ -77,35 +82,38 @@ async def main() -> None: |
77 | 82 | context_providers=[provider], |
78 | 83 | ) |
79 | 84 |
|
| 85 | + # Create a session to manage conversation state |
| 86 | + session = agent.create_session() |
| 87 | + |
80 | 88 | # Teach a user preference; the agent writes this to the provider's memory |
81 | 89 | query = "Remember that I enjoy gumbo" |
82 | | - result = await agent.run(query) |
| 90 | + result = await agent.run(query, session=session) |
83 | 91 | print("User: ", query) |
84 | 92 | print("Agent: ", result) |
85 | 93 |
|
86 | 94 | # Ask the agent to recall the stored preference; it should retrieve from memory |
87 | 95 | query = "What do I enjoy?" |
88 | | - result = await agent.run(query) |
| 96 | + result = await agent.run(query, session=session) |
89 | 97 | print("User: ", query) |
90 | 98 | print("Agent: ", result) |
91 | 99 |
|
92 | 100 | query = "What did I say to you just now?" |
93 | | - result = await agent.run(query) |
| 101 | + result = await agent.run(query, session=session) |
94 | 102 | print("User: ", query) |
95 | 103 | print("Agent: ", result) |
96 | 104 |
|
97 | 105 | query = "Remember that I have a meeting at 3pm tomorro" |
98 | | - result = await agent.run(query) |
| 106 | + result = await agent.run(query, session=session) |
99 | 107 | print("User: ", query) |
100 | 108 | print("Agent: ", result) |
101 | 109 |
|
102 | 110 | query = "Tulips are red" |
103 | | - result = await agent.run(query) |
| 111 | + result = await agent.run(query, session=session) |
104 | 112 | print("User: ", query) |
105 | 113 | print("Agent: ", result) |
106 | 114 |
|
107 | 115 | query = "What was the first thing I said to you this conversation?" |
108 | | - result = await agent.run(query) |
| 116 | + result = await agent.run(query, session=session) |
109 | 117 | print("User: ", query) |
110 | 118 | print("Agent: ", result) |
111 | 119 | # Drop / delete the provider index in Redis |
|
0 commit comments