Skip to content

[API Proposal]: Add Files resource to HostedCodeInterpreterTool abstraction. #6614

@rogerbarreto

Description

@rogerbarreto

Background and motivation

Currently the existing HostedCodeInterpreterTool type is lacking the concept of resources/files.

As we see an increasing adoption of the Code Interpretation Tooling support most notably by Agentic API's from different providers like below, seems reasonable to bring this to the abstraction level.

API Proposal

Start as immutable files/resources for minimal API public exposure.

Using the therm "File" which seems to be common so far.

public class HostedCodeInterpreterTool : AITool
{
    /// <summary>Initializes a new instance of the <see cref="HostedCodeInterpreterTool"/> class.</summary>
    public HostedCodeInterpreterTool(IReadOnlyCollection<AIContent> files)
    {
         Files = files;
    }

    public IReadOnlyCollection<AIContent> Files { get; }
}

Additional HostedFile Content

public class HostedFileContent : AIContent 
{
    // The Id is required for any hosted file
    public HostedFileContent (string id)
    {
        Id = id;
    }

    // Represents the fully qualified identifier for the hosted file to be found
    public string Id { get; set; }
}

API Usage

End user usage.

// Preparing to invoke a ChatClient 
var files = 
[
    new UrlContent("https://public-facing-resource"),
    new HostedFileContent("Hosted file UID"),
    new DataContent(fileByteArray, "text/csv")
];

chatOptions.Tools = [new HostedCodeInterpreterTool(files)];

Library implementers usage

// Within a ChatClient GetResponse/GetResponseStream impl.

// Can be multiple code tools, but for sample brevity, using single
var hciTool = chatOptinos.Tools.Single(tool => tool is HostedCodeInterpreterTool);
foreach (AIContent file in hciTool.Files)
{
    switch (file) 
    {
        case UrlContent urlContent:
            // providers logic
            break;
        case HostedFileContent hostedFileContent:
            // providers logic
            break;
        case DataContent dataContent:
            // providers logic
            break;
        case TextContent rawTextContent:
            // providers logic
            break;
    } 
}

Alternative Designs

We may consider files to be mutable in the tool if proven useful.

Risks

This leaves freedom for the caller to create different resources in the Files that may not be supported by the underline providers which may surprise the caller with a Exception or ignored depending on how Providers will handle unexpected/unsupported AIContents passed into the tool.Files.

Metadata

Metadata

Labels

api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-aiMicrosoft.Extensions.AI libraries

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions