3 types derived from AIFunction
Microsoft.Extensions.AI (1)
Functions\AIFunctionFactory.cs (1)
563
private sealed class ReflectionAIFunction :
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
179 references to AIFunction
Microsoft.Extensions.AI (95)
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 (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>
695
AIFunction
? 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
124
public 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"/>
214
public 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
326
public 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"/>
426
public 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
552
public 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"/>
26
private readonly ConditionalWeakTable<
AIFunction
, object> _functionSchemaCache = new();
29
private readonly ConditionalWeakTable<
AIFunction
, object>.CreateValueCallback _functionSchemaCreateValueCallback;
56
/// Gets or creates a transformed JSON schema for the specified <see cref="
AIFunction
"/> instance.
60
public JsonElement GetOrCreateTransformedSchema(
AIFunction
function)
Microsoft.Extensions.AI.Abstractions.Tests (8)
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);
Utilities\AIJsonSchemaTransformCacheTests.cs (1)
51
AIFunction
func = AIFunctionFactory.Create((int x, int y) => x + y);
Utilities\AIJsonUtilitiesTests.cs (3)
297
AIFunction
func = AIFunctionFactory.Create((int x, int y) => x + y, serializerOptions: options);
344
AIFunction
func = AIFunctionFactory.Create((
369
AIFunction
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)
344
if (tool is
AIFunction
af)
411
private static ChatCompletionsToolDefinition ToAzureAIChatTool(
AIFunction
aiFunction)
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.");
428
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
517
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
592
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
636
private static ChatCompletionsToolDefinition ToAzureAIChatTool(
AIFunction
aiFunction)
Microsoft.Extensions.AI.Integration.Tests (6)
ChatClientIntegrationTests.cs (5)
293
AIFunction
func = AIFunctionFactory.Create((string id = "defaultId") => id is "foo" ? secretNumber : -1, "GetSecretNumberById");
325
AIFunction
func = AIFunctionFactory.Create((PersonRecord person) => person.Name is "John" ? secretNumber + person.Age : -1, "GetSecretNumberByPerson");
434
var
tool = AIFunctionFactory.Create(() =>
457
var
getSecretNumberTool = AIFunctionFactory.Create(() => 123, "GetSecretNumber");
458
var
shieldsUpTool = AIFunctionFactory.Create(() => shieldsUp = true, "ShieldsUp");
PromptBasedFunctionCallingChatClient.cs (1)
169
var toolDescriptorsJson = JsonSerializer.Serialize(tools.OfType<
AIFunction
>().Select(t => t.JsonSchema), _jsonOptions);
Microsoft.Extensions.AI.Ollama (2)
OllamaChatClient.cs (2)
316
Tools = options?.ToolMode is not NoneChatToolMode && options?.Tools is { Count: > 0 } tools ? tools.OfType<
AIFunction
>().Select(ToOllamaTool) : null,
482
private static OllamaTool ToOllamaTool(
AIFunction
function)
Microsoft.Extensions.AI.Ollama.Tests (2)
OllamaChatClientIntegrationTests.cs (2)
65
var
stockPriceTool = AIFunctionFactory.Create([Description("Returns the stock price for a given ticker symbol")] (
75
var
irrelevantTool = AIFunctionFactory.Create(() => { didCallIrrelevantTool = true; return 123; }, "GetSecretNumber");
Microsoft.Extensions.AI.OpenAI (3)
OpenAIChatClient.cs (2)
485
if (tool is
AIFunction
af)
535
private static ChatTool ToOpenAIChatTool(
AIFunction
aiFunction)
OpenAIResponseChatClient.cs (1)
375
case
AIFunction
af:
Microsoft.Extensions.AI.OpenAI.Tests (5)
OpenAIChatClientTests.cs (5)
320
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
399
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
484
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
560
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
604
private static ChatTool ToOpenAIChatTool(
AIFunction
aiFunction)
Microsoft.Extensions.AI.Tests (36)
ChatCompletion\FunctionInvocationContextTests.cs (1)
51
AIFunction
function = AIFunctionFactory.Create(() => { }, nameof(Properties_Roundtrip));
ChatCompletion\FunctionInvokingChatClientTests.cs (2)
674
var
function = AIFunctionFactory.Create(async (int i) =>
749
var
function = AIFunctionFactory.Create((string? result = null) =>
Functions\AIFunctionFactoryTest.cs (33)
39
AIFunction
func;
54
AIFunction
func = AIFunctionFactory.Create((string a = "test") => a + " " + a);
62
AIFunction
[] funcs =
70
foreach (
AIFunction
f in funcs)
85
AIFunction
func = AIFunctionFactory.Create((int value1 = 1, string value2 = "2", CancellationToken cancellationToken = default) =>
99
AIFunction
func;
143
AIFunction
func;
188
AIFunction
func = AIFunctionFactory.Create(dotnetFunc, options);
212
AIFunction
func = AIFunctionFactory.Create(
239
AIFunction
func = AIFunctionFactory.Create((
281
AIFunction
func = AIFunctionFactory.Create((
309
AIFunction
func;
362
AIFunction
func = AIFunctionFactory.Create(
379
AIFunction
func = AIFunctionFactory.Create(
396
AIFunction
func = AIFunctionFactory.Create(
423
AIFunction
func = AIFunctionFactory.Create(
442
AIFunction
func = AIFunctionFactory.Create(
461
AIFunction
func = AIFunctionFactory.Create(
488
AIFunction
f = AIFunctionFactory.Create(([FromKeyedServices("key")] MyService service, int myInteger) => service.Value + myInteger);
509
AIFunction
f = AIFunctionFactory.Create(([FromKeyedServices(null!)] MyService service, int myInteger) => service.Value + myInteger);
530
AIFunction
f = AIFunctionFactory.Create(([FromKeyedServices("key")] MyService? service = null, int myInteger = 0) =>
548
AIFunction
f = AIFunctionFactory.Create(
600
AIFunction
f = AIFunctionFactory.Create(
617
AIFunction
f = AIFunctionFactory.Create(
633
AIFunction
f = AIFunctionFactory.Create(
656
AIFunction
f = AIFunctionFactory.Create(
679
AIFunction
f = AIFunctionFactory.Create(
702
AIFunction
f = AIFunctionFactory.Create(
725
AIFunction
f = AIFunctionFactory.Create(
748
AIFunction
f = AIFunctionFactory.Create(
771
AIFunction
f = AIFunctionFactory.Create(
794
AIFunction
f = AIFunctionFactory.Create(
817
AIFunction
f = AIFunctionFactory.Create((Guid g = default, StructWithDefaultCtor s = default) => g.ToString() + "," + s.Value.ToString(), serializerOptions: JsonContext.Default.Options);