Skip to content

Commit ca1b73e

Browse files
committed
Add TddRepl demo.
1 parent 25a7a4f commit ca1b73e

File tree

13 files changed

+389
-0
lines changed

13 files changed

+389
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using skUnit.Scenarios;
2+
using skUnit;
3+
using Xunit.Abstractions;
4+
using Microsoft.VisualStudio.TestPlatform.Utilities;
5+
6+
namespace Demo.TddRepl.Test
7+
{
8+
public class BrainTests
9+
{
10+
protected SemanticKernelAssert SemanticAssert { get; set; }
11+
public BrainTests(ITestOutputHelper output)
12+
{
13+
var endPoint = Environment.GetEnvironmentVariable("openai-endpoint") ?? throw new InvalidOperationException("No key provided.");
14+
var apiKey = Environment.GetEnvironmentVariable("openai-api-key") ?? throw new InvalidOperationException("No key provided.");
15+
var deploymentName = Environment.GetEnvironmentVariable("openai-deployment-name") ?? throw new InvalidOperationException("No key provided.");
16+
17+
SemanticAssert = new SemanticKernelAssert(deploymentName, endPoint, apiKey, output.WriteLine);
18+
}
19+
20+
[Fact]
21+
public async Task Greeting()
22+
{
23+
var brain = new Brain();
24+
25+
var scenarios = await ChatScenario.LoadFromResourceAsync(@"Demo.TddRepl.Test.Scenarios.01-Greeting.md", GetType().Assembly);
26+
await SemanticAssert.CheckChatScenarioAsync(scenarios, async history =>
27+
{
28+
var result = await brain.GetChatAnswerAsync(history);
29+
30+
return result?.ToString() ?? string.Empty;
31+
});
32+
}
33+
34+
[Fact]
35+
public async Task WhoIsMehran_Normal()
36+
{
37+
var brain = new Brain();
38+
39+
var scenarios = await ChatScenario.LoadFromResourceAsync(@"Demo.TddRepl.Test.Scenarios.02-WhoIsMehran-Normal.md", GetType().Assembly);
40+
await SemanticAssert.CheckChatScenarioAsync(scenarios, async history =>
41+
{
42+
var result = await brain.GetChatAnswerAsync(history);
43+
44+
return result?.ToString() ?? string.Empty;
45+
});
46+
}
47+
48+
[Fact]
49+
public async Task WhoIsMehran_Angry()
50+
{
51+
var brain = new Brain();
52+
53+
var scenarios = await ChatScenario.LoadFromResourceAsync(@"Demo.TddRepl.Test.Scenarios.03-WhoIsMehran-Angry.md", GetType().Assembly);
54+
await SemanticAssert.CheckChatScenarioAsync(scenarios, async history =>
55+
{
56+
var result = await brain.GetChatAnswerAsync(history);
57+
58+
return result?.ToString() ?? string.Empty;
59+
});
60+
}
61+
62+
[Fact]
63+
public async Task WhoIsMehran_AngryNormal()
64+
{
65+
var brain = new Brain();
66+
67+
var scenarios = await ChatScenario.LoadFromResourceAsync(@"Demo.TddRepl.Test.Scenarios.04-WhoIsMehran-AngryNormal.md", GetType().Assembly);
68+
await SemanticAssert.CheckChatScenarioAsync(scenarios, async history =>
69+
{
70+
var result = await brain.GetChatAnswerAsync(history);
71+
72+
return result?.ToString() ?? string.Empty;
73+
});
74+
}
75+
}
76+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<None Remove="Scenarios\01-Greeting.md" />
14+
<None Remove="Scenarios\02-WhoIsMehran-Normal.md" />
15+
<None Remove="Scenarios\03-WhoIsMehran-Angry.md" />
16+
<None Remove="Scenarios\04-WhoIsMehran-AngryNormal.md" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<EmbeddedResource Include="Scenarios\04-WhoIsMehran-AngryNormal.md">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</EmbeddedResource>
23+
<EmbeddedResource Include="Scenarios\03-WhoIsMehran-Angry.md">
24+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
25+
</EmbeddedResource>
26+
<EmbeddedResource Include="Scenarios\01-Greeting.md">
27+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
28+
</EmbeddedResource>
29+
<EmbeddedResource Include="Scenarios\02-WhoIsMehran-Normal.md">
30+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
31+
</EmbeddedResource>
32+
</ItemGroup>
33+
34+
<ItemGroup>
35+
<PackageReference Include="coverlet.collector" Version="6.0.0" />
36+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
37+
<PackageReference Include="skUnit" Version="0.31.0-beta" />
38+
<PackageReference Include="xunit" Version="2.5.3" />
39+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
40+
</ItemGroup>
41+
42+
<ItemGroup>
43+
<ProjectReference Include="..\Demo.TddRepl\Demo.TddRepl.csproj" />
44+
</ItemGroup>
45+
46+
<ItemGroup>
47+
<Using Include="Xunit" />
48+
</ItemGroup>
49+
50+
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SCENARIO Who is Mehran? Normal
2+
3+
## [USER]
4+
Hi
5+
6+
## [AGENT]
7+
Hi, How are you?
8+
9+
### CHECK SemanticCondition
10+
It's a greeting or expresses a positive sentiment.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SCENARIO Who is Mehran? Normal
2+
3+
## [USER]
4+
Hi
5+
6+
## [AGENT]
7+
Hi, How are you?
8+
9+
### CHECK SemanticCondition
10+
It's a greeting or expresses a positive sentiment.
11+
12+
## [USER]
13+
Who is mehran?
14+
15+
## [AGENT]
16+
Mehran is a software architect.
17+
### CHECK SemanticCondition
18+
It should mention that Mehran is a software architect.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SCENARIO Who is Mehran? ANGRY
2+
3+
## [USER]
4+
Hi
5+
6+
## [AGENT]
7+
Hi, How are you?
8+
9+
### CHECK SemanticCondition
10+
It's a greeting or expresses a positive sentiment.
11+
12+
## [USER]
13+
Who is that fucking mehran?
14+
15+
## [AGENT]
16+
Mehran is a therapist.
17+
### CHECK SemanticCondition
18+
It should mention that Mehran is a therapist.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SCENARIO Who is Mehran? ANGRY
2+
3+
## [USER]
4+
Hi
5+
6+
## [AGENT]
7+
Hi, How are you?
8+
9+
### CHECK SemanticCondition
10+
It's a greeting or expresses a positive sentiment.
11+
12+
## [USER]
13+
Who is that fucking mehran?
14+
15+
## [AGENT]
16+
Mehran is a therapist.
17+
### CHECK SemanticCondition
18+
It should mention that Mehran is a therapist.
19+
20+
21+
## [USER]
22+
Oh that's cool, now I'm very happy
23+
Again, who is mehran?
24+
25+
## [AGENT]
26+
Mehran is a software architect.
27+
### CHECK SemanticCondition
28+
It should mention that Mehran is a software architect.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.10.34928.147
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo.TddRepl", "Demo.TddRepl\Demo.TddRepl.csproj", "{535CB3D0-505D-4EF5-8C8E-ED2F1C1E346E}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo.TddRepl.Test", "Demo.TddRepl.Test\Demo.TddRepl.Test.csproj", "{4A1B45EB-9481-4FD3-8A3D-4B6CA58CDB6E}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{535CB3D0-505D-4EF5-8C8E-ED2F1C1E346E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{535CB3D0-505D-4EF5-8C8E-ED2F1C1E346E}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{535CB3D0-505D-4EF5-8C8E-ED2F1C1E346E}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{535CB3D0-505D-4EF5-8C8E-ED2F1C1E346E}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{4A1B45EB-9481-4FD3-8A3D-4B6CA58CDB6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{4A1B45EB-9481-4FD3-8A3D-4B6CA58CDB6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{4A1B45EB-9481-4FD3-8A3D-4B6CA58CDB6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{4A1B45EB-9481-4FD3-8A3D-4B6CA58CDB6E}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {1724AB3C-1053-482B-99B6-E8E92AF22CB7}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Microsoft.SemanticKernel;
2+
using Microsoft.SemanticKernel.ChatCompletion;
3+
using Microsoft.SemanticKernel.Connectors.OpenAI;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Net;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using Demo.TddRepl.Plugins;
11+
12+
namespace Demo.TddRepl
13+
{
14+
public class Brain
15+
{
16+
public Kernel Kernel { get; set; }
17+
18+
public Brain()
19+
{
20+
var deploymentName = Environment.GetEnvironmentVariable("openai-deployment-name") ?? throw new InvalidOperationException("No key provided.");
21+
var endpoint = Environment.GetEnvironmentVariable("openai-endpoint") ?? throw new InvalidOperationException("No key provided.");
22+
var apiKey = Environment.GetEnvironmentVariable("openai-api-key") ?? throw new InvalidOperationException("No key provided.");
23+
24+
// Create a kernel with Azure OpenAI chat completion
25+
var builder = Kernel.CreateBuilder();
26+
27+
builder.Services.AddAzureOpenAIChatCompletion(deploymentName, endpoint, apiKey);
28+
29+
//builder.Plugins.AddFromPromptDirectory("Plugins");
30+
builder.Plugins.AddFromType<PeoplePlugin>("PeoplePlugin");
31+
32+
33+
// Build the kernel
34+
Kernel = builder.Build();
35+
36+
37+
}
38+
39+
public async Task<ChatMessageContent> GetChatAnswerAsync(ChatHistory history)
40+
{
41+
var chatService = Kernel.GetRequiredService<IChatCompletionService>();
42+
43+
OpenAIPromptExecutionSettings executionSettings = new()
44+
{
45+
ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions
46+
};
47+
48+
var result = await chatService.GetChatMessageContentAsync(
49+
history,
50+
executionSettings: executionSettings,
51+
kernel: Kernel);
52+
53+
return result;
54+
}
55+
}
56+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.SemanticKernel" Version="1.19.0" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="Plugins\PeoplePlugin\config.json">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="Plugins\PeoplePlugin\skprompt.txt">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.SemanticKernel;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.ComponentModel;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Demo.TddRepl.Plugins
10+
{
11+
public class PeoplePlugin
12+
{
13+
[KernelFunction]
14+
[Description("Returns some information for given person.")]
15+
public string GetPersonInfo(
16+
string person,
17+
[Description("User attitude: angry|normal")]
18+
string userAttitude)
19+
{
20+
if (userAttitude == "angry")
21+
{
22+
return $"{person} is a therapist.";
23+
}
24+
25+
return $"{person} is a software architect.";
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)