forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKernelFunctionFromMethodTests2.cs
510 lines (430 loc) · 16.2 KB
/
KernelFunctionFromMethodTests2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Xunit;
namespace SemanticKernel.UnitTests.Functions;
public sealed class KernelFunctionFromMethodTests2
{
private static readonly KernelFunction s_nopFunction = KernelFunctionFactory.CreateFromMethod(() => { });
[Fact]
public void ItDoesntThrowForValidFunctionsViaDelegate()
{
// Arrange
var pluginInstance = new LocalExamplePlugin();
MethodInfo[] methods = pluginInstance.GetType()
.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod)
.Where(m => m.Name is not ("GetType" or "Equals" or "GetHashCode" or "ToString" or "Finalize" or "MemberwiseClone"))
.ToArray();
KernelFunction[] functions = (from method in methods select KernelFunctionFactory.CreateFromMethod(method, pluginInstance, "plugin")).ToArray();
// Act
Assert.Equal(methods.Length, functions.Length);
Assert.All(functions, Assert.NotNull);
}
[Fact]
public void ItDoesNotThrowForValidFunctionsViaPlugin()
{
// Arrange
var pluginInstance = new LocalExamplePlugin();
MethodInfo[] methods = pluginInstance.GetType()
.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod)
.Where(m => m.Name is not ("GetType" or "Equals" or "GetHashCode" or "ToString" or "Finalize" or "MemberwiseClone"))
.ToArray();
KernelFunction[] functions = [.. KernelPluginFactory.CreateFromObject(pluginInstance)];
// Act
Assert.Equal(methods.Length, functions.Length);
Assert.All(functions, Assert.NotNull);
}
[Fact]
public void ItKeepsDefaultValueNullWhenNotProvided()
{
// Arrange & Act
var pluginInstance = new LocalExamplePlugin();
var plugin = KernelPluginFactory.CreateFromObject(pluginInstance);
// Assert
this.AssertDefaultValue(plugin, "Type04Nullable", "input", null, true);
this.AssertDefaultValue(plugin, "Type04Optional", "input", null, false);
this.AssertDefaultValue(plugin, "Type05", "input", null, true);
this.AssertDefaultValue(plugin, "Type05Nullable", "input", null, false);
this.AssertDefaultValue(plugin, "Type05EmptyDefault", "input", string.Empty, false);
this.AssertDefaultValue(plugin, "Type05DefaultProvided", "input", "someDefault", false);
}
internal void AssertDefaultValue(KernelPlugin plugin, string functionName, string parameterName, object? expectedDefaultValue, bool expectedIsRequired)
{
var functionExists = plugin.TryGetFunction(functionName, out var function);
Assert.True(functionExists);
Assert.NotNull(function);
var parameter = function.Metadata.Parameters.First(p => p.Name == parameterName);
Assert.NotNull(parameter);
Assert.Equal(expectedDefaultValue, parameter.DefaultValue);
Assert.Equal(expectedIsRequired, parameter.IsRequired);
}
[Fact]
public async Task ItCanImportMethodFunctionsAsync()
{
// Arrange
var canary = false;
// Note: the function doesn't have any SK attributes
async Task ExecuteAsync(string done)
{
Assert.Equal("NO", done);
canary = true;
await Task.Delay(0);
}
// Act
KernelFunction function = KernelFunctionFactory.CreateFromMethod(
method: ExecuteAsync,
parameters: null,
description: "description",
functionName: "functionName");
FunctionResult result = await function.InvokeAsync(new(), new KernelArguments
{
["done"] = "NO"
});
// Assert
Assert.True(canary);
Assert.Null(result.GetValue<object?>());
Assert.Empty(result.ToString());
}
[Fact]
public async Task ItCanImportClosedGenericsAsync()
{
await Validate(KernelPluginFactory.CreateFromObject(new GenericPlugin<int>()));
await Validate(KernelPluginFactory.CreateFromType<GenericPlugin<int>>());
async Task Validate(KernelPlugin plugin)
{
Assert.Equal("GenericPlugin_Int32", plugin.Name);
Assert.Equal(3, plugin.FunctionCount);
foreach (KernelFunction function in plugin)
{
FunctionResult result = await function.InvokeAsync(new(), new() { { "input", 42 } });
Assert.Equal(42, result.Value);
}
}
}
[Fact]
public async Task ItCanImportMethodFunctionsWithExternalReferencesAsync()
{
// Arrange
var arguments = new KernelArguments
{
["done"] = "NO"
};
// Note: This is an important edge case that affects the function signature and how delegates
// are handled internally: the function references an external variable and cannot be static.
// This scenario is used for gRPC functions.
string variableOutsideTheFunction = "foo";
async Task<string> ExecuteAsync(string done)
{
string referenceToExternalVariable = variableOutsideTheFunction;
await Task.Delay(0);
return referenceToExternalVariable;
}
// Act. Note: this will throw an exception if the KernelFunction doesn't handle the function type.
KernelFunction function = KernelFunctionFactory.CreateFromMethod(
method: ExecuteAsync,
description: "description",
functionName: "functionName");
FunctionResult result = await function.InvokeAsync(new(), arguments);
// Assert
Assert.Equal(variableOutsideTheFunction, result.GetValue<string>());
Assert.Equal(variableOutsideTheFunction, result.ToString());
}
[Fact]
public async Task ItFlowsSpecialArgumentsIntoFunctionsAsync()
{
KernelBuilder builder = new();
builder.Services.AddLogging(c => c.SetMinimumLevel(LogLevel.Warning));
Kernel kernel = builder.Build();
kernel.Culture = new CultureInfo("fr-FR");
KernelArguments args = [];
using CancellationTokenSource cts = new();
bool invoked = false;
KernelFunction func = null!;
func = KernelFunctionFactory.CreateFromMethod(
(Kernel kernelArg, KernelFunction funcArg, KernelArguments argsArg, ILoggerFactory loggerFactoryArg,
ILogger loggerArg, IAIServiceSelector serviceSelectorArg, CultureInfo cultureArg, CancellationToken cancellationToken) =>
{
Assert.Same(kernel, kernelArg);
Assert.Same(func, funcArg);
Assert.Same(args, argsArg);
Assert.Same(kernel.LoggerFactory, loggerFactoryArg);
Assert.NotNull(loggerArg);
Assert.Same(kernel.ServiceSelector, serviceSelectorArg);
Assert.Same(kernel.Culture, cultureArg);
Assert.Equal(cts.Token, cancellationToken);
invoked = true;
});
await func.InvokeAsync(kernel, args, cts.Token);
Assert.True(invoked);
}
[Fact]
public async Task ItInjectsServicesFromDIIntoFunctionsAsync()
{
var serviceA = new ExampleService();
var serviceB = new ExampleService();
var serviceC = new ExampleService();
KernelBuilder builder = new();
builder.Services.AddKeyedSingleton<IExampleService>("something", serviceA);
builder.Services.AddSingleton<IExampleService>(serviceB);
builder.Services.AddKeyedSingleton<IExampleService>("somethingelse", serviceC);
Kernel kernel = builder.Build();
bool invoked = false;
KernelFunction func = KernelFunctionFactory.CreateFromMethod(
([FromKernelServices] IExampleService service1Arg,
[FromKernelServices("something")] IExampleService service2Arg,
[FromKernelServices("somethingelse")] IExampleService service3Arg,
[FromKernelServices] IExampleService service4Arg,
[FromKernelServices("doesntexist")] IExampleService? service5Arg = null) =>
{
Assert.Same(serviceB, service1Arg);
Assert.Same(serviceA, service2Arg);
Assert.Same(serviceC, service3Arg);
Assert.Same(serviceB, service4Arg);
Assert.Null(service5Arg);
invoked = true;
});
await func.InvokeAsync(kernel);
Assert.True(invoked);
Assert.DoesNotContain(func.Metadata.Parameters, p => p.Name.Contains("service", StringComparison.Ordinal));
}
[Fact]
public async Task ItThrowsForMissingServicesWithoutDefaultsAsync()
{
Kernel kernel = new();
KernelFunction func;
func = KernelFunctionFactory.CreateFromMethod(([FromKernelServices] IExampleService service) => { });
await Assert.ThrowsAsync<KernelException>(() => func.InvokeAsync(kernel));
func = KernelFunctionFactory.CreateFromMethod(([FromKernelServices] IExampleService? service) => { });
await Assert.ThrowsAsync<KernelException>(() => func.InvokeAsync(kernel));
func = KernelFunctionFactory.CreateFromMethod(([FromKernelServices("name")] IExampleService? service) => { });
await Assert.ThrowsAsync<KernelException>(() => func.InvokeAsync(kernel));
}
[Fact]
public void ItMakesProvidedExtensionPropertiesAvailableViaMetadataWhenConstructedFromDelegate()
{
// Act.
var func = KernelFunctionFactory.CreateFromMethod(() => { return "Value1"; }, new KernelFunctionFromMethodOptions
{
AdditionalMetadata = new ReadOnlyDictionary<string, object?>(new Dictionary<string, object?>
{
["key1"] = "value1",
})
});
// Assert.
Assert.Contains("key1", func.Metadata.AdditionalProperties.Keys);
Assert.Equal("value1", func.Metadata.AdditionalProperties["key1"]);
}
[Fact]
public void ItMakesProvidedExtensionPropertiesAvailableViaMetadataWhenConstructedFromMethodInfo()
{
// Arrange.
var target = new LocalExamplePlugin();
var methodInfo = target.GetType().GetMethod(nameof(LocalExamplePlugin.Type02))!;
// Act.
var func = KernelFunctionFactory.CreateFromMethod(methodInfo, target, new KernelFunctionFromMethodOptions
{
AdditionalMetadata = new ReadOnlyDictionary<string, object?>(new Dictionary<string, object?>
{
["key1"] = "value1",
})
});
// Assert.
Assert.Contains("key1", func.Metadata.AdditionalProperties.Keys);
Assert.Equal("value1", func.Metadata.AdditionalProperties["key1"]);
}
[Fact]
public void ItShouldExposeUnderlyingMethod()
{
// Arrange
var target = new LocalExamplePlugin();
var methodInfo = target.GetType().GetMethod(nameof(LocalExamplePlugin.FunctionWithCustomAttribute))!;
var kernelFunction = KernelFunctionFactory.CreateFromMethod(methodInfo, target);
// Assert
Assert.NotNull(kernelFunction.UnderlyingMethod);
Assert.Equal(methodInfo, kernelFunction.UnderlyingMethod);
Assert.NotNull(kernelFunction.UnderlyingMethod.GetCustomAttribute<CustomAttribute>());
}
private interface IExampleService;
private sealed class ExampleService : IExampleService;
private sealed class LocalExamplePlugin
{
[KernelFunction]
public void Type01()
{
}
[KernelFunction]
public string Type02()
{
return "";
}
[KernelFunction]
public string? Type02Nullable()
{
return null;
}
[KernelFunction]
public async Task<string> Type03Async()
{
await Task.Delay(0);
return "";
}
[KernelFunction]
public async Task<string?> Type03NullableAsync()
{
await Task.Delay(0);
return null;
}
[KernelFunction]
public void Type04(string input)
{
}
[KernelFunction]
public void Type04Nullable(string? input)
{
}
[KernelFunction]
public void Type04Optional([Optional] string input)
{
}
[KernelFunction]
public string Type05(string input)
{
return "";
}
[KernelFunction]
private string? Type05Nullable(string? input = null)
{
return "";
}
[KernelFunction]
internal string? Type05EmptyDefault(string? input = "")
{
return "";
}
[KernelFunction]
public string? Type05DefaultProvided(string? input = "someDefault")
{
return "";
}
[KernelFunction]
public async Task<string> Type06Async(string input)
{
await Task.Delay(0);
return "";
}
[KernelFunction]
public async Task<string?> Type06NullableAsync(string? input)
{
await Task.Delay(0);
return "";
}
[KernelFunction]
public async Task Type07Async(string input)
{
await Task.Delay(0);
}
[KernelFunction]
public async Task Type08Async()
{
await Task.Delay(0);
}
[KernelFunction]
public async ValueTask ReturnsValueTaskAsync()
{
await Task.Delay(0);
}
[KernelFunction]
public async ValueTask<string> ReturnsValueTaskStringAsync()
{
await Task.Delay(0);
return "hello world";
}
[KernelFunction]
public FunctionResult ReturnsFunctionResult()
{
return new FunctionResult(s_nopFunction, "fake-result", CultureInfo.InvariantCulture);
}
[KernelFunction]
public async Task<FunctionResult> ReturnsTaskFunctionResultAsync()
{
await Task.Delay(0);
return new FunctionResult(s_nopFunction, "fake-result", CultureInfo.InvariantCulture);
}
[KernelFunction]
public async ValueTask<FunctionResult> ReturnsValueTaskFunctionResultAsync()
{
await Task.Delay(0);
return new FunctionResult(s_nopFunction, "fake-result", CultureInfo.InvariantCulture);
}
[KernelFunction]
public string WithPrimitives(
byte a1,
byte? b1,
sbyte c1,
sbyte? d1,
short e1,
short? f1,
ushort g1,
ushort? h1,
int i1,
int? j1,
uint k1,
uint? l1,
long m1,
long? n1,
ulong o1,
ulong? p1,
float q1,
float? r1,
double s1,
double? t1,
decimal u1,
decimal? v1,
char w1,
char? x1,
bool y1,
bool? z1,
DateTime a2,
DateTime? b2,
DateTimeOffset c2,
DateTimeOffset? d2,
TimeSpan e2,
TimeSpan? f2,
Guid g2,
Guid? h2,
DayOfWeek i2,
DayOfWeek? j2,
Uri k2,
string l2)
{
return string.Empty;
}
[KernelFunction, CustomAttribute]
public void FunctionWithCustomAttribute()
{
}
}
private sealed class GenericPlugin<T>
{
[KernelFunction]
public int GetValue1(int input) => input;
[KernelFunction]
public T GetValue2(T input) => input;
[KernelFunction]
public Task<T> GetValue3Async(T input) => Task.FromResult(input);
}
[AttributeUsage(AttributeTargets.Method)]
private sealed class CustomAttribute : Attribute
{
}
}