Skip to content

Commit 2f973c9

Browse files
eiriktsarpalisstephentoubSteveSandersonMS
authored
Replace AIFunctionParameterMetadata with MethodInfo (#5886)
* Replace AIFunctionParemeterMetadata with MethodInfo * Update src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionMetadata.cs Co-authored-by: Stephen Toub <[email protected]> * Update src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionMetadata.cs Co-authored-by: Stephen Toub <[email protected]> * Address feedback, * Update src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.cs Co-authored-by: Steve Sanderson <[email protected]> * Flatten AIFunctionMetadata properties into AIFunction. * Address feedback. * Mark JSO as virtual --------- Co-authored-by: Stephen Toub <[email protected]> Co-authored-by: Steve Sanderson <[email protected]>
1 parent 584a56a commit 2f973c9

File tree

24 files changed

+207
-735
lines changed

24 files changed

+207
-735
lines changed

src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunction.cs

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
using System.Collections.Generic;
55
using System.Diagnostics;
6+
using System.Reflection;
7+
using System.Text.Json;
68
using System.Threading;
79
using System.Threading.Tasks;
810
using Microsoft.Shared.Collections;
@@ -13,8 +15,52 @@ namespace Microsoft.Extensions.AI;
1315
[DebuggerDisplay("{DebuggerDisplay,nq}")]
1416
public abstract class AIFunction : AITool
1517
{
16-
/// <summary>Gets metadata describing the function.</summary>
17-
public abstract AIFunctionMetadata Metadata { get; }
18+
/// <summary>Gets the name of the function.</summary>
19+
public abstract string Name { get; }
20+
21+
/// <summary>Gets a description of the function, suitable for use in describing the purpose to a model.</summary>
22+
public abstract string Description { get; }
23+
24+
/// <summary>Gets a JSON Schema describing the function and its input parameters.</summary>
25+
/// <remarks>
26+
/// <para>
27+
/// When specified, declares a self-contained JSON schema document that describes the function and its input parameters.
28+
/// A simple example of a JSON schema for a function that adds two numbers together is shown below:
29+
/// </para>
30+
/// <code>
31+
/// {
32+
/// "title" : "addNumbers",
33+
/// "description": "A simple function that adds two numbers together.",
34+
/// "type": "object",
35+
/// "properties": {
36+
/// "a" : { "type": "number" },
37+
/// "b" : { "type": "number", "default": 1 }
38+
/// },
39+
/// "required" : ["a"]
40+
/// }
41+
/// </code>
42+
/// <para>
43+
/// The metadata present in the schema document plays an important role in guiding AI function invocation.
44+
/// </para>
45+
/// <para>
46+
/// When no schema is specified, consuming chat clients should assume the "{}" or "true" schema, indicating that any JSON input is admissible.
47+
/// </para>
48+
/// </remarks>
49+
public virtual JsonElement JsonSchema => AIJsonUtilities.DefaultJsonSchema;
50+
51+
/// <summary>
52+
/// Gets the underlying <see cref="MethodInfo"/> that this <see cref="AIFunction"/> might be wrapping.
53+
/// </summary>
54+
/// <remarks>
55+
/// Provides additional metadata on the function and its signature. Implementations not wrapping .NET methods may return <see langword="null"/>.
56+
/// </remarks>
57+
public virtual MethodInfo? UnderlyingMethod => null;
58+
59+
/// <summary>Gets any additional properties associated with the function.</summary>
60+
public virtual IReadOnlyDictionary<string, object?> AdditionalProperties => EmptyReadOnlyDictionary<string, object?>.Instance;
61+
62+
/// <summary>Gets a <see cref="JsonSerializerOptions"/> that can be used to marshal function parameters.</summary>
63+
public virtual JsonSerializerOptions? JsonSerializerOptions => AIJsonUtilities.DefaultOptions;
1864

1965
/// <summary>Invokes the <see cref="AIFunction"/> and returns its result.</summary>
2066
/// <param name="arguments">The arguments to pass to the function's invocation.</param>
@@ -30,7 +76,7 @@ public abstract class AIFunction : AITool
3076
}
3177

3278
/// <inheritdoc/>
33-
public override string ToString() => Metadata.Name;
79+
public override string ToString() => Name;
3480

3581
/// <summary>Invokes the <see cref="AIFunction"/> and returns its result.</summary>
3682
/// <param name="arguments">The arguments to pass to the function's invocation.</param>
@@ -42,8 +88,5 @@ public abstract class AIFunction : AITool
4288

4389
/// <summary>Gets the string to display in the debugger for this instance.</summary>
4490
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
45-
private string DebuggerDisplay =>
46-
string.IsNullOrWhiteSpace(Metadata.Description) ?
47-
Metadata.Name :
48-
$"{Metadata.Name} ({Metadata.Description})";
91+
private string DebuggerDisplay => string.IsNullOrWhiteSpace(Description) ? Name : $"{Name} ({Description})";
4992
}

src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionMetadata.cs

Lines changed: 0 additions & 153 deletions
This file was deleted.

src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionParameterMetadata.cs

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionReturnParameterMetadata.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)