Skip to content

Commit

Permalink
add tests for CreateFunctionFromPrompty
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleLittleCloud committed Apr 24, 2024
1 parent fb7a062 commit da663ad
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 76 deletions.
18 changes: 0 additions & 18 deletions dotnet/SK-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PromptTemplates.Liquid.Unit
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Functions.Prompty.UnitTests", "src\Functions\Functions.Prompty.UnitTests\Functions.Prompty.UnitTests.csproj", "{AD787471-5E43-44DF-BF3E-5CD26C765B4E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Experimental.Prompty", "src\Extensions\PromptTemplates.Prompty\Experimental.Prompty.csproj", "{1D72540D-2635-4069-B43B-E84AA981E198}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Experimental.Prompty.UnitTests", "src\Experimental\Experimental.Prompty.UnitTests\Experimental.Prompty.UnitTests.csproj", "{DD5271B0-4A94-46A0-A9A5-66F550CE6302}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -655,18 +651,6 @@ Global
{AD787471-5E43-44DF-BF3E-5CD26C765B4E}.Publish|Any CPU.Build.0 = Debug|Any CPU
{AD787471-5E43-44DF-BF3E-5CD26C765B4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD787471-5E43-44DF-BF3E-5CD26C765B4E}.Release|Any CPU.Build.0 = Release|Any CPU
{1D72540D-2635-4069-B43B-E84AA981E198}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1D72540D-2635-4069-B43B-E84AA981E198}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D72540D-2635-4069-B43B-E84AA981E198}.Publish|Any CPU.ActiveCfg = Publish|Any CPU
{1D72540D-2635-4069-B43B-E84AA981E198}.Publish|Any CPU.Build.0 = Publish|Any CPU
{1D72540D-2635-4069-B43B-E84AA981E198}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D72540D-2635-4069-B43B-E84AA981E198}.Release|Any CPU.Build.0 = Release|Any CPU
{DD5271B0-4A94-46A0-A9A5-66F550CE6302}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DD5271B0-4A94-46A0-A9A5-66F550CE6302}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DD5271B0-4A94-46A0-A9A5-66F550CE6302}.Publish|Any CPU.ActiveCfg = Debug|Any CPU
{DD5271B0-4A94-46A0-A9A5-66F550CE6302}.Publish|Any CPU.Build.0 = Debug|Any CPU
{DD5271B0-4A94-46A0-A9A5-66F550CE6302}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DD5271B0-4A94-46A0-A9A5-66F550CE6302}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -759,8 +743,6 @@ Global
{66D94E25-9B63-4C29-B7A1-3DFA17A90745} = {078F96B4-09E1-4E0E-B214-F71A4F4BF633}
{CC6DEE89-57AA-494D-B40D-B09E1CCC6FAD} = {078F96B4-09E1-4E0E-B214-F71A4F4BF633}
{AD787471-5E43-44DF-BF3E-5CD26C765B4E} = {9ECD1AA0-75B3-4E25-B0B5-9F0945B64974}
{1D72540D-2635-4069-B43B-E84AA981E198} = {078F96B4-09E1-4E0E-B214-F71A4F4BF633}
{DD5271B0-4A94-46A0-A9A5-66F550CE6302} = {A2357CF8-3BB9-45A1-93F1-B366C9B63658}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FBDC56A3-86AD-4323-AA0F-201E59123B83}
Expand Down
90 changes: 32 additions & 58 deletions dotnet/src/Functions/Functions.Prompty.UnitTests/PromptyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,59 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Prompty.Extension;
using Xunit;
using Xunit.Abstractions;
using YamlDotNet.Serialization.NamingConventions;
using YamlDotNet.Serialization;

namespace SemanticKernel.Functions.Prompty.UnitTests;
public sealed class PromptyTest
{
[Fact]
public async Task ChatPromptyTemplateTestAsync()
public async Task ChatPromptyTestAsync()
{
var modelId = "gpt-35-turbo-16k";
var endPoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new Exception("AZURE_OPENAI_ENDPOINT is not set");
var key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new Exception("AZURE_OPENAI_KEY is not set");
var kernel = Kernel.CreateBuilder()
.AddAzureOpenAIChatCompletion(modelId, endPoint, key)
.Build();

var cwd = Directory.GetCurrentDirectory();
var chatPromptyPath = Path.Combine(cwd, "TestData", "chat.prompty");
var function = kernel.CreateFunctionFromPrompty(chatPromptyPath);
// create a dynamic customer object
// customer contains the following properties
// - firstName
// - lastName
// - age
// - membership
// - orders []
// - name
// - description
var customer = new
{
firstName = "John",
lastName = "Doe",
age = 30,
membership = "Gold",
orders = new[]
{
new { name = "apple", description = "2 fuji apples", date = "2024/04/01" },
new { name = "banana", description = "1 free banana from amazon banana hub", date = "2024/04/03" },
},
};
var kernelFunction = kernel.CreateFunctionFromPrompty(chatPromptyPath);

// create a list of documents
// documents contains the following properties
// - id
// - title
// - content
var documents = new[]
{
new { id = "1", title = "apple", content = "2 apples"},
new { id = "2", title = "banana", content = "3 bananas"},
};
Assert.Equal("Contoso_Chat_Prompt", kernelFunction.Name);
Assert.Equal("A retail assistent for Contoso Outdoors products retailer.", kernelFunction.Description);

// create chat history
// each chat message contains the following properties
// - role (system, user, assistant)
// - content
// chat prompty doesn't contain input parameters
Assert.Empty(kernelFunction.Metadata.Parameters);
}

[Fact]
public void ChatPromptyShouldSupportCreatingOpenAIExecutionSettings()
{
var kernel = Kernel.CreateBuilder()
.Build();

var cwd = Directory.GetCurrentDirectory();
var chatPromptyPath = Path.Combine(cwd, "TestData", "chat.prompty");
var kernelFunction = kernel.CreateFunctionFromPrompty(chatPromptyPath);

var chatHistory = new[]
{
new { role = "user", content = "When is the last time I bought apple? Give me specific date and year" },
};
// kernel function created from chat.prompty should have a single execution setting
Assert.Single(kernelFunction.ExecutionSettings!);
Assert.True(kernelFunction.ExecutionSettings!.ContainsKey("default"));

// create
var result = await kernel.InvokeAsync(function, arguments: new()
{
{ "customer", customer },
{ "documentation", documents },
{ "history", chatHistory },
});
var defaultExecutionSetting = kernelFunction.ExecutionSettings["default"];

Assert.IsType<OpenAIChatMessageContent>(result.GetValue<OpenAIChatMessageContent>());
// Act
var executionSettings = OpenAIPromptExecutionSettings.FromExecutionSettings(defaultExecutionSetting);

if (result.GetValue<OpenAIChatMessageContent>() is OpenAIChatMessageContent openAIChatMessageContent)
{
Assert.Equal(AuthorRole.Assistant, openAIChatMessageContent.Role);
Assert.Contains("2024", openAIChatMessageContent.Content, StringComparison.InvariantCultureIgnoreCase);
}
// Assert
Assert.NotNull(executionSettings);
Assert.Equal("gpt-35-turbo", executionSettings.ModelId);
Assert.Equal(1.0, executionSettings.Temperature);
Assert.Equal(1.0, executionSettings.TopP);
}
}

0 comments on commit da663ad

Please sign in to comment.