mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
fix: Azure Redis sample missing session for history persistence (#4692)
This commit is contained in:
committed by
GitHub
Unverified
parent
55011b7258
commit
414496dda7
@@ -3,7 +3,11 @@
|
||||
"""Azure Managed Redis History Provider with Azure AD Authentication
|
||||
|
||||
This example demonstrates how to use Azure Managed Redis with Azure AD authentication
|
||||
to persist conversational details using RedisHistoryProvider.
|
||||
to persist conversation history using RedisHistoryProvider.
|
||||
|
||||
Key concepts:
|
||||
- RedisHistoryProvider = durable storage (where messages are persisted)
|
||||
- AgentSession = conversation identity (which conversation the messages belong to)
|
||||
|
||||
Requirements:
|
||||
- Azure Managed Redis instance with Azure AD authentication enabled
|
||||
@@ -61,11 +65,11 @@ async def main() -> None:
|
||||
print("Get your Object ID from the Azure Portal")
|
||||
return
|
||||
|
||||
# Create Azure CLI credential provider (uses 'az login' credentials)
|
||||
# 1. Create Azure CLI credential provider (uses 'az login' credentials)
|
||||
azure_credential = AsyncAzureCliCredential()
|
||||
credential_provider = AzureCredentialProvider(azure_credential, user_object_id)
|
||||
|
||||
# Create Azure Redis history provider
|
||||
# 2. Create Azure Redis history provider (the durable storage backend)
|
||||
history_provider = RedisHistoryProvider(
|
||||
source_id="redis_memory",
|
||||
credential_provider=credential_provider,
|
||||
@@ -76,49 +80,54 @@ async def main() -> None:
|
||||
max_messages=100,
|
||||
)
|
||||
|
||||
# Create chat client
|
||||
# 3. Create chat client
|
||||
client = AzureOpenAIResponsesClient(
|
||||
project_endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
|
||||
deployment_name=os.environ["AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"],
|
||||
credential=AzureCliCredential(),
|
||||
)
|
||||
|
||||
# Create agent with Azure Redis history provider
|
||||
# 4. Create agent with Azure Redis history provider
|
||||
agent = client.as_agent(
|
||||
name="AzureRedisAssistant",
|
||||
instructions="You are a helpful assistant.",
|
||||
context_providers=[history_provider],
|
||||
)
|
||||
|
||||
# Conversation
|
||||
# 5. Create a session to provide conversation identity.
|
||||
# The session ID is used as the Redis key — all runs sharing the same session
|
||||
# will read/write the same conversation history in Redis.
|
||||
session = agent.create_session()
|
||||
|
||||
# 6. Conversation — each run passes the same session for continuity
|
||||
query = "Remember that I enjoy gumbo"
|
||||
result = await agent.run(query)
|
||||
result = await agent.run(query, session=session)
|
||||
print("User: ", query)
|
||||
print("Agent: ", result)
|
||||
|
||||
# Ask the agent to recall the stored preference; it should retrieve from memory
|
||||
query = "What do I enjoy?"
|
||||
result = await agent.run(query)
|
||||
result = await agent.run(query, session=session)
|
||||
print("User: ", query)
|
||||
print("Agent: ", result)
|
||||
|
||||
query = "What did I say to you just now?"
|
||||
result = await agent.run(query)
|
||||
result = await agent.run(query, session=session)
|
||||
print("User: ", query)
|
||||
print("Agent: ", result)
|
||||
|
||||
query = "Remember that I have a meeting at 3pm tomorrow"
|
||||
result = await agent.run(query)
|
||||
result = await agent.run(query, session=session)
|
||||
print("User: ", query)
|
||||
print("Agent: ", result)
|
||||
|
||||
query = "Tulips are red"
|
||||
result = await agent.run(query)
|
||||
result = await agent.run(query, session=session)
|
||||
print("User: ", query)
|
||||
print("Agent: ", result)
|
||||
|
||||
query = "What was the first thing I said to you this conversation?"
|
||||
result = await agent.run(query)
|
||||
result = await agent.run(query, session=session)
|
||||
print("User: ", query)
|
||||
print("Agent: ", result)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user