A programming framework for agentic AI
[.NET] Create tools from M.E.A.I AIFunctionFactory (#4041)
Xiaoyun Zhang5e0b677acc10
diff --git a/dotnet/AutoGen.sln b/dotnet/AutoGen.slnindex 291cb4846..4f82713b5 100644--- a/dotnet/AutoGen.sln+++ b/dotnet/AutoGen.sln@@ -68,6 +68,13 @@ EndProjectProject("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{243E768F-EA7D-4AF1-B625-0398440BB1AB}"ProjectSection(SolutionItems) = preProject.editorconfig = .editorconfig+ .gitattributes = .gitattributes+ .gitignore = .gitignore+ Directory.Build.props = Directory.Build.props+ Directory.Build.targets = Directory.Build.targets+ Directory.Packages.props = Directory.Packages.props+ global.json = global.json+ NuGet.config = NuGet.configspelling.dic = spelling.dicEndProjectSectionEndProject@@ -123,7 +130,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HelloAgent", "samples\HelloEndProjectProject("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AIModelClientHostingExtensions", "src\Microsoft.AutoGen\Extensions\AIModelClientHostingExtensions\AIModelClientHostingExtensions.csproj", "{97550E87-48C6-4EBF-85E1-413ABAE9DBFD}"EndProject-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.Agents.Tests", "Microsoft.AutoGen.Agents.Tests\Microsoft.AutoGen.Agents.Tests.csproj", "{CF4C92BD-28AE-4B8F-B173-601004AEC9BF}"+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AutoGen.Agents.Tests", "Microsoft.AutoGen.Agents.Tests\Microsoft.AutoGen.Agents.Tests.csproj", "{CF4C92BD-28AE-4B8F-B173-601004AEC9BF}"EndProjectProject("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{686480D7-8FEC-4ED3-9C5D-CEBE1057A7ED}"EndProjectdiff --git a/dotnet/Directory.Build.props b/dotnet/Directory.Build.propsindex bb78c84d1..ae30d2c48 100644--- a/dotnet/Directory.Build.props+++ b/dotnet/Directory.Build.props@@ -32,10 +32,6 @@<NoWarn>$(NoWarn);CA1829</NoWarn></PropertyGroup>- <ItemGroup>- <PackageReference Include="System.Text.Json" />- </ItemGroup>-<ItemGroup Condition="'$(IsTestProject)' == 'true'"><PackageReference Include="ApprovalTests" /><PackageReference Include="FluentAssertions" />diff --git a/dotnet/samples/AutoGen.BasicSamples/AutoGen.BasicSample.csproj b/dotnet/samples/AutoGen.BasicSamples/AutoGen.BasicSample.csprojindex 460c95f37..9e510ffa2 100644--- a/dotnet/samples/AutoGen.BasicSamples/AutoGen.BasicSample.csproj+++ b/dotnet/samples/AutoGen.BasicSamples/AutoGen.BasicSample.csproj@@ -15,5 +15,6 @@<ProjectReference Include="..\..\src\AutoGen\AutoGen.csproj" /><PackageReference Include="FluentAssertions" /><PackageReference Include="Microsoft.SemanticKernel.Plugins.Web" />+ <PackageReference Include="Microsoft.Extensions.AI" /></ItemGroup></Project>diff --git a/dotnet/samples/AutoGen.BasicSamples/Example03_Agent_FunctionCall.cs b/dotnet/samples/AutoGen.BasicSamples/Example03_Agent_FunctionCall.csindex ca05a42ca..c10870803 100644--- a/dotnet/samples/AutoGen.BasicSamples/Example03_Agent_FunctionCall.cs+++ b/dotnet/samples/AutoGen.BasicSamples/Example03_Agent_FunctionCall.cs@@ -6,6 +6,7 @@ using AutoGen.Core;using AutoGen.OpenAI;using AutoGen.OpenAI.Extension;using FluentAssertions;+using Microsoft.Extensions.AI;/// <summary>/// This example shows how to add type-safe function call to an agent.@@ -37,13 +38,20 @@ public partial class Example03_Agent_FunctionCall/// </summary>/// <param name="price">price, should be an integer</param>/// <param name="taxRate">tax rate, should be in range (0, 1)</param>- [FunctionAttribute]+ [Function]public async Task<string> CalculateTax(int price, float taxRate){return $"tax is {price * taxRate}";}- public static async Task RunAsync()+ /// <summary>+ /// This example shows how to add type-safe function call using AutoGen.SourceGenerator.+ /// The SourceGenerator will automatically generate FunctionDefinition and FunctionCallWrapper during compiling time.+ ///+ /// For adding type-safe function call from M.E.A.I tools, please refer to <see cref="ToolCallWithMEAITools"/>.+ /// </summary>+ /// <returns></returns>+ public static async Task ToolCallWithSourceGenerator(){var instance = new Example03_Agent_FunctionCall();var gpt4o = LLMConfiguration.GetOpenAIGPT4o_mini();@@ -101,4 +109,60 @@ public partial class Example03_Agent_FunctionCall// send aggregate message back to llm to get the final resultvar finalResult = await agent.SendAsync(calculateTaxes);}++ /// <summary>+ /// This example shows how to add type-safe function call from M.E.A.I tools.+ ///+ /// For adding type-safe function call from source generator, please refer to <see cref="ToolCallWithSourceGenerator"/>.+ /// </summary>+ public static async Task ToolCallWithMEAITools()+ {+ var gpt4o = LLMConfiguration.GetOpenAIGPT4o_mini();+ var instance = new Example03_Agent_FunctionCall();++ AIFunction[] tools = [+ AIFunctionFactory.Create(instance.UpperCase),+ AIFunctionFactory.Create(instance.ConcatString),+ AIFunctionFactory.Create(instance.CalculateTax),+ ];++ var toolCallMiddleware = new FunctionCallMiddleware(tools);++ var agent = new OpenAIChatAgent(+ chatClient: gpt4o,+ name: "agent",+ systemMessage: "You are a helpful AI assistant")+ .RegisterMessageConnector()+ .RegisterStreamingMiddleware(toolCallMiddleware)+ .RegisterPrintMessage();++ // talk to the assistant agent+ var upperCase = await agent.SendAsync("convert to upper case: hello world");+ upperCase.GetContent()?.Should().Be("HELLO WORLD");+ upperCase.Should().BeOfType<ToolCallAggregateMessage>();+ upperCase.GetToolCalls().Should().HaveCount(1);+ upperCase.GetToolCalls().First().FunctionName.Should().Be(nameof(UpperCase));++ var concatString = await agent.SendAsync("concatenate strings: a, b, c, d, e");+ concatString.GetContent()?.Should().Be("a b c d e");+ concatString.Should().BeOfType<ToolCallAggregateMessage>();+ concatString.GetToolCalls().Should().HaveCount(1);+ concatString.GetToolCalls().First().FunctionName.Should().Be(nameof(ConcatString));++ var calculateTax = await agent.SendAsync("calculate tax: 100, 0.1");+ calculateTax.GetContent().Should().Be("tax is 10");+ calculateTax.Should().BeOfType<ToolCallAggregateMessage>();+ calculateTax.GetToolCalls().Should().HaveCount(1);+ calculateTax.GetToolCalls().First().FunctionName.Should().Be(nameof(CalculateTax));++ // parallel function calls+ var calculateTaxes = await agent.SendAsync("calculate tax: 100, 0.1; calculate tax: 200, 0.2");+ calculateTaxes.GetContent().Should().Be("tax is 10\ntax is 40"); // "tax is 10\n tax is 40+ calculateTaxes.Should().BeOfType<ToolCallAggregateMessage>();+ calculateTaxes.GetToolCalls().Should().HaveCount(2);+ calculateTaxes.GetToolCalls().First().FunctionName.Should().Be(nameof(CalculateTax));++ // send aggregate message back to llm to get the final result+ var finalResult = await agent.SendAsync(calculateTaxes);+ }}diff --git a/dotnet/samples/AutoGen.BasicSamples/Program.cs b/dotnet/samples/AutoGen.BasicSamples/Program.csindex 3a2edbb58..16a79e75c 100644--- a/dotnet/samples/AutoGen.BasicSamples/Program.cs+++ b/dotnet/samples/AutoGen.BasicSamples/Program.cs@@ -11,7 +11,8 @@ var allSamples = new List<(string, Func<Task>)>// When a new sample is created please add them to the allSamples collection("Assistant Agent", Example01_AssistantAgent.RunAsync),("Two-agent Math Chat", Example02_TwoAgent_MathChat.RunAsync),- ("Agent Function Call", Example03_Agent_FunctionCall.RunAsync),+ ("Agent Function Call With Source Generator", Example03_Agent_FunctionCall.ToolCallWithSourceGenerator),+ ("Agent Function Call With M.E.A.I AI Functions", Example03_Agent_FunctionCall.ToolCallWithMEAITools),("Dynamic Group Chat Coding Task", Example04_Dynamic_GroupChat_Coding_Task.RunAsync),("DALL-E and GPT4v", Example05_Dalle_And_GPT4V.RunAsync),("User Proxy Agent", Example06_UserProxyAgent.RunAsync),diff --git a/dotnet/src/AutoGen.Core/AutoGen.Core.csproj b/dotnet/src/AutoGen.Core/AutoGen.Core.csprojindex c34c03af2..f46d48dc8 100644--- a/dotnet/src/AutoGen.Core/AutoGen.Core.csproj+++ b/dotnet/src/AutoGen.Core/AutoGen.Core.csproj@@ -17,6 +17,7 @@<ItemGroup><PackageReference Include="JsonSchema.Net.Generation" /><PackageReference Include="System.Memory.Data" />+ <PackageReference Include="Microsoft.Extensions.AI.Abstractions" /></ItemGroup><ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">diff --git a/dotnet/src/AutoGen.Core/Function/FunctionAttribute.cs b/dotnet/src/AutoGen.Core/Function/FunctionAttribute.csindex bb37f1cb2..9418dc7fd 100644--- a/dotnet/src/AutoGen.Core/Function/FunctionAttribute.cs+++ b/dotnet/src/AutoGen.Core/Function/FunctionAttribute.cs@@ -3,6 +3,9 @@using System;using System.Collections.Generic;+using System.Linq;+using System.Text.Json.Serialization;+using Microsoft.Extensions.AI;namespace AutoGen.Core;@@ -22,6 +25,10 @@ public class FunctionAttribute : Attributepublic class FunctionContract{+ private const string NamespaceKey = nameof(Namespace);++ private const string ClassNameKey = nameof(ClassName);+/// <summary>/// The namespace of the function./// </summary>@@ -52,6 +59,7 @@ public class FunctionContract/// <summary>/// The return type of the function./// </summary>+ [JsonIgnore]public Type? ReturnType { get; set; }/// <summary>@@ -60,6 +68,39 @@ public class FunctionContract/// Otherwise, the description will be null./// </summary>public string? ReturnDescription { get; set; }++ public static implicit operator FunctionContract(AIFunctionMetadata metadata)+ {+ return new FunctionContract+ {+ Namespace = metadata.AdditionalProperties.ContainsKey(NamespaceKey) ? metadata.AdditionalProperties[NamespaceKey] as string : null,+ ClassName = metadata.AdditionalProperties.ContainsKey(ClassNameKey) ? metadata.AdditionalProperties[ClassNameKey] as string : null,+ Name = metadata.Name,+ Description = metadata.Description,+ Parameters = metadata.Parameters?.Select(p => (FunctionParameterContract)p).ToList(),+ ReturnType = metadata.ReturnParameter.ParameterType,+ ReturnDescription = metadata.ReturnParameter.Description,+ };+ }++ public static implicit operator AIFunctionMetadata(FunctionContract contract)+ {+ return new AIFunctionMetadata(contract.Name)+ {+ Description = contract.Description,+ ReturnParameter = new AIFunctionReturnParameterMetadata()+ {+ Description = contract.ReturnDescription,+ ParameterType = contract.ReturnType,+ },+ AdditionalProperties = new Dictionary<string, object?>+ {+ [NamespaceKey] = contract.Namespace,+ [ClassNameKey] = contract.ClassName,+ },+ Parameters = [.. contract.Parameters?.Select(p => (AIFunctionParameterMetadata)p)],+ };+ }}public class FunctionParameterContract@@ -79,6 +120,7 @@ public class FunctionParameterContract/// <summary>/// The type of the parameter./// </summary>+ [JsonIgnore]public Type? ParameterType { get; set; }/// <summary>@@ -90,4 +132,29 @@ public class FunctionParameterContract/// The default value of the parameter./// </summary>public object? DefaultValue { get; set; }++ // convert to/from FunctionParameterMetadata+ public static implicit operator FunctionParameterContract(AIFunctionParameterMetadata metadata)+ {+ return new FunctionParameterContract+ {+ Name = metadata.Name,+ Description = metadata.Description,+ ParameterType = metadata.ParameterType,+ IsRequired = metadata.IsRequired,+ DefaultValue = metadata.DefaultValue,+ };+ }++ public static implicit operator AIFunctionParameterMetadata(FunctionParameterContract contract)+ {+ return new AIFunctionParameterMetadata(contract.Name!)+ {+ DefaultValue = contract.DefaultValue,+ Description = contract.Description,+ IsRequired = contract.IsRequired,+ ParameterType = contract.ParameterType,+ HasDefaultValue = contract.DefaultValue != null,+ };+ }}diff --git a/dotnet/src/AutoGen.Core/Middleware/FunctionCallMiddleware.cs b/dotnet/src/AutoGen.Core/Middleware/FunctionCallMiddleware.csindex 21461834d..266155316 100644--- a/dotnet/src/AutoGen.Core/Middleware/FunctionCallMiddleware.cs+++ b/dotnet/src/AutoGen.Core/Middleware/FunctionCallMiddleware.cs@@ -5,8 +5,10 @@ using System;using System.Collections.Generic;using System.Linq;using System.Runtime.CompilerServices;+using System.Text.Json;using System.Threading;using System.Threading.Tasks;+using Microsoft.Extensions.AI;namespace AutoGen.Core;@@ -43,6 +45,19 @@ public class FunctionCallMiddleware : IStreamingMiddlewarethis.functionMap = functionMap;}+ /// <summary>+ /// Create a new instance of <see cref="FunctionCallMiddleware"/> with a list of <see cref="AIFunction"/>.+ /// </summary>+ /// <param name="functions">function list</param>+ /// <param name="name">optional middleware name. If not provided, the class name <see cref="FunctionCallMiddleware"/> will be used.</param>+ public FunctionCallMiddleware(IEnumerable<AIFunction> functions, string? name = null)+ {+ this.Name = name ?? nameof(FunctionCallMiddleware);+ this.functions = functions.Select(f => (FunctionContract)f.Metadata).ToArray();++ this.functionMap = functions.Select(f => (f.Metadata.Name, this.AIToolInvokeWrapper(f.InvokeAsync))).ToDictionary(f => f.Name, f => f.Item2);+ }+public string? Name { get; }public async Task<IMessage> InvokeAsync(MiddlewareContext context, IAgent agent, CancellationToken cancellationToken = default)@@ -173,4 +188,20 @@ public class FunctionCallMiddleware : IStreamingMiddlewarereturn toolCallMsg;}}++ private Func<string, Task<string>> AIToolInvokeWrapper(Func<IEnumerable<KeyValuePair<string, object?>>?, CancellationToken, Task<object?>> lambda)+ {+ return async (string args) =>+ {+ var arguments = JsonSerializer.Deserialize<Dictionary<string, object?>>(args);+ var result = await lambda(arguments, CancellationToken.None);++ return result switch+ {+ string s => s,+ JsonElement e => e.ToString(),+ _ => JsonSerializer.Serialize(result),+ };+ };+ }}diff --git a/dotnet/test/AutoGen.Tests/AutoGen.Tests.csproj b/dotnet/test/AutoGen.Tests/AutoGen.Tests.csprojindex 12c31e1a4..248a9e29b 100644--- a/dotnet/test/AutoGen.Tests/AutoGen.Tests.csproj+++ b/dotnet/test/AutoGen.Tests/AutoGen.Tests.csproj@@ -13,6 +13,8 @@<ProjectReference Include="..\..\src\AutoGen.SourceGenerator\AutoGen.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /><ProjectReference Include="..\..\src\AutoGen\AutoGen.csproj" /><ProjectReference Include="..\AutoGen.Test.Share\AutoGen.Tests.Share.csproj" />+ <PackageReference Include="Microsoft.Extensions.AI" />+ <PackageReference Include="System.Text.Json" VersionOverride="9.0.0-rc.2.24473.5" /></ItemGroup><ItemGroup>diff --git a/dotnet/test/AutoGen.Tests/BasicSampleTest.cs b/dotnet/test/AutoGen.Tests/BasicSampleTest.csindex df02bb3dc..5cf470403 100644--- a/dotnet/test/AutoGen.Tests/BasicSampleTest.cs+++ b/dotnet/test/AutoGen.Tests/BasicSampleTest.cs@@ -34,7 +34,8 @@ public class BasicSampleTest[ApiKeyFact("OPENAI_API_KEY")]public async Task AgentFunctionCallTestAsync(){- await Example03_Agent_FunctionCall.RunAsync();+ await Example03_Agent_FunctionCall.ToolCallWithSourceGenerator();+ await Example03_Agent_FunctionCall.ToolCallWithMEAITools();}[ApiKeyFact("MISTRAL_API_KEY")]diff --git a/dotnet/test/AutoGen.Tests/Function/ApprovalTests/FunctionTests.CreateGetWeatherFunctionFromAIFunctionFactoryAsync.approved.txt b/dotnet/test/AutoGen.Tests/Function/ApprovalTests/FunctionTests.CreateGetWeatherFunctionFromAIFunctionFactoryAsync.approved.txtnew file mode 100644index 000000000..f57e0203e--- /dev/null+++ b/dotnet/test/AutoGen.Tests/Function/ApprovalTests/FunctionTests.CreateGetWeatherFunctionFromAIFunctionFactoryAsync.approved.txt@@ -0,0 +1,76 @@+[+ {+ "Kind": 0,+ "FunctionName": "GetWeather",+ "FunctionDescription": "get weather",+ "FunctionParameters": {+ "type": "object",+ "properties": {+ "city": {+ "type": "string"+ },+ "date": {+ "type": "string"+ }+ },+ "required": [+ "city"+ ]+ }+ },+ {+ "Kind": 0,+ "FunctionName": "GetWeatherStatic",+ "FunctionDescription": "get weather from static method",+ "FunctionParameters": {+ "type": "object",+ "properties": {+ "city": {+ "type": "string"+ },+ "date": {+ "type": "array",+ "items": {+ "type": "string"+ }+ }+ },+ "required": [+ "city",+ "date"+ ]+ }+ },+ {+ "Kind": 0,+ "FunctionName": "GetWeather",+ "FunctionDescription": "get weather from async method",+ "FunctionParameters": {+ "type": "object",+ "properties": {+ "city": {+ "type": "string"+ }+ },+ "required": [+ "city"+ ]+ }+ },+ {+ "Kind": 0,+ "FunctionName": "GetWeatherAsyncStatic",+ "FunctionDescription": "get weather from async static method",+ "FunctionParameters": {+ "type": "object",+ "properties": {+ "city": {+ "type": "string"+ }+ },+ "required": [+ "city"+ ]+ }+ }+]\ No newline at end of filediff --git a/dotnet/test/AutoGen.Tests/Function/FunctionTests.cs b/dotnet/test/AutoGen.Tests/Function/FunctionTests.csnew file mode 100644index 000000000..64abb293b--- /dev/null+++ b/dotnet/test/AutoGen.Tests/Function/FunctionTests.cs@@ -0,0 +1,82 @@+// Copyright (c) Microsoft Corporation. All rights reserved.+// FunctionTests.cs++using System;+using System.ComponentModel;+using System.Linq;+using System.Text.Json;+using System.Text.Json.Serialization;+using System.Threading.Tasks;+using ApprovalTests;+using ApprovalTests.Namers;+using ApprovalTests.Reporters;+using AutoGen.OpenAI.Extension;+using FluentAssertions;+using Microsoft.Extensions.AI;+using Xunit;++namespace AutoGen.Tests.Function;+public class FunctionTests+{+ private readonly JsonSerializerOptions _jsonSerializerOptions = new() { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull };+ [Description("get weather")]+ public string GetWeather(string city, string date = "today")+ {+ return $"The weather in {city} is sunny.";+ }++ [Description("get weather from static method")]+ [return: Description("weather information")]+ public static string GetWeatherStatic(string city, string[] date)+ {+ return $"The weather in {city} is sunny.";+ }++ [Description("get weather from async method")]+ public async Task<string> GetWeatherAsync(string city)+ {+ await Task.Delay(100);+ return $"The weather in {city} is sunny.";+ }++ [Description("get weather from async static method")]+ public static async Task<string> GetWeatherAsyncStatic(string city)+ {+ await Task.Delay(100);+ return $"The weather in {city} is sunny.";+ }++ [Fact]+ [UseReporter(typeof(DiffReporter))]+ [UseApprovalSubdirectory("ApprovalTests")]+ public async Task CreateGetWeatherFunctionFromAIFunctionFactoryAsync()+ {+ Delegate[] availableDelegates = [+ GetWeather,+ GetWeatherStatic,+ GetWeatherAsync,+ GetWeatherAsyncStatic,+ ];++ var functionContracts = availableDelegates.Select(function => (FunctionContract)AIFunctionFactory.Create(function).Metadata).ToList();++ // Verify the function contracts+ functionContracts.Should().HaveCount(4);++ var openAIToolContracts = functionContracts.Select(f =>+ {+ var tool = f.ToChatTool();++ return new+ {+ tool.Kind,+ tool.FunctionName,+ tool.FunctionDescription,+ FunctionParameters = tool.FunctionParameters.ToObjectFromJson<object>(),+ };+ });++ var json = JsonSerializer.Serialize(openAIToolContracts, _jsonSerializerOptions);+ Approvals.Verify(json);+ }+}diff --git a/dotnet/test/AutoGen.Tests/MiddlewareTest.cs b/dotnet/test/AutoGen.Tests/MiddlewareTest.csindex d98fa14ec..61691b225 100644--- a/dotnet/test/AutoGen.Tests/MiddlewareTest.cs+++ b/dotnet/test/AutoGen.Tests/MiddlewareTest.cs@@ -7,6 +7,7 @@ using System.Linq;using System.Text.Json;using System.Threading.Tasks;using FluentAssertions;+using Microsoft.Extensions.AI;using Xunit;namespace AutoGen.Tests;@@ -72,7 +73,7 @@ public partial class MiddlewareTestvar agent = new EchoAgent("echo");var args = new EchoSchema { message = "hello" };var argsJson = JsonSerializer.Serialize(args) ?? throw new InvalidOperationException("Failed to serialize args");- var functionCall = new ToolCall("echo", argsJson);+ var functionCall = new ToolCall("Echo", argsJson);var functionCallAgent = agent.RegisterMiddleware(async (messages, options, agent, ct) =>{if (options?.Functions is null)@@ -86,7 +87,7 @@ public partial class MiddlewareTest// test 1// middleware should invoke function call if the message is a function call messagevar mw = new FunctionCallMiddleware(- functionMap: new Dictionary<string, Func<string, Task<string>>> { { "echo", EchoWrapper } });+ functionMap: new Dictionary<string, Func<string, Task<string>>> { { "Echo", EchoWrapper } });var testAgent = agent.RegisterMiddleware(mw);var functionCallMessage = new ToolCallMessage(functionCall.FunctionName, functionCall.FunctionArguments, from: "user");@@ -96,30 +97,38 @@ public partial class MiddlewareTestreply.From.Should().Be("echo");// test 2+ // middleware should work with AIFunction from M.E.A.I+ var getWeatherTool = AIFunctionFactory.Create(this.Echo);+ mw = new FunctionCallMiddleware([getWeatherTool]);+ testAgent = agent.RegisterMiddleware(mw);+ reply = await testAgent.SendAsync(functionCallMessage);+ reply.GetContent()!.Should().Be("[FUNC] hello");++ // test 3// middleware should invoke function call if agent reply is a function call messagemw = new FunctionCallMiddleware(functions: [this.EchoFunctionContract],- functionMap: new Dictionary<string, Func<string, Task<string>>> { { "echo", EchoWrapper } });+ functionMap: new Dictionary<string, Func<string, Task<string>>> { { "Echo", EchoWrapper } });testAgent = functionCallAgent.RegisterMiddleware(mw);reply = await testAgent.SendAsync("hello");reply.GetContent()!.Should().Be("[FUNC] hello");reply.From.Should().Be("echo");- // test 3+ // test 4// middleware should return original reply if the reply from agent is not a function call messagemw = new FunctionCallMiddleware(- functionMap: new Dictionary<string, Func<string, Task<string>>> { { "echo", EchoWrapper } });+ functionMap: new Dictionary<string, Func<string, Task<string>>> { { "Echo", EchoWrapper } });testAgent = agent.RegisterMiddleware(mw);reply = await testAgent.SendAsync("hello");reply.GetContent()!.Should().Be("hello");reply.From.Should().Be("echo");- // test 4+ // test 5// middleware should return an error message if the function name is not available when invoking the function from previous agent replymw = new FunctionCallMiddleware(- functionMap: new Dictionary<string, Func<string, Task<string>>> { { "echo2", EchoWrapper } });+ functionMap: new Dictionary<string, Func<string, Task<string>>> { { "Echo2", EchoWrapper } });testAgent = agent.RegisterMiddleware(mw);reply = await testAgent.SendAsync(functionCallMessage);- reply.GetContent()!.Should().Be("Function echo is not available. Available functions are: echo2");+ reply.GetContent()!.Should().Be("Function Echo is not available. Available functions are: Echo2");}}diff --git a/dotnet/website/articles/Create-type-safe-function-call.md b/dotnet/website/articles/Create-type-safe-function-call.mdindex a12869661..059a912d0 100644--- a/dotnet/website/articles/Create-type-safe-function-call.md+++ b/dotnet/website/articles/Create-type-safe-function-call.md@@ -1,4 +1,4 @@-## Type-safe function call+## Create type-safe function call using AutoGen.SourceGenerator`AutoGen` provides a source generator to easness the trouble of manually craft function definition and function call wrapper from a function. To use this feature, simply add the `AutoGen.SourceGenerator` package to your project and decorate your function with @AutoGen.Core.FunctionAttribute.