.Net: Code interpreter tool abstraction and implementation examples (#110)

* Added code interpreter abstraction updates for OpenAI Assistants

* Updated Persistent Agents implementation based on latest changes in SDK

* Added code interpreter abstraction updates for Azure AI Persistent Agents

* Small note for OpenAI responses code interpreter

* Small update

* Fixes after merge

* Addressed PR feedback

* Small update

* Small fix

* Fix after merge
This commit is contained in:
Dmytro Struk
2025-07-01 07:59:51 -07:00
committed by GitHub
Unverified
parent 95738b0ac2
commit fbb0fdfe0d
10 changed files with 339 additions and 33 deletions
@@ -0,0 +1,47 @@
// Copyright (c) Microsoft. All rights reserved.
using Azure.AI.Agents.Persistent;
using GettingStarted.Tools.Abstractions;
using Microsoft.Extensions.AI;
namespace GettingStarted.Tools.Extensions;
/// <summary>
/// <see cref="ChatOptions"/> conversion for Azure AI Persistent Agent.
/// When abstraction is in place, this logic should go to Azure AI Persistent Agents SDK.
/// </summary>
internal static class AzureAIPersistentAgentChatOptionsExtensions
{
public static ChatOptions ToAzureAIPersistentAgentChatOptions(this ChatOptions chatOptions)
{
var fileIds = new List<string>();
foreach (var tool in chatOptions.Tools!)
{
if (tool is NewHostedCodeInterpreterTool codeInterpreterTool &&
codeInterpreterTool.FileIds is { Count: > 0 })
{
fileIds.AddRange(codeInterpreterTool.FileIds);
}
}
if (fileIds.Count > 0)
{
var toolResources = new Azure.AI.Agents.Persistent.ToolResources()
{
CodeInterpreter = new Azure.AI.Agents.Persistent.CodeInterpreterToolResource()
};
foreach (var fileId in fileIds)
{
toolResources.CodeInterpreter.FileIds.Add(fileId);
}
var threadAndRunOptions = new ThreadAndRunOptions { ToolResources = toolResources };
chatOptions.RawRepresentationFactory = (_) => threadAndRunOptions;
}
return chatOptions;
}
}
@@ -0,0 +1,49 @@
// Copyright (c) Microsoft. All rights reserved.
using GettingStarted.Tools.Abstractions;
using Microsoft.Extensions.AI;
using OpenAI.Assistants;
#pragma warning disable OPENAI001
namespace GettingStarted.Tools.Extensions;
/// <summary>
/// <see cref="ChatOptions"/> conversion for OpenAI Assistants.
/// When abstraction is in place, this logic should go to OpenAI Assistants SDK.
/// </summary>
internal static class OpenAIAssistantChatOptionsExtensions
{
public static ChatOptions ToOpenAIAssistantChatOptions(this ChatOptions chatOptions)
{
// File references can be added on message attachment level only and not on code interpreter tool definition level.
// Message attachment content should be non-empty.
var threadInitializationMessage = new ThreadInitializationMessage(MessageRole.User, [MessageContent.FromText("attachments")]);
var toolDefinitions = new List<ToolDefinition>();
foreach (var tool in chatOptions.Tools!)
{
if (tool is NewHostedCodeInterpreterTool codeInterpreterTool)
{
var codeInterpreterToolDefinition = new CodeInterpreterToolDefinition();
toolDefinitions.Add(codeInterpreterToolDefinition);
if (codeInterpreterTool.FileIds is { Count: > 0 })
{
foreach (var fileId in codeInterpreterTool.FileIds)
{
threadInitializationMessage.Attachments.Add(new(fileId, [codeInterpreterToolDefinition]));
}
}
}
}
var runCreationOptions = new RunCreationOptions();
runCreationOptions.AdditionalMessages.Add(threadInitializationMessage);
chatOptions.RawRepresentationFactory = (_) => runCreationOptions;
return chatOptions;
}
}