Skip to content

Commit

Permalink
.Net: Fix Inline Functions Definition Extensions and add tests (#2366)
Browse files Browse the repository at this point in the history
### 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
lemillermicrosoft authored Aug 9, 2023
1 parent 2b82637 commit b2433cb
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Is it weekend time (weekend/not weekend)?

// Show the result
Console.WriteLine("--- Semantic Function result");
var result = await kindOfDay.InvokeAsync();
var result = await kernel.RunAsync(kindOfDay);
Console.WriteLine(result);

/* OUTPUT:
Expand Down
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static Task<SKContext> InvokeSemanticFunctionAsync(
frequencyPenalty,
stopSequences);

return skfunction.InvokeAsync();
return kernel.RunAsync(skfunction);
}

private static string RandomFunctionName() => "func" + Guid.NewGuid().ToString("N");
Expand Down

0 comments on commit b2433cb

Please sign in to comment.