-
Notifications
You must be signed in to change notification settings - Fork 828
Split AIFunction into a base class #6695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
src/Libraries/Microsoft.Extensions.AI.Abstractions/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionDeclaration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
|
||
#pragma warning disable S1694 // An abstract class should have both abstract and concrete methods | ||
|
||
namespace Microsoft.Extensions.AI; | ||
|
||
/// <summary>Represents a function that can be described to an AI service.</summary> | ||
/// <remarks> | ||
/// <see cref="AIFunctionDeclaration"/> is the base class for <see cref="AIFunction"/>, which | ||
/// adds the ability to invoke the function. Components may type test <see cref="AITool"/> instances | ||
/// for <see cref="AIFunctionDeclaration"/> to determine whether they can be described as functions, | ||
/// and may type test for <see cref="AIFunction"/> to determine whether they can be invoked. | ||
/// </remarks> | ||
public abstract class AIFunctionDeclaration : AITool | ||
{ | ||
/// <summary>Initializes a new instance of the <see cref="AIFunctionDeclaration"/> class.</summary> | ||
protected AIFunctionDeclaration() | ||
{ | ||
} | ||
|
||
/// <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 a JSON Schema describing the function's return value.</summary> | ||
/// <remarks> | ||
/// A <see langword="null"/> typically reflects a function that doesn't specify a return schema | ||
/// or a function that returns <see cref="void"/>, <see cref="Task"/>, or <see cref="ValueTask"/>. | ||
/// </remarks> | ||
public virtual JsonElement? ReturnJsonSchema => null; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...braries/Microsoft.Extensions.AI.Abstractions/Functions/DelegatingAIFunctionDeclaration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using Microsoft.Shared.Diagnostics; | ||
|
||
#pragma warning disable SA1202 // Elements should be ordered by access | ||
|
||
namespace Microsoft.Extensions.AI; | ||
|
||
/// <summary> | ||
/// Provides an optional base class for an <see cref="AIFunctionDeclaration"/> that passes through calls to another instance. | ||
/// </summary> | ||
internal class DelegatingAIFunctionDeclaration : AIFunctionDeclaration // could be made public in the future if there's demand | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DelegatingAIFunctionDeclaration"/> class as a wrapper around <paramref name="innerFunction"/>. | ||
/// </summary> | ||
/// <param name="innerFunction">The inner AI function to which all calls are delegated by default.</param> | ||
/// <exception cref="ArgumentNullException"><paramref name="innerFunction"/> is <see langword="null"/>.</exception> | ||
protected DelegatingAIFunctionDeclaration(AIFunctionDeclaration innerFunction) | ||
{ | ||
InnerFunction = Throw.IfNull(innerFunction); | ||
} | ||
|
||
/// <summary>Gets the inner <see cref="AIFunctionDeclaration" />.</summary> | ||
protected AIFunctionDeclaration InnerFunction { get; } | ||
|
||
/// <inheritdoc /> | ||
public override string Name => InnerFunction.Name; | ||
|
||
/// <inheritdoc /> | ||
public override string Description => InnerFunction.Description; | ||
|
||
/// <inheritdoc /> | ||
public override JsonElement JsonSchema => InnerFunction.JsonSchema; | ||
|
||
/// <inheritdoc /> | ||
public override JsonElement? ReturnJsonSchema => InnerFunction.ReturnJsonSchema; | ||
|
||
/// <inheritdoc /> | ||
public override IReadOnlyDictionary<string, object?> AdditionalProperties => InnerFunction.AdditionalProperties; | ||
|
||
/// <inheritdoc /> | ||
public override string ToString() => InnerFunction.ToString(); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.