-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding HandlebarsPlanCreationException (#4973)
### 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. --> Resolves #4440 This PR adds a more detailed `HandlebarsPlanCreationException` type, which allows users to inspect the prompt and model results, if available. Als includes a minor update to sample to add clarifying comments. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Users can catch `HandlebarsPlanCreationException` to inspect the prompt, proposed plan, and exception details on error. All exceptions will be bubbled up to the caller. ``` var planner = new HandlebarsPlanner(); try { var plan = await planner.CreatePlanAsync(kernel, intent, cancellationToken); } catch (HandlebarsPlanCreationException ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.InnerException?.Message); Console.WriteLine($"CreatePlan Prompt: {ex.CreatePlanPrompt}"); Console.WriteLine($"Proposed plan (model output): {ex.ModelResults.Content}"); throw ex; } ``` ### 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
1 parent
d38b45d
commit 8858733
Showing
4 changed files
with
103 additions
and
21 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
59 changes: 59 additions & 0 deletions
59
dotnet/src/InternalUtilities/planning/Exceptions/PlanCreationException.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,59 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
|
||
namespace Microsoft.SemanticKernel.Planning; | ||
|
||
/// <summary> | ||
/// Exception thrown when a plan cannot be created. | ||
/// </summary> | ||
public sealed class PlanCreationException : KernelException | ||
{ | ||
/// <summary> | ||
/// Gets the prompt template used to generate the plan. | ||
/// </summary> | ||
public string? CreatePlanPrompt { get; set; } = null; | ||
|
||
/// <summary> | ||
/// Completion results from the model; generally, this is the proposed plan. | ||
/// </summary> | ||
public ChatMessageContent? ModelResults { get; set; } = null; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PlanCreationException"/> class. | ||
/// </summary> | ||
public PlanCreationException() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PlanCreationException"/> class with a specified error message. | ||
/// </summary> | ||
/// <param name="message">The error message that explains the reason for the exception.</param> | ||
public PlanCreationException(string? message) : base(message) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PlanCreationException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception. | ||
/// </summary> | ||
/// <param name="message">The error message that explains the reason for the exception.</param> | ||
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param> | ||
public PlanCreationException(string? message, Exception? innerException) : base(message, innerException) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PlanCreationException"/> class. | ||
/// Exception thrown when a plan cannot be created containing the prompt and model results. | ||
/// </summary> | ||
/// <param name="message">The error message that explains the reason for the exception.</param> | ||
/// <param name="createPlanPrompt">The prompt template used to generate the plan.</param> | ||
/// <param name="modelResults">Completion results from the model; generally, this is the proposed plan.</param> | ||
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param> | ||
public PlanCreationException(string? message, string? createPlanPrompt, ChatMessageContent? modelResults, Exception? innerException = null) : base(message, innerException) | ||
{ | ||
this.CreatePlanPrompt = createPlanPrompt; | ||
this.ModelResults = modelResults; | ||
} | ||
} |
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