Skip to content

[API Proposal]: A Code Generator For Function Calling in M.E.A.I. #6011

@gunpal5

Description

@gunpal5

Background and motivation

Hello,

Based on my analysis of the M.E.A.I. source code, the current function calling mechanism utilizes Reflection to generate method schemas and invoke functions. While AOT works with Reflection, it presents potential future compatibility risks as the .NET ecosystem evolves. Therefore, I propose a code generator for AI Functions. This would create JSON schemas and invocation code at compile time, leading to more explicit and robust code. Developers would simply annotate their functions, classes, or interfaces with a designated attribute.

The underlying AI specification and mechanism will remain the same. The code generator will generate a separate class inherited from AIFunction.

Mr. Konstantin S. and I have successfully employed a similar code generation approach in our repositories for LangChain.net and Google_GenerativeAI SDK. Therefore, we possess the necessary experience and background to develop this code generator.

I would like to know about your views and opinions about this.

API Proposal

//Single Function
[GenAIFunction(Description = "Single function Description")]
public async Task<MyResponse> SingleAIFunction( [Description("My param1 description")] int param1, Description[("My param2 description")] MyParamClass param2)
{
}
//Interface with multiple functions
[GenAITool]
public interface IMyToolInterface
{
        [Description("My function 1 Description")]
        public async Task<MyResponse> MyToolFunction( [Description("My param1 description")] int param1, Description[("My param2 description")] MyParamClass param2)
        {
        }

        [Description("My function 2 Description")]
        public async Task<MyResponse> MyToolFunction2( [Description("My param1 description")] int param1, Description[("My param2 description")] MyParamClass param2)
        {
        }
}

API Usage

// Single Function AsTool() Generated by Code Generator
var tool = SingleAIFunction.AsTool()
//multiple functions, AsTools() generated by code generator for interface implementations
var tools = MyToolImpl.AsTools();

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions