Skip to content

Commit 8efc6c6

Browse files
committed
Add marker AITool for enabling code interpreters
Various AI services (Gemini, Bedrock, OpenAI, etc.) now support server-side code interpreting, where the model can generate code it can then execute in a sandbox. For good reason, they all require opting in. We can model that well as an AITool-derived type.
1 parent 89b0d7e commit 8efc6c6

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.Extensions.AI;
5+
6+
/// <summary>Represents a tool that can be specified to an AI service to enable it to execute code it generates.</summary>
7+
/// <remarks>
8+
/// This tool does not itself implement code interpration. It is a marker that can be used to inform a service
9+
/// that the service is allowed to execute its generated code if the service is capable of doing so.
10+
/// </remarks>
11+
public class CodeInterpreterTool : AITool
12+
{
13+
/// <summary>Initializes a new instance of the <see cref="CodeInterpreterTool"/> class.</summary>
14+
public CodeInterpreterTool()
15+
{
16+
}
17+
18+
/// <summary>Gets or sets additional properties that impact the behavior of the tool.</summary>
19+
public AdditionalPropertiesDictionary? AdditionalProperties { get; set; }
20+
}

src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIAssistantClient.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,27 +212,38 @@ private static (RunCreationOptions RunOptions, List<FunctionResultContent>? Tool
212212
{
213213
foreach (AITool tool in tools)
214214
{
215-
if (tool is AIFunction aiFunction)
215+
switch (tool)
216216
{
217-
bool? strict =
218-
aiFunction.Metadata.AdditionalProperties.TryGetValue("Strict", out object? strictObj) &&
219-
strictObj is bool strictValue ?
220-
strictValue : null;
221-
222-
var functionParameters = BinaryData.FromBytes(
223-
JsonSerializer.SerializeToUtf8Bytes(
224-
JsonSerializer.Deserialize(aiFunction.Metadata.Schema, OpenAIJsonContext.Default.OpenAIChatToolJson)!,
225-
OpenAIJsonContext.Default.OpenAIChatToolJson));
226-
227-
runOptions.ToolsOverride.Add(ToolDefinition.CreateFunction(aiFunction.Metadata.Name, aiFunction.Metadata.Description, functionParameters, strict));
217+
case AIFunction aiFunction:
218+
bool? strict =
219+
aiFunction.Metadata.AdditionalProperties.TryGetValue("Strict", out object? strictObj) &&
220+
strictObj is bool strictValue ?
221+
strictValue : null;
222+
223+
var functionParameters = BinaryData.FromBytes(
224+
JsonSerializer.SerializeToUtf8Bytes(
225+
JsonSerializer.Deserialize(aiFunction.Metadata.Schema, OpenAIJsonContext.Default.OpenAIChatToolJson)!,
226+
OpenAIJsonContext.Default.OpenAIChatToolJson));
227+
228+
runOptions.ToolsOverride.Add(ToolDefinition.CreateFunction(aiFunction.Metadata.Name, aiFunction.Metadata.Description, functionParameters, strict));
229+
break;
230+
231+
case CodeInterpreterTool:
232+
runOptions.ToolsOverride.Add(ToolDefinition.CreateCodeInterpreter());
233+
break;
228234
}
229235
}
230236
}
231237

232238
// Store the tool mode.
233239
switch (options.ToolMode)
234240
{
241+
case NoneChatToolMode:
242+
runOptions.ToolConstraint = ToolConstraint.None;
243+
break;
244+
235245
case AutoChatToolMode:
246+
case null:
236247
runOptions.ToolConstraint = ToolConstraint.Auto;
237248
break;
238249

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Xunit;
5+
6+
namespace Microsoft.Extensions.AI;
7+
8+
public class CodeInterpreterToolTests
9+
{
10+
[Fact]
11+
public void Constructor_Roundtrips()
12+
{
13+
var tool = new CodeInterpreterTool();
14+
Assert.Null(tool.AdditionalProperties);
15+
16+
var props = new AdditionalPropertiesDictionary();
17+
tool.AdditionalProperties = props;
18+
Assert.Same(props, tool.AdditionalProperties);
19+
}
20+
}

0 commit comments

Comments
 (0)