3 types derived from AIFunction
Microsoft.Extensions.AI.Abstractions (1)
Functions\AIFunctionFactory.cs (1)
469
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
172 references to AIFunction
Microsoft.Extensions.AI (8)
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);
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
110
public 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"/>
183
public 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
276
public 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"/>
359
public 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
463
public 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"/>
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)
303
AIFunction
func = AIFunctionFactory.Create((int x, int y) => x + y, serializerOptions: options);
357
AIFunction
func = AIFunctionFactory.Create((
378
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)
345
if (tool is
AIFunction
af)
412
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.");
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 (9)
ChatClientIntegrationTests.cs (8)
295
AIFunction
func = AIFunctionFactory.Create((string id = "defaultId") => id is "foo" ? secretNumber : -1, "GetSecretNumberById");
327
AIFunction
func = AIFunctionFactory.Create((PersonRecord person) => person.Name is "John" ? secretNumber + person.Age : -1, "GetSecretNumberByPerson");
436
var
tool = AIFunctionFactory.Create(() =>
459
var
getSecretNumberTool = AIFunctionFactory.Create(() => 123, "GetSecretNumber");
460
var
shieldsUpTool = AIFunctionFactory.Create(() => shieldsUp = true, "ShieldsUp");
556
var
getTemperature = AIFunctionFactory.Create([Description("Gets the current temperature")] () =>
595
var
getTemperature = AIFunctionFactory.Create([Description("Gets the current temperature")] () =>
637
var
getTemperature = AIFunctionFactory.Create([Description("Gets the current temperature")] () =>
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)
517
if (tool is
AIFunction
af)
567
private static ChatTool ToOpenAIChatTool(
AIFunction
aiFunction)
OpenAIResponseChatClient.cs (1)
345
case
AIFunction
af:
Microsoft.Extensions.AI.OpenAI.Tests (7)
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)
OpenAIResponseClientTests.cs (2)
369
AIFunction
tool = AIFunctionFactory.Create(([Description("The person whose age is being requested")] string personName) => 42, "GetPersonAge", "Gets the age of the specified person.");
408
private static ResponseTool ToOpenAIResponseChatTool(
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)
40
AIFunction
func;
55
AIFunction
func = AIFunctionFactory.Create((string a = "test") => a + " " + a);
63
AIFunction
[] funcs =
71
foreach (
AIFunction
f in funcs)
86
AIFunction
func = AIFunctionFactory.Create((int value1 = 1, string value2 = "2", CancellationToken cancellationToken = default) =>
100
AIFunction
func;
149
AIFunction
func;
194
AIFunction
func = AIFunctionFactory.Create(dotnetFunc, options);
218
AIFunction
func = AIFunctionFactory.Create(
247
AIFunction
func = AIFunctionFactory.Create((
291
AIFunction
func = AIFunctionFactory.Create((
323
AIFunction
func = AIFunctionFactory.Create(
341
AIFunction
func = AIFunctionFactory.Create(
352
AIFunction
func = AIFunctionFactory.Create(
371
AIFunction
func = AIFunctionFactory.Create(
390
AIFunction
func = AIFunctionFactory.Create(
409
AIFunction
func = AIFunctionFactory.Create(
436
AIFunction
f = AIFunctionFactory.Create(([FromKeyedServices("key")] MyService service, int myInteger) => service.Value + myInteger,
459
AIFunction
f = AIFunctionFactory.Create(([FromKeyedServices(null!)] MyService service, int myInteger) => service.Value + myInteger,
482
AIFunction
f = AIFunctionFactory.Create(([FromKeyedServices("key")] MyService? service = null, int myInteger = 0) =>
501
AIFunction
f = AIFunctionFactory.Create(
553
AIFunction
f = AIFunctionFactory.Create(
570
AIFunction
f = AIFunctionFactory.Create(
586
AIFunction
f = AIFunctionFactory.Create(
609
AIFunction
f = AIFunctionFactory.Create(
632
AIFunction
f = AIFunctionFactory.Create(
655
AIFunction
f = AIFunctionFactory.Create(
678
AIFunction
f = AIFunctionFactory.Create(
701
AIFunction
f = AIFunctionFactory.Create(
724
AIFunction
f = AIFunctionFactory.Create(
747
AIFunction
f = AIFunctionFactory.Create(
771
AIFunction
f = AIFunctionFactory.Create((Guid g = default, StructWithDefaultCtor s = default) => g.ToString() + "," + s.Value.ToString(), serializerOptions: JsonContext.Default.Options);
780
AIFunction
f = AIFunctionFactory.Create(Add, serializerOptions: JsonContext.Default.Options);