dotnet / dotnet/extensions

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

Open
#6,011 6 comments 0 reactions 0 assignees View on GitHub
api-suggestion area-ai
Dominant language
C#
Stars
3.2k
Forks
894
Avg merge
1d 12h
Merged PRs (30d)
23

Description

### 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](https://github.com/tryAGI/LangChain) and [Google_GenerativeAI](https://github.com/gunpal5/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

```csharp
//Single Function
[GenAIFunction(Description = "Single function Description")]
public async Task SingleAIFunction( [Description("My param1 description")] int param1, Description[("My param2 description")] MyParamClass param2)
{
}

```

```csharp
//Interface with multiple functions
[GenAITool]
public interface IMyToolInterface
{
[Description("My function 1 Description")]
public async Task MyToolFunction( [Description("My param1 description")] int param1, Description[("My param2 description")] MyParamClass param2)
{
}

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

### API Usage

```csharp
// Single Function AsTool() Generated by Code Generator
var tool = SingleAIFunction.AsTool()

```

```csharp
//multiple functions, AsTools() generated by code generator for interface implementations
var tools = MyToolImpl.AsTools();
```

### Alternative Designs

_No response_

### Risks

_No response_

Contributor guide

Open the contributing guide

Research direction

The proposal names no repository files, tests, or entry points; begin by locating the existing reflection-based function-calling implementation. Clarify the generator's attributes, generated AIFunction shape, and acceptance criteria with maintainers before implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
ai, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.