-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathAIFunctionFactory.xml
More file actions
651 lines (651 loc) · 65.1 KB
/
Copy pathAIFunctionFactory.xml
File metadata and controls
651 lines (651 loc) · 65.1 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
<Type Name="AIFunctionFactory" FullName="Microsoft.Extensions.AI.AIFunctionFactory">
<TypeSignature Language="C#" Value="public static class AIFunctionFactory" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi abstract sealed beforefieldinit AIFunctionFactory extends System.Object" />
<TypeSignature Language="DocId" Value="T:Microsoft.Extensions.AI.AIFunctionFactory" />
<TypeSignature Language="VB.NET" Value="Public Class AIFunctionFactory" />
<TypeSignature Language="F#" Value="type AIFunctionFactory = class" />
<TypeSignature Language="C++ CLI" Value="public ref class AIFunctionFactory abstract sealed" />
<AssemblyInfo>
<AssemblyName>Microsoft.Extensions.AI</AssemblyName>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<AssemblyVersion>9.1.0.0</AssemblyVersion>
<AssemblyVersion>9.3.0.0</AssemblyVersion>
<AssemblyVersion>9.4.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>Microsoft.Extensions.AI.Abstractions</AssemblyName>
<AssemblyVersion>9.4.0.0</AssemblyVersion>
<AssemblyVersion>9.5.0.0</AssemblyVersion>
<AssemblyVersion>9.6.0.0</AssemblyVersion>
<AssemblyVersion>9.7.0.0</AssemblyVersion>
<AssemblyVersion>9.9.0.0</AssemblyVersion>
<AssemblyVersion>9.10.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
<AssemblyVersion>10.1.0.0</AssemblyVersion>
<AssemblyVersion>10.2.0.0</AssemblyVersion>
<AssemblyVersion>10.3.0.0</AssemblyVersion>
<AssemblyVersion>10.4.0.0</AssemblyVersion>
</AssemblyInfo>
<Base>
<BaseTypeName>System.Object</BaseTypeName>
</Base>
<Interfaces />
<Attributes>
<Attribute FrameworkAlternate="net-10.0-pp;net-11.0-pp;net-8.0-pp;net-9.0-pp">
<AttributeName Language="C#">[System.Runtime.CompilerServices.Nullable(0)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.CompilerServices.Nullable(0)>]</AttributeName>
</Attribute>
</Attributes>
<Docs>
<summary>Provides factory methods for creating commonly-used implementations of <see cref="T:Microsoft.Extensions.AI.AIFunction" />.</summary>
<remarks>
<para>
The <see cref="T:Microsoft.Extensions.AI.AIFunctionFactory" /> class creates <see cref="T:Microsoft.Extensions.AI.AIFunction" /> instances that wrap .NET methods
(specified as <see cref="T:System.Delegate" /> or <see cref="T:System.Reflection.MethodInfo" />). As part of this process, JSON schemas are
automatically derived for both the function's input parameters (exposed via <see cref="P:Microsoft.Extensions.AI.AIFunctionDeclaration.JsonSchema" />)
and, by default, the function's return type (exposed via <see cref="P:Microsoft.Extensions.AI.AIFunctionDeclaration.ReturnJsonSchema" />).
These schemas are produced using the <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.SerializerOptions" /> and
<see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.JsonSchemaCreateOptions" />, and enable AI services to understand and
interact with the function. Return value serialization and schema derivation behavior can be customized
via <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.MarshalResult" /> and <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ExcludeResultSchema" />,
respectively.
</para>
</remarks>
<related type="Article" href="https://learn.microsoft.com/dotnet/ai/quickstarts/use-function-calling">Invoke .NET functions using an AI model.</related>
</Docs>
<Members>
<Member MemberName="Create">
<MemberSignature Language="C#" Value="public static Microsoft.Extensions.AI.AIFunction Create (Delegate method, Microsoft.Extensions.AI.AIFunctionFactoryOptions? options);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig class Microsoft.Extensions.AI.AIFunction Create(class System.Delegate method, class Microsoft.Extensions.AI.AIFunctionFactoryOptions options) cil managed" />
<MemberSignature Language="DocId" Value="M:Microsoft.Extensions.AI.AIFunctionFactory.Create(System.Delegate,Microsoft.Extensions.AI.AIFunctionFactoryOptions)" />
<MemberSignature Language="VB.NET" Value="Public Shared Function Create (method As Delegate, options As AIFunctionFactoryOptions) As AIFunction" />
<MemberSignature Language="F#" Value="static member Create : Delegate * Microsoft.Extensions.AI.AIFunctionFactoryOptions -> Microsoft.Extensions.AI.AIFunction" Usage="Microsoft.Extensions.AI.AIFunctionFactory.Create (method, options)" />
<MemberSignature Language="C++ CLI" Value="public:
 static Microsoft::Extensions::AI::AIFunction ^ Create(Delegate ^ method, Microsoft::Extensions::AI::AIFunctionFactoryOptions ^ options);" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>Microsoft.Extensions.AI</AssemblyName>
<AssemblyVersion>9.4.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>Microsoft.Extensions.AI.Abstractions</AssemblyName>
<AssemblyVersion>10.4.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Microsoft.Extensions.AI.AIFunction</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="method" Type="System.Delegate" />
<Parameter Name="options" Type="Microsoft.Extensions.AI.AIFunctionFactoryOptions">
<Attributes>
<Attribute FrameworkAlternate="net-10.0-pp;net-11.0-pp;net-8.0-pp;net-9.0-pp">
<AttributeName Language="C#">[System.Runtime.CompilerServices.Nullable(2)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.CompilerServices.Nullable(2)>]</AttributeName>
</Attribute>
</Attributes>
</Parameter>
</Parameters>
<Docs>
<param name="method">The method to be represented via the created <see cref="T:Microsoft.Extensions.AI.AIFunction" />.</param>
<param name="options">Metadata to use to override defaults inferred from <paramref name="method" />.</param>
<summary>Creates an <see cref="T:Microsoft.Extensions.AI.AIFunction" /> instance for a method, specified via a delegate.</summary>
<returns>The created <see cref="T:Microsoft.Extensions.AI.AIFunction" /> for invoking <paramref name="method" />.</returns>
<remarks>
<para>
By default, any parameters to <paramref name="method" /> are sourced from the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />'s dictionary
of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="T:Microsoft.Extensions.AI.AIFunction" />'s
<see cref="P:Microsoft.Extensions.AI.AIFunctionDeclaration.JsonSchema" />. There are a few exceptions to this:
<list type="bullet"><item><see cref="T:System.Threading.CancellationToken" /> parameters are automatically bound to the <see cref="T:System.Threading.CancellationToken" /> passed into
the invocation via <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" />'s <see cref="T:System.Threading.CancellationToken" /> parameter. The parameter is
not included in the generated JSON schema. The behavior of <see cref="T:System.Threading.CancellationToken" /> parameters can't be overridden.
</item><item>
By default, <see cref="T:System.IServiceProvider" /> parameters are bound from the <see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Services" /> property
and are not included in the JSON schema. If the parameter is optional, such that a default value is provided,
<see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Services" /> is allowed to be <see langword="null" />; otherwise, <see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Services" />
must be non-<see langword="null" />, or else the invocation will fail with an exception due to the required nature of the parameter.
The handling of <see cref="T:System.IServiceProvider" /> parameters can be overridden via <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ConfigureParameterBinding" />.
</item><item>
By default, <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> parameters are bound directly to <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> instance
passed into <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" /> and are not included in the JSON schema. If the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />
instance passed to <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" /> is <see langword="null" />, the <see cref="T:Microsoft.Extensions.AI.AIFunction" /> implementation
manufactures an empty instance, such that parameters of type <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> can always be satisfied, whether
optional or not. The handling of <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> parameters can be overridden via
<see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ConfigureParameterBinding" />.
</item></list>
All other parameter types are, by default, bound from the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> dictionary passed into <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" />
and are included in the generated JSON schema. This can be overridden by the <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ConfigureParameterBinding" /> provided
via the <paramref name="options" /> parameter; for every parameter, the delegate is enabled to choose if the parameter should be included in the
generated schema and how its value should be bound, including handling of optionality (by default, required parameters that are not included in the
<see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> dictionary will result in an exception being thrown). Loosely-typed additional context information can be passed
into <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" /> via the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />'s <see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Context" /> dictionary; the default
binding ignores this collection, but a custom binding supplied via <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ConfigureParameterBinding" /> can choose to
source arguments from this data.
</para>
<para>
The default marshaling of parameters from the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> dictionary permits values to be passed into the <paramref name="method" />'s
invocation directly if the object is already of a compatible type. Otherwise, if the argument is a <see cref="T:System.Text.Json.JsonElement" />, <see cref="T:System.Text.Json.JsonDocument" />,
or <see cref="T:System.Text.Json.Nodes.JsonNode" />, it is deserialized into the parameter type, utilizing <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.SerializerOptions" /> if provided,
or else using <see cref="P:Microsoft.Extensions.AI.AIJsonUtilities.DefaultOptions" />. If the argument is anything else, it is round-tripped through JSON, serializing the object as JSON
and then deserializing it to the expected type.
</para>
<para>
In general, the data supplied via an <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />'s dictionary is supplied from an AI service and should be considered
unvalidated and untrusted. To provide validated and trusted data to the invocation of <paramref name="method" />, consider having <paramref name="method" />
point to an instance method on an instance configured to hold the appropriate state. An <see cref="T:System.IServiceProvider" /> parameter can also be
used to resolve services from a dependency injection container.
</para>
<para>
By default, return values are serialized to <see cref="T:System.Text.Json.JsonElement" /> using <paramref name="options" />'s
<see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.SerializerOptions" /> if provided, or else using <see cref="P:Microsoft.Extensions.AI.AIJsonUtilities.DefaultOptions" />.
However, return values whose declared type is <see cref="T:Microsoft.Extensions.AI.AIContent" />, a derived type of <see cref="T:Microsoft.Extensions.AI.AIContent" />, or
any type assignable from <see cref="T:System.Collections.Generic.IEnumerable`1" /> (e.g. <c>AIContent[]</c>, <c>List<AIContent></c>) are
special-cased and are not serialized: the created function returns the original instance(s) directly to enable
callers (such as an <c>IChatClient</c>) to perform type tests and implement specialized handling. If
<see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.MarshalResult" /> is supplied, that delegate governs the behavior instead.
</para>
<para>
In addition to the parameter schema, a JSON schema is also derived from the method's return type and exposed via the
returned <see cref="T:Microsoft.Extensions.AI.AIFunction" />'s <see cref="P:Microsoft.Extensions.AI.AIFunctionDeclaration.ReturnJsonSchema" />. For methods returning
<see cref="T:System.Void" />, <see cref="T:System.Threading.Tasks.Task" />, or <see cref="T:System.Threading.Tasks.ValueTask" />, no return schema is produced (the property is <see langword="null" />).
For methods returning <see cref="T:System.Threading.Tasks.Task`1" /> or <see cref="T:System.Threading.Tasks.ValueTask`1" />, the schema is derived from the
unwrapped result type. Return schema generation can be excluded via <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ExcludeResultSchema" />,
and its generation is governed by <paramref name="options" />'s <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.JsonSchemaCreateOptions" />.
</para>
</remarks>
<exception cref="T:System.ArgumentNullException">
<paramref name="method" /> is <see langword="null" />.</exception>
<exception cref="T:System.Text.Json.JsonException">A parameter to <paramref name="method" /> is not serializable.</exception>
</Docs>
</Member>
<Member MemberName="Create">
<MemberSignature Language="C#" Value="public static Microsoft.Extensions.AI.AIFunction Create (System.Reflection.MethodInfo method, Func<Microsoft.Extensions.AI.AIFunctionArguments,object> createInstanceFunc, Microsoft.Extensions.AI.AIFunctionFactoryOptions? options = default);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig class Microsoft.Extensions.AI.AIFunction Create(class System.Reflection.MethodInfo method, class System.Func`2<class Microsoft.Extensions.AI.AIFunctionArguments, object> createInstanceFunc, class Microsoft.Extensions.AI.AIFunctionFactoryOptions options) cil managed" />
<MemberSignature Language="DocId" Value="M:Microsoft.Extensions.AI.AIFunctionFactory.Create(System.Reflection.MethodInfo,System.Func{Microsoft.Extensions.AI.AIFunctionArguments,System.Object},Microsoft.Extensions.AI.AIFunctionFactoryOptions)" />
<MemberSignature Language="VB.NET" Value="Public Shared Function Create (method As MethodInfo, createInstanceFunc As Func(Of AIFunctionArguments, Object), Optional options As AIFunctionFactoryOptions = Nothing) As AIFunction" />
<MemberSignature Language="F#" Value="static member Create : System.Reflection.MethodInfo * Func<Microsoft.Extensions.AI.AIFunctionArguments, obj> * Microsoft.Extensions.AI.AIFunctionFactoryOptions -> Microsoft.Extensions.AI.AIFunction" Usage="Microsoft.Extensions.AI.AIFunctionFactory.Create (method, createInstanceFunc, options)" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>Microsoft.Extensions.AI.Abstractions</AssemblyName>
<AssemblyVersion>10.4.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Microsoft.Extensions.AI.AIFunction</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="method" Type="System.Reflection.MethodInfo" />
<Parameter Name="createInstanceFunc" Type="System.Func<Microsoft.Extensions.AI.AIFunctionArguments,System.Object>" />
<Parameter Name="options" Type="Microsoft.Extensions.AI.AIFunctionFactoryOptions">
<Attributes>
<Attribute FrameworkAlternate="net-10.0-pp;net-11.0-pp;net-8.0-pp;net-9.0-pp">
<AttributeName Language="C#">[System.Runtime.CompilerServices.Nullable(2)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.CompilerServices.Nullable(2)>]</AttributeName>
</Attribute>
</Attributes>
</Parameter>
</Parameters>
<Docs>
<param name="method">The instance method to be represented via the created <see cref="T:Microsoft.Extensions.AI.AIFunction" />.</param>
<param name="createInstanceFunc">
Callback used on each function invocation to create an instance of the type on which the instance method <paramref name="method" />
will be invoked. If the returned instance is <see cref="T:System.IAsyncDisposable" /> or <see cref="T:System.IDisposable" />, it will be disposed of
after <paramref name="method" /> completes its invocation.
</param>
<param name="options">Metadata to use to override defaults inferred from <paramref name="method" />.</param>
<summary>
Creates an <see cref="T:Microsoft.Extensions.AI.AIFunction" /> instance for a method, specified via a <see cref="T:System.Reflection.MethodInfo" /> for
an instance method and a <see cref="T:System.Func`2" /> for constructing an instance of
the receiver object each time the <see cref="T:Microsoft.Extensions.AI.AIFunction" /> is invoked.
</summary>
<returns>The created <see cref="T:Microsoft.Extensions.AI.AIFunction" /> for invoking <paramref name="method" />.</returns>
<remarks>
<para>
Return values are serialized to <see cref="T:System.Text.Json.JsonElement" /> using <paramref name="options" />'s
<see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.SerializerOptions" />. Arguments that are not already of the expected type are
marshaled to the expected type via JSON and using <paramref name="options" />'s
<see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.SerializerOptions" />. If the argument is a <see cref="T:System.Text.Json.JsonElement" />,
<see cref="T:System.Text.Json.JsonDocument" />, or <see cref="T:System.Text.Json.Nodes.JsonNode" />, it is deserialized directly. If the argument is anything else unknown,
it is round-tripped through JSON, serializing the object as JSON and then deserializing it to the expected type.
</para>
<para>
By default, any parameters to <paramref name="method" /> are sourced from the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />'s dictionary
of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="T:Microsoft.Extensions.AI.AIFunction" />'s
<see cref="P:Microsoft.Extensions.AI.AIFunctionDeclaration.JsonSchema" />. There are a few exceptions to this:
<list type="bullet"><item><see cref="T:System.Threading.CancellationToken" /> parameters are automatically bound to the <see cref="T:System.Threading.CancellationToken" /> passed into
the invocation via <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" />'s <see cref="T:System.Threading.CancellationToken" /> parameter. The parameter is
not included in the generated JSON schema. The behavior of <see cref="T:System.Threading.CancellationToken" /> parameters can't be overridden.
</item><item>
By default, <see cref="T:System.IServiceProvider" /> parameters are bound from the <see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Services" /> property
and are not included in the JSON schema. If the parameter is optional, such that a default value is provided,
<see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Services" /> is allowed to be <see langword="null" />; otherwise, <see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Services" />
must be non-<see langword="null" />, or else the invocation will fail with an exception due to the required nature of the parameter.
The handling of <see cref="T:System.IServiceProvider" /> parameters can be overridden via <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ConfigureParameterBinding" />.
</item><item>
By default, <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> parameters are bound directly to <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> instance
passed into <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" /> and are not included in the JSON schema. If the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />
instance passed to <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" /> is <see langword="null" />, the <see cref="T:Microsoft.Extensions.AI.AIFunction" /> implementation
manufactures an empty instance, such that parameters of type <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> can always be satisfied, whether
optional or not. The handling of <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> parameters can be overridden via
<see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ConfigureParameterBinding" />.
</item></list>
All other parameter types are, by default, bound from the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> dictionary passed into <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" />
and are included in the generated JSON schema. This can be overridden by the <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ConfigureParameterBinding" /> provided
via the <paramref name="options" /> parameter; for every parameter, the delegate is enabled to choose if the parameter should be included in the
generated schema and how its value should be bound, including handling of optionality (by default, required parameters that are not included in the
<see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> dictionary will result in an exception being thrown). Loosely-typed additional context information can be passed
into <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" /> via the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />'s <see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Context" /> dictionary; the default
binding ignores this collection, but a custom binding supplied via <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ConfigureParameterBinding" /> can choose to
source arguments from this data.
</para>
<para>
The default marshaling of parameters from the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> dictionary permits values to be passed into the <paramref name="method" />'s
invocation directly if the object is already of a compatible type. Otherwise, if the argument is a <see cref="T:System.Text.Json.JsonElement" />, <see cref="T:System.Text.Json.JsonDocument" />,
or <see cref="T:System.Text.Json.Nodes.JsonNode" />, it is deserialized into the parameter type, utilizing <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.SerializerOptions" /> if provided,
or else using <see cref="P:Microsoft.Extensions.AI.AIJsonUtilities.DefaultOptions" />. If the argument is anything else, it is round-tripped through JSON, serializing the object as JSON
and then deserializing it to the expected type.
</para>
<para>
In general, the data supplied via an <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />'s dictionary is supplied from an AI service and should be considered
unvalidated and untrusted. To provide validated and trusted data to the invocation of <paramref name="method" />, the instance constructed
for each invocation can contain that data in it, such that it's then available to <paramref name="method" /> as instance data.
An <see cref="T:System.IServiceProvider" /> parameter can also be used to resolve services from a dependency injection container.
</para>
<para>
By default, return values are serialized to <see cref="T:System.Text.Json.JsonElement" /> using <paramref name="options" />'s
<see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.SerializerOptions" /> if provided, or else using <see cref="P:Microsoft.Extensions.AI.AIJsonUtilities.DefaultOptions" />.
However, return values whose declared type is <see cref="T:Microsoft.Extensions.AI.AIContent" />, a derived type of <see cref="T:Microsoft.Extensions.AI.AIContent" />, or any type
assignable from <see cref="T:System.Collections.Generic.IEnumerable`1" /> are returned directly without serialization.
Handling of return values can be overridden via <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.MarshalResult" />.
</para>
<para>
In addition to the parameter schema, a JSON schema is also derived from the method's return type and exposed via the
returned <see cref="T:Microsoft.Extensions.AI.AIFunction" />'s <see cref="P:Microsoft.Extensions.AI.AIFunctionDeclaration.ReturnJsonSchema" />. For methods returning
<see cref="T:System.Void" />, <see cref="T:System.Threading.Tasks.Task" />, or <see cref="T:System.Threading.Tasks.ValueTask" />, no return schema is produced (the property is <see langword="null" />).
For methods returning <see cref="T:System.Threading.Tasks.Task`1" /> or <see cref="T:System.Threading.Tasks.ValueTask`1" />, the schema is derived from the
unwrapped result type. Return schema generation can be excluded via <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ExcludeResultSchema" />,
and its generation is governed by <paramref name="options" />'s <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.JsonSchemaCreateOptions" />.
</para>
</remarks>
<exception cref="T:System.ArgumentNullException">
<paramref name="createInstanceFunc" /> is <see langword="null" />.</exception>
<exception cref="T:System.ArgumentException">
<paramref name="method" /> contains a parameter without a parameter name.</exception>
<exception cref="T:System.Text.Json.JsonException">A parameter to <paramref name="method" /> or its return type is not serializable.</exception>
</Docs>
</Member>
<Member MemberName="Create">
<MemberSignature Language="C#" Value="public static Microsoft.Extensions.AI.AIFunction Create (System.Reflection.MethodInfo method, object? target, Microsoft.Extensions.AI.AIFunctionFactoryOptions? options);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig class Microsoft.Extensions.AI.AIFunction Create(class System.Reflection.MethodInfo method, object target, class Microsoft.Extensions.AI.AIFunctionFactoryOptions options) cil managed" />
<MemberSignature Language="DocId" Value="M:Microsoft.Extensions.AI.AIFunctionFactory.Create(System.Reflection.MethodInfo,System.Object,Microsoft.Extensions.AI.AIFunctionFactoryOptions)" />
<MemberSignature Language="VB.NET" Value="Public Shared Function Create (method As MethodInfo, target As Object, options As AIFunctionFactoryOptions) As AIFunction" />
<MemberSignature Language="F#" Value="static member Create : System.Reflection.MethodInfo * obj * Microsoft.Extensions.AI.AIFunctionFactoryOptions -> Microsoft.Extensions.AI.AIFunction" Usage="Microsoft.Extensions.AI.AIFunctionFactory.Create (method, target, options)" />
<MemberSignature Language="C++ CLI" Value="public:
 static Microsoft::Extensions::AI::AIFunction ^ Create(System::Reflection::MethodInfo ^ method, System::Object ^ target, Microsoft::Extensions::AI::AIFunctionFactoryOptions ^ options);" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>Microsoft.Extensions.AI</AssemblyName>
<AssemblyVersion>9.4.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>Microsoft.Extensions.AI.Abstractions</AssemblyName>
<AssemblyVersion>10.4.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Microsoft.Extensions.AI.AIFunction</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="method" Type="System.Reflection.MethodInfo" />
<Parameter Name="target" Type="System.Object">
<Attributes>
<Attribute FrameworkAlternate="net-10.0-pp;net-11.0-pp;net-8.0-pp;net-9.0-pp">
<AttributeName Language="C#">[System.Runtime.CompilerServices.Nullable(2)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.CompilerServices.Nullable(2)>]</AttributeName>
</Attribute>
</Attributes>
</Parameter>
<Parameter Name="options" Type="Microsoft.Extensions.AI.AIFunctionFactoryOptions">
<Attributes>
<Attribute FrameworkAlternate="net-10.0-pp;net-11.0-pp;net-8.0-pp;net-9.0-pp">
<AttributeName Language="C#">[System.Runtime.CompilerServices.Nullable(2)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.CompilerServices.Nullable(2)>]</AttributeName>
</Attribute>
</Attributes>
</Parameter>
</Parameters>
<Docs>
<param name="method">The method to be represented via the created <see cref="T:Microsoft.Extensions.AI.AIFunction" />.</param>
<param name="target">
The target object for the <paramref name="method" /> if it represents an instance method.
This should be <see langword="null" /> if and only if <paramref name="method" /> is a static method.
</param>
<param name="options">Metadata to use to override defaults inferred from <paramref name="method" />.</param>
<summary>
Creates an <see cref="T:Microsoft.Extensions.AI.AIFunction" /> instance for a method, specified via an <see cref="T:System.Reflection.MethodInfo" /> instance
and an optional target object if the method is an instance method.
</summary>
<returns>The created <see cref="T:Microsoft.Extensions.AI.AIFunction" /> for invoking <paramref name="method" />.</returns>
<remarks>
<para>
By default, any parameters to <paramref name="method" /> are sourced from the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />'s dictionary
of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="T:Microsoft.Extensions.AI.AIFunction" />'s
<see cref="P:Microsoft.Extensions.AI.AIFunctionDeclaration.JsonSchema" />. There are a few exceptions to this:
<list type="bullet"><item><see cref="T:System.Threading.CancellationToken" /> parameters are automatically bound to the <see cref="T:System.Threading.CancellationToken" /> passed into
the invocation via <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" />'s <see cref="T:System.Threading.CancellationToken" /> parameter. The parameter is
not included in the generated JSON schema. The behavior of <see cref="T:System.Threading.CancellationToken" /> parameters can't be overridden.
</item><item>
By default, <see cref="T:System.IServiceProvider" /> parameters are bound from the <see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Services" /> property
and are not included in the JSON schema. If the parameter is optional, such that a default value is provided,
<see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Services" /> is allowed to be <see langword="null" />; otherwise, <see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Services" />
must be non-<see langword="null" />, or else the invocation will fail with an exception due to the required nature of the parameter.
The handling of <see cref="T:System.IServiceProvider" /> parameters can be overridden via <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ConfigureParameterBinding" />.
</item><item>
By default, <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> parameters are bound directly to <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> instance
passed into <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" /> and are not included in the JSON schema. If the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />
instance passed to <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" /> is <see langword="null" />, the <see cref="T:Microsoft.Extensions.AI.AIFunction" /> implementation
manufactures an empty instance, such that parameters of type <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> can always be satisfied, whether
optional or not. The handling of <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> parameters can be overridden via
<see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ConfigureParameterBinding" />.
</item></list>
All other parameter types are, by default, bound from the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> dictionary passed into <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" />
and are included in the generated JSON schema. This can be overridden by the <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ConfigureParameterBinding" /> provided
via the <paramref name="options" /> parameter; for every parameter, the delegate is enabled to choose if the parameter should be included in the
generated schema and how its value should be bound, including handling of optionality (by default, required parameters that are not included in the
<see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> dictionary will result in an exception being thrown). Loosely typed additional context information can be passed
into <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" /> via the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />'s <see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Context" /> dictionary; the default
binding ignores this collection, but a custom binding supplied via <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ConfigureParameterBinding" /> can choose to
source arguments from this data.
</para>
<para>
The default marshaling of parameters from the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> dictionary permits values to be passed into the <paramref name="method" />'s
invocation directly if the object is already of a compatible type. Otherwise, if the argument is a <see cref="T:System.Text.Json.JsonElement" />, <see cref="T:System.Text.Json.JsonDocument" />,
or <see cref="T:System.Text.Json.Nodes.JsonNode" />, it is deserialized into the parameter type, utilizing <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.SerializerOptions" /> if provided,
or else using <see cref="P:Microsoft.Extensions.AI.AIJsonUtilities.DefaultOptions" />. If the argument is anything else, it is round-tripped through JSON, serializing the object as JSON
and then deserializing it to the expected type.
</para>
<para>
In general, the data supplied via an <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />'s dictionary is supplied from an AI service and should be considered
unvalidated and untrusted. To provide validated and trusted data to the invocation of <paramref name="method" />, consider having <paramref name="method" />
point to an instance method on an instance configured to hold the appropriate state. An <see cref="T:System.IServiceProvider" /> parameter can also be
used to resolve services from a dependency injection container.
</para>
<para>
By default, return values are serialized to <see cref="T:System.Text.Json.JsonElement" /> using <paramref name="options" />'s
<see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.SerializerOptions" /> if provided, or else using <see cref="P:Microsoft.Extensions.AI.AIJsonUtilities.DefaultOptions" />.
However, return values whose declared type is <see cref="T:Microsoft.Extensions.AI.AIContent" />, a derived type of <see cref="T:Microsoft.Extensions.AI.AIContent" />, or
any type assignable from <see cref="T:System.Collections.Generic.IEnumerable`1" /> are not serialized and are instead returned directly.
Handling of return values can be overridden via <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.MarshalResult" />.
</para>
<para>
In addition to the parameter schema, a JSON schema is also derived from the method's return type and exposed via the
returned <see cref="T:Microsoft.Extensions.AI.AIFunction" />'s <see cref="P:Microsoft.Extensions.AI.AIFunctionDeclaration.ReturnJsonSchema" />. For methods returning
<see cref="T:System.Void" />, <see cref="T:System.Threading.Tasks.Task" />, or <see cref="T:System.Threading.Tasks.ValueTask" />, no return schema is produced (the property is <see langword="null" />).
For methods returning <see cref="T:System.Threading.Tasks.Task`1" /> or <see cref="T:System.Threading.Tasks.ValueTask`1" />, the schema is derived from the
unwrapped result type. Return schema generation can be excluded via <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.ExcludeResultSchema" />,
and its generation is governed by <paramref name="options" />'s <see cref="P:Microsoft.Extensions.AI.AIFunctionFactoryOptions.JsonSchemaCreateOptions" />.
</para>
</remarks>
<exception cref="T:System.ArgumentNullException">
<paramref name="method" /> represents an instance method but <paramref name="target" /> is null.</exception>
<exception cref="T:System.ArgumentException">
<paramref name="method" /> contains a parameter without a parameter name.</exception>
<exception cref="T:System.Text.Json.JsonException">A parameter to <paramref name="method" /> or its return type is not serializable.</exception>
</Docs>
</Member>
<Member MemberName="Create">
<MemberSignature Language="C#" Value="public static Microsoft.Extensions.AI.AIFunction Create (Delegate method, string? name = default, string? description = default, System.Text.Json.JsonSerializerOptions? serializerOptions = default);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig class Microsoft.Extensions.AI.AIFunction Create(class System.Delegate method, string name, string description, class System.Text.Json.JsonSerializerOptions serializerOptions) cil managed" />
<MemberSignature Language="DocId" Value="M:Microsoft.Extensions.AI.AIFunctionFactory.Create(System.Delegate,System.String,System.String,System.Text.Json.JsonSerializerOptions)" />
<MemberSignature Language="VB.NET" Value="Public Shared Function Create (method As Delegate, Optional name As String = Nothing, Optional description As String = Nothing, Optional serializerOptions As JsonSerializerOptions = Nothing) As AIFunction" />
<MemberSignature Language="F#" Value="static member Create : Delegate * string * string * System.Text.Json.JsonSerializerOptions -> Microsoft.Extensions.AI.AIFunction" Usage="Microsoft.Extensions.AI.AIFunctionFactory.Create (method, name, description, serializerOptions)" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>Microsoft.Extensions.AI</AssemblyName>
<AssemblyVersion>9.4.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>Microsoft.Extensions.AI.Abstractions</AssemblyName>
<AssemblyVersion>10.4.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Microsoft.Extensions.AI.AIFunction</ReturnType>
<Attributes>
<Attribute FrameworkAlternate="net-10.0-pp">
<AttributeName Language="C#">[System.Runtime.CompilerServices.Nullable(1)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.CompilerServices.Nullable(1)>]</AttributeName>
</Attribute>
</Attributes>
</ReturnValue>
<Parameters>
<Parameter Name="method" Type="System.Delegate">
<Attributes>
<Attribute FrameworkAlternate="net-10.0-pp;net-11.0-pp;net-8.0-pp;net-9.0-pp">
<AttributeName Language="C#">[System.Runtime.CompilerServices.Nullable(1)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.CompilerServices.Nullable(1)>]</AttributeName>
</Attribute>
</Attributes>
</Parameter>
<Parameter Name="name" Type="System.String" />
<Parameter Name="description" Type="System.String" />
<Parameter Name="serializerOptions" Type="System.Text.Json.JsonSerializerOptions" />
</Parameters>
<Docs>
<param name="method">The method to be represented via the created <see cref="T:Microsoft.Extensions.AI.AIFunction" />.</param>
<param name="name">
The name to use for the <see cref="T:Microsoft.Extensions.AI.AIFunction" />. If <see langword="null" />, the name will be derived from
any <see cref="T:System.ComponentModel.DisplayNameAttribute" /> on <paramref name="method" />, if available, or else from the name of <paramref name="method" />.
</param>
<param name="description">
The description to use for the <see cref="T:Microsoft.Extensions.AI.AIFunction" />. If <see langword="null" />, a description will be derived from
any <see cref="T:System.ComponentModel.DescriptionAttribute" /> on <paramref name="method" />, if available.
</param>
<param name="serializerOptions">The <see cref="T:System.Text.Json.JsonSerializerOptions" /> used to marshal function parameters and any return value.</param>
<summary>Creates an <see cref="T:Microsoft.Extensions.AI.AIFunction" /> instance for a method, specified via a delegate.</summary>
<returns>The created <see cref="T:Microsoft.Extensions.AI.AIFunction" /> for invoking <paramref name="method" />.</returns>
<remarks>
<para>
Any parameters to <paramref name="method" /> are sourced from the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />'s dictionary
of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="T:Microsoft.Extensions.AI.AIFunction" />'s
<see cref="P:Microsoft.Extensions.AI.AIFunctionDeclaration.JsonSchema" />. There are a few exceptions to this:
<list type="bullet"><item><see cref="T:System.Threading.CancellationToken" /> parameters are automatically bound to the <see cref="T:System.Threading.CancellationToken" /> passed into
the invocation via <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" />'s <see cref="T:System.Threading.CancellationToken" /> parameter. The parameter is
not included in the generated JSON schema.
</item><item>
By default, <see cref="T:System.IServiceProvider" /> parameters are bound from the <see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Services" /> property
and are not included in the JSON schema. If the parameter is optional, such that a default value is provided,
<see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Services" /> is allowed to be <see langword="null" />; otherwise, <see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Services" />
must be non-<see langword="null" />, or else the invocation will fail with an exception due to the required nature of the parameter.
</item><item>
By default, <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> parameters are bound directly to <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> instance
passed into <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" /> and are not included in the JSON schema. If the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />
instance passed to <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" /> is <see langword="null" />, the <see cref="T:Microsoft.Extensions.AI.AIFunction" /> implementation
manufactures an empty instance, such that parameters of type <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> can always be satisfied, whether
optional or not.
</item></list>
All other parameter types are bound from the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> dictionary passed into <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" />
and are included in the generated JSON schema.
</para>
<para>
The marshaling of parameters from the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> dictionary permits values to be passed into the <paramref name="method" />'s
invocation directly if the object is already of a compatible type. Otherwise, if the argument is a <see cref="T:System.Text.Json.JsonElement" />, <see cref="T:System.Text.Json.JsonDocument" />,
or <see cref="T:System.Text.Json.Nodes.JsonNode" />, it is deserialized into the parameter type, utilizing <paramref name="serializerOptions" /> if provided, or else
<see cref="P:Microsoft.Extensions.AI.AIJsonUtilities.DefaultOptions" />. If the argument is anything else, it is round-tripped through JSON, serializing the object as JSON
and then deserializing it to the expected type.
</para>
<para>
In general, the data supplied via an <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />'s dictionary is supplied from an AI service and should be considered
unvalidated and untrusted. To provide validated and trusted data to the invocation of <paramref name="method" />, consider having <paramref name="method" />
point to an instance method on an instance configured to hold the appropriate state. An <see cref="T:System.IServiceProvider" /> parameter can also be
used to resolve services from a dependency injection container.
</para>
<para>
Return values are serialized to <see cref="T:System.Text.Json.JsonElement" /> using <paramref name="serializerOptions" /> if provided,
or else using <see cref="P:Microsoft.Extensions.AI.AIJsonUtilities.DefaultOptions" />. However, return values whose declared type is <see cref="T:Microsoft.Extensions.AI.AIContent" />, a
derived type of <see cref="T:Microsoft.Extensions.AI.AIContent" />, or any type assignable from <see cref="T:System.Collections.Generic.IEnumerable`1" /> are not serialized;
they are returned as-is to facilitate specialized handling.
</para>
<para>
A JSON schema is also derived from the method's return type and exposed via <see cref="P:Microsoft.Extensions.AI.AIFunctionDeclaration.ReturnJsonSchema" />.
For methods returning <see cref="T:System.Void" />, <see cref="T:System.Threading.Tasks.Task" />, or <see cref="T:System.Threading.Tasks.ValueTask" />, no return schema is produced.
For methods returning <see cref="T:System.Threading.Tasks.Task`1" /> or <see cref="T:System.Threading.Tasks.ValueTask`1" />, the schema is derived from the unwrapped result type.
</para>
</remarks>
<exception cref="T:System.ArgumentNullException">
<paramref name="method" /> is <see langword="null" />.</exception>
<exception cref="T:System.Text.Json.JsonException">A parameter to <paramref name="method" /> is not serializable.</exception>
</Docs>
</Member>
<Member MemberName="Create">
<MemberSignature Language="C#" Value="public static Microsoft.Extensions.AI.AIFunction Create (System.Reflection.MethodInfo method, object? target, string? name = default, string? description = default, System.Text.Json.JsonSerializerOptions? serializerOptions = default);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig class Microsoft.Extensions.AI.AIFunction Create(class System.Reflection.MethodInfo method, object target, string name, string description, class System.Text.Json.JsonSerializerOptions serializerOptions) cil managed" />
<MemberSignature Language="DocId" Value="M:Microsoft.Extensions.AI.AIFunctionFactory.Create(System.Reflection.MethodInfo,System.Object,System.String,System.String,System.Text.Json.JsonSerializerOptions)" />
<MemberSignature Language="VB.NET" Value="Public Shared Function Create (method As MethodInfo, target As Object, Optional name As String = Nothing, Optional description As String = Nothing, Optional serializerOptions As JsonSerializerOptions = Nothing) As AIFunction" />
<MemberSignature Language="F#" Value="static member Create : System.Reflection.MethodInfo * obj * string * string * System.Text.Json.JsonSerializerOptions -> Microsoft.Extensions.AI.AIFunction" Usage="Microsoft.Extensions.AI.AIFunctionFactory.Create (method, target, name, description, serializerOptions)" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>Microsoft.Extensions.AI</AssemblyName>
<AssemblyVersion>9.4.0.0</AssemblyVersion>
</AssemblyInfo>
<AssemblyInfo>
<AssemblyName>Microsoft.Extensions.AI.Abstractions</AssemblyName>
<AssemblyVersion>10.4.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Microsoft.Extensions.AI.AIFunction</ReturnType>
<Attributes>
<Attribute FrameworkAlternate="net-10.0-pp">
<AttributeName Language="C#">[System.Runtime.CompilerServices.Nullable(1)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.CompilerServices.Nullable(1)>]</AttributeName>
</Attribute>
</Attributes>
</ReturnValue>
<Parameters>
<Parameter Name="method" Type="System.Reflection.MethodInfo">
<Attributes>
<Attribute FrameworkAlternate="net-10.0-pp;net-11.0-pp;net-8.0-pp;net-9.0-pp">
<AttributeName Language="C#">[System.Runtime.CompilerServices.Nullable(1)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.CompilerServices.Nullable(1)>]</AttributeName>
</Attribute>
</Attributes>
</Parameter>
<Parameter Name="target" Type="System.Object" />
<Parameter Name="name" Type="System.String" />
<Parameter Name="description" Type="System.String" />
<Parameter Name="serializerOptions" Type="System.Text.Json.JsonSerializerOptions" />
</Parameters>
<Docs>
<param name="method">The method to be represented via the created <see cref="T:Microsoft.Extensions.AI.AIFunction" />.</param>
<param name="target">
The target object for the <paramref name="method" /> if it represents an instance method.
This should be <see langword="null" /> if and only if <paramref name="method" /> is a static method.
</param>
<param name="name">
The name to use for the <see cref="T:Microsoft.Extensions.AI.AIFunction" />. If <see langword="null" />, the name will be derived from
any <see cref="T:System.ComponentModel.DisplayNameAttribute" /> on <paramref name="method" />, if available, or else from the name of <paramref name="method" />.
</param>
<param name="description">
The description to use for the <see cref="T:Microsoft.Extensions.AI.AIFunction" />. If <see langword="null" />, a description will be derived from
any <see cref="T:System.ComponentModel.DescriptionAttribute" /> on <paramref name="method" />, if available.
</param>
<param name="serializerOptions">The <see cref="T:System.Text.Json.JsonSerializerOptions" /> used to marshal function parameters and return value.</param>
<summary>
Creates an <see cref="T:Microsoft.Extensions.AI.AIFunction" /> instance for a method, specified via an <see cref="T:System.Reflection.MethodInfo" /> instance
and an optional target object if the method is an instance method.
</summary>
<returns>The created <see cref="T:Microsoft.Extensions.AI.AIFunction" /> for invoking <paramref name="method" />.</returns>
<remarks>
<para>
Any parameters to <paramref name="method" /> are sourced from the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />'s dictionary
of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="T:Microsoft.Extensions.AI.AIFunction" />'s
<see cref="P:Microsoft.Extensions.AI.AIFunctionDeclaration.JsonSchema" />. There are a few exceptions to this:
<list type="bullet"><item><see cref="T:System.Threading.CancellationToken" /> parameters are automatically bound to the <see cref="T:System.Threading.CancellationToken" /> passed into
the invocation via <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" />'s <see cref="T:System.Threading.CancellationToken" /> parameter. The parameter is
not included in the generated JSON schema.
</item><item>
By default, <see cref="T:System.IServiceProvider" /> parameters are bound from the <see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Services" /> property
and are not included in the JSON schema. If the parameter is optional, such that a default value is provided,
<see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Services" /> is allowed to be <see langword="null" />; otherwise, <see cref="P:Microsoft.Extensions.AI.AIFunctionArguments.Services" />
must be non-<see langword="null" />, or else the invocation will fail with an exception due to the required nature of the parameter.
</item><item>
By default, <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> parameters are bound directly to <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> instance
passed into <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" /> and are not included in the JSON schema. If the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />
instance passed to <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" /> is <see langword="null" />, the <see cref="T:Microsoft.Extensions.AI.AIFunction" /> implementation
manufactures an empty instance, such that parameters of type <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> can always be satisfied, whether
optional or not.
</item></list>
All other parameter types are bound from the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> dictionary passed into <see cref="M:Microsoft.Extensions.AI.AIFunction.InvokeAsync(Microsoft.Extensions.AI.AIFunctionArguments,System.Threading.CancellationToken)" />
and are included in the generated JSON schema.
</para>
<para>
The marshaling of parameters from the <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" /> dictionary permits values to be passed into the <paramref name="method" />'s
invocation directly if the object is already of a compatible type. Otherwise, if the argument is a <see cref="T:System.Text.Json.JsonElement" />, <see cref="T:System.Text.Json.JsonDocument" />,
or <see cref="T:System.Text.Json.Nodes.JsonNode" />, it is deserialized into the parameter type, utilizing <paramref name="serializerOptions" /> if provided, or else
<see cref="P:Microsoft.Extensions.AI.AIJsonUtilities.DefaultOptions" />. If the argument is anything else, it is round-tripped through JSON, serializing the object as JSON
and then deserializing it to the expected type.
</para>
<para>
In general, the data supplied via an <see cref="T:Microsoft.Extensions.AI.AIFunctionArguments" />'s dictionary is supplied from an AI service and should be considered
unvalidated and untrusted. To provide validated and trusted data to the invocation of <paramref name="method" />, consider having <paramref name="method" />
point to an instance method on an instance configured to hold the appropriate state. An <see cref="T:System.IServiceProvider" /> parameter can also be
used to resolve services from a dependency injection container.
</para>
<para>
Return values are serialized to <see cref="T:System.Text.Json.JsonElement" /> using <paramref name="serializerOptions" /> if provided,
or else using <see cref="P:Microsoft.Extensions.AI.AIJsonUtilities.DefaultOptions" />. However, return values whose declared type is <see cref="T:Microsoft.Extensions.AI.AIContent" />, a
derived type of <see cref="T:Microsoft.Extensions.AI.AIContent" />, or any type assignable from <see cref="T:System.Collections.Generic.IEnumerable`1" /> are returned
without serialization to enable specialized handling.
</para>
<para>
A JSON schema is also derived from the method's return type and exposed via <see cref="P:Microsoft.Extensions.AI.AIFunctionDeclaration.ReturnJsonSchema" />.
For methods returning <see cref="T:System.Void" />, <see cref="T:System.Threading.Tasks.Task" />, or <see cref="T:System.Threading.Tasks.ValueTask" />, no return schema is produced.
For methods returning <see cref="T:System.Threading.Tasks.Task`1" /> or <see cref="T:System.Threading.Tasks.ValueTask`1" />, the schema is derived from the unwrapped result type.
</para>
</remarks>
<exception cref="T:System.ArgumentNullException">
<paramref name="method" /> represents an instance method but <paramref name="target" /> is null.</exception>
<exception cref="T:System.ArgumentException">
<paramref name="method" /> contains a parameter without a parameter name.</exception>
<exception cref="T:System.Text.Json.JsonException">A parameter to <paramref name="method" /> or its return type is not serializable.</exception>
</Docs>
</Member>
<Member MemberName="CreateDeclaration">
<MemberSignature Language="C#" Value="public static Microsoft.Extensions.AI.AIFunctionDeclaration CreateDeclaration (string name, string? description, System.Text.Json.JsonElement jsonSchema, System.Text.Json.JsonElement? returnJsonSchema = default);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig class Microsoft.Extensions.AI.AIFunctionDeclaration CreateDeclaration(string name, string description, valuetype System.Text.Json.JsonElement jsonSchema, valuetype System.Nullable`1<valuetype System.Text.Json.JsonElement> returnJsonSchema) cil managed" />
<MemberSignature Language="DocId" Value="M:Microsoft.Extensions.AI.AIFunctionFactory.CreateDeclaration(System.String,System.String,System.Text.Json.JsonElement,System.Nullable{System.Text.Json.JsonElement})" />
<MemberSignature Language="VB.NET" Value="Public Shared Function CreateDeclaration (name As String, description As String, jsonSchema As JsonElement, Optional returnJsonSchema As Nullable(Of JsonElement) = Nothing) As AIFunctionDeclaration" />
<MemberSignature Language="F#" Value="static member CreateDeclaration : string * string * System.Text.Json.JsonElement * Nullable<System.Text.Json.JsonElement> -> Microsoft.Extensions.AI.AIFunctionDeclaration" Usage="Microsoft.Extensions.AI.AIFunctionFactory.CreateDeclaration (name, description, jsonSchema, returnJsonSchema)" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>Microsoft.Extensions.AI.Abstractions</AssemblyName>
<AssemblyVersion>10.4.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Microsoft.Extensions.AI.AIFunctionDeclaration</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="name" Type="System.String" />
<Parameter Name="description" Type="System.String">
<Attributes>
<Attribute FrameworkAlternate="net-10.0-pp;net-11.0-pp;net-8.0-pp;net-9.0-pp">
<AttributeName Language="C#">[System.Runtime.CompilerServices.Nullable(2)]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.CompilerServices.Nullable(2)>]</AttributeName>
</Attribute>
</Attributes>
</Parameter>
<Parameter Name="jsonSchema" Type="System.Text.Json.JsonElement" />
<Parameter Name="returnJsonSchema" Type="System.Nullable<System.Text.Json.JsonElement>" />
</Parameters>
<Docs>
<param name="name">The name of the function.</param>
<param name="description">A description of the function, suitable for use in describing the purpose to a model.</param>
<param name="jsonSchema">A JSON schema describing the function and its input parameters.</param>
<param name="returnJsonSchema">A JSON schema describing the function's return value.</param>
<summary>Creates an <see cref="T:Microsoft.Extensions.AI.AIFunctionDeclaration" /> using the specified parameters as the implementation of its corresponding properties.</summary>
<returns>The created <see cref="T:Microsoft.Extensions.AI.AIFunctionDeclaration" /> that describes a function.</returns>
<remarks>
<see cref="M:Microsoft.Extensions.AI.AIFunctionFactory.CreateDeclaration(System.String,System.String,System.Text.Json.JsonElement,System.Nullable{System.Text.Json.JsonElement})" /> creates an <see cref="T:Microsoft.Extensions.AI.AIFunctionDeclaration" /> that can be used to describe a function
but not invoke it. To create an invocable <see cref="T:Microsoft.Extensions.AI.AIFunction" />, use Create. A non-invocable <see cref="T:Microsoft.Extensions.AI.AIFunctionDeclaration" />
can also be created from an invocable <see cref="T:Microsoft.Extensions.AI.AIFunction" /> using that function's <see cref="M:Microsoft.Extensions.AI.AIFunction.AsDeclarationOnly" /> method.
</remarks>
<exception cref="T:System.ArgumentNullException">
<paramref name="name" /> is <see langword="null" />.</exception>
</Docs>
</Member>
</Members>
</Type>