Skip to content

Commit ec8d3ab

Browse files
Move AIFunction parameter schematization from parameter level to function level.
1 parent d63f2b4 commit ec8d3ab

File tree

15 files changed

+224
-295
lines changed

15 files changed

+224
-295
lines changed

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ namespace Microsoft.Extensions.AI;
1717
public sealed class AIFunctionMetadata
1818
{
1919
/// <summary>The name of the function.</summary>
20-
private string _name = string.Empty;
20+
private readonly string _name = string.Empty;
2121

2222
/// <summary>The description of the function.</summary>
23-
private string _description = string.Empty;
23+
private readonly string _description = string.Empty;
24+
25+
/// <summary>The JSON schema describing the function and its input parameters.</summary>
26+
private readonly JsonElement _schema = AIJsonUtilities.DefaultJsonSchema;
2427

2528
/// <summary>The function's parameters.</summary>
2629
private IReadOnlyList<AIFunctionParameterMetadata> _parameters = [];
@@ -55,6 +58,7 @@ public AIFunctionMetadata(AIFunctionMetadata metadata)
5558
Parameters = metadata.Parameters;
5659
ReturnParameter = metadata.ReturnParameter;
5760
AdditionalProperties = metadata.AdditionalProperties;
61+
Schema = metadata.Schema;
5862
}
5963

6064
/// <summary>Gets the name of the function.</summary>
@@ -100,6 +104,43 @@ public AIFunctionReturnParameterMetadata ReturnParameter
100104
init => _returnParameter = Throw.IfNull(value);
101105
}
102106

107+
/// <summary>Gets a JSON Schema describing the function and its input parameters.</summary>
108+
/// <remarks>
109+
/// <para>
110+
/// When specified, declares a self-contained JSON schema document that describes the function and its input parameters.
111+
/// A simple example of a JSON schema for a function that adds two numbers together is shown below:
112+
/// </para>
113+
/// <code>
114+
/// {
115+
/// "title" : "addNumbers",
116+
/// "description": "A simple function that adds two numbers together.",
117+
/// "type": "object",
118+
/// "properties": {
119+
/// "a" : { "type": "number" },
120+
/// "b" : { "type": "number", "default": 1 }
121+
/// },
122+
/// "required" : ["a"]
123+
/// }
124+
/// </code>
125+
/// <para>
126+
/// The metadata present in the schema document plays an important role in guiding AI function invocation.
127+
/// Functions should incorporate as much detail as possible. The arity of the "properties" keyword should
128+
/// also match the length of the <see cref="Parameters"/> list.
129+
/// </para>
130+
/// <para>
131+
/// When no schema is specified, consuming chat clients should assume the "{}" or "true" schema, indicating that any JSON input is admissible.
132+
/// </para>
133+
/// </remarks>
134+
public JsonElement Schema
135+
{
136+
get => _schema;
137+
init
138+
{
139+
AIJsonUtilities.ValidateSchemaDocument(value);
140+
_schema = value;
141+
}
142+
}
143+
103144
/// <summary>Gets any additional properties associated with the function.</summary>
104145
public IReadOnlyDictionary<string, object?> AdditionalProperties
105146
{

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public AIFunctionParameterMetadata(AIFunctionParameterMetadata metadata)
3737
DefaultValue = metadata.DefaultValue;
3838
IsRequired = metadata.IsRequired;
3939
ParameterType = metadata.ParameterType;
40-
Schema = metadata.Schema;
4140
}
4241

4342
/// <summary>Gets the name of the parameter.</summary>
@@ -61,7 +60,4 @@ public string Name
6160

6261
/// <summary>Gets the .NET type of the parameter.</summary>
6362
public Type? ParameterType { get; init; }
64-
65-
/// <summary>Gets a JSON Schema describing the parameter's type.</summary>
66-
public object? Schema { get; init; }
6763
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Text.Json;
56
using Microsoft.Shared.Diagnostics;
67

78
namespace Microsoft.Extensions.AI;
@@ -14,6 +15,9 @@ public sealed class AIFunctionReturnParameterMetadata
1415
/// <summary>Gets an empty return parameter metadata instance.</summary>
1516
public static AIFunctionReturnParameterMetadata Empty { get; } = new();
1617

18+
/// <summary>The JSON schema describing the function and its input parameters.</summary>
19+
private readonly JsonElement _schema = AIJsonUtilities.DefaultJsonSchema;
20+
1721
/// <summary>Initializes a new instance of the <see cref="AIFunctionReturnParameterMetadata"/> class.</summary>
1822
public AIFunctionReturnParameterMetadata()
1923
{
@@ -34,5 +38,13 @@ public AIFunctionReturnParameterMetadata(AIFunctionReturnParameterMetadata metad
3438
public Type? ParameterType { get; init; }
3539

3640
/// <summary>Gets a JSON Schema describing the type of the return parameter.</summary>
37-
public object? Schema { get; init; }
41+
public JsonElement Schema
42+
{
43+
get => _schema;
44+
init
45+
{
46+
AIJsonUtilities.ValidateSchemaDocument(value);
47+
_schema = value;
48+
}
49+
}
3850
}

0 commit comments

Comments
 (0)