mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
92925a8bc7
* Draft * Nullable init * Complete * Consistency * Test fix * Typo * Comment * Updated * Fix identifier * Test fix * Comment typo * Better naming * Comment * Tweak comment
100 lines
4.4 KiB
C#
100 lines
4.4 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Agents.AI.Workflows.Declarative.Events;
|
|
using Microsoft.Agents.AI.Workflows.Declarative.Extensions;
|
|
using Microsoft.Agents.AI.Workflows.Declarative.IntegrationTests.Agents;
|
|
using Microsoft.Agents.AI.Workflows.Declarative.IntegrationTests.Framework;
|
|
using Microsoft.Agents.AI.Workflows.Declarative.Kit;
|
|
using Microsoft.Extensions.AI;
|
|
using Xunit.Abstractions;
|
|
|
|
namespace Microsoft.Agents.AI.Workflows.Declarative.IntegrationTests;
|
|
|
|
/// <summary>
|
|
/// Tests execution of workflow created by <see cref="DeclarativeWorkflowBuilder"/>.
|
|
/// </summary>
|
|
public sealed class FunctionCallingWorkflowTest(ITestOutputHelper output) : IntegrationTest(output)
|
|
{
|
|
[Fact]
|
|
public Task ValidateAutoInvokeAsync() =>
|
|
this.RunWorkflowAsync(autoInvoke: true, new MenuPlugin().GetTools());
|
|
|
|
[Fact]
|
|
public Task ValidateRequestInvokeAsync() =>
|
|
this.RunWorkflowAsync(autoInvoke: false, new MenuPlugin().GetTools());
|
|
|
|
private static string GetWorkflowPath(string workflowFileName) => Path.Combine(Environment.CurrentDirectory, "Workflows", workflowFileName);
|
|
|
|
private async Task RunWorkflowAsync(bool autoInvoke, params IEnumerable<AIFunction> functionTools)
|
|
{
|
|
string workflowPath = GetWorkflowPath("FunctionTool.yaml");
|
|
Dictionary<string, AIFunction> functionMap = autoInvoke ? [] : functionTools.ToDictionary(tool => tool.Name, tool => tool);
|
|
DeclarativeWorkflowOptions workflowOptions = await this.CreateOptionsAsync(externalConversation: false, autoInvoke ? functionTools : []);
|
|
Workflow workflow = DeclarativeWorkflowBuilder.Build<string>(workflowPath, workflowOptions);
|
|
|
|
WorkflowHarness harness = new(workflow, runId: Path.GetFileNameWithoutExtension(workflowPath));
|
|
WorkflowEvents workflowEvents = await harness.RunWorkflowAsync("hi!").ConfigureAwait(false);
|
|
int requestCount = (workflowEvents.InputEvents.Count + 1) / 2;
|
|
int responseCount = 0;
|
|
while (requestCount > responseCount)
|
|
{
|
|
Assert.False(autoInvoke);
|
|
|
|
RequestInfoEvent inputEvent = workflowEvents.InputEvents[workflowEvents.InputEvents.Count - 1];
|
|
AgentFunctionToolRequest? toolRequest = inputEvent.Request.Data.As<AgentFunctionToolRequest>();
|
|
Assert.NotNull(toolRequest);
|
|
|
|
List<(FunctionCallContent, AIFunction)> functionCalls = [];
|
|
foreach (FunctionCallContent functionCall in toolRequest.FunctionCalls)
|
|
{
|
|
this.Output.WriteLine($"TOOL REQUEST: {functionCall.Name}");
|
|
if (!functionMap.TryGetValue(functionCall.Name, out AIFunction? functionTool))
|
|
{
|
|
Assert.Fail($"TOOL FAILURE [{functionCall.Name}] - MISSING");
|
|
return;
|
|
}
|
|
functionCalls.Add((functionCall, functionTool));
|
|
}
|
|
|
|
IList<FunctionResultContent> functionResults = await InvokeToolsAsync(functionCalls);
|
|
|
|
++responseCount;
|
|
|
|
WorkflowEvents runEvents = await harness.ResumeAsync(AgentFunctionToolResponse.Create(toolRequest, functionResults)).ConfigureAwait(false);
|
|
workflowEvents = new WorkflowEvents([.. workflowEvents.Events, .. runEvents.Events]);
|
|
}
|
|
|
|
if (autoInvoke)
|
|
{
|
|
Assert.Empty(workflowEvents.InputEvents);
|
|
}
|
|
else
|
|
{
|
|
Assert.NotEmpty(workflowEvents.InputEvents);
|
|
}
|
|
|
|
Assert.Equal(autoInvoke ? 3 : 5, workflowEvents.AgentResponseEvents.Count);
|
|
Assert.All(workflowEvents.AgentResponseEvents, response => response.Response.Text.Contains("4.95"));
|
|
}
|
|
|
|
private static async ValueTask<IList<FunctionResultContent>> InvokeToolsAsync(IEnumerable<(FunctionCallContent, AIFunction)> functionCalls)
|
|
{
|
|
List<FunctionResultContent> results = [];
|
|
|
|
foreach ((FunctionCallContent functionCall, AIFunction functionTool) in functionCalls)
|
|
{
|
|
AIFunctionArguments? functionArguments = functionCall.Arguments is null ? null : new(functionCall.Arguments.NormalizePortableValues());
|
|
object? result = await functionTool.InvokeAsync(functionArguments).ConfigureAwait(false);
|
|
results.Add(new FunctionResultContent(functionCall.CallId, JsonSerializer.Serialize(result)));
|
|
}
|
|
|
|
return results;
|
|
}
|
|
}
|