-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: More telemetry for Sequential, Action and Stepwise planners (#2156
) ### 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. --> This is follow-up PR to the previous one regarding telemetry: #1905 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> 1. Added small improvements in instrumentation for `SequentialPlanner`. 2. Added instrumentation for `ActionPlanner`. 3. Added instrumentation for `StepwisePlanner`. 4. Updated log levels in `StepwisePlanner` to disable sensitive data by default. ### 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 😄 --------- Co-authored-by: Shawn Callegari <36091529+shawncal@users.noreply.github.com>
- Loading branch information
1 parent
f8b6e2a
commit bd41daa
Showing
12 changed files
with
358 additions
and
27 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
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
21 changes: 21 additions & 0 deletions
21
dotnet/src/Extensions/Planning.ActionPlanner/ActionPlannerExtensions.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,21 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.SemanticKernel.Planning.Action; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="ActionPlanner"/> class. | ||
/// </summary> | ||
public static class ActionPlannerExtensions | ||
{ | ||
/// <summary> | ||
/// Returns decorated instance of <see cref="IActionPlanner"/> with enabled instrumentation. | ||
/// </summary> | ||
/// <param name="planner">Instance of <see cref="IActionPlanner"/> to decorate.</param> | ||
/// <param name="logger">Optional logger.</param> | ||
public static IActionPlanner WithInstrumentation(this IActionPlanner planner, ILogger? logger = null) | ||
{ | ||
return new InstrumentedActionPlanner(planner, logger); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
dotnet/src/Extensions/Planning.ActionPlanner/IActionPlanner.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,21 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.SemanticKernel.Planning.Action; | ||
|
||
/// <summary> | ||
/// Interface for planner that uses a set of semantic functions to select one function out of many and create a plan. | ||
/// </summary> | ||
public interface IActionPlanner | ||
{ | ||
/// <summary> | ||
/// Create a plan for a goal. | ||
/// </summary> | ||
/// <param name="goal">The goal to create a plan for.</param> | ||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param> | ||
/// <returns>The plan.</returns> | ||
/// <exception cref="PlanningException">Thrown when the plan cannot be created.</exception> | ||
Task<Plan> CreatePlanAsync(string goal, CancellationToken cancellationToken = default); | ||
} |
103 changes: 103 additions & 0 deletions
103
dotnet/src/Extensions/Planning.ActionPlanner/InstrumentedActionPlanner.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,103 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Diagnostics.Metrics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
|
||
namespace Microsoft.SemanticKernel.Planning.Action; | ||
|
||
/// <summary> | ||
/// Instrumented planner that uses set of semantic functions to select one function out of many and create a plan. | ||
/// Captures planner-related logs and metrics. | ||
/// </summary> | ||
public class InstrumentedActionPlanner : IActionPlanner | ||
{ | ||
/// <summary> | ||
/// Initialize a new instance of the <see cref="InstrumentedActionPlanner"/> class. | ||
/// </summary> | ||
/// <param name="planner">Instance of <see cref="IActionPlanner"/> to decorate.</param> | ||
/// <param name="logger">Optional logger.</param> | ||
public InstrumentedActionPlanner( | ||
IActionPlanner planner, | ||
ILogger? logger = null) | ||
{ | ||
this._planner = planner; | ||
this._logger = logger ?? NullLogger.Instance; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<Plan> CreatePlanAsync(string goal, CancellationToken cancellationToken = default) | ||
{ | ||
using var activity = s_activitySource.StartActivity($"{PlannerType}.CreatePlan"); | ||
|
||
this._logger.LogInformation("{PlannerType}: Plan creation started.", PlannerType); | ||
|
||
// Sensitive data, logging as trace, disabled by default | ||
this._logger.LogTrace("{PlannerType}: Plan Goal: {Goal}", PlannerType, goal); | ||
|
||
var stopwatch = new Stopwatch(); | ||
|
||
try | ||
{ | ||
stopwatch.Start(); | ||
|
||
var plan = await this._planner.CreatePlanAsync(goal, cancellationToken).ConfigureAwait(false); | ||
|
||
stopwatch.Stop(); | ||
|
||
this._logger.LogInformation("{PlannerType}: Plan creation status: {Status}", PlannerType, "Success"); | ||
|
||
this._logger.LogInformation("{PlannerType}: Created plan: \n {Plan}", PlannerType, plan.ToSafePlanString()); | ||
|
||
// Sensitive data, logging as trace, disabled by default | ||
this._logger.LogTrace("{PlannerType}: Created plan with details: \n {Plan}", PlannerType, plan.ToPlanString()); | ||
|
||
return plan; | ||
} | ||
catch (Exception ex) | ||
{ | ||
this._logger.LogInformation("{PlannerType}: Plan creation status: {Status}", PlannerType, "Failed"); | ||
this._logger.LogError(ex, "{PlannerType}: Plan creation exception details: {Message}", PlannerType, ex.Message); | ||
|
||
throw; | ||
} | ||
finally | ||
{ | ||
this._logger.LogInformation("{PlannerType}: Plan creation finished in {ExecutionTime}ms.", PlannerType, stopwatch.ElapsedMilliseconds); | ||
|
||
s_createPlanExecutionTime.Record(stopwatch.ElapsedMilliseconds); | ||
} | ||
} | ||
|
||
#region private ================================================================================ | ||
|
||
private const string PlannerType = nameof(ActionPlanner); | ||
|
||
private readonly IActionPlanner _planner; | ||
private readonly ILogger _logger; | ||
|
||
/// <summary> | ||
/// Instance of <see cref="ActivitySource"/> for planner-related activities. | ||
/// </summary> | ||
private static ActivitySource s_activitySource = new(typeof(InstrumentedActionPlanner).FullName); | ||
|
||
/// <summary> | ||
/// Instance of <see cref="Meter"/> for planner-related metrics. | ||
/// </summary> | ||
private static Meter s_meter = new(typeof(InstrumentedActionPlanner).FullName); | ||
|
||
/// <summary> | ||
/// Instance of <see cref="Histogram{T}"/> to record plan creation execution time. | ||
/// </summary> | ||
private static Histogram<double> s_createPlanExecutionTime = | ||
s_meter.CreateHistogram<double>( | ||
name: $"SK.{PlannerType}.CreatePlan.ExecutionTime", | ||
unit: "ms", | ||
description: "Execution time of plan creation"); | ||
|
||
#endregion | ||
} |
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
17 changes: 17 additions & 0 deletions
17
dotnet/src/Extensions/Planning.StepwisePlanner/IStepwisePlanner.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,17 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
namespace Microsoft.SemanticKernel.Planning.Stepwise; | ||
|
||
/// <summary> | ||
/// Interface for planner that creates a Stepwise plan using Mrkl systems. | ||
/// </summary> | ||
public interface IStepwisePlanner | ||
{ | ||
/// <summary> | ||
/// Create a plan for a goal. | ||
/// </summary> | ||
/// <param name="goal">The goal to create a plan for.</param> | ||
/// <returns>The plan.</returns> | ||
/// <exception cref="PlanningException">Thrown when the plan cannot be created.</exception> | ||
Plan CreatePlan(string goal); | ||
} |
Oops, something went wrong.