Skip to content

Commit

Permalink
.Net: Test with the LightPlugin (microsoft#7285)
Browse files Browse the repository at this point in the history
### Motivation and Context

Update sample to match issue microsoft#7268 

### Description

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

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

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

---------

Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>
  • Loading branch information
markwallace-microsoft and dmytrostruk authored Jul 26, 2024
1 parent ca78ff7 commit 897bc40
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions dotnet/samples/Concepts/ChatCompletion/OpenAI_FunctionCalling.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.

using System.ComponentModel;
using System.Text.Json;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
Expand Down Expand Up @@ -67,7 +68,26 @@ public async Task AutoInvokeKernelFunctionsWithComplexParameterAsync()
Console.WriteLine(chatPromptResult);
}

public sealed class WeatherPlugin
[Fact]
public async Task AutoInvokeLightPluginAsync()
{
// Create a kernel with OpenAI chat completion and LightPlugin
Kernel kernel = CreateKernelWithPlugin<LightPlugin>();
kernel.FunctionInvocationFilters.Add(new FunctionFilterExample(this.Output));

// Invoke chat prompt with auto invocation of functions enabled
const string ChatPrompt = """
<message role="user">Turn on the light?</message>
""";
var executionSettings = new OpenAIPromptExecutionSettings { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
var chatSemanticFunction = kernel.CreateFunctionFromPrompt(
ChatPrompt, executionSettings);
var chatPromptResult = await kernel.InvokeAsync(chatSemanticFunction);

Console.WriteLine(chatPromptResult);
}

private sealed class WeatherPlugin
{
[KernelFunction]
[Description("Get the current weather in a given location.")]
Expand All @@ -76,7 +96,7 @@ public string GetWeather(
) => $"12°C\nWind: 11 KMPH\nHumidity: 48%\nMostly cloudy\nLocation: {location}";
}

public sealed class HolidayPlugin
private sealed class HolidayPlugin
{
[KernelFunction]
[Description("Book a holiday for a specified time period.")]
Expand All @@ -85,7 +105,7 @@ public string BookHoliday(
) => $"Holiday booked, starting {holidayRequest.StartDate} and ending {holidayRequest.EndDate}";
}

public sealed class HolidayRequest
private sealed class HolidayRequest
{
[Description("The date when the holiday period starts in ISO 8601 format")]
public string StartDate { get; set; } = string.Empty;
Expand All @@ -94,6 +114,24 @@ public sealed class HolidayRequest
public string EndDate { get; set; } = string.Empty;
}

private sealed class LightPlugin
{
public bool IsOn { get; set; } = false;

[KernelFunction]
[Description("Gets the state of the light.")]
public string GetState() => IsOn ? "on" : "off";

[KernelFunction]
[Description("Changes the state of the light.'")]
public string ChangeState(bool newState)
{
this.IsOn = newState;
var state = GetState();
return state;
}
}

private Kernel CreateKernelWithPlugin<T>()
{
// Create a logging handler to output HTTP requests and responses
Expand All @@ -110,4 +148,14 @@ private Kernel CreateKernelWithPlugin<T>()
Kernel kernel = kernelBuilder.Build();
return kernel;
}

private sealed class FunctionFilterExample(ITestOutputHelper output) : IFunctionInvocationFilter
{
public async Task OnFunctionInvocationAsync(FunctionInvocationContext context, Func<FunctionInvocationContext, Task> next)
{
output.WriteLine($"Function {context.Function.Name} is being invoked with arguments: {JsonSerializer.Serialize(context.Arguments)}");

await next(context);
}
}
}

0 comments on commit 897bc40

Please sign in to comment.