mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: Add helpers to more easily access in-memory ChatHistory and make ChatHistoryProvider management more configurable. (#4224)
* Add helpers to more easily access in-memory ChatHistory and make ChatHistoryProvider management more configurable. * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -10,7 +10,6 @@ using Azure.Identity;
|
||||
using Microsoft.Agents.AI;
|
||||
using Microsoft.Extensions.AI;
|
||||
using OpenAI.Chat;
|
||||
using ChatMessage = Microsoft.Extensions.AI.ChatMessage;
|
||||
|
||||
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
|
||||
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
|
||||
@@ -39,9 +38,10 @@ Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate.", session
|
||||
// We can use the ChatHistoryProvider, that is also used by the agent, to read the
|
||||
// chat history from the session state, and see how the reducer is affecting the stored messages.
|
||||
// Here we expect to see 2 messages, the original user message and the agent response message.
|
||||
var provider = agent.GetService<InMemoryChatHistoryProvider>();
|
||||
List<ChatMessage>? chatHistory = provider?.GetMessages(session);
|
||||
Console.WriteLine($"\nChat history has {chatHistory?.Count} messages.\n");
|
||||
if (session.TryGetInMemoryChatHistory(out var chatHistory))
|
||||
{
|
||||
Console.WriteLine($"\nChat history has {chatHistory.Count} messages.\n");
|
||||
}
|
||||
|
||||
// Invoke the agent a few more times.
|
||||
Console.WriteLine(await agent.RunAsync("Tell me a joke about a robot.", session));
|
||||
@@ -51,16 +51,22 @@ Console.WriteLine(await agent.RunAsync("Tell me a joke about a robot.", session)
|
||||
// to trigger the reducer is just before messages are contributed to a new agent run.
|
||||
// So at this time, we have not yet triggered the reducer for the most recently added messages,
|
||||
// and they are still in the chat history.
|
||||
chatHistory = provider?.GetMessages(session);
|
||||
Console.WriteLine($"\nChat history has {chatHistory?.Count} messages.\n");
|
||||
if (session.TryGetInMemoryChatHistory(out chatHistory))
|
||||
{
|
||||
Console.WriteLine($"\nChat history has {chatHistory.Count} messages.\n");
|
||||
}
|
||||
|
||||
Console.WriteLine(await agent.RunAsync("Tell me a joke about a lemur.", session));
|
||||
chatHistory = provider?.GetMessages(session);
|
||||
Console.WriteLine($"\nChat history has {chatHistory?.Count} messages.\n");
|
||||
if (session.TryGetInMemoryChatHistory(out chatHistory))
|
||||
{
|
||||
Console.WriteLine($"\nChat history has {chatHistory.Count} messages.\n");
|
||||
}
|
||||
|
||||
// At this point, the chat history has exceeded the limit and the original message will not exist anymore,
|
||||
// so asking a follow up question about it may not work as expected.
|
||||
Console.WriteLine(await agent.RunAsync("What was the first joke I asked you to tell again?", session));
|
||||
|
||||
chatHistory = provider?.GetMessages(session);
|
||||
Console.WriteLine($"\nChat history has {chatHistory?.Count} messages.\n");
|
||||
if (session.TryGetInMemoryChatHistory(out chatHistory))
|
||||
{
|
||||
Console.WriteLine($"\nChat history has {chatHistory.Count} messages.\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user