Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Shared.Collections;
Expand All @@ -13,8 +15,52 @@ namespace Microsoft.Extensions.AI;
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public abstract class AIFunction : AITool
{
/// <summary>Gets metadata describing the function.</summary>
public abstract AIFunctionMetadata Metadata { get; }
/// <summary>Gets the name of the function.</summary>
public abstract string Name { get; }

/// <summary>Gets a description of the function, suitable for use in describing the purpose to a model.</summary>
public abstract string Description { get; }

/// <summary>Gets a JSON Schema describing the function and its input parameters.</summary>
/// <remarks>
/// <para>
/// When specified, declares a self-contained JSON schema document that describes the function and its input parameters.
/// A simple example of a JSON schema for a function that adds two numbers together is shown below:
/// </para>
/// <code>
/// {
/// "title" : "addNumbers",
/// "description": "A simple function that adds two numbers together.",
/// "type": "object",
/// "properties": {
/// "a" : { "type": "number" },
/// "b" : { "type": "number", "default": 1 }
/// },
/// "required" : ["a"]
/// }
/// </code>
/// <para>
/// The metadata present in the schema document plays an important role in guiding AI function invocation.
/// </para>
/// <para>
/// When no schema is specified, consuming chat clients should assume the "{}" or "true" schema, indicating that any JSON input is admissible.
/// </para>
/// </remarks>
public virtual JsonElement JsonSchema => AIJsonUtilities.DefaultJsonSchema;

/// <summary>
/// Gets the underlying <see cref="MethodInfo"/> that this <see cref="AIFunction"/> might be wrapping.
/// </summary>
/// <remarks>
/// Provides additional metadata on the function and its signature. Implementations not wrapping .NET methods may return <see langword="null"/>.
/// </remarks>
public virtual MethodInfo? UnderlyingMethod => null;

/// <summary>Gets any additional properties associated with the function.</summary>
public virtual IReadOnlyDictionary<string, object?> AdditionalProperties => EmptyReadOnlyDictionary<string, object?>.Instance;

/// <summary>Gets a <see cref="JsonSerializerOptions"/> that can be used to marshal function parameters.</summary>
public virtual JsonSerializerOptions? JsonSerializerOptions => AIJsonUtilities.DefaultOptions;

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

/// <inheritdoc/>
public override string ToString() => Metadata.Name;
public override string ToString() => Name;

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

/// <summary>Gets the string to display in the debugger for this instance.</summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay =>
string.IsNullOrWhiteSpace(Metadata.Description) ?
Metadata.Name :
$"{Metadata.Name} ({Metadata.Description})";
private string DebuggerDisplay => string.IsNullOrWhiteSpace(Description) ? Name : $"{Name} ({Description})";
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading