Skip to content

Commit

Permalink
.Net: Add early return if Freeze method is called on a frozen object (#…
Browse files Browse the repository at this point in the history
…5057)

### 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.
-->

In PromptExecutionSettings is a bug which creates new ReadOnlyCollection
even object is already freezed.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

The `Freeze` method in both `PromptExecutionSettings` and
`OpenAIPromptExecutionSettings` classes now include an early return
condition. This checks if the instance is already frozen, and if so, the
method execution is immediately stopped. This improvement prevents
unnecessary computations in these classes' `Freeze` methods.

### 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: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>
  • Loading branch information
Krzysztof318 and dmytrostruk committed Feb 21, 2024
1 parent 1f731ca commit f8e563a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public string ChatSystemPrompt
{
value = DefaultChatSystemPrompt;
}

this._chatSystemPrompt = value;
}
}
Expand Down Expand Up @@ -263,12 +264,18 @@ public string ChatSystemPrompt
/// <inheritdoc/>
public override void Freeze()
{
if (this.IsFrozen)
{
return;
}

base.Freeze();

if (this._stopSequences is not null)
{
this._stopSequences = new ReadOnlyCollection<string>(this._stopSequences);
}

if (this._tokenSelectionBiases is not null)
{
this._tokenSelectionBiases = new ReadOnlyDictionary<int, int>(this._tokenSelectionBiases);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public bool IsFrozen
/// </summary>
public virtual void Freeze()
{
if (this.IsFrozen)
{
return;
}

this._isFrozen = true;

if (this._extensionData is not null)
Expand Down

0 comments on commit f8e563a

Please sign in to comment.