Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ public static ReflectionAIFunctionDescriptor GetOrCreate(MethodInfo method, AIFu
serializerOptions.MakeReadOnly();
ConcurrentDictionary<DescriptorKey, ReflectionAIFunctionDescriptor> innerCache = _descriptorCache.GetOrCreateValue(serializerOptions);

DescriptorKey key = new(method, options.Name, options.Description, options.ConfigureParameterBinding, options.MarshalResult, schemaOptions);
DescriptorKey key = new(method, options.Name, options.Description, options.ConfigureParameterBinding, options.MarshalResult, options.ExcludeResultSchema, schemaOptions);
if (innerCache.TryGetValue(key, out ReflectionAIFunctionDescriptor? descriptor))
{
return descriptor;
Expand Down Expand Up @@ -690,7 +690,7 @@ private ReflectionAIFunctionDescriptor(DescriptorKey key, JsonSerializerOptions
Name = key.Name ?? GetFunctionName(key.Method);
Description = key.Description ?? key.Method.GetCustomAttribute<DescriptionAttribute>(inherit: true)?.Description ?? string.Empty;
JsonSerializerOptions = serializerOptions;
ReturnJsonSchema = returnType is null ? null : AIJsonUtilities.CreateJsonSchema(
ReturnJsonSchema = returnType is null || key.ExcludeResultSchema ? null : AIJsonUtilities.CreateJsonSchema(
returnType,
description: key.Method.ReturnParameter.GetCustomAttribute<DescriptionAttribute>(inherit: true)?.Description,
serializerOptions: serializerOptions,
Expand Down Expand Up @@ -1036,6 +1036,7 @@ private record struct DescriptorKey(
string? Description,
Func<ParameterInfo, AIFunctionFactoryOptions.ParameterBindingOptions>? GetBindParameterOptions,
Func<object?, Type?, CancellationToken, ValueTask<object?>>? MarshalResult,
bool ExcludeResultSchema,
AIJsonSchemaCreateOptions SchemaOptions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ public AIFunctionFactoryOptions()
/// </remarks>
public Func<object?, Type?, CancellationToken, ValueTask<object?>>? MarshalResult { get; set; }

/// <summary>
/// Gets or sets a value indicating whether a schema should be created for the function's result type, if possible, and included as <see cref="AIFunction.ReturnJsonSchema" />.
/// </summary>
/// <remarks>
/// <para>
/// The default value is <see langword="false"/>.
/// </para>
/// <para>
/// When set to <see langword="true"/>, results in the produced <see cref="AIFunction.ReturnJsonSchema"/> to always be <see langword="null"/>.
/// </para>
/// </remarks>
public bool ExcludeResultSchema { get; set; }

/// <summary>Provides configuration options produced by the <see cref="ConfigureParameterBinding"/> delegate.</summary>
public readonly record struct ParameterBindingOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@
"Member": "string? Microsoft.Extensions.AI.AIFunctionFactoryOptions.Description { get; set; }",
"Stage": "Stable"
},
{
"Member": "bool Microsoft.Extensions.AI.AIFunctionFactoryOptions.ExcludeResultSchema { get; set; }",
"Stage": "Stable"
},
{
"Member": "Microsoft.Extensions.AI.AIJsonSchemaCreateOptions? Microsoft.Extensions.AI.AIFunctionFactoryOptions.JsonSchemaCreateOptions { get; set; }",
"Stage": "Stable"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ public void AIFunctionFactoryOptions_DefaultValues()
Assert.Null(options.SerializerOptions);
Assert.Null(options.JsonSchemaCreateOptions);
Assert.Null(options.ConfigureParameterBinding);
Assert.False(options.ExcludeResultSchema);
}

[Fact]
Expand All @@ -300,6 +301,21 @@ public async Task AIFunctionFactoryOptions_SupportsSkippingParameters()
Assert.Contains("test42", result.ToString());
}

[Fact]
public void AIFunctionFactoryOptions_SupportsSkippingReturnSchema()
{
AIFunction func = AIFunctionFactory.Create(
(string firstParameter, int secondParameter) => firstParameter + secondParameter,
new()
{
ExcludeResultSchema = true,
});

Assert.Contains("firstParameter", func.JsonSchema.ToString());
Assert.Contains("secondParameter", func.JsonSchema.ToString());
Assert.Null(func.ReturnJsonSchema);
}

[Fact]
public async Task AIFunctionArguments_SatisfiesParameters()
{
Expand Down
Loading