.NET: Create a sample to show bounded chat history with overflow into chat history memory#4136
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a new sample (AgentWithMemory_Step05_BoundedChatHistory) that demonstrates how to combine InMemoryChatHistoryProvider with ChatHistoryMemoryProvider to implement a bounded chat history with vector store overflow. The sample keeps a configurable number of recent messages in session state and automatically archives older messages to a vector store, where they can be recalled via semantic search.
Changes:
- Adds a new sample showing bounded chat history with automatic overflow to vector store
- Implements
TruncatingChatReducerto truncate chat history and expose removed messages - Implements
BoundedChatHistoryProviderto coordinate between in-memory and vector-based storage - Updates the AgentWithMemory README to include the new sample
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
dotnet/samples/GettingStarted/AgentWithMemory/README.md |
Adds entry for the new Step05 sample in the table of contents |
dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step05_BoundedChatHistory/TruncatingChatReducer.cs |
Implements IChatReducer to keep N most recent messages and expose removed messages |
dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step05_BoundedChatHistory/README.md |
Documents the sample's purpose, prerequisites, configuration, and how it works |
dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step05_BoundedChatHistory/Program.cs |
Sample demonstrating bounded chat history with 4-message session window and vector overflow |
dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step05_BoundedChatHistory/BoundedChatHistoryProvider.cs |
Custom ChatHistoryProvider coordinating InMemoryChatHistoryProvider and ChatHistoryMemoryProvider |
dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step05_BoundedChatHistory/AgentWithMemory_Step05_BoundedChatHistory.csproj |
Project file with required package references (Azure.AI.OpenAI, Azure.Identity, SemanticKernel.Connectors.InMemory) |
dotnet/agent-framework-dotnet.slnx |
Adds the new sample project to the solution |
...ngStarted/AgentWithMemory/AgentWithMemory_Step05_BoundedChatHistory/TruncatingChatReducer.cs
Show resolved
Hide resolved
| /// (via <see cref="ChatHistoryMemoryProvider"/>). When providing chat history, it searches the vector | ||
| /// store for relevant older messages and prepends them as a memory context message. | ||
| /// </summary> | ||
| internal sealed class BoundedChatHistoryProvider : ChatHistoryProvider, IDisposable |
There was a problem hiding this comment.
The BoundedChatHistoryProvider does not override the StateKey property. This means it will default to "BoundedChatHistoryProvider" according to the base class behavior. However, it internally uses InMemoryChatHistoryProvider which will use "InMemoryChatHistoryProvider" as its state key, and ChatHistoryMemoryProvider which uses "ChatHistoryMemoryProvider" by default. Since BoundedChatHistoryProvider doesn't store any state itself, this might not be an issue. However, consider whether overriding StateKey to return the InMemoryChatHistoryProvider's StateKey would be more appropriate, or documenting that this provider composes two others with their own state keys.
Motivation and Context
#3955
Description
Contribution Checklist