3 types derived from AIFunction
Microsoft.Extensions.AI.Abstractions (1)
Functions\AIFunctionFactory.cs (1)
469private 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
172 references to AIFunction
Microsoft.Extensions.AI (8)
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);
Microsoft.Extensions.AI.Abstractions (90)
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)
49/// Gets the underlying <see cref="MethodInfo"/> that this <see cref="AIFunction"/> might be wrapping. 59/// <summary>Invokes the <see cref="AIFunction"/> and returns its result.</summary> 68/// <summary>Invokes the <see cref="AIFunction"/> and returns its result.</summary>
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\AIFunctionFactory.cs (64)
32/// <summary>Provides factory methods for creating commonly-used implementations of <see cref="AIFunction"/>.</summary> 43/// <summary>Creates an <see cref="AIFunction"/> instance for a method, specified via a delegate.</summary> 44/// <param name="method">The method to be represented via the created <see cref="AIFunction"/>.</param> 46/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns> 50/// of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="AIFunction"/>'s 51/// <see cref="AIFunction.JsonSchema"/>. There are a few exceptions to this: 56/// the invocation via <see cref="AIFunction.InvokeAsync"/>'s <see cref="CancellationToken"/> parameter. The parameter is 72/// passed into <see cref="AIFunction.InvokeAsync"/> and are not included in the JSON schema. If the <see cref="AIFunctionArguments"/> 73/// instance passed to <see cref="AIFunction.InvokeAsync"/> is <see langword="null"/>, the <see cref="AIFunction"/> implementation 80/// All other parameter types are, by default, bound from the <see cref="AIFunctionArguments"/> dictionary passed into <see cref="AIFunction.InvokeAsync"/> 85/// into <see cref="AIFunction.InvokeAsync"/> via the <see cref="AIFunctionArguments"/>'s <see cref="AIFunctionArguments.Context"/> dictionary; the default 110public static AIFunction Create(Delegate method, AIFunctionFactoryOptions? options) 117/// <summary>Creates an <see cref="AIFunction"/> instance for a method, specified via a delegate.</summary> 118/// <param name="method">The method to be represented via the created <see cref="AIFunction"/>.</param> 120/// The name to use for the <see cref="AIFunction"/>. If <see langword="null"/>, the name will be derived from 124/// The description to use for the <see cref="AIFunction"/>. If <see langword="null"/>, a description will be derived from 128/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns> 132/// of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="AIFunction"/>'s 133/// <see cref="AIFunction.JsonSchema"/>. There are a few exceptions to this: 138/// the invocation via <see cref="AIFunction.InvokeAsync"/>'s <see cref="CancellationToken"/> parameter. The parameter is 153/// passed into <see cref="AIFunction.InvokeAsync"/> and are not included in the JSON schema. If the <see cref="AIFunctionArguments"/> 154/// instance passed to <see cref="AIFunction.InvokeAsync"/> is <see langword="null"/>, the <see cref="AIFunction"/> implementation 160/// All other parameter types are bound from the <see cref="AIFunctionArguments"/> dictionary passed into <see cref="AIFunction.InvokeAsync"/> 183public static AIFunction Create(Delegate method, string? name = null, string? description = null, JsonSerializerOptions? serializerOptions = null) 200/// Creates an <see cref="AIFunction"/> instance for a method, specified via an <see cref="MethodInfo"/> instance 203/// <param name="method">The method to be represented via the created <see cref="AIFunction"/>.</param> 209/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns> 213/// of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="AIFunction"/>'s 214/// <see cref="AIFunction.JsonSchema"/>. There are a few exceptions to this: 219/// the invocation via <see cref="AIFunction.InvokeAsync"/>'s <see cref="CancellationToken"/> parameter. The parameter is 235/// passed into <see cref="AIFunction.InvokeAsync"/> and are not included in the JSON schema. If the <see cref="AIFunctionArguments"/> 236/// instance passed to <see cref="AIFunction.InvokeAsync"/> is <see langword="null"/>, the <see cref="AIFunction"/> implementation 243/// All other parameter types are, by default, bound from the <see cref="AIFunctionArguments"/> dictionary passed into <see cref="AIFunction.InvokeAsync"/> 248/// into <see cref="AIFunction.InvokeAsync"/> via the <see cref="AIFunctionArguments"/>'s <see cref="AIFunctionArguments.Context"/> dictionary; the default 276public static AIFunction Create(MethodInfo method, object? target, AIFunctionFactoryOptions? options) 284/// Creates an <see cref="AIFunction"/> instance for a method, specified via an <see cref="MethodInfo"/> instance 287/// <param name="method">The method to be represented via the created <see cref="AIFunction"/>.</param> 293/// The name to use for the <see cref="AIFunction"/>. If <see langword="null"/>, the name will be derived from 297/// The description to use for the <see cref="AIFunction"/>. If <see langword="null"/>, a description will be derived from 301/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns> 305/// of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="AIFunction"/>'s 306/// <see cref="AIFunction.JsonSchema"/>. There are a few exceptions to this: 311/// the invocation via <see cref="AIFunction.InvokeAsync"/>'s <see cref="CancellationToken"/> parameter. The parameter is 326/// passed into <see cref="AIFunction.InvokeAsync"/> and are not included in the JSON schema. If the <see cref="AIFunctionArguments"/> 327/// instance passed to <see cref="AIFunction.InvokeAsync"/> is <see langword="null"/>, the <see cref="AIFunction"/> implementation 333/// All other parameter types are bound from the <see cref="AIFunctionArguments"/> dictionary passed into <see cref="AIFunction.InvokeAsync"/> 359public static AIFunction Create(MethodInfo method, object? target, string? name = null, string? description = null, JsonSerializerOptions? serializerOptions = null) 376/// Creates an <see cref="AIFunction"/> instance for a method, specified via a <see cref="MethodInfo"/> for 378/// the receiver object each time the <see cref="AIFunction"/> is invoked. 380/// <param name="method">The instance method to be represented via the created <see cref="AIFunction"/>.</param> 387/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns> 399/// of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned <see cref="AIFunction"/>'s 400/// <see cref="AIFunction.JsonSchema"/>. There are a few exceptions to this: 405/// the invocation via <see cref="AIFunction.InvokeAsync"/>'s <see cref="CancellationToken"/> parameter. The parameter is 421/// passed into <see cref="AIFunction.InvokeAsync"/> and are not included in the JSON schema. If the <see cref="AIFunctionArguments"/> 422/// instance passed to <see cref="AIFunction.InvokeAsync"/> is <see langword="null"/>, the <see cref="AIFunction"/> implementation 429/// All other parameter types are, by default, bound from the <see cref="AIFunctionArguments"/> dictionary passed into <see cref="AIFunction.InvokeAsync"/> 434/// into <see cref="AIFunction.InvokeAsync"/> via the <see cref="AIFunctionArguments"/>'s <see cref="AIFunctionArguments.Context"/> dictionary; the default 463public static AIFunction Create(
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 119/// If non- <see langword="null"/>, each time the <see cref="AIFunction"/> is invoked, this delegate will be invoked 136/// It, however, is permissible for cases where invocation of the <see cref="AIFunction"/> is tightly controlled, and the caller
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)
303AIFunction func = AIFunctionFactory.Create((int x, int y) => x + y, serializerOptions: options); 357AIFunction func = AIFunctionFactory.Create(( 378AIFunction 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)
345if (tool is AIFunction af) 412private 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."); 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 (9)
ChatClientIntegrationTests.cs (8)
295AIFunction func = AIFunctionFactory.Create((string id = "defaultId") => id is "foo" ? secretNumber : -1, "GetSecretNumberById"); 327AIFunction func = AIFunctionFactory.Create((PersonRecord person) => person.Name is "John" ? secretNumber + person.Age : -1, "GetSecretNumberByPerson"); 436var tool = AIFunctionFactory.Create(() => 459var getSecretNumberTool = AIFunctionFactory.Create(() => 123, "GetSecretNumber"); 460var shieldsUpTool = AIFunctionFactory.Create(() => shieldsUp = true, "ShieldsUp"); 556var getTemperature = AIFunctionFactory.Create([Description("Gets the current temperature")] () => 595var getTemperature = AIFunctionFactory.Create([Description("Gets the current temperature")] () => 637var getTemperature = AIFunctionFactory.Create([Description("Gets the current temperature")] () =>
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)
517if (tool is AIFunction af) 567private static ChatTool ToOpenAIChatTool(AIFunction aiFunction)
OpenAIResponseChatClient.cs (1)
345case AIFunction af:
Microsoft.Extensions.AI.OpenAI.Tests (7)
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)
OpenAIResponseClientTests.cs (2)
369AIFunction tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person."); 408private static ResponseTool ToOpenAIResponseChatTool(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)
40AIFunction func; 55AIFunction func = AIFunctionFactory.Create((string a = "test") => a + " " + a); 63AIFunction[] funcs = 71foreach (AIFunction f in funcs) 86AIFunction func = AIFunctionFactory.Create((int value1 = 1, string value2 = "2", CancellationToken cancellationToken = default) => 100AIFunction func; 149AIFunction func; 194AIFunction func = AIFunctionFactory.Create(dotnetFunc, options); 218AIFunction func = AIFunctionFactory.Create( 247AIFunction func = AIFunctionFactory.Create(( 291AIFunction func = AIFunctionFactory.Create(( 323AIFunction func = AIFunctionFactory.Create( 341AIFunction func = AIFunctionFactory.Create( 352AIFunction func = AIFunctionFactory.Create( 371AIFunction func = AIFunctionFactory.Create( 390AIFunction func = AIFunctionFactory.Create( 409AIFunction func = AIFunctionFactory.Create( 436AIFunction f = AIFunctionFactory.Create(([FromKeyedServices("key")] MyService service, int myInteger) => service.Value + myInteger, 459AIFunction f = AIFunctionFactory.Create(([FromKeyedServices(null!)] MyService service, int myInteger) => service.Value + myInteger, 482AIFunction f = AIFunctionFactory.Create(([FromKeyedServices("key")] MyService? service = null, int myInteger = 0) => 501AIFunction f = AIFunctionFactory.Create( 553AIFunction f = AIFunctionFactory.Create( 570AIFunction f = AIFunctionFactory.Create( 586AIFunction f = AIFunctionFactory.Create( 609AIFunction f = AIFunctionFactory.Create( 632AIFunction f = AIFunctionFactory.Create( 655AIFunction f = AIFunctionFactory.Create( 678AIFunction f = AIFunctionFactory.Create( 701AIFunction f = AIFunctionFactory.Create( 724AIFunction f = AIFunctionFactory.Create( 747AIFunction f = AIFunctionFactory.Create( 771AIFunction f = AIFunctionFactory.Create((Guid g = default, StructWithDefaultCtor s = default) => g.ToString() + "," + s.Value.ToString(), serializerOptions: JsonContext.Default.Options); 780AIFunction f = AIFunctionFactory.Create(Add, serializerOptions: JsonContext.Default.Options);