3 types derived from AIFunction
Microsoft.Extensions.AI (1)
Functions\AIFunctionFactory.cs (1)
563private sealed class ReflectionAIFunction : 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
179 references to AIFunction
Microsoft.Extensions.AI (95)
ChatCompletion\FunctionInvocationContext.cs (3)
18private static readonly AIFunction _nopFunction = AIFunctionFactory.Create(() => { }, nameof(FunctionInvocationContext)); 24private AIFunction _function = _nopFunction; 38public AIFunction Function
ChatCompletion\FunctionInvokingChatClient.cs (5)
31/// by calling the corresponding <see cref="AIFunction"/> defined in <see cref="ChatOptions.Tools"/>, 38/// <see cref="AIFunction"/> instances employed as part of the supplied <see cref="ChatOptions"/> are also safe. 74/// <param name="functionInvocationServices">An optional <see cref="IServiceProvider"/> to use for resolving services required by the <see cref="AIFunction"/> instances being invoked.</param> 695AIFunction? aiFunction = options.Tools!.OfType<AIFunction>().FirstOrDefault(t => t.Name == callContent.Name);
Functions\AIFunctionFactory.cs (74)
31/// <summary>Provides factory methods for creating commonly used implementations of <see cref="AIFunction"/>.</summary> 38/// <summary>Creates an <see cref="AIFunction"/> instance for a method, specified via a delegate.</summary> 39/// <param name="method">The method to be represented via the created <see cref="AIFunction"/>.</param> 41/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns> 45/// of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="AIFunction"/>'s 46/// <see cref="AIFunction.JsonSchema"/>. There are a few exceptions to this: 51/// the invocation via <see cref="AIFunction.InvokeAsync"/>'s <see cref="CancellationToken"/> parameter. The parameter is 67/// passed into <see cref="AIFunction.InvokeAsync"/> and are not included in the JSON schema. If the <see cref="AIFunctionArguments"/> 68/// instance passed to <see cref="AIFunction.InvokeAsync"/> is <see langword="null"/>, the <see cref="AIFunction"/> implementation 85/// When the <see cref="AIFunction"/> is constructed, it may be passed an <see cref="IServiceProvider"/> via 88/// from the <see cref="IServiceProvider"/> provided to <see cref="AIFunction.InvokeAsync"/> via <see cref="AIFunctionArguments.Services"/>, 94/// All other parameter types are, by default, bound from the <see cref="AIFunctionArguments"/> dictionary passed into <see cref="AIFunction.InvokeAsync"/> 99/// into <see cref="AIFunction.InvokeAsync"/> via the <see cref="AIFunctionArguments"/>'s <see cref="AIFunctionArguments.Context"/> dictionary; the default 124public static AIFunction Create(Delegate method, AIFunctionFactoryOptions? options) 131/// <summary>Creates an <see cref="AIFunction"/> instance for a method, specified via a delegate.</summary> 132/// <param name="method">The method to be represented via the created <see cref="AIFunction"/>.</param> 134/// The name to use for the <see cref="AIFunction"/>. If <see langword="null"/>, the name will be derived from 138/// The description to use for the <see cref="AIFunction"/>. If <see langword="null"/>, a description will be derived from 142/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns> 146/// of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="AIFunction"/>'s 147/// <see cref="AIFunction.JsonSchema"/>. There are a few exceptions to this: 152/// the invocation via <see cref="AIFunction.InvokeAsync"/>'s <see cref="CancellationToken"/> parameter. The parameter is 167/// passed into <see cref="AIFunction.InvokeAsync"/> and are not included in the JSON schema. If the <see cref="AIFunctionArguments"/> 168/// instance passed to <see cref="AIFunction.InvokeAsync"/> is <see langword="null"/>, the <see cref="AIFunction"/> implementation 183/// When the <see cref="AIFunction"/> is constructed, it may be passed an <see cref="IServiceProvider"/> via 186/// from the <see cref="IServiceProvider"/> provided to <see cref="AIFunction.InvokeAsync"/> via <see cref="AIFunctionArguments.Services"/>, 191/// All other parameter types are bound from the <see cref="AIFunctionArguments"/> dictionary passed into <see cref="AIFunction.InvokeAsync"/> 214public static AIFunction Create(Delegate method, string? name = null, string? description = null, JsonSerializerOptions? serializerOptions = null) 231/// Creates an <see cref="AIFunction"/> instance for a method, specified via an <see cref="MethodInfo"/> instance 234/// <param name="method">The method to be represented via the created <see cref="AIFunction"/>.</param> 240/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns> 244/// of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="AIFunction"/>'s 245/// <see cref="AIFunction.JsonSchema"/>. There are a few exceptions to this: 250/// the invocation via <see cref="AIFunction.InvokeAsync"/>'s <see cref="CancellationToken"/> parameter. The parameter is 266/// passed into <see cref="AIFunction.InvokeAsync"/> and are not included in the JSON schema. If the <see cref="AIFunctionArguments"/> 267/// instance passed to <see cref="AIFunction.InvokeAsync"/> is <see langword="null"/>, the <see cref="AIFunction"/> implementation 284/// When the <see cref="AIFunction"/> is constructed, it may be passed an <see cref="IServiceProvider"/> via 287/// from the <see cref="IServiceProvider"/> provided to <see cref="AIFunction.InvokeAsync"/> via <see cref="AIFunctionArguments.Services"/>, 293/// All other parameter types are, by default, bound from the <see cref="AIFunctionArguments"/> dictionary passed into <see cref="AIFunction.InvokeAsync"/> 298/// into <see cref="AIFunction.InvokeAsync"/> via the <see cref="AIFunctionArguments"/>'s <see cref="AIFunctionArguments.Context"/> dictionary; the default 326public static AIFunction Create(MethodInfo method, object? target, AIFunctionFactoryOptions? options) 334/// Creates an <see cref="AIFunction"/> instance for a method, specified via an <see cref="MethodInfo"/> instance 337/// <param name="method">The method to be represented via the created <see cref="AIFunction"/>.</param> 343/// The name to use for the <see cref="AIFunction"/>. If <see langword="null"/>, the name will be derived from 347/// The description to use for the <see cref="AIFunction"/>. If <see langword="null"/>, a description will be derived from 351/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns> 355/// of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="AIFunction"/>'s 356/// <see cref="AIFunction.JsonSchema"/>. There are a few exceptions to this: 361/// the invocation via <see cref="AIFunction.InvokeAsync"/>'s <see cref="CancellationToken"/> parameter. The parameter is 376/// passed into <see cref="AIFunction.InvokeAsync"/> and are not included in the JSON schema. If the <see cref="AIFunctionArguments"/> 377/// instance passed to <see cref="AIFunction.InvokeAsync"/> is <see langword="null"/>, the <see cref="AIFunction"/> implementation 391/// When the <see cref="AIFunction"/> is constructed, it may be passed an <see cref="IServiceProvider"/> via 394/// from the <see cref="IServiceProvider"/> provided to <see cref="AIFunction.InvokeAsync"/> via <see cref="AIFunctionArguments.Services"/>, 400/// All other parameter types are bound from the <see cref="AIFunctionArguments"/> dictionary passed into <see cref="AIFunction.InvokeAsync"/> 426public static AIFunction Create(MethodInfo method, object? target, string? name = null, string? description = null, JsonSerializerOptions? serializerOptions = null) 443/// Creates an <see cref="AIFunction"/> instance for a method, specified via an <see cref="MethodInfo"/> for 447/// <param name="method">The instance method to be represented via the created <see cref="AIFunction"/>.</param> 450/// the resulting <see cref="AIFunction"/> is invoked. If <see cref="AIFunctionArguments.Services"/> is provided, 456/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns> 468/// of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="AIFunction"/>'s 469/// <see cref="AIFunction.JsonSchema"/>. There are a few exceptions to this: 474/// the invocation via <see cref="AIFunction.InvokeAsync"/>'s <see cref="CancellationToken"/> parameter. The parameter is 490/// passed into <see cref="AIFunction.InvokeAsync"/> and are not included in the JSON schema. If the <see cref="AIFunctionArguments"/> 491/// instance passed to <see cref="AIFunction.InvokeAsync"/> is <see langword="null"/>, the <see cref="AIFunction"/> implementation 508/// When the <see cref="AIFunction"/> is constructed, it may be passed an <see cref="IServiceProvider"/> via 511/// from the <see cref="IServiceProvider"/> provided to <see cref="AIFunction.InvokeAsync"/> via <see cref="AIFunctionArguments.Services"/>, 517/// All other parameter types are, by default, bound from the <see cref="AIFunctionArguments"/> dictionary passed into <see cref="AIFunction.InvokeAsync"/> 522/// into <see cref="AIFunction.InvokeAsync"/> via the <see cref="AIFunctionArguments"/>'s <see cref="AIFunctionArguments.Context"/> dictionary; the default 552public static AIFunction Create(
Functions\AIFunctionFactoryOptions.cs (13)
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 110/// Gets or sets optional services used in the construction of the <see cref="AIFunction"/>. 115/// <see cref="AIFunction.InvokeAsync"/> via <see cref="AIFunctionArguments.Services"/>. 129/// If non- <see langword="null"/>, each time the <see cref="AIFunction"/> is invoked, this delegate will be invoked 146/// It, however, is permissible for cases where invocation of the <see cref="AIFunction"/> is tightly controlled, and the caller
Microsoft.Extensions.AI.Abstractions (15)
ChatCompletion\ChatToolMode.cs (1)
58/// and that the specified <see cref="AIFunction"/> must be selected. The function name
ChatCompletion\RequiredChatToolMode.cs (1)
18/// Gets the name of a specific <see cref="AIFunction"/> that must be called.
Functions\AIFunction.cs (3)
42/// Gets the underlying <see cref="MethodInfo"/> that this <see cref="AIFunction"/> might be wrapping. 52/// <summary>Invokes the <see cref="AIFunction"/> and returns its result.</summary> 61/// <summary>Invokes the <see cref="AIFunction"/> and returns its result.</summary>
Functions\AIFunctionArguments.cs (5)
19/// <summary>Represents arguments to be used with <see cref="AIFunction.InvokeAsync"/>.</summary> 22/// as inputs to an <see cref="AIFunction"/>. However, an instance carries additional non-nominal 24/// an <see cref="AIFunction"/> if it needs to resolve any services from a dependency injection 96/// information for use by an <see cref="AIFunction"/> implementation. The meaning of this 97/// data is left up to the implementer of the <see cref="AIFunction"/>.
Utilities\AIJsonSchemaTransformCache.cs (5)
16/// This cache stores weak references from AI abstractions that declare JSON schemas such as <see cref="AIFunction"/> or <see cref="ChatResponseFormatJson"/> 26private readonly ConditionalWeakTable<AIFunction, object> _functionSchemaCache = new(); 29private readonly ConditionalWeakTable<AIFunction, object>.CreateValueCallback _functionSchemaCreateValueCallback; 56/// Gets or creates a transformed JSON schema for the specified <see cref="AIFunction"/> instance. 60public JsonElement GetOrCreateTransformedSchema(AIFunction function)
Microsoft.Extensions.AI.Abstractions.Tests (8)
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);
Utilities\AIJsonSchemaTransformCacheTests.cs (1)
51AIFunction func = AIFunctionFactory.Create((int x, int y) => x + y);
Utilities\AIJsonUtilitiesTests.cs (3)
297AIFunction func = AIFunctionFactory.Create((int x, int y) => x + y, serializerOptions: options); 344AIFunction func = AIFunctionFactory.Create(( 369AIFunction func = AIFunctionFactory.Create((int a, int? b, long c, short d, float e, double f, decimal g) => { }, serializerOptions: options);
Microsoft.Extensions.AI.AzureAIInference (2)
AzureAIInferenceChatClient.cs (2)
344if (tool is AIFunction af) 411private static ChatCompletionsToolDefinition ToAzureAIChatTool(AIFunction aiFunction)
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."); 428AIFunction tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person."); 517AIFunction tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person."); 592AIFunction tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person."); 636private static ChatCompletionsToolDefinition ToAzureAIChatTool(AIFunction aiFunction)
Microsoft.Extensions.AI.Integration.Tests (6)
ChatClientIntegrationTests.cs (5)
293AIFunction func = AIFunctionFactory.Create((string id = "defaultId") => id is "foo" ? secretNumber : -1, "GetSecretNumberById"); 325AIFunction func = AIFunctionFactory.Create((PersonRecord person) => person.Name is "John" ? secretNumber + person.Age : -1, "GetSecretNumberByPerson"); 434var tool = AIFunctionFactory.Create(() => 457var getSecretNumberTool = AIFunctionFactory.Create(() => 123, "GetSecretNumber"); 458var shieldsUpTool = AIFunctionFactory.Create(() => shieldsUp = true, "ShieldsUp");
PromptBasedFunctionCallingChatClient.cs (1)
169var toolDescriptorsJson = JsonSerializer.Serialize(tools.OfType<AIFunction>().Select(t => t.JsonSchema), _jsonOptions);
Microsoft.Extensions.AI.Ollama (2)
OllamaChatClient.cs (2)
316Tools = options?.ToolMode is not NoneChatToolMode && options?.Tools is { Count: > 0 } tools ? tools.OfType<AIFunction>().Select(ToOllamaTool) : null, 482private static OllamaTool ToOllamaTool(AIFunction function)
Microsoft.Extensions.AI.Ollama.Tests (2)
OllamaChatClientIntegrationTests.cs (2)
65var stockPriceTool = AIFunctionFactory.Create([Description("Returns the stock price for a given ticker symbol")] ( 75var irrelevantTool = AIFunctionFactory.Create(() => { didCallIrrelevantTool = true; return 123; }, "GetSecretNumber");
Microsoft.Extensions.AI.OpenAI (3)
OpenAIChatClient.cs (2)
485if (tool is AIFunction af) 535private static ChatTool ToOpenAIChatTool(AIFunction aiFunction)
OpenAIResponseChatClient.cs (1)
375case AIFunction af:
Microsoft.Extensions.AI.OpenAI.Tests (5)
OpenAIChatClientTests.cs (5)
320AIFunction tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person."); 399AIFunction tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person."); 484AIFunction tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person."); 560AIFunction tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person."); 604private static ChatTool ToOpenAIChatTool(AIFunction aiFunction)
Microsoft.Extensions.AI.Tests (36)
ChatCompletion\FunctionInvocationContextTests.cs (1)
51AIFunction function = AIFunctionFactory.Create(() => { }, nameof(Properties_Roundtrip));
ChatCompletion\FunctionInvokingChatClientTests.cs (2)
674var function = AIFunctionFactory.Create(async (int i) => 749var function = AIFunctionFactory.Create((string? result = null) =>
Functions\AIFunctionFactoryTest.cs (33)
39AIFunction func; 54AIFunction func = AIFunctionFactory.Create((string a = "test") => a + " " + a); 62AIFunction[] funcs = 70foreach (AIFunction f in funcs) 85AIFunction func = AIFunctionFactory.Create((int value1 = 1, string value2 = "2", CancellationToken cancellationToken = default) => 99AIFunction func; 143AIFunction func; 188AIFunction func = AIFunctionFactory.Create(dotnetFunc, options); 212AIFunction func = AIFunctionFactory.Create( 239AIFunction func = AIFunctionFactory.Create(( 281AIFunction func = AIFunctionFactory.Create(( 309AIFunction func; 362AIFunction func = AIFunctionFactory.Create( 379AIFunction func = AIFunctionFactory.Create( 396AIFunction func = AIFunctionFactory.Create( 423AIFunction func = AIFunctionFactory.Create( 442AIFunction func = AIFunctionFactory.Create( 461AIFunction func = AIFunctionFactory.Create( 488AIFunction f = AIFunctionFactory.Create(([FromKeyedServices("key")] MyService service, int myInteger) => service.Value + myInteger); 509AIFunction f = AIFunctionFactory.Create(([FromKeyedServices(null!)] MyService service, int myInteger) => service.Value + myInteger); 530AIFunction f = AIFunctionFactory.Create(([FromKeyedServices("key")] MyService? service = null, int myInteger = 0) => 548AIFunction f = AIFunctionFactory.Create( 600AIFunction f = AIFunctionFactory.Create( 617AIFunction f = AIFunctionFactory.Create( 633AIFunction f = AIFunctionFactory.Create( 656AIFunction f = AIFunctionFactory.Create( 679AIFunction f = AIFunctionFactory.Create( 702AIFunction f = AIFunctionFactory.Create( 725AIFunction f = AIFunctionFactory.Create( 748AIFunction f = AIFunctionFactory.Create( 771AIFunction f = AIFunctionFactory.Create( 794AIFunction f = AIFunctionFactory.Create( 817AIFunction f = AIFunctionFactory.Create((Guid g = default, StructWithDefaultCtor s = default) => g.ToString() + "," + s.Value.ToString(), serializerOptions: JsonContext.Default.Options);