5 types derived from AIFunction
Microsoft.Extensions.AI.Abstractions (2)
Functions\AIFunctionFactory.cs (1)
503
private sealed class ReflectionAIFunction :
AIFunction
Functions\DelegatingAIFunction.cs (1)
19
public class DelegatingAIFunction :
AIFunction
Microsoft.Extensions.AI.Abstractions.Tests (2)
Contents\FunctionCallContentTests.cs (1)
248
private sealed class NetTypelessAIFunction :
AIFunction
Functions\AIFunctionTests.cs (1)
19
private sealed class DerivedAIFunction :
AIFunction
Microsoft.Extensions.AI.Integration.Tests (1)
ChatClientIntegrationTests.cs (1)
508
private sealed class CustomAIFunction(string name, string jsonSchema, IReadOnlyDictionary<string, object?> additionalProperties) :
AIFunction
197 references to AIFunction
Microsoft.Extensions.AI (12)
ChatCompletion\FunctionInvocationContext.cs (3)
18
private static readonly
AIFunction
_nopFunction = AIFunctionFactory.Create(() => { }, nameof(FunctionInvocationContext));
24
private
AIFunction
_function = _nopFunction;
38
public
AIFunction
Function
ChatCompletion\FunctionInvokingChatClient.cs (9)
37
/// <see cref="IChatClient"/>, it responds by invoking the corresponding <see cref="
AIFunction
"/> defined
43
/// If a requested function is an <see cref="AIFunctionDeclaration"/> but not an <see cref="
AIFunction
"/>, the
63
/// <see cref="
AIFunction
"/> instances employed as part of the supplied <see cref="ChatOptions"/> are also safe.
99
/// <param name="functionInvocationServices">An optional <see cref="IServiceProvider"/> to use for resolving services required by the <see cref="
AIFunction
"/> instances being invoked.</param>
262
/// <see cref="ChatOptions.Tools"/> or <see cref="AdditionalTools"/>) but that aren't <see cref="
AIFunction
"/>s aren't considered
269
/// <summary>Gets or sets a delegate used to invoke <see cref="
AIFunction
"/> instances.</summary>
271
/// By default, the protected <see cref="InvokeFunctionAsync"/> method is called for each <see cref="
AIFunction
"/> to be invoked,
842
if (tool is not
AIFunction
)
1014
tool is not
AIFunction
aiFunction)
Microsoft.Extensions.AI.Abstractions (98)
Functions\AIFunction.cs (7)
16
/// <summary>Initializes a new instance of the <see cref="
AIFunction
"/> class.</summary>
22
/// Gets the underlying <see cref="MethodInfo"/> that this <see cref="
AIFunction
"/> might be wrapping.
32
/// <summary>Invokes the <see cref="
AIFunction
"/> and returns its result.</summary>
41
/// <summary>Invokes the <see cref="
AIFunction
"/> and returns its result.</summary>
49
/// <summary>Creates a <see cref="AIFunctionDeclaration"/> representation of this <see cref="
AIFunction
"/> that can't be invoked.</summary>
52
/// <see cref="
AIFunction
"/> derives from <see cref="AIFunctionDeclaration"/>, layering on the ability to invoke the function in addition
57
private sealed class NonInvocableAIFunction(
AIFunction
function) : DelegatingAIFunctionDeclaration(function);
Functions\AIFunctionArguments.cs (5)
20
/// <summary>Represents arguments to be used with <see cref="
AIFunction
.InvokeAsync"/>.</summary>
23
/// as inputs to an <see cref="
AIFunction
"/>. However, an instance carries additional non-nominal
25
/// an <see cref="
AIFunction
"/> if it needs to resolve any services from a dependency injection
98
/// information for use by an <see cref="
AIFunction
"/> implementation. The meaning of this
99
/// data is left up to the implementer of the <see cref="
AIFunction
"/>.
Functions\AIFunctionDeclaration.cs (2)
13
/// <see cref="AIFunctionDeclaration"/> is the base class for <see cref="
AIFunction
"/>, which
16
/// and can type test for <see cref="
AIFunction
"/> to determine whether they can be invoked.
Functions\AIFunctionFactory.cs (62)
33
/// <summary>Provides factory methods for creating commonly-used implementations of <see cref="
AIFunction
"/>.</summary>
44
/// <summary>Creates an <see cref="
AIFunction
"/> instance for a method, specified via a delegate.</summary>
45
/// <param name="method">The method to be represented via the created <see cref="
AIFunction
"/>.</param>
47
/// <returns>The created <see cref="
AIFunction
"/> for invoking <paramref name="method"/>.</returns>
51
/// of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="
AIFunction
"/>'s
57
/// the invocation via <see cref="
AIFunction
.InvokeAsync"/>'s <see cref="CancellationToken"/> parameter. The parameter is
73
/// passed into <see cref="
AIFunction
.InvokeAsync"/> and are not included in the JSON schema. If the <see cref="AIFunctionArguments"/>
74
/// instance passed to <see cref="
AIFunction
.InvokeAsync"/> is <see langword="null"/>, the <see cref="
AIFunction
"/> implementation
81
/// All other parameter types are, by default, bound from the <see cref="AIFunctionArguments"/> dictionary passed into <see cref="
AIFunction
.InvokeAsync"/>
86
/// into <see cref="
AIFunction
.InvokeAsync"/> via the <see cref="AIFunctionArguments"/>'s <see cref="AIFunctionArguments.Context"/> dictionary; the default
111
public static
AIFunction
Create(Delegate method, AIFunctionFactoryOptions? options)
118
/// <summary>Creates an <see cref="
AIFunction
"/> instance for a method, specified via a delegate.</summary>
119
/// <param name="method">The method to be represented via the created <see cref="
AIFunction
"/>.</param>
121
/// The name to use for the <see cref="
AIFunction
"/>. If <see langword="null"/>, the name will be derived from
125
/// The description to use for the <see cref="
AIFunction
"/>. If <see langword="null"/>, a description will be derived from
129
/// <returns>The created <see cref="
AIFunction
"/> for invoking <paramref name="method"/>.</returns>
133
/// of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="
AIFunction
"/>'s
139
/// the invocation via <see cref="
AIFunction
.InvokeAsync"/>'s <see cref="CancellationToken"/> parameter. The parameter is
154
/// passed into <see cref="
AIFunction
.InvokeAsync"/> and are not included in the JSON schema. If the <see cref="AIFunctionArguments"/>
155
/// instance passed to <see cref="
AIFunction
.InvokeAsync"/> is <see langword="null"/>, the <see cref="
AIFunction
"/> implementation
161
/// All other parameter types are bound from the <see cref="AIFunctionArguments"/> dictionary passed into <see cref="
AIFunction
.InvokeAsync"/>
184
public static
AIFunction
Create(Delegate method, string? name = null, string? description = null, JsonSerializerOptions? serializerOptions = null)
201
/// Creates an <see cref="
AIFunction
"/> instance for a method, specified via an <see cref="MethodInfo"/> instance
204
/// <param name="method">The method to be represented via the created <see cref="
AIFunction
"/>.</param>
210
/// <returns>The created <see cref="
AIFunction
"/> for invoking <paramref name="method"/>.</returns>
214
/// of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="
AIFunction
"/>'s
220
/// the invocation via <see cref="
AIFunction
.InvokeAsync"/>'s <see cref="CancellationToken"/> parameter. The parameter is
236
/// passed into <see cref="
AIFunction
.InvokeAsync"/> and are not included in the JSON schema. If the <see cref="AIFunctionArguments"/>
237
/// instance passed to <see cref="
AIFunction
.InvokeAsync"/> is <see langword="null"/>, the <see cref="
AIFunction
"/> implementation
244
/// All other parameter types are, by default, bound from the <see cref="AIFunctionArguments"/> dictionary passed into <see cref="
AIFunction
.InvokeAsync"/>
249
/// into <see cref="
AIFunction
.InvokeAsync"/> via the <see cref="AIFunctionArguments"/>'s <see cref="AIFunctionArguments.Context"/> dictionary; the default
277
public static
AIFunction
Create(MethodInfo method, object? target, AIFunctionFactoryOptions? options)
285
/// Creates an <see cref="
AIFunction
"/> instance for a method, specified via an <see cref="MethodInfo"/> instance
288
/// <param name="method">The method to be represented via the created <see cref="
AIFunction
"/>.</param>
294
/// The name to use for the <see cref="
AIFunction
"/>. If <see langword="null"/>, the name will be derived from
298
/// The description to use for the <see cref="
AIFunction
"/>. If <see langword="null"/>, a description will be derived from
302
/// <returns>The created <see cref="
AIFunction
"/> for invoking <paramref name="method"/>.</returns>
306
/// of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="
AIFunction
"/>'s
312
/// the invocation via <see cref="
AIFunction
.InvokeAsync"/>'s <see cref="CancellationToken"/> parameter. The parameter is
327
/// passed into <see cref="
AIFunction
.InvokeAsync"/> and are not included in the JSON schema. If the <see cref="AIFunctionArguments"/>
328
/// instance passed to <see cref="
AIFunction
.InvokeAsync"/> is <see langword="null"/>, the <see cref="
AIFunction
"/> implementation
334
/// All other parameter types are bound from the <see cref="AIFunctionArguments"/> dictionary passed into <see cref="
AIFunction
.InvokeAsync"/>
360
public static
AIFunction
Create(MethodInfo method, object? target, string? name = null, string? description = null, JsonSerializerOptions? serializerOptions = null)
377
/// Creates an <see cref="
AIFunction
"/> instance for a method, specified via a <see cref="MethodInfo"/> for
379
/// the receiver object each time the <see cref="
AIFunction
"/> is invoked.
381
/// <param name="method">The instance method to be represented via the created <see cref="
AIFunction
"/>.</param>
388
/// <returns>The created <see cref="
AIFunction
"/> for invoking <paramref name="method"/>.</returns>
400
/// of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="
AIFunction
"/>'s
406
/// the invocation via <see cref="
AIFunction
.InvokeAsync"/>'s <see cref="CancellationToken"/> parameter. The parameter is
422
/// passed into <see cref="
AIFunction
.InvokeAsync"/> and are not included in the JSON schema. If the <see cref="AIFunctionArguments"/>
423
/// instance passed to <see cref="
AIFunction
.InvokeAsync"/> is <see langword="null"/>, the <see cref="
AIFunction
"/> implementation
430
/// All other parameter types are, by default, bound from the <see cref="AIFunctionArguments"/> dictionary passed into <see cref="
AIFunction
.InvokeAsync"/>
435
/// into <see cref="
AIFunction
.InvokeAsync"/> via the <see cref="AIFunctionArguments"/>'s <see cref="AIFunctionArguments.Context"/> dictionary; the default
464
public static
AIFunction
Create(
479
/// but not invoke it. To create an invocable <see cref="
AIFunction
"/>, use Create. A non-invocable <see cref="AIFunctionDeclaration"/>
480
/// can also be created from an invocable <see cref="
AIFunction
"/> using that function's <see cref="
AIFunction
.AsDeclarationOnly"/> method.
Functions\AIFunctionFactoryOptions.cs (11)
15
/// Represents options that can be provided when creating an <see cref="
AIFunction
"/> from a method.
65
/// this delegate will be invoked once for each parameter in the function as part of creating the <see cref="
AIFunction
"/> instance.
67
/// provided to the <see cref="
AIFunction
.InvokeAsync"/> invocation.
76
/// <summary>Gets or sets a delegate used to determine the <see cref="object"/> returned by <see cref="
AIFunction
.InvokeAsync"/>.</summary>
79
/// By default, the return value of invoking the method wrapped into an <see cref="
AIFunction
"/> by <see cref="AIFunctionFactory"/>
80
/// is then JSON serialized, with the resulting <see cref="JsonElement"/> returned from the <see cref="
AIFunction
.InvokeAsync"/> method.
84
/// and its return value is then returned from the <see cref="
AIFunction
.InvokeAsync"/> method.
88
/// a <see langword="null"/> argument. By default, <see langword="null"/> is returned from the <see cref="
AIFunction
.InvokeAsync"/>
89
/// method for <see cref="
AIFunction
"/> instances produced by <see cref="AIFunctionFactory"/> to wrap
132
/// If non- <see langword="null"/>, each time the <see cref="
AIFunction
"/> is invoked, this delegate will be invoked
149
/// It, however, is permissible for cases where invocation of the <see cref="
AIFunction
"/> is tightly controlled, and the caller
Functions\ApprovalRequiredAIFunction.cs (4)
10
/// Represents an <see cref="
AIFunction
"/> that can be described to an AI service and invoked, but for which
14
/// This class simply augments an <see cref="
AIFunction
"/> with an indication that approval is required before invocation.
23
/// <param name="innerFunction">The <see cref="
AIFunction
"/> represented by this instance.</param>
25
public ApprovalRequiredAIFunction(
AIFunction
innerFunction)
Functions\DelegatingAIFunction.cs (4)
17
/// Provides an optional base class for an <see cref="
AIFunction
"/> that passes through calls to another instance.
26
protected DelegatingAIFunction(
AIFunction
innerFunction)
31
/// <summary>Gets the inner <see cref="
AIFunction
" />.</summary>
32
protected
AIFunction
InnerFunction { get; }
Utilities\AIJsonSchemaTransformCache.cs (3)
17
/// This cache stores weak references from AI abstractions that declare JSON schemas such as <see cref="
AIFunction
"/> or <see cref="ChatResponseFormatJson"/>
57
/// Gets or creates a transformed JSON schema for the specified <see cref="
AIFunction
"/> instance.
62
public JsonElement GetOrCreateTransformedSchema(
AIFunction
function) =>
Microsoft.Extensions.AI.Abstractions.Tests (17)
Contents\FunctionCallContentTests.cs (4)
111
AIFunction
function = AIFunctionFactory.Create((DayOfWeek[] a, double b, Guid c, Dictionary<string, string> d) => b, serializerOptions: TestJsonSerializerContext.Default.Options);
133
AIFunction
function = AIFunctionFactory.Create((DayOfWeek[] a, double b, Guid c, Dictionary<string, string> d) => b, serializerOptions: TestJsonSerializerContext.Default.Options);
166
AIFunction
function = AIFunctionFactory.Create((DayOfWeek[] a, double b, Guid c, Dictionary<string, string> d) => b, serializerOptions: TestJsonSerializerContext.Default.Options);
187
AIFunction
function = AIFunctionFactory.Create((DayOfWeek[] a, double b, Guid c, Dictionary<string, string> d) => b, serializerOptions: TestJsonSerializerContext.Default.Options);
Functions\ApprovalRequiredAIFunctionTests.cs (2)
21
var
inner = AIFunctionFactory.Create(() => 42);
37
var
inner = AIFunctionFactory.Create(() => "result");
Functions\DelegatingAIFunctionTests.cs (6)
23
AIFunction
expected = AIFunctionFactory.Create(() => 42);
37
private sealed class DerivedFunction(
AIFunction
innerFunction) : DelegatingAIFunction(innerFunction)
39
public new
AIFunction
InnerFunction => base.InnerFunction;
64
AIFunction
inner = AIFunctionFactory.Create(int () =>
70
AIFunction
actual = new OverridesInvocation(inner, (args, ct) => new ValueTask<object?>(84));
87
private sealed class OverridesInvocation(
AIFunction
innerFunction, Func<AIFunctionArguments, CancellationToken, ValueTask<object?>> invokeAsync) : DelegatingAIFunction(innerFunction)
Utilities\AIJsonSchemaTransformCacheTests.cs (1)
51
AIFunction
func = AIFunctionFactory.Create((int x, int y) => x + y);
Utilities\AIJsonUtilitiesTests.cs (4)
285
AIFunction
func = AIFunctionFactory.Create((int x, int y) => x + y, serializerOptions: options);
339
AIFunction
func = AIFunctionFactory.Create((
360
AIFunction
func = AIFunctionFactory.Create((int a, int? b, long c, short d, float e, double f, decimal g) => { }, serializerOptions: options);
410
AIFunction
func = AIFunctionFactory.Create(([Range(1, 10)] int num, [StringLength(100, MinimumLength = 1)] string str) => num + str.Length, serializerOptions: options);
Microsoft.Extensions.AI.AzureAIInference.Tests (5)
AzureAIInferenceChatClientTests.cs (5)
328
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
412
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
501
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
576
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
620
private static ChatCompletionsToolDefinition ToAzureAIChatTool(
AIFunction
aiFunction)
Microsoft.Extensions.AI.Integration.Tests (12)
ChatClientIntegrationTests.cs (11)
302
AIFunction
func = AIFunctionFactory.Create((string id = "defaultId") => id is "foo" ? secretNumber : -1, "GetSecretNumberById");
334
AIFunction
func = AIFunctionFactory.Create((PersonRecord person) => person.Name is "John" ? secretNumber + person.Age : -1, "GetSecretNumberByPerson");
366
AIFunction
func = AIFunctionFactory.Create((string[] items, string shopperId) => { shoppingCart.AddRange(items); shopperName = shopperId; }, "AddItemsToShoppingCart");
432
Func<string,
AIFunction
> createWithSchema = schema =>
597
var
tool = AIFunctionFactory.Create(() =>
620
var
getSecretNumberTool = AIFunctionFactory.Create(() => 123, "GetSecretNumber");
621
var
shieldsUpTool = AIFunctionFactory.Create(() => shieldsUp = true, "ShieldsUp");
717
var
getTemperature = AIFunctionFactory.Create([Description("Gets the current temperature")] () =>
756
var
getTemperature = AIFunctionFactory.Create([Description("Gets the current temperature")] () =>
798
var
getTemperature = AIFunctionFactory.Create([Description("Gets the current temperature")] () =>
1219
var
getWeather = AIFunctionFactory.Create(([Description("Gets weather for a city")] string city) =>
PromptBasedFunctionCallingChatClient.cs (1)
169
var toolDescriptorsJson = JsonSerializer.Serialize(tools.OfType<
AIFunction
>().Select(t => t.JsonSchema), _jsonOptions);
Microsoft.Extensions.AI.OllamaSharp.Integration.Tests (2)
OllamaSharpChatClientIntegrationTests.cs (2)
66
var
stockPriceTool = AIFunctionFactory.Create([Description("Returns the stock price for a given ticker symbol")] (
76
var
irrelevantTool = AIFunctionFactory.Create(() => { didCallIrrelevantTool = true; return 123; }, "GetSecretNumber");
Microsoft.Extensions.AI.OpenAI.Tests (6)
OpenAIChatClientTests.cs (4)
381
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
458
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
541
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
615
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
OpenAIConversionTests.cs (1)
20
private static readonly
AIFunction
_testFunction = AIFunctionFactory.Create(
OpenAIResponseClientTests.cs (1)
674
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
Microsoft.Extensions.AI.Tests (45)
ChatCompletion\FunctionInvocationContextTests.cs (1)
51
AIFunction
function = AIFunctionFactory.Create(() => { }, nameof(Properties_Roundtrip));
ChatCompletion\FunctionInvokingChatClientApprovalsTests.cs (2)
99
AIFunction
func1 = AIFunctionFactory.Create(() => "Result 1", "Func1");
100
AIFunction
func2 = AIFunctionFactory.Create((int i) => $"Result 2: {i}", "Func2");
ChatCompletion\FunctionInvokingChatClientTests.cs (3)
819
var
function = AIFunctionFactory.Create(async (int i) =>
894
var
function = AIFunctionFactory.Create((string? result = null) =>
1148
var
known = AIFunctionFactory.Create(() => { invoked++; return "OK"; }, "Known");
Functions\AIFunctionFactoryTest.cs (39)
42
AIFunction
func;
57
AIFunction
func = AIFunctionFactory.Create((string a = "test") => a + " " + a);
65
AIFunction
[] funcs =
73
foreach (
AIFunction
f in funcs)
83
AIFunction
func = AIFunctionFactory.Create((int x, int y, int z, int w, int u) => x + y + z + w + u);
112
AIFunction
func = AIFunctionFactory.Create((JsonElement param) => param, serializerOptions: options);
132
AIFunction
func = AIFunctionFactory.Create((JsonElement param) => param);
151
AIFunction
func = AIFunctionFactory.Create((int value1 = 1, string value2 = "2", CancellationToken cancellationToken = default) =>
165
AIFunction
func;
214
AIFunction
func;
259
AIFunction
func = AIFunctionFactory.Create(dotnetFunc, options);
284
AIFunction
func = AIFunctionFactory.Create(
308
AIFunction
func = AIFunctionFactory.Create(
328
AIFunction
func = AIFunctionFactory.Create((
372
AIFunction
func = AIFunctionFactory.Create((
404
AIFunction
func = AIFunctionFactory.Create(
422
AIFunction
func = AIFunctionFactory.Create(
433
AIFunction
func = AIFunctionFactory.Create(
452
AIFunction
func = AIFunctionFactory.Create(
471
AIFunction
func = AIFunctionFactory.Create(
490
AIFunction
func = AIFunctionFactory.Create(
517
AIFunction
f = AIFunctionFactory.Create(([FromKeyedServices("key")] MyService service, int myInteger) => service.Value + myInteger,
540
AIFunction
f = AIFunctionFactory.Create(([FromKeyedServices(null!)] MyService service, int myInteger) => service.Value + myInteger,
563
AIFunction
f = AIFunctionFactory.Create(([FromKeyedServices("key")] MyService? service = null, int myInteger = 0) =>
582
AIFunction
f = AIFunctionFactory.Create(
634
AIFunction
f = AIFunctionFactory.Create(
651
AIFunction
f = AIFunctionFactory.Create(
667
AIFunction
f = AIFunctionFactory.Create(
690
AIFunction
f = AIFunctionFactory.Create(
713
AIFunction
f = AIFunctionFactory.Create(
736
AIFunction
f = AIFunctionFactory.Create(
759
AIFunction
f = AIFunctionFactory.Create(
782
AIFunction
f = AIFunctionFactory.Create(
805
AIFunction
f = AIFunctionFactory.Create(
828
AIFunction
f = AIFunctionFactory.Create(
852
AIFunction
f = AIFunctionFactory.Create((Guid g = default, StructWithDefaultCtor s = default) => g.ToString() + "," + s.Value.ToString(), serializerOptions: JsonContext.Default.Options);
863
AIFunction
f = AIFunctionFactory.Create(
896
AIFunction
f = AIFunctionFactory.Create(
926
AIFunction
f = AIFunctionFactory.Create(Add, serializerOptions: JsonContext.Default.Options);