Skip to content

Commit 9d638e5

Browse files
shawncalrogerbarretolemillermicrosoft
authored
.Net: Replacing FunctionsView class with List (#2878)
### Motivation and Context The `FunctionsView` class wraps two inner Dictionaries of `FunctionView` objects: Native and Semantic. Since we no longer differentiate between these two for any practical purpose, we could merge those into one list, and we no longer need a special class for that. ### Description - Removing the different collections to store Semantic vs Native functions - Removing `FunctionsView` collection - From `SkillCollection.GetFunctionsView()`, returning an `IReadOnlyList<FunctionView>` - Callers can then easily use LINQ to filter the resulting flat list, or group by skill name. - Updating callers, tests Resolves #604 ### 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 - [ ] I didn't break anyone 😄 - BREAKING CHANGE as it modifies the public API. Low impact. --------- Co-authored-by: Roger Barreto <[email protected]> Co-authored-by: Lee Miller <[email protected]>
1 parent b71cd5d commit 9d638e5

17 files changed

Lines changed: 88 additions & 340 deletions

File tree

‎dotnet/samples/KernelSyntaxExamples/Example10_DescribeAllSkillsAndFunctions.cs‎

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

33
using System;
4-
using System.Collections.Concurrent;
5-
using System.Collections.Generic;
64
using System.Threading.Tasks;
75
using Microsoft.SemanticKernel;
86
using Microsoft.SemanticKernel.Plugins.Core;
@@ -51,30 +49,16 @@ public static Task RunAsync()
5149
description: "Write a bedtime story",
5250
maxTokens: 150);
5351

54-
FunctionsView functions = kernel.Skills.GetFunctionsView();
55-
ConcurrentDictionary<string, List<FunctionView>> nativeFunctions = functions.NativeFunctions;
56-
ConcurrentDictionary<string, List<FunctionView>> semanticFunctions = functions.SemanticFunctions;
52+
var functions = kernel.Skills.GetFunctionViews();
5753

5854
Console.WriteLine("*****************************************");
59-
Console.WriteLine("****** Native skills and functions ******");
55+
Console.WriteLine("****** Registered skills and functions ******");
6056
Console.WriteLine("*****************************************");
6157
Console.WriteLine();
6258

63-
foreach (KeyValuePair<string, List<FunctionView>> skill in nativeFunctions)
59+
foreach (FunctionView func in functions)
6460
{
65-
Console.WriteLine("Skill: " + skill.Key);
66-
foreach (FunctionView func in skill.Value) { PrintFunction(func); }
67-
}
68-
69-
Console.WriteLine("*****************************************");
70-
Console.WriteLine("***** Semantic skills and functions *****");
71-
Console.WriteLine("*****************************************");
72-
Console.WriteLine();
73-
74-
foreach (KeyValuePair<string, List<FunctionView>> skill in semanticFunctions)
75-
{
76-
Console.WriteLine("Skill: " + skill.Key);
77-
foreach (FunctionView func in skill.Value) { PrintFunction(func); }
61+
PrintFunction(func);
7862
}
7963

8064
return Task.CompletedTask;

‎dotnet/src/Extensions/Extensions.UnitTests/Planning/ActionPlanner/ActionPlannerTests.cs‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,7 @@ private Mock<IKernel> CreateMockKernelAndFunctionFlowWithTestString(string testP
146146
if (skills is null)
147147
{
148148
skills = new Mock<ISkillCollection>();
149-
150-
var functionsView = new FunctionsView();
151-
skills.Setup(x => x.GetFunctionsView(It.IsAny<bool>(), It.IsAny<bool>())).Returns(functionsView);
149+
skills.Setup(x => x.GetFunctionViews()).Returns(new List<FunctionView>());
152150
}
153151

154152
var returnContext = new SKContext(kernel.Object,
@@ -202,13 +200,13 @@ private Mock<ISkillCollection> CreateMockSkillCollection()
202200
("RepoList", "GitHubSkill", "List repositories", true),
203201
};
204202

205-
var functionsView = new FunctionsView();
203+
var functionsView = new List<FunctionView>();
206204
var skills = new Mock<ISkillCollection>();
207205
foreach (var (name, skillName, description, isSemantic) in functions)
208206
{
209207
var functionView = new FunctionView(name, skillName, description, new List<ParameterView>(), isSemantic, true);
210208
var mockFunction = CreateMockFunction(functionView);
211-
functionsView.AddFunction(functionView);
209+
functionsView.Add(functionView);
212210

213211
mockFunction.Setup(x =>
214212
x.InvokeAsync(It.IsAny<SKContext>(), It.IsAny<CompleteRequestSettings>(), It.IsAny<CancellationToken>()))
@@ -223,7 +221,7 @@ private Mock<ISkillCollection> CreateMockSkillCollection()
223221
skills.Setup(x => x.TryGetFunction(skillName, name, out outFunc)).Returns(true);
224222
}
225223

226-
skills.Setup(x => x.GetFunctionsView(It.IsAny<bool>(), It.IsAny<bool>())).Returns(functionsView);
224+
skills.Setup(x => x.GetFunctionViews()).Returns(functionsView);
227225
return skills;
228226
}
229227

‎dotnet/src/Extensions/Extensions.UnitTests/Planning/SequentialPlanner/SKContextExtensionsTests.cs‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@ public async Task CanCallGetAvailableFunctionsWithFunctionsAsync()
6868

6969
// Arrange FunctionView
7070
var functionMock = new Mock<ISKFunction>();
71-
var functionsView = new FunctionsView();
7271
var functionView = new FunctionView("functionName", "skillName", "description", new List<ParameterView>(), true, false);
7372
var nativeFunctionView = new FunctionView("nativeFunctionName", "skillName", "description", new List<ParameterView>(), false, false);
74-
functionsView.AddFunction(functionView);
75-
functionsView.AddFunction(nativeFunctionView);
73+
var functionsView = new List<FunctionView>() { functionView, nativeFunctionView };
7674

7775
// Arrange Mock Memory and Result
7876
var skills = new Mock<ISkillCollection>();
@@ -95,7 +93,7 @@ public async Task CanCallGetAvailableFunctionsWithFunctionsAsync()
9593

9694
skills.Setup(x => x.TryGetFunction(It.IsAny<string>(), It.IsAny<string>(), out It.Ref<ISKFunction?>.IsAny)).Returns(true);
9795
skills.Setup(x => x.GetFunction(It.IsAny<string>(), It.IsAny<string>())).Returns(functionMock.Object);
98-
skills.Setup(x => x.GetFunctionsView(It.IsAny<bool>(), It.IsAny<bool>())).Returns(functionsView);
96+
skills.Setup(x => x.GetFunctionViews()).Returns(functionsView);
9997

10098
// Arrange GetAvailableFunctionsAsync parameters
10199
var context = new SKContext(kernel.Object, variables, skills.Object);
@@ -135,11 +133,9 @@ public async Task CanCallGetAvailableFunctionsWithFunctionsWithRelevancyAsync()
135133

136134
// Arrange FunctionView
137135
var functionMock = new Mock<ISKFunction>();
138-
var functionsView = new FunctionsView();
139136
var functionView = new FunctionView("functionName", "skillName", "description", new List<ParameterView>(), true, false);
140137
var nativeFunctionView = new FunctionView("nativeFunctionName", "skillName", "description", new List<ParameterView>(), false, false);
141-
functionsView.AddFunction(functionView);
142-
functionsView.AddFunction(nativeFunctionView);
138+
var functionsView = new List<FunctionView>() { functionView, nativeFunctionView };
143139

144140
// Arrange Mock Memory and Result
145141
var skills = new Mock<ISkillCollection>();
@@ -162,7 +158,7 @@ public async Task CanCallGetAvailableFunctionsWithFunctionsWithRelevancyAsync()
162158

163159
skills.Setup(x => x.TryGetFunction(It.IsAny<string>(), It.IsAny<string>(), out It.Ref<ISKFunction?>.IsAny)).Returns(true);
164160
skills.Setup(x => x.GetFunction(It.IsAny<string>(), It.IsAny<string>())).Returns(functionMock.Object);
165-
skills.Setup(x => x.GetFunctionsView(It.IsAny<bool>(), It.IsAny<bool>())).Returns(functionsView);
161+
skills.Setup(x => x.GetFunctionViews()).Returns(functionsView);
166162

167163
// Arrange GetAvailableFunctionsAsync parameters
168164
var context = new SKContext(kernel.Object, variables, skills.Object);

‎dotnet/src/Extensions/Extensions.UnitTests/Planning/SequentialPlanner/SequentialPlanParserTests.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ private void CreateKernelAndFunctionCreateMocks(List<(string name, string skillN
6363
// For Create
6464
kernelMock.Setup(k => k.CreateNewContext()).Returns(this.CreateSKContext(kernel));
6565

66-
var functionsView = new FunctionsView();
66+
var functionsView = new List<FunctionView>();
6767
foreach (var (name, skillName, description, isSemantic, resultString) in functions)
6868
{
6969
var functionView = new FunctionView(name, skillName, description, new List<ParameterView>() { new(name: "param", description: "description") }, isSemantic, true);
7070
var mockFunction = CreateMockFunction(functionView);
71-
functionsView.AddFunction(functionView);
71+
functionsView.Add(functionView);
7272

7373
var result = this.CreateSKContext(kernel);
7474
result.Variables.Update(resultString);
@@ -93,7 +93,7 @@ private void CreateKernelAndFunctionCreateMocks(List<(string name, string skillN
9393
}
9494
}
9595

96-
skills.Setup(x => x.GetFunctionsView(It.IsAny<bool>(), It.IsAny<bool>())).Returns(functionsView);
96+
skills.Setup(x => x.GetFunctionViews()).Returns(functionsView);
9797
}
9898

9999
[Fact]

‎dotnet/src/Extensions/Extensions.UnitTests/Planning/SequentialPlanner/SequentialPlannerTests.cs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ public async Task ItCanCreatePlanAsync(string goal)
3434
("Summarize", "SummarizeSkill", "Summarize something", true)
3535
};
3636

37-
var functionsView = new FunctionsView();
37+
var functionsView = new List<FunctionView>();
3838
var skills = new Mock<ISkillCollection>();
3939
foreach (var (name, skillName, description, isSemantic) in input)
4040
{
4141
var functionView = new FunctionView(name, skillName, description, new List<ParameterView>(), isSemantic, true);
4242
var mockFunction = CreateMockFunction(functionView);
43-
functionsView.AddFunction(functionView);
43+
functionsView.Add(functionView);
4444

4545
mockFunction.Setup(x =>
4646
x.InvokeAsync(It.IsAny<SKContext>(), It.IsAny<CompleteRequestSettings>(), It.IsAny<CancellationToken>()))
@@ -56,7 +56,7 @@ public async Task ItCanCreatePlanAsync(string goal)
5656
skills.Setup(x => x.TryGetFunction(It.Is<string>(s => s == skillName), It.Is<string>(s => s == name), out outFunc)).Returns(true);
5757
}
5858

59-
skills.Setup(x => x.GetFunctionsView(It.IsAny<bool>(), It.IsAny<bool>())).Returns(functionsView);
59+
skills.Setup(x => x.GetFunctionViews()).Returns(functionsView);
6060

6161
var expectedFunctions = input.Select(x => x.name).ToList();
6262
var expectedSkills = input.Select(x => x.skillName).ToList();
@@ -150,8 +150,7 @@ public async Task InvalidXMLThrowsAsync()
150150
var kernel = new Mock<IKernel>();
151151
var skills = new Mock<ISkillCollection>();
152152

153-
var functionsView = new FunctionsView();
154-
skills.Setup(x => x.GetFunctionsView(It.IsAny<bool>(), It.IsAny<bool>())).Returns(functionsView);
153+
skills.Setup(x => x.GetFunctionViews()).Returns(new List<FunctionView>());
155154

156155
var planString = "<plan>notvalid<</plan>";
157156
var returnContext = new SKContext(

‎dotnet/src/Extensions/Planning.ActionPlanner/ActionPlanner.cs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,16 +314,13 @@ private static string AddPeriod(string x)
314314
private IOrderedEnumerable<FunctionView> GetAvailableFunctions(SKContext context)
315315
{
316316
Verify.NotNull(context.Skills);
317-
FunctionsView functionsView = context.Skills.GetFunctionsView();
318317

319318
var excludedSkills = this._config.ExcludedSkills ?? new();
320319
var excludedFunctions = this._config.ExcludedFunctions ?? new();
321320

322-
var availableFunctions =
323-
functionsView.NativeFunctions
324-
.Concat(functionsView.SemanticFunctions)
325-
.SelectMany(x => x.Value)
326-
.Where(s => !excludedSkills.Contains(s.SkillName) && !excludedFunctions.Contains(s.Name))
321+
var availableFunctions = context.Skills.GetFunctionViews()
322+
.Where(s => !excludedSkills.Contains(s.SkillName, StringComparer.CurrentCultureIgnoreCase)
323+
&& !excludedFunctions.Contains(s.Name, StringComparer.CurrentCultureIgnoreCase))
327324
.OrderBy(x => x.SkillName)
328325
.ThenBy(x => x.Name);
329326

‎dotnet/src/Extensions/Planning.SequentialPlanner/SKContextSequentialPlannerExtensions.cs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3+
using System;
34
using System.Collections.Concurrent;
45
using System.Collections.Generic;
56
using System.Linq;
@@ -62,12 +63,11 @@ public static async Task<IOrderedEnumerable<FunctionView>> GetAvailableFunctions
6263
string? semanticQuery = null,
6364
CancellationToken cancellationToken = default)
6465
{
65-
var functionsView = context.Skills.GetFunctionsView();
66+
var functionsView = context.Skills.GetFunctionViews();
6667

67-
var availableFunctions = functionsView.SemanticFunctions
68-
.Concat(functionsView.NativeFunctions)
69-
.SelectMany(x => x.Value)
70-
.Where(s => !config.ExcludedSkills.Contains(s.SkillName) && !config.ExcludedFunctions.Contains(s.Name))
68+
var availableFunctions = functionsView
69+
.Where(s => !config.ExcludedSkills.Contains(s.SkillName, StringComparer.OrdinalIgnoreCase)
70+
&& !config.ExcludedFunctions.Contains(s.Name, StringComparer.OrdinalIgnoreCase))
7171
.ToList();
7272

7373
List<FunctionView>? result = null;

‎dotnet/src/Extensions/Planning.StepwisePlanner/StepwisePlanner.cs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,15 +570,13 @@ private Task<IOrderedEnumerable<FunctionView>> GetAvailableFunctionsAsync()
570570
{
571571
if (this.Config.GetAvailableFunctionsAsync is null)
572572
{
573-
FunctionsView functionsView = this._context.Skills!.GetFunctionsView();
573+
var functionsView = this._context.Skills!.GetFunctionViews();
574574

575575
var excludedSkills = this.Config.ExcludedSkills ?? new();
576576
var excludedFunctions = this.Config.ExcludedFunctions ?? new();
577577

578578
var availableFunctions =
579-
functionsView.NativeFunctions
580-
.Concat(functionsView.SemanticFunctions)
581-
.SelectMany(x => x.Value)
579+
functionsView
582580
.Where(s => !excludedSkills.Contains(s.SkillName) && !excludedFunctions.Contains(s.Name))
583581
.OrderBy(x => x.SkillName)
584582
.ThenBy(x => x.Name);

‎dotnet/src/SemanticKernel.Abstractions/Orchestration/SKContext.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ private string DebuggerDisplay
160160

161161
if (this.Skills is IReadOnlySkillCollection skills)
162162
{
163-
var view = skills.GetFunctionsView();
164-
display += $", Skills = {view.NativeFunctions.Count + view.SemanticFunctions.Count}";
163+
var view = skills.GetFunctionViews();
164+
display += $", Skills = {view.Count}";
165165
}
166166

167167
display += $", Culture = {this.Culture.EnglishName}";

‎dotnet/src/SemanticKernel.Abstractions/SkillDefinition/FunctionsView.cs‎

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)