-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Fix Inline Functions Definition Extensions and add tests (#2366)
### Motivation and Context This pull request introduces improvements to the Inline Functions Definition Extensions Tests, adds a new test case for function calls with input, and performs minor refactoring in the codebase. The changes aim to enhance the test coverage and maintainability of the code. Resolves #2361 ### Description 1. In `dotnet/src/IntegrationTests/SkillDefinition/InlineFunctionsDefinitionExtensionsTests.cs`, the `RedirectTextCompletion` class has been marked as `sealed` to prevent further inheritance and improve performance. 2. A new test case, `ItSupportsFunctionCallsWithInput`, has been added to `dotnet/src/IntegrationTests/SkillDefinition/InlineFunctionsDefinitionExtensionsTests.cs`. This test case checks if the function calls with input are supported correctly. 3. In `dotnet/src/SemanticKernel/SkillDefinition/InlineFunctionsDefinitionExtension.cs`, the `InvokeAsync()` method has been replaced with `kernel.RunAsync(skfunction)` to ensure proper execution of the semantic function. 4. Minor formatting changes have been made to the code for better readability and consistency. ### 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
2b82637
commit b2433cb
Showing
3 changed files
with
106 additions
and
2 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
104 changes: 104 additions & 0 deletions
104
dotnet/src/IntegrationTests/SkillDefinition/InlineFunctionsDefinitionExtensionsTests.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,104 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.AI.TextCompletion; | ||
using Microsoft.SemanticKernel.Orchestration; | ||
using Microsoft.SemanticKernel.TemplateEngine; | ||
using SemanticKernel.IntegrationTests.Fakes; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace SemanticKernel.IntegrationTests.SkillDefinition; | ||
|
||
public sealed class InlineFunctionsDefinitionExtensionsTests : IDisposable | ||
{ | ||
public InlineFunctionsDefinitionExtensionsTests(ITestOutputHelper output) | ||
{ | ||
this._logger = new RedirectOutput(output); | ||
this._target = new PromptTemplateEngine(); | ||
} | ||
|
||
[Fact] | ||
public async Task ItSupportsFunctionCalls() | ||
{ | ||
var builder = Kernel.Builder | ||
.WithAIService<ITextCompletion>(null, new RedirectTextCompletion(), true) | ||
.WithLogger(this._logger); | ||
IKernel target = builder.Build(); | ||
|
||
var emailSkill = target.ImportSkill(new EmailSkillFake()); | ||
|
||
var prompt = "Hey {{_GLOBAL_FUNCTIONS_.GetEmailAddress}}"; | ||
|
||
// Act | ||
SKContext actual = await target.InvokeSemanticFunctionAsync(prompt, maxTokens: 150); | ||
|
||
// Assert | ||
Assert.Null(actual.LastException); | ||
Assert.False(actual.ErrorOccurred); | ||
Assert.Equal("Hey johndoe1234@example.com", actual.Result); | ||
} | ||
|
||
[Fact] | ||
public async Task ItSupportsFunctionCallsWithInput() | ||
{ | ||
var builder = Kernel.Builder | ||
.WithAIService<ITextCompletion>(null, new RedirectTextCompletion(), true) | ||
.WithLogger(this._logger); | ||
IKernel target = builder.Build(); | ||
|
||
var emailSkill = target.ImportSkill(new EmailSkillFake()); | ||
|
||
var prompt = "Hey {{_GLOBAL_FUNCTIONS_.GetEmailAddress \"a person\"}}"; | ||
|
||
// Act | ||
SKContext actual = await target.InvokeSemanticFunctionAsync(prompt, maxTokens: 150); | ||
|
||
// Assert | ||
Assert.Null(actual.LastException); | ||
Assert.False(actual.ErrorOccurred); | ||
Assert.Equal("Hey a person@example.com", actual.Result); | ||
} | ||
|
||
private readonly RedirectOutput _logger; | ||
private readonly PromptTemplateEngine _target; | ||
|
||
public void Dispose() | ||
{ | ||
this._logger.Dispose(); | ||
} | ||
|
||
private sealed class RedirectTextCompletion : ITextCompletion | ||
{ | ||
Task<IReadOnlyList<ITextResult>> ITextCompletion.GetCompletionsAsync(string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult<IReadOnlyList<ITextResult>>(new List<ITextResult> { new RedirectTextCompletionResult(text) }); | ||
} | ||
|
||
IAsyncEnumerable<ITextStreamingResult> ITextCompletion.GetStreamingCompletionsAsync(string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken) | ||
{ | ||
throw new NotImplementedException(); // TODO | ||
} | ||
} | ||
|
||
internal sealed class RedirectTextCompletionResult : ITextResult | ||
{ | ||
private readonly string _completion; | ||
|
||
public RedirectTextCompletionResult(string completion) | ||
{ | ||
this._completion = completion; | ||
} | ||
|
||
public ModelResult ModelResult => new(this._completion); | ||
|
||
public Task<string> GetCompletionAsync(CancellationToken cancellationToken = default) | ||
{ | ||
return Task.FromResult(this._completion); | ||
} | ||
} | ||
} |
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