forked from dotnet/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClientIntegrationTests.cs
More file actions
1138 lines (924 loc) · 45 KB
/
Copy pathChatClientIntegrationTests.cs
File metadata and controls
1138 lines (924 loc) · 45 KB
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Microsoft.TestUtilities;
using OpenTelemetry.Trace;
using Xunit;
#pragma warning disable CA2000 // Dispose objects before losing scope
#pragma warning disable CA2214 // Do not call overridable methods in constructors
#pragma warning disable CA2249 // Consider using 'string.Contains' instead of 'string.IndexOf'
#pragma warning disable S103 // Lines should not be too long
#pragma warning disable S1144 // Unused private types or members should be removed
#pragma warning disable S3604 // Member initializer values should not be redundant
#pragma warning disable SA1515 // Single-line comment should be preceded by blank line
namespace Microsoft.Extensions.AI;
public abstract class ChatClientIntegrationTests : IDisposable
{
private readonly IChatClient? _chatClient;
protected ChatClientIntegrationTests()
{
_chatClient = CreateChatClient();
}
public void Dispose()
{
_chatClient?.Dispose();
GC.SuppressFinalize(this);
}
protected abstract IChatClient? CreateChatClient();
[ConditionalFact]
public virtual async Task GetResponseAsync_SingleRequestMessage()
{
SkipIfNotEnabled();
var response = await _chatClient.GetResponseAsync("What's the biggest animal?");
Assert.Contains("whale", response.Text, StringComparison.OrdinalIgnoreCase);
}
[ConditionalFact]
public virtual async Task GetResponseAsync_MultipleRequestMessages()
{
SkipIfNotEnabled();
var response = await _chatClient.GetResponseAsync(
[
new(ChatRole.User, "Pick a city, any city"),
new(ChatRole.Assistant, "Seattle"),
new(ChatRole.User, "And another one"),
new(ChatRole.Assistant, "Jakarta"),
new(ChatRole.User, "What continent are they each in?"),
]);
Assert.Contains("America", response.Text);
Assert.Contains("Asia", response.Text);
}
[ConditionalFact]
public virtual async Task GetResponseAsync_WithEmptyMessage()
{
SkipIfNotEnabled();
var response = await _chatClient.GetResponseAsync(
[
new(ChatRole.System, []),
new(ChatRole.User, []),
new(ChatRole.Assistant, []),
new(ChatRole.User, "What is 1 + 2? Reply with a single number."),
]);
Assert.Contains("3", response.Text);
}
[ConditionalFact]
public virtual async Task GetStreamingResponseAsync()
{
SkipIfNotEnabled();
IList<ChatMessage> chatHistory =
[
new(ChatRole.User, "Quote, word for word, Neil Armstrong's famous words.")
];
StringBuilder sb = new();
await foreach (var chunk in _chatClient.GetStreamingResponseAsync(chatHistory))
{
sb.Append(chunk.Text);
}
string responseText = sb.ToString();
Assert.Contains("one small step", responseText, StringComparison.OrdinalIgnoreCase);
Assert.Contains("one giant leap", responseText, StringComparison.OrdinalIgnoreCase);
}
[ConditionalFact]
public virtual async Task GetResponseAsync_UsageDataAvailable()
{
SkipIfNotEnabled();
var response = await _chatClient.GetResponseAsync("Explain in 10 words how AI works");
Assert.True(response.Usage?.InputTokenCount > 1);
Assert.True(response.Usage?.OutputTokenCount > 1);
Assert.Equal(response.Usage?.InputTokenCount + response.Usage?.OutputTokenCount, response.Usage?.TotalTokenCount);
}
[ConditionalFact]
public virtual async Task GetStreamingResponseAsync_UsageDataAvailable()
{
SkipIfNotEnabled();
var response = _chatClient.GetStreamingResponseAsync("Explain in 10 words how AI works", new()
{
AdditionalProperties = new()
{
["stream_options"] = new Dictionary<string, object> { ["include_usage"] = true, },
},
});
List<ChatResponseUpdate> chunks = [];
await foreach (var chunk in response)
{
chunks.Add(chunk);
}
Assert.True(chunks.Count > 1);
UsageContent usage = chunks.SelectMany(c => c.Contents).OfType<UsageContent>().Single();
Assert.True(usage.Details.InputTokenCount > 1);
Assert.True(usage.Details.OutputTokenCount > 1);
Assert.Equal(usage.Details.InputTokenCount + usage.Details.OutputTokenCount, usage.Details.TotalTokenCount);
}
[ConditionalFact]
public virtual async Task GetStreamingResponseAsync_AppendToHistory()
{
SkipIfNotEnabled();
List<ChatMessage> history = [new(ChatRole.User, "Explain in 100 words how AI works")];
var streamingResponse = _chatClient.GetStreamingResponseAsync(history);
Assert.Single(history);
await history.AddMessagesAsync(streamingResponse);
Assert.Equal(2, history.Count);
Assert.Equal(ChatRole.Assistant, history[1].Role);
var singleTextContent = (TextContent)history[1].Contents.Single();
Assert.NotEmpty(singleTextContent.Text);
Assert.Equal(history[1].Text, singleTextContent.Text);
}
protected virtual string? GetModel_MultiModal_DescribeImage() => null;
[ConditionalFact]
public virtual async Task MultiModal_DescribeImage()
{
SkipIfNotEnabled();
var response = await _chatClient.GetResponseAsync(
[
new(ChatRole.User,
[
new TextContent("What does this logo say?"),
new DataContent(ImageDataUri.GetImageDataUri(), "image/png"),
])
],
new() { ModelId = GetModel_MultiModal_DescribeImage() });
Assert.True(response.Text.IndexOf("net", StringComparison.OrdinalIgnoreCase) >= 0, response.Text);
}
[ConditionalFact]
public virtual async Task MultiModal_DescribePdf()
{
SkipIfNotEnabled();
var response = await _chatClient.GetResponseAsync(
[
new(ChatRole.User,
[
new TextContent("What text does this document contain?"),
new DataContent(ImageDataUri.GetPdfDataUri(), "application/pdf"),
])
],
new() { ModelId = GetModel_MultiModal_DescribeImage() });
Assert.True(response.Text.IndexOf("hello", StringComparison.OrdinalIgnoreCase) >= 0, response.Text);
}
[ConditionalFact]
public virtual async Task FunctionInvocation_AutomaticallyInvokeFunction_Parameterless()
{
SkipIfNotEnabled();
var sourceName = Guid.NewGuid().ToString();
var activities = new List<Activity>();
using var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
.AddSource(sourceName)
.AddInMemoryExporter(activities)
.Build();
using var chatClient = new FunctionInvokingChatClient(
new OpenTelemetryChatClient(_chatClient, sourceName: sourceName));
int secretNumber = 42;
List<ChatMessage> messages =
[
new(ChatRole.User, "What is the current secret number?")
];
var response = await chatClient.GetResponseAsync(messages, new()
{
Tools = [AIFunctionFactory.Create(() => secretNumber, "GetSecretNumber")]
});
Assert.Contains(secretNumber.ToString(), response.Text);
AssertUsageAgainstActivities(response, activities);
}
[ConditionalFact]
public virtual async Task FunctionInvocation_AutomaticallyInvokeFunction_WithParameters_NonStreaming()
{
SkipIfNotEnabled();
using var chatClient = new FunctionInvokingChatClient(_chatClient);
var response = await chatClient.GetResponseAsync("What is the result of SecretComputation on 42 and 84?", new()
{
Tools = [AIFunctionFactory.Create((int a, int b) => a * b, "SecretComputation")]
});
Assert.Contains("3528", response.Text);
}
[ConditionalFact]
public virtual async Task FunctionInvocation_AutomaticallyInvokeFunction_WithParameters_Streaming()
{
SkipIfNotEnabled();
using var chatClient = new FunctionInvokingChatClient(_chatClient);
var response = chatClient.GetStreamingResponseAsync("What is the result of SecretComputation on 42 and 84?", new()
{
Tools = [AIFunctionFactory.Create((int a, int b) => a * b, "SecretComputation")]
});
StringBuilder sb = new();
await foreach (var chunk in response)
{
sb.Append(chunk.Text);
}
Assert.Contains("3528", sb.ToString());
}
[ConditionalFact]
public virtual async Task FunctionInvocation_OptionalParameter()
{
SkipIfNotEnabled();
var sourceName = Guid.NewGuid().ToString();
var activities = new List<Activity>();
using var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
.AddSource(sourceName)
.AddInMemoryExporter(activities)
.Build();
using var chatClient = new FunctionInvokingChatClient(
new OpenTelemetryChatClient(_chatClient, sourceName: sourceName));
int secretNumber = 42;
List<ChatMessage> messages =
[
new(ChatRole.User, "What is the secret number for id foo?")
];
AIFunction func = AIFunctionFactory.Create((string id = "defaultId") => id is "foo" ? secretNumber : -1, "GetSecretNumberById");
var response = await chatClient.GetResponseAsync(messages, new()
{
Tools = [func]
});
Assert.Contains(secretNumber.ToString(), response.Text);
AssertUsageAgainstActivities(response, activities);
}
[ConditionalFact]
public virtual async Task FunctionInvocation_NestedParameters()
{
SkipIfNotEnabled();
var sourceName = Guid.NewGuid().ToString();
var activities = new List<Activity>();
using var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
.AddSource(sourceName)
.AddInMemoryExporter(activities)
.Build();
using var chatClient = new FunctionInvokingChatClient(
new OpenTelemetryChatClient(_chatClient, sourceName: sourceName));
int secretNumber = 42;
List<ChatMessage> messages =
[
new(ChatRole.User, "What is the secret number for John aged 19?")
];
AIFunction func = AIFunctionFactory.Create((PersonRecord person) => person.Name is "John" ? secretNumber + person.Age : -1, "GetSecretNumberByPerson");
var response = await chatClient.GetResponseAsync(messages, new()
{
Tools = [func]
});
Assert.Contains((secretNumber + 19).ToString(), response.Text);
AssertUsageAgainstActivities(response, activities);
}
[ConditionalFact]
public virtual async Task FunctionInvocation_ArrayParameter()
{
SkipIfNotEnabled();
var sourceName = Guid.NewGuid().ToString();
var activities = new List<Activity>();
using var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
.AddSource(sourceName)
.AddInMemoryExporter(activities)
.Build();
using var chatClient = new FunctionInvokingChatClient(
new OpenTelemetryChatClient(_chatClient, sourceName: sourceName));
List<ChatMessage> messages =
[
new(ChatRole.User, "Can you add bacon, lettuce, and tomatoes to Peter's shopping cart?")
];
string? shopperName = null;
List<string> shoppingCart = [];
AIFunction func = AIFunctionFactory.Create((string[] items, string shopperId) => { shoppingCart.AddRange(items); shopperName = shopperId; }, "AddItemsToShoppingCart");
var response = await chatClient.GetResponseAsync(messages, new()
{
Tools = [func]
});
Assert.Equal("Peter", shopperName);
Assert.Equal(["bacon", "lettuce", "tomatoes"], shoppingCart);
AssertUsageAgainstActivities(response, activities);
}
private static void AssertUsageAgainstActivities(ChatResponse response, List<Activity> activities)
{
// If the underlying IChatClient provides usage data, function invocation should aggregate the
// usage data across all calls to produce a single Usage value on the final response.
// The FunctionInvokingChatClient then itself creates a span that will also be tagged with a sum
// across all consituent calls, which means our final answer will be double.
if (response.Usage is { } finalUsage)
{
var totalInputTokens = activities.Sum(a => (int?)a.GetTagItem("gen_ai.usage.input_tokens")!);
var totalOutputTokens = activities.Sum(a => (int?)a.GetTagItem("gen_ai.usage.output_tokens")!);
Assert.Equal(totalInputTokens, finalUsage.InputTokenCount * 2);
Assert.Equal(totalOutputTokens, finalUsage.OutputTokenCount * 2);
}
}
public record PersonRecord(string Name, int Age = 42);
[ConditionalFact]
public virtual Task AvailableTools_SchemasAreAccepted_Strict() =>
AvailableTools_SchemasAreAccepted(strict: true);
[ConditionalFact]
public virtual Task AvailableTools_SchemasAreAccepted_NonStrict() =>
AvailableTools_SchemasAreAccepted(strict: false);
private async Task AvailableTools_SchemasAreAccepted(bool strict)
{
SkipIfNotEnabled();
var sourceName = Guid.NewGuid().ToString();
var activities = new List<Activity>();
using var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
.AddSource(sourceName)
.AddInMemoryExporter(activities)
.Build();
using var chatClient = new FunctionInvokingChatClient(
new OpenTelemetryChatClient(_chatClient, sourceName: sourceName));
int methodCount = 1;
Func<AIFunctionFactoryOptions> createOptions = () =>
{
AIFunctionFactoryOptions aiFuncOptions = new()
{
Name = $"Method{methodCount++}",
};
if (strict)
{
aiFuncOptions.AdditionalProperties = new Dictionary<string, object?> { ["strictJsonSchema"] = true };
}
return aiFuncOptions;
};
Func<string, AIFunction> createWithSchema = schema =>
{
Dictionary<string, object?> additionalProperties = new();
if (strict)
{
additionalProperties["strictJsonSchema"] = true;
}
return new CustomAIFunction($"CustomMethod{methodCount++}", schema, additionalProperties);
};
ChatOptions options = new()
{
MaxOutputTokens = 100,
Tools =
[
// Using AIFunctionFactory
AIFunctionFactory.Create((int? i) => i, createOptions()),
AIFunctionFactory.Create((string? s) => s, createOptions()),
AIFunctionFactory.Create((int? i = null) => i, createOptions()),
AIFunctionFactory.Create((bool b) => b, createOptions()),
AIFunctionFactory.Create((double d) => d, createOptions()),
AIFunctionFactory.Create((decimal d) => d, createOptions()),
AIFunctionFactory.Create((float f) => f, createOptions()),
AIFunctionFactory.Create((long l) => l, createOptions()),
AIFunctionFactory.Create((char c) => c, createOptions()),
AIFunctionFactory.Create((DateTime dt) => dt, createOptions()),
AIFunctionFactory.Create((DateTimeOffset? dt) => dt, createOptions()),
AIFunctionFactory.Create((TimeSpan ts) => ts, createOptions()),
#if NET
AIFunctionFactory.Create((DateOnly d) => d, createOptions()),
AIFunctionFactory.Create((TimeOnly t) => t, createOptions()),
#endif
AIFunctionFactory.Create((Uri uri) => uri, createOptions()),
AIFunctionFactory.Create((Guid guid) => guid, createOptions()),
AIFunctionFactory.Create((List<int> list) => list, createOptions()),
AIFunctionFactory.Create((int[] arr, ComplexObject? co) => arr, createOptions()),
AIFunctionFactory.Create((string p1 = "str", int p2 = 42, BindingFlags p3 = BindingFlags.IgnoreCase, char p4 = 'x') => p1, createOptions()),
AIFunctionFactory.Create((string? p1 = "str", int? p2 = 42, BindingFlags? p3 = BindingFlags.IgnoreCase, char? p4 = 'x') => p1, createOptions()),
// Selection from @modelcontextprotocol/server-everything
createWithSchema("""
{"type":"object","properties":{},"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}
"""),
createWithSchema("""
{"type":"object","properties":{"duration":{"type":"number","default":10,"description":"Duration of the operation in seconds"},"steps":{"type":"number","default":5,"description":"Number of steps in the operation"}},"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}
"""),
createWithSchema("""
{"type":"object","properties":{"prompt":{"type":"string","description":"The prompt to send to the LLM"},"maxTokens":{"type":"number","default":100,"description":"Maximum number of tokens to generate"}},"required":["prompt"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}
"""),
createWithSchema("""
{"type":"object","properties":{},"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}
"""),
createWithSchema("""
{"type":"object","properties":{"messageType":{"type":"string","enum":["error","success","debug"],"description":"Type of message to demonstrate different annotation patterns"},"includeImage":{"type":"boolean","default":false,"description":"Whether to include an example image"}},"required":["messageType"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}
"""),
createWithSchema("""
{"type":"object","properties":{"resourceId":{"type":"number","minimum":1,"maximum":100,"description":"ID of the resource to reference (1-100)"}},"required":["resourceId"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}
"""),
// Selection from GH MCP server
createWithSchema("""
{"properties":{"body":{"description":"The text of the review comment","type":"string"},"line":{"description":"The line of the blob in the pull request diff that the comment applies to. For multi-line comments, the last line of the range","type":"number"},"owner":{"description":"Repository owner","type":"string"},"path":{"description":"The relative path to the file that necessitates a comment","type":"string"},"pullNumber":{"description":"Pull request number","type":"number"},"repo":{"description":"Repository name","type":"string"},"side":{"description":"The side of the diff to comment on. LEFT indicates the previous state, RIGHT indicates the new state","enum":["LEFT","RIGHT"],"type":"string"},"startLine":{"description":"For multi-line comments, the first line of the range that the comment applies to","type":"number"},"startSide":{"description":"For multi-line comments, the starting side of the diff that the comment applies to. LEFT indicates the previous state, RIGHT indicates the new state","enum":["LEFT","RIGHT"],"type":"string"},"subjectType":{"description":"The level at which the comment is targeted","enum":["FILE","LINE"],"type":"string"}},"required":["owner","repo","pullNumber","path","body","subjectType"],"type":"object"}
"""),
createWithSchema("""
{"properties":{"commit_message":{"description":"Extra detail for merge commit","type":"string"},"commit_title":{"description":"Title for merge commit","type":"string"},"merge_method":{"description":"Merge method","enum":["merge","squash","rebase"],"type":"string"},"owner":{"description":"Repository owner","type":"string"},"pullNumber":{"description":"Pull request number","type":"number"},"repo":{"description":"Repository name","type":"string"}},"required":["owner","repo","pullNumber"],"type":"object"}
"""),
],
};
// We don't care about the response, only that we get one and that an exception isn't thrown due to unacceptable schema.
var response = await chatClient.GetResponseAsync("Briefly, what is the most popular tower in Paris?", options);
Assert.NotNull(response);
}
private sealed class CustomAIFunction(string name, string jsonSchema, IReadOnlyDictionary<string, object?> additionalProperties) : AIFunction
{
public override string Name => name;
public override IReadOnlyDictionary<string, object?> AdditionalProperties => additionalProperties;
public override JsonElement JsonSchema { get; } = JsonSerializer.Deserialize<JsonElement>(jsonSchema, AIJsonUtilities.DefaultOptions);
protected override ValueTask<object?> InvokeCoreAsync(AIFunctionArguments arguments, CancellationToken cancellationToken) => throw new NotSupportedException();
}
private class ComplexObject
{
[DisplayName("Something cool")]
#if NET
[DeniedValues("abc", "def", "default")]
#endif
public string? SomeString { get; set; }
#if NET
[AllowedValues("abc", "def", "default")]
#endif
public string AnotherString { get; set; } = "default";
#if NET
[Range(25, 75)]
#endif
public int Value { get; set; }
[EmailAddress]
public string? Email { get; set; }
[RegularExpression("[abc]")]
public string? RegexString { get; set; }
[StringLength(42)]
public string MeasuredString { get; set; } = "default";
#if NET
[Length(1, 2)]
#endif
public int[]? MeasuredArray1 { get; set; }
#if NET
[MinLength(1)]
#endif
public int[]? MeasuredArray2 { get; set; }
#if NET
[MaxLength(10)]
#endif
public int[]? MeasuredArray3 { get; set; }
}
protected virtual bool SupportsParallelFunctionCalling => true;
[ConditionalFact]
public virtual async Task FunctionInvocation_SupportsMultipleParallelRequests()
{
SkipIfNotEnabled();
if (!SupportsParallelFunctionCalling)
{
throw new SkipTestException("Parallel function calling is not supported by this chat client");
}
using var chatClient = new FunctionInvokingChatClient(_chatClient);
// The service/model isn't guaranteed to request two calls to GetPersonAge in the same turn, but it's common that it will.
var response = await chatClient.GetResponseAsync("How much older is Elsa than Anna? Return the age difference as a single number.", new()
{
Tools = [AIFunctionFactory.Create((string personName) =>
{
return personName switch
{
"Elsa" => 21,
"Anna" => 18,
_ => 30,
};
}, "GetPersonAge")]
});
Assert.True(
Regex.IsMatch(response.Text ?? "", @"\b(3|three)\b", RegexOptions.IgnoreCase),
$"Doesn't contain three: {response.Text}");
}
[ConditionalFact]
public virtual async Task FunctionInvocation_RequireAny()
{
SkipIfNotEnabled();
int callCount = 0;
var tool = AIFunctionFactory.Create(() =>
{
callCount++;
return 123;
}, "GetSecretNumber");
using var chatClient = new FunctionInvokingChatClient(_chatClient);
var response = await chatClient.GetResponseAsync("Are birds real?", new()
{
Tools = [tool],
ToolMode = ChatToolMode.RequireAny,
});
Assert.True(callCount >= 1);
}
[ConditionalFact]
public virtual async Task FunctionInvocation_RequireSpecific()
{
SkipIfNotEnabled();
bool shieldsUp = false;
var getSecretNumberTool = AIFunctionFactory.Create(() => 123, "GetSecretNumber");
var shieldsUpTool = AIFunctionFactory.Create(() => shieldsUp = true, "ShieldsUp");
using var chatClient = new FunctionInvokingChatClient(_chatClient);
// Even though the user doesn't ask for the shields to be activated, verify that the tool is invoked
var response = await chatClient.GetResponseAsync("What's the current secret number?", new()
{
Tools = [getSecretNumberTool, shieldsUpTool],
ToolMode = ChatToolMode.RequireSpecific(shieldsUpTool.Name),
});
Assert.True(shieldsUp);
}
[ConditionalFact]
public virtual async Task Caching_OutputVariesWithoutCaching()
{
SkipIfNotEnabled();
var message = new ChatMessage(ChatRole.User, "Pick a random number, uniformly distributed between 1 and 1000000");
var firstResponse = await _chatClient.GetResponseAsync([message]);
var secondResponse = await _chatClient.GetResponseAsync([message]);
Assert.NotEqual(firstResponse.Text, secondResponse.Text);
}
[ConditionalFact]
public virtual async Task Caching_SamePromptResultsInCacheHit_NonStreaming()
{
SkipIfNotEnabled();
using var chatClient = new DistributedCachingChatClient(
_chatClient,
new MemoryDistributedCache(Options.Options.Create(new MemoryDistributedCacheOptions())));
var message = new ChatMessage(ChatRole.User, "Pick a random number, uniformly distributed between 1 and 1000000");
var firstResponse = await chatClient.GetResponseAsync([message]);
// No matter what it said before, we should see identical output due to caching
for (int i = 0; i < 3; i++)
{
var secondResponse = await chatClient.GetResponseAsync([message]);
Assert.Equal(firstResponse.Messages.Select(m => m.Text), secondResponse.Messages.Select(m => m.Text));
}
// ... but if the conversation differs, we should see different output
((TextContent)message.Contents[0]).Text += "!";
var thirdResponse = await chatClient.GetResponseAsync([message]);
Assert.NotEqual(firstResponse.Messages, thirdResponse.Messages);
}
[ConditionalFact]
public virtual async Task Caching_SamePromptResultsInCacheHit_Streaming()
{
SkipIfNotEnabled();
using var chatClient = new DistributedCachingChatClient(
_chatClient,
new MemoryDistributedCache(Options.Options.Create(new MemoryDistributedCacheOptions())));
var message = new ChatMessage(ChatRole.User, "Pick a random number, uniformly distributed between 1 and 1000000");
StringBuilder orig = new();
await foreach (var update in chatClient.GetStreamingResponseAsync([message]))
{
orig.Append(update.Text);
}
// No matter what it said before, we should see identical output due to caching
for (int i = 0; i < 3; i++)
{
StringBuilder second = new();
await foreach (var update in chatClient.GetStreamingResponseAsync([message]))
{
second.Append(update.Text);
}
Assert.Equal(orig.ToString(), second.ToString());
}
// ... but if the conversation differs, we should see different output
((TextContent)message.Contents[0]).Text += "!";
StringBuilder third = new();
await foreach (var update in chatClient.GetStreamingResponseAsync([message]))
{
third.Append(update.Text);
}
Assert.NotEqual(orig.ToString(), third.ToString());
}
[ConditionalFact]
public virtual async Task Caching_BeforeFunctionInvocation_AvoidsExtraCalls()
{
SkipIfNotEnabled();
int functionCallCount = 0;
var getTemperature = AIFunctionFactory.Create([Description("Gets the current temperature")] () =>
{
functionCallCount++;
return $"{100 + functionCallCount} degrees celsius";
}, "GetTemperature");
// First call executes the function and calls the LLM
using var chatClient = CreateChatClient()!
.AsBuilder()
.ConfigureOptions(options => options.Tools = [getTemperature])
.UseDistributedCache(new MemoryDistributedCache(Options.Options.Create(new MemoryDistributedCacheOptions())))
.UseFunctionInvocation()
.UseCallCounting()
.Build();
var llmCallCount = chatClient.GetService<CallCountingChatClient>();
var message = new ChatMessage(ChatRole.User, "What is the temperature?");
var response = await chatClient.GetResponseAsync([message]);
Assert.Contains("101", response.Text);
// First LLM call tells us to call the function, second deals with the result
Assert.Equal(2, llmCallCount!.CallCount);
// Second call doesn't execute the function or call the LLM, but rather just returns the cached result
var secondResponse = await chatClient.GetResponseAsync([message]);
Assert.Equal(response.Text, secondResponse.Text);
Assert.Equal(1, functionCallCount);
Assert.Equal(2, llmCallCount!.CallCount);
}
[ConditionalFact]
public virtual async Task Caching_AfterFunctionInvocation_FunctionOutputUnchangedAsync()
{
SkipIfNotEnabled();
// This means that if the function call produces the same result, we can avoid calling the LLM
// whereas if the function call produces a different result, we do call the LLM
var functionCallCount = 0;
var getTemperature = AIFunctionFactory.Create([Description("Gets the current temperature")] () =>
{
functionCallCount++;
return "58 degrees celsius";
}, "GetTemperature");
// First call executes the function and calls the LLM
using var chatClient = CreateChatClient()!
.AsBuilder()
.ConfigureOptions(options => options.Tools = [getTemperature])
.UseFunctionInvocation()
.UseDistributedCache(new MemoryDistributedCache(Options.Options.Create(new MemoryDistributedCacheOptions())))
.UseCallCounting()
.Build();
var llmCallCount = chatClient.GetService<CallCountingChatClient>();
var message = new ChatMessage(ChatRole.User, "What is the temperature?");
var response = await chatClient.GetResponseAsync([message]);
Assert.Contains("58", response.Text);
// First LLM call tells us to call the function, second deals with the result
Assert.Equal(1, functionCallCount);
Assert.Equal(2, llmCallCount!.CallCount);
// Second time, the calls to the LLM don't happen, but the function is called again
var secondResponse = await chatClient.GetResponseAsync([message]);
Assert.Equal(2, functionCallCount);
Assert.Equal(FunctionInvokingChatClientSetsConversationId ? 3 : 2, llmCallCount!.CallCount);
Assert.Equal(response.Text, secondResponse.Text);
}
public virtual bool FunctionInvokingChatClientSetsConversationId => false;
[ConditionalFact]
public virtual async Task Caching_AfterFunctionInvocation_FunctionOutputChangedAsync()
{
SkipIfNotEnabled();
// This means that if the function call produces the same result, we can avoid calling the LLM
// whereas if the function call produces a different result, we do call the LLM
var functionCallCount = 0;
var getTemperature = AIFunctionFactory.Create([Description("Gets the current temperature")] () =>
{
functionCallCount++;
return $"{80 + functionCallCount} degrees celsius";
}, "GetTemperature");
// First call executes the function and calls the LLM
using var chatClient = CreateChatClient()!
.AsBuilder()
.ConfigureOptions(options => options.Tools = [getTemperature])
.UseFunctionInvocation()
.UseDistributedCache(new MemoryDistributedCache(Options.Options.Create(new MemoryDistributedCacheOptions())))
.UseCallCounting()
.Build();
var llmCallCount = chatClient.GetService<CallCountingChatClient>();
var message = new ChatMessage(ChatRole.User, "What is the temperature?");
var response = await chatClient.GetResponseAsync([message]);
Assert.Contains("81", response.Text);
// First LLM call tells us to call the function, second deals with the result
Assert.Equal(1, functionCallCount);
Assert.Equal(2, llmCallCount!.CallCount);
// Second time, the first call to the LLM don't happen, but the function is called again,
// and since its output now differs, we no longer hit the cache so the second LLM call does happen
var secondResponse = await chatClient.GetResponseAsync([message]);
Assert.Contains("82", secondResponse.Text);
Assert.Equal(2, functionCallCount);
Assert.Equal(3, llmCallCount!.CallCount);
}
[ConditionalFact]
public virtual async Task Logging_LogsCalls_NonStreaming()
{
SkipIfNotEnabled();
var collector = new FakeLogCollector();
using ILoggerFactory loggerFactory = LoggerFactory.Create(b => b.AddProvider(new FakeLoggerProvider(collector)).SetMinimumLevel(LogLevel.Trace));
using var chatClient = CreateChatClient()!.AsBuilder()
.UseLogging(loggerFactory)
.Build();
await chatClient.GetResponseAsync([new(ChatRole.User, "What's the biggest animal?")]);
Assert.Collection(collector.GetSnapshot(),
entry => Assert.Contains("What's the biggest animal?", entry.Message),
entry => Assert.Contains("whale", entry.Message));
}
[ConditionalFact]
public virtual async Task Logging_LogsCalls_Streaming()
{
SkipIfNotEnabled();
var collector = new FakeLogCollector();
using ILoggerFactory loggerFactory = LoggerFactory.Create(b => b.AddProvider(new FakeLoggerProvider(collector)).SetMinimumLevel(LogLevel.Trace));
using var chatClient = CreateChatClient()!.AsBuilder()
.UseLogging(loggerFactory)
.Build();
await foreach (var update in chatClient.GetStreamingResponseAsync("What's the biggest animal?"))
{
// Do nothing with the updates
}
var logs = collector.GetSnapshot();
Assert.Contains(logs, e => e.Message.Contains("What's the biggest animal?"));
Assert.Contains(logs, e => e.Message.Contains("whale"));
}
[ConditionalFact]
public virtual async Task Logging_LogsFunctionCalls_NonStreaming()
{
SkipIfNotEnabled();
var collector = new FakeLogCollector();
using ILoggerFactory loggerFactory = LoggerFactory.Create(b => b.AddProvider(new FakeLoggerProvider(collector)).SetMinimumLevel(LogLevel.Trace));
using var chatClient = CreateChatClient()!
.AsBuilder()
.UseFunctionInvocation()
.UseLogging(loggerFactory)
.Build();
int secretNumber = 42;
await chatClient.GetResponseAsync(
"What is the current secret number?",
new ChatOptions { Tools = [AIFunctionFactory.Create(() => secretNumber, "GetSecretNumber")] });
Assert.Collection(collector.GetSnapshot(),
entry => Assert.Contains("What is the current secret number?", entry.Message),
entry => Assert.Contains("\"name\": \"GetSecretNumber\"", entry.Message),
entry => Assert.Contains($"\"result\": {secretNumber}", entry.Message),
entry => Assert.Contains(secretNumber.ToString(), entry.Message));
}
[ConditionalFact]
public virtual async Task Logging_LogsFunctionCalls_Streaming()
{
SkipIfNotEnabled();
var collector = new FakeLogCollector();
using ILoggerFactory loggerFactory = LoggerFactory.Create(b => b.AddProvider(new FakeLoggerProvider(collector)).SetMinimumLevel(LogLevel.Trace));
using var chatClient = CreateChatClient()!
.AsBuilder()
.UseFunctionInvocation()
.UseLogging(loggerFactory)
.Build();
int secretNumber = 42;
await foreach (var update in chatClient.GetStreamingResponseAsync(
"What is the current secret number?",
new ChatOptions { Tools = [AIFunctionFactory.Create(() => secretNumber, "GetSecretNumber")] }))
{
// Do nothing with the updates
}
var logs = collector.GetSnapshot();
Assert.Contains(logs, e => e.Message.Contains("What is the current secret number?"));
Assert.Contains(logs, e => e.Message.Contains("\"name\": \"GetSecretNumber\""));
Assert.Contains(logs, e => e.Message.Contains($"\"result\": {secretNumber}"));
}
[ConditionalFact]
public virtual async Task OpenTelemetry_CanEmitTracesAndMetrics()
{
SkipIfNotEnabled();
var sourceName = Guid.NewGuid().ToString();
var activities = new List<Activity>();
using var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
.AddSource(sourceName)
.AddInMemoryExporter(activities)
.Build();
var chatClient = CreateChatClient()!.AsBuilder()
.UseOpenTelemetry(sourceName: sourceName)
.Build();
var response = await chatClient.GetResponseAsync([new(ChatRole.User, "What's the biggest animal?")]);
var activity = Assert.Single(activities);
Assert.StartsWith("chat", activity.DisplayName);
Assert.StartsWith("http", (string)activity.GetTagItem("server.address")!);
Assert.Equal(chatClient.GetService<ChatClientMetadata>()?.ProviderUri?.Port, (int)activity.GetTagItem("server.port")!);
Assert.NotNull(activity.Id);
Assert.NotEmpty(activity.Id);
Assert.NotEqual(0, (int)activity.GetTagItem("gen_ai.usage.input_tokens")!);
Assert.NotEqual(0, (int)activity.GetTagItem("gen_ai.usage.output_tokens")!);
Assert.True(activity.Duration.TotalMilliseconds > 0);
}
[ConditionalFact]
public virtual async Task GetResponseAsync_StructuredOutput()
{
SkipIfNotEnabled();
var response = await _chatClient.GetResponseAsync<Person>("""
Who is described in the following sentence?
Jimbo Smith is a 35-year-old programmer from Cardiff, Wales.
""");
Assert.Equal("Jimbo Smith", response.Result.FullName);
Assert.Equal(35, response.Result.AgeInYears);
Assert.Contains("Cardiff", response.Result.HomeTown);
Assert.Equal(JobType.Programmer, response.Result.Job);
}
[ConditionalFact]
public virtual async Task GetResponseAsync_StructuredOutputArray()
{
SkipIfNotEnabled();
var response = await _chatClient.GetResponseAsync<Person[]>("""
Who are described in the following sentence?
Jimbo Smith is a 35-year-old software developer from Cardiff, Wales.
Josh Simpson is a 25-year-old software developer from Newport, Wales.
""");
Assert.Equal(2, response.Result.Length);
Assert.Contains(response.Result, x => x.FullName == "Jimbo Smith");
Assert.Contains(response.Result, x => x.FullName == "Josh Simpson");
}
[ConditionalFact]
public virtual async Task GetResponseAsync_StructuredOutputInteger()
{
SkipIfNotEnabled();
var response = await _chatClient.GetResponseAsync<int>("""
There were 14 abstractions for AI programming, which was too many.
To fix this we added another one. How many are there now?
""");
Assert.Equal(15, response.Result);
}
[ConditionalFact]