-
Notifications
You must be signed in to change notification settings - Fork 829
Add marker AITool for enabling code interpreters #5898
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
44 changes: 43 additions & 1 deletion
44
src/Libraries/Microsoft.Extensions.AI.Abstractions/AITool.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 |
---|---|---|
@@ -1,13 +1,55 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Text; | ||
using Microsoft.Shared.Collections; | ||
|
||
namespace Microsoft.Extensions.AI; | ||
|
||
#pragma warning disable S1694 // An abstract class should have both abstract and concrete methods | ||
|
||
/// <summary>Represents a tool that can be specified to an AI service.</summary> | ||
public class AITool | ||
[DebuggerDisplay("{DebuggerDisplay,nq}")] | ||
public abstract class AITool | ||
{ | ||
/// <summary>Initializes a new instance of the <see cref="AITool"/> class.</summary> | ||
protected AITool() | ||
{ | ||
} | ||
|
||
/// <summary>Gets the name of the tool.</summary> | ||
public virtual string Name => GetType().Name; | ||
|
||
/// <summary>Gets a description of the tool, suitable for use in describing the purpose to a model.</summary> | ||
public virtual string Description => string.Empty; | ||
|
||
/// <summary>Gets any additional properties associated with the tool.</summary> | ||
public virtual IReadOnlyDictionary<string, object?> AdditionalProperties => EmptyReadOnlyDictionary<string, object?>.Instance; | ||
|
||
/// <inheritdoc/> | ||
public override string ToString() => Name; | ||
|
||
/// <summary>Gets the string to display in the debugger for this instance.</summary> | ||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | ||
private string DebuggerDisplay | ||
{ | ||
get | ||
{ | ||
StringBuilder sb = new(Name); | ||
|
||
if (Description is string description && !string.IsNullOrEmpty(description)) | ||
{ | ||
_ = sb.Append(" (").Append(description).Append(')'); | ||
} | ||
|
||
foreach (var entry in AdditionalProperties) | ||
{ | ||
_ = sb.Append(", ").Append(entry.Key).Append(" = ").Append(entry.Value); | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Libraries/Microsoft.Extensions.AI.Abstractions/CodeInterpreterTool.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,17 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.Extensions.AI; | ||
|
||
/// <summary>Represents a tool that can be specified to an AI service to enable it to execute code it generates.</summary> | ||
/// <remarks> | ||
/// This tool does not itself implement code interpration. It is a marker that can be used to inform a service | ||
/// that the service is allowed to execute its generated code if the service is capable of doing so. | ||
/// </remarks> | ||
public class CodeInterpreterTool : AITool | ||
{ | ||
/// <summary>Initializes a new instance of the <see cref="CodeInterpreterTool"/> class.</summary> | ||
public CodeInterpreterTool() | ||
{ | ||
} | ||
} | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
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
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
21 changes: 21 additions & 0 deletions
21
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/AIToolTests.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,21 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.Extensions.AI; | ||
|
||
public class AIToolTests | ||
{ | ||
[Fact] | ||
public void Constructor_Roundtrips() | ||
{ | ||
DerivedAITool tool = new(); | ||
Assert.Equal(nameof(DerivedAITool), tool.Name); | ||
Assert.Equal(nameof(DerivedAITool), tool.ToString()); | ||
Assert.Empty(tool.Description); | ||
Assert.Empty(tool.AdditionalProperties); | ||
} | ||
|
||
private sealed class DerivedAITool : AITool; | ||
} |
19 changes: 19 additions & 0 deletions
19
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/CodeInterpreterToolTests.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,19 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.Extensions.AI; | ||
|
||
public class CodeInterpreterToolTests | ||
{ | ||
[Fact] | ||
public void Constructor_Roundtrips() | ||
{ | ||
var tool = new CodeInterpreterTool(); | ||
Assert.Equal(nameof(CodeInterpreterTool), tool.Name); | ||
Assert.Empty(tool.Description); | ||
Assert.Empty(tool.AdditionalProperties); | ||
Assert.Equal(nameof(CodeInterpreterTool), tool.ToString()); | ||
} | ||
} |
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.