-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Introduce logging for _Agent Framework_ ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Basic diagnostics are critical. Added a single logging sample in _Getting Started_. (Enabling logging for all samples obscures the sample focus) Logging Levels: - Pre-Action ([verb]-ing): DEBUG - Post-Action ([verb]-ed): INFORMATION (This often contains the result of the action) - Exception Thrown: ERROR (intuitive) - Sensitive Data: TRACE (Tao was explicit on this / I think from working with Toub) Intent is that INFORMATION is for the messages we want enabled by default for a deployed service (with agents). Whereas DEBUG/TRACE messages are more opt-in detail for developers, as you've suggested. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄
- Loading branch information
Showing
29 changed files
with
360 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
dotnet/samples/GettingStartedWithAgents/Step7_Logging.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Agents; | ||
using Microsoft.SemanticKernel.Agents.Chat; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
|
||
namespace GettingStarted; | ||
|
||
/// <summary> | ||
/// A repeat of <see cref="Step3_Chat"/> with logging enabled via assignment | ||
/// of a <see cref="LoggerFactory"/> to <see cref="AgentChat.LoggerFactory"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// Samples become super noisy with logging always enabled. | ||
/// </remarks> | ||
public class Step7_Logging(ITestOutputHelper output) : BaseTest(output) | ||
{ | ||
private const string ReviewerName = "ArtDirector"; | ||
private const string ReviewerInstructions = | ||
""" | ||
You are an art director who has opinions about copywriting born of a love for David Ogilvy. | ||
The goal is to determine if the given copy is acceptable to print. | ||
If so, state that it is approved. | ||
If not, provide insight on how to refine suggested copy without examples. | ||
"""; | ||
|
||
private const string CopyWriterName = "CopyWriter"; | ||
private const string CopyWriterInstructions = | ||
""" | ||
You are a copywriter with ten years of experience and are known for brevity and a dry humor. | ||
The goal is to refine and decide on the single best copy as an expert in the field. | ||
Only provide a single proposal per response. | ||
You're laser focused on the goal at hand. | ||
Don't waste time with chit chat. | ||
Consider suggestions when refining an idea. | ||
"""; | ||
|
||
[Fact] | ||
public async Task RunAsync() | ||
{ | ||
// Define the agents | ||
ChatCompletionAgent agentReviewer = | ||
new() | ||
{ | ||
Instructions = ReviewerInstructions, | ||
Name = ReviewerName, | ||
Kernel = this.CreateKernelWithChatCompletion(), | ||
}; | ||
|
||
ChatCompletionAgent agentWriter = | ||
new() | ||
{ | ||
Instructions = CopyWriterInstructions, | ||
Name = CopyWriterName, | ||
Kernel = this.CreateKernelWithChatCompletion(), | ||
}; | ||
|
||
// Create a chat for agent interaction. | ||
AgentGroupChat chat = | ||
new(agentWriter, agentReviewer) | ||
{ | ||
// This is all that is required to enable logging across the agent framework/ | ||
LoggerFactory = this.LoggerFactory, | ||
ExecutionSettings = | ||
new() | ||
{ | ||
// Here a TerminationStrategy subclass is used that will terminate when | ||
// an assistant message contains the term "approve". | ||
TerminationStrategy = | ||
new ApprovalTerminationStrategy() | ||
{ | ||
// Only the art-director may approve. | ||
Agents = [agentReviewer], | ||
// Limit total number of turns | ||
MaximumIterations = 10, | ||
} | ||
} | ||
}; | ||
|
||
// Invoke chat and display messages. | ||
string input = "concept: maps made out of egg cartons."; | ||
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input)); | ||
Console.WriteLine($"# {AuthorRole.User}: '{input}'"); | ||
|
||
await foreach (var content in chat.InvokeAsync()) | ||
{ | ||
Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); | ||
} | ||
|
||
Console.WriteLine($"# IS COMPLETE: {chat.IsComplete}"); | ||
} | ||
|
||
private sealed class ApprovalTerminationStrategy : TerminationStrategy | ||
{ | ||
// Terminate when the final message contains the term "approve" | ||
protected override Task<bool> ShouldAgentTerminateAsync(Agent agent, IReadOnlyList<ChatMessageContent> history, CancellationToken cancellationToken) | ||
=> Task.FromResult(history[history.Count - 1].Content?.Contains("approve", StringComparison.OrdinalIgnoreCase) ?? false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.