5 types derived from AIFunction
Microsoft.Extensions.AI.Abstractions (2)
Functions\AIFunctionFactory.cs (1)
503private sealed class ReflectionAIFunction : AIFunction
Functions\DelegatingAIFunction.cs (1)
19public class DelegatingAIFunction : AIFunction
Microsoft.Extensions.AI.Abstractions.Tests (2)
Contents\FunctionCallContentTests.cs (1)
248private sealed class NetTypelessAIFunction : AIFunction
Functions\AIFunctionTests.cs (1)
19private sealed class DerivedAIFunction : AIFunction
Microsoft.Extensions.AI.Integration.Tests (1)
ChatClientIntegrationTests.cs (1)
508private sealed class CustomAIFunction(string name, string jsonSchema, IReadOnlyDictionary<string, object?> additionalProperties) : AIFunction
197 references to AIFunction
Microsoft.Extensions.AI (12)
ChatCompletion\FunctionInvocationContext.cs (3)
18private static readonly AIFunction _nopFunction = AIFunctionFactory.Create(() => { }, nameof(FunctionInvocationContext)); 24private AIFunction _function = _nopFunction; 38public 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, 842if (tool is not AIFunction) 1014tool 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 57private 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 111public 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"/> 184public 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 277public 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"/> 360public 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 464public 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> 25public 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. 26protected DelegatingAIFunction(AIFunction innerFunction) 31/// <summary>Gets the inner <see cref="AIFunction" />.</summary> 32protected 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. 62public JsonElement GetOrCreateTransformedSchema(AIFunction function) =>
Microsoft.Extensions.AI.Abstractions.Tests (17)
Contents\FunctionCallContentTests.cs (4)
111AIFunction function = AIFunctionFactory.Create((DayOfWeek[] a, double b, Guid c, Dictionary<string, string> d) => b, serializerOptions: TestJsonSerializerContext.Default.Options); 133AIFunction function = AIFunctionFactory.Create((DayOfWeek[] a, double b, Guid c, Dictionary<string, string> d) => b, serializerOptions: TestJsonSerializerContext.Default.Options); 166AIFunction function = AIFunctionFactory.Create((DayOfWeek[] a, double b, Guid c, Dictionary<string, string> d) => b, serializerOptions: TestJsonSerializerContext.Default.Options); 187AIFunction function = AIFunctionFactory.Create((DayOfWeek[] a, double b, Guid c, Dictionary<string, string> d) => b, serializerOptions: TestJsonSerializerContext.Default.Options);
Functions\ApprovalRequiredAIFunctionTests.cs (2)
21var inner = AIFunctionFactory.Create(() => 42); 37var inner = AIFunctionFactory.Create(() => "result");
Functions\DelegatingAIFunctionTests.cs (6)
23AIFunction expected = AIFunctionFactory.Create(() => 42); 37private sealed class DerivedFunction(AIFunction innerFunction) : DelegatingAIFunction(innerFunction) 39public new AIFunction InnerFunction => base.InnerFunction; 64AIFunction inner = AIFunctionFactory.Create(int () => 70AIFunction actual = new OverridesInvocation(inner, (args, ct) => new ValueTask<object?>(84)); 87private sealed class OverridesInvocation(AIFunction innerFunction, Func<AIFunctionArguments, CancellationToken, ValueTask<object?>> invokeAsync) : DelegatingAIFunction(innerFunction)
Utilities\AIJsonSchemaTransformCacheTests.cs (1)
51AIFunction func = AIFunctionFactory.Create((int x, int y) => x + y);
Utilities\AIJsonUtilitiesTests.cs (4)
285AIFunction func = AIFunctionFactory.Create((int x, int y) => x + y, serializerOptions: options); 339AIFunction func = AIFunctionFactory.Create(( 360AIFunction func = AIFunctionFactory.Create((int a, int? b, long c, short d, float e, double f, decimal g) => { }, serializerOptions: options); 410AIFunction 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)
328AIFunction tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person."); 412AIFunction tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person."); 501AIFunction tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person."); 576AIFunction tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person."); 620private static ChatCompletionsToolDefinition ToAzureAIChatTool(AIFunction aiFunction)
Microsoft.Extensions.AI.Integration.Tests (12)
ChatClientIntegrationTests.cs (11)
302AIFunction func = AIFunctionFactory.Create((string id = "defaultId") => id is "foo" ? secretNumber : -1, "GetSecretNumberById"); 334AIFunction func = AIFunctionFactory.Create((PersonRecord person) => person.Name is "John" ? secretNumber + person.Age : -1, "GetSecretNumberByPerson"); 366AIFunction func = AIFunctionFactory.Create((string[] items, string shopperId) => { shoppingCart.AddRange(items); shopperName = shopperId; }, "AddItemsToShoppingCart"); 432Func<string, AIFunction> createWithSchema = schema => 597var tool = AIFunctionFactory.Create(() => 620var getSecretNumberTool = AIFunctionFactory.Create(() => 123, "GetSecretNumber"); 621var shieldsUpTool = AIFunctionFactory.Create(() => shieldsUp = true, "ShieldsUp"); 717var getTemperature = AIFunctionFactory.Create([Description("Gets the current temperature")] () => 756var getTemperature = AIFunctionFactory.Create([Description("Gets the current temperature")] () => 798var getTemperature = AIFunctionFactory.Create([Description("Gets the current temperature")] () => 1219var getWeather = AIFunctionFactory.Create(([Description("Gets weather for a city")] string city) =>
PromptBasedFunctionCallingChatClient.cs (1)
169var toolDescriptorsJson = JsonSerializer.Serialize(tools.OfType<AIFunction>().Select(t => t.JsonSchema), _jsonOptions);
Microsoft.Extensions.AI.OllamaSharp.Integration.Tests (2)
OllamaSharpChatClientIntegrationTests.cs (2)
66var stockPriceTool = AIFunctionFactory.Create([Description("Returns the stock price for a given ticker symbol")] ( 76var irrelevantTool = AIFunctionFactory.Create(() => { didCallIrrelevantTool = true; return 123; }, "GetSecretNumber");
Microsoft.Extensions.AI.OpenAI.Tests (6)
OpenAIChatClientTests.cs (4)
381AIFunction tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person."); 458AIFunction tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person."); 541AIFunction tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person."); 615AIFunction 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)
20private static readonly AIFunction _testFunction = AIFunctionFactory.Create(
OpenAIResponseClientTests.cs (1)
674AIFunction 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)
51AIFunction function = AIFunctionFactory.Create(() => { }, nameof(Properties_Roundtrip));
ChatCompletion\FunctionInvokingChatClientApprovalsTests.cs (2)
99AIFunction func1 = AIFunctionFactory.Create(() => "Result 1", "Func1"); 100AIFunction func2 = AIFunctionFactory.Create((int i) => $"Result 2: {i}", "Func2");
ChatCompletion\FunctionInvokingChatClientTests.cs (3)
819var function = AIFunctionFactory.Create(async (int i) => 894var function = AIFunctionFactory.Create((string? result = null) => 1148var known = AIFunctionFactory.Create(() => { invoked++; return "OK"; }, "Known");
Functions\AIFunctionFactoryTest.cs (39)
42AIFunction func; 57AIFunction func = AIFunctionFactory.Create((string a = "test") => a + " " + a); 65AIFunction[] funcs = 73foreach (AIFunction f in funcs) 83AIFunction func = AIFunctionFactory.Create((int x, int y, int z, int w, int u) => x + y + z + w + u); 112AIFunction func = AIFunctionFactory.Create((JsonElement param) => param, serializerOptions: options); 132AIFunction func = AIFunctionFactory.Create((JsonElement param) => param); 151AIFunction func = AIFunctionFactory.Create((int value1 = 1, string value2 = "2", CancellationToken cancellationToken = default) => 165AIFunction func; 214AIFunction func; 259AIFunction func = AIFunctionFactory.Create(dotnetFunc, options); 284AIFunction func = AIFunctionFactory.Create( 308AIFunction func = AIFunctionFactory.Create( 328AIFunction func = AIFunctionFactory.Create(( 372AIFunction func = AIFunctionFactory.Create(( 404AIFunction func = AIFunctionFactory.Create( 422AIFunction func = AIFunctionFactory.Create( 433AIFunction func = AIFunctionFactory.Create( 452AIFunction func = AIFunctionFactory.Create( 471AIFunction func = AIFunctionFactory.Create( 490AIFunction func = AIFunctionFactory.Create( 517AIFunction f = AIFunctionFactory.Create(([FromKeyedServices("key")] MyService service, int myInteger) => service.Value + myInteger, 540AIFunction f = AIFunctionFactory.Create(([FromKeyedServices(null!)] MyService service, int myInteger) => service.Value + myInteger, 563AIFunction f = AIFunctionFactory.Create(([FromKeyedServices("key")] MyService? service = null, int myInteger = 0) => 582AIFunction f = AIFunctionFactory.Create( 634AIFunction f = AIFunctionFactory.Create( 651AIFunction f = AIFunctionFactory.Create( 667AIFunction f = AIFunctionFactory.Create( 690AIFunction f = AIFunctionFactory.Create( 713AIFunction f = AIFunctionFactory.Create( 736AIFunction f = AIFunctionFactory.Create( 759AIFunction f = AIFunctionFactory.Create( 782AIFunction f = AIFunctionFactory.Create( 805AIFunction f = AIFunctionFactory.Create( 828AIFunction f = AIFunctionFactory.Create( 852AIFunction f = AIFunctionFactory.Create((Guid g = default, StructWithDefaultCtor s = default) => g.ToString() + "," + s.Value.ToString(), serializerOptions: JsonContext.Default.Options); 863AIFunction f = AIFunctionFactory.Create( 896AIFunction f = AIFunctionFactory.Create( 926AIFunction f = AIFunctionFactory.Create(Add, serializerOptions: JsonContext.Default.Options);