Skip to content

Commit

Permalink
.Net: Fix FunctionChoiceBehavior State Restoration in OllamaPromptExe…
Browse files Browse the repository at this point in the history
…cutionSettings. (#9718)

### Description

This PR addresses a bug in `OllamaPromptExecutionSettings` where the
`FunctionChoiceBehavior` state was not properly restored during the
conversion process from `PromptExecutionSettings`. Specifically, the
internal state of `FunctionChoiceBehavior`, including its list of
associated functions, was lost during serialization and deserialization.

### Motivation and Context

**Why is this change required?**
The lack of proper state restoration led to missing function definitions
in HTTP requests when using the Ollama connector, causing failures in
scenarios that rely on function calls.

**What problem does it solve?**
This change ensures that the `FunctionChoiceBehavior` is fully restored
when converting execution settings, maintaining the expected behavior
for function calls in the Ollama connector.

**What scenario does it contribute to?**
This fix enables consistent and reliable function calling with the
Ollama connector, ensuring that defined functions are included in
requests and operate as expected.

**Related Issues**
This change fixes the issue described in the bug report, improving
usability and reliability when working with function behaviors in
Semantic Kernel integrations.

### Contribution Checklist

- [Y] The code builds clean without any errors or warnings
- [Y] 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
- [Y] All unit tests pass, and I have added new tests where possible
- [Y] I didn't break anyone :)

Fixes #9682

---------

Co-authored-by: Adit Sheth <adsheth@microsoft.com>
Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 19, 2024
1 parent 7d5b50c commit 0d11264
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,20 @@ public void FromExecutionSettingsWhenSerializedHasPropertiesShouldPopulateSpecia
Assert.Equal(0.9f, ollamaExecutionSettings.TopP!.Value, 0.1f);
Assert.Equal(100, ollamaExecutionSettings.TopK);
}

[Fact]
public void FromExecutionSettingsShouldRestoreFunctionChoiceBehavior()
{
// Arrange
var functionChoiceBehavior = FunctionChoiceBehavior.Auto();

var originalExecutionSettings = new PromptExecutionSettings();
originalExecutionSettings.FunctionChoiceBehavior = functionChoiceBehavior;

// Act
var result = OllamaPromptExecutionSettings.FromExecutionSettings(originalExecutionSettings);

// Assert
Assert.Equal(functionChoiceBehavior, result.FunctionChoiceBehavior);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ public static OllamaPromptExecutionSettings FromExecutionSettings(PromptExecutio

var json = JsonSerializer.Serialize(executionSettings);
var ollamaExecutionSettings = JsonSerializer.Deserialize<OllamaPromptExecutionSettings>(json, JsonOptionsCache.ReadPermissive);
if (ollamaExecutionSettings is not null)
if (ollamaExecutionSettings is null)
{
return ollamaExecutionSettings;
}

throw new ArgumentException(
throw new ArgumentException(
$"Invalid execution settings, cannot convert to {nameof(OllamaPromptExecutionSettings)}",
nameof(executionSettings));
}

// Restore the function choice behavior that lost internal state(list of function instances) during serialization/deserialization process.
ollamaExecutionSettings!.FunctionChoiceBehavior = executionSettings.FunctionChoiceBehavior;

return ollamaExecutionSettings;
}

/// <summary>
Expand Down

0 comments on commit 0d11264

Please sign in to comment.