-
Notifications
You must be signed in to change notification settings - Fork 1.2k
.NET: Support InvokeFunctionTool for declarative workflows #4014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
df99a04
Initial Implementation of InvokeFunctionTool
peibekwe 6593880
Merge branch 'main' into peibekwe/declarative-invoke-function-tool
peibekwe c844e77
Merge branch 'main' into peibekwe/declarative-invoke-function-tool
peibekwe 3a05291
Added unit test for InvokeFunctionTool executor.
peibekwe 1879b41
Implemented unit and integration tests for InvokeFunctionTool.
peibekwe c6cfa14
Add sample for InvokeFunctionTool in declarative workflows.
peibekwe a6b92a9
Remove unused sample and updated comments.
peibekwe 042463a
Merge from main
peibekwe 670cd47
Updating to official OM release with InvokeFunctionTool
peibekwe e181782
Merge branch 'main' into peibekwe/declarative-invoke-function-tool
peibekwe fb306a8
Fix formatting issues.
peibekwe 70e6030
Updated PowerFx version
peibekwe 2c332a0
Update test fixture
peibekwe de40b09
Cleanup - Removed unused method in InvokeFunctionToolExecutor
peibekwe 001e67f
Update test based on PR feedback.
peibekwe d8e4d6f
Update based on PR comments
peibekwe 37d37df
Merge branch 'main' into peibekwe/declarative-invoke-function-tool
peibekwe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...samples/GettingStarted/Workflows/Declarative/InvokeFunctionTool/InvokeFunctionTool.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFrameworks>net10.0</TargetFrameworks> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <InjectIsExternalInitOnLegacy>true</InjectIsExternalInitOnLegacy> | ||
| <InjectSharedFoundryAgents>true</InjectSharedFoundryAgents> | ||
| <InjectSharedWorkflowsExecution>true</InjectSharedWorkflowsExecution> | ||
| <InjectSharedWorkflowsSettings>true</InjectSharedWorkflowsSettings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.Binder" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.Json" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" /> | ||
| <PackageReference Include="Microsoft.Extensions.DependencyInjection" /> | ||
| <PackageReference Include="Microsoft.Extensions.Logging" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI.Workflows.Declarative\Microsoft.Agents.AI.Workflows.Declarative.csproj" /> | ||
| <ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI.Workflows.Declarative.AzureAI\Microsoft.Agents.AI.Workflows.Declarative.AzureAI.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Include="InvokeFunctionTool.yaml"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
55 changes: 55 additions & 0 deletions
55
...t/samples/GettingStarted/Workflows/Declarative/InvokeFunctionTool/InvokeFunctionTool.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # | ||
| # This workflow demonstrates using InvokeFunctionTool to call functions directly | ||
| # from the workflow without going through an AI agent first. | ||
| # | ||
| # InvokeFunctionTool allows workflows to: | ||
| # - Pre-fetch data before calling an AI agent | ||
| # - Execute operations directly without AI involvement | ||
| # - Store function results in workflow variables for later use | ||
| # | ||
| # Example input: | ||
| # What are the specials in the menu? | ||
| # | ||
| kind: Workflow | ||
| trigger: | ||
|
|
||
| kind: OnConversationStart | ||
| id: workflow_invoke_function_tool_demo | ||
| actions: | ||
|
|
||
| # Invoke GetSpecials function to get today's specials directly from the workflow | ||
| - kind: InvokeFunctionTool | ||
| id: invoke_get_specials | ||
| conversationId: =System.ConversationId | ||
| requireApproval: true | ||
| functionName: GetSpecials | ||
| output: | ||
| autoSend: true | ||
| result: Local.Specials | ||
| messages: Local.FunctionMessage | ||
|
|
||
| # Display a message showing we retrieved the specials | ||
| - kind: SendMessage | ||
| id: show_specials_intro | ||
| message: "Today's specials have been retrieved. Here they are: {Local.Specials}" | ||
|
|
||
| # Now use an agent to format and present the specials to the user | ||
| - kind: InvokeAzureAgent | ||
| id: invoke_menu_agent | ||
| conversationId: =System.ConversationId | ||
| agent: | ||
| name: FunctionMenuAgent | ||
| input: | ||
| messages: =UserMessage("Please describe today's specials in an appealing way.") | ||
| output: | ||
| messages: Local.AgentResponse | ||
|
|
||
| # Allow the user to ask follow-up questions in a loop | ||
| - kind: InvokeAzureAgent | ||
| id: invoke_followup | ||
| conversationId: =System.ConversationId | ||
| agent: | ||
| name: FunctionMenuAgent | ||
| input: | ||
| externalLoop: | ||
| when: =Upper(System.LastMessage.Text) <> "EXIT" |
85 changes: 85 additions & 0 deletions
85
dotnet/samples/GettingStarted/Workflows/Declarative/InvokeFunctionTool/MenuPlugin.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.ComponentModel; | ||
|
|
||
| namespace Demo.Workflows.Declarative.InvokeFunctionTool; | ||
|
|
||
| #pragma warning disable CA1822 // Mark members as static | ||
|
|
||
| /// <summary> | ||
| /// Plugin providing menu-related functions that can be invoked directly by the workflow | ||
| /// using the InvokeFunctionTool action. | ||
| /// </summary> | ||
| public sealed class MenuPlugin | ||
| { | ||
| [Description("Provides a list items on the menu.")] | ||
| public MenuItem[] GetMenu() | ||
| { | ||
| return s_menuItems; | ||
| } | ||
|
|
||
| [Description("Provides a list of specials from the menu.")] | ||
| public MenuItem[] GetSpecials() | ||
| { | ||
| return [.. s_menuItems.Where(i => i.IsSpecial)]; | ||
| } | ||
|
|
||
| [Description("Provides the price of the requested menu item.")] | ||
| public float? GetItemPrice( | ||
| [Description("The name of the menu item.")] | ||
| string name) | ||
| { | ||
| return s_menuItems.FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase))?.Price; | ||
| } | ||
|
|
||
| private static readonly MenuItem[] s_menuItems = | ||
| [ | ||
| new() | ||
| { | ||
| Category = "Soup", | ||
| Name = "Clam Chowder", | ||
| Price = 4.95f, | ||
| IsSpecial = true, | ||
| }, | ||
| new() | ||
| { | ||
| Category = "Soup", | ||
| Name = "Tomato Soup", | ||
| Price = 4.95f, | ||
| IsSpecial = false, | ||
| }, | ||
| new() | ||
| { | ||
| Category = "Salad", | ||
| Name = "Cobb Salad", | ||
| Price = 9.99f, | ||
| }, | ||
| new() | ||
| { | ||
| Category = "Salad", | ||
| Name = "House Salad", | ||
| Price = 4.95f, | ||
| }, | ||
| new() | ||
| { | ||
| Category = "Drink", | ||
| Name = "Chai Tea", | ||
| Price = 2.95f, | ||
| IsSpecial = true, | ||
| }, | ||
| new() | ||
| { | ||
| Category = "Drink", | ||
| Name = "Soda", | ||
| Price = 1.95f, | ||
| }, | ||
| ]; | ||
|
|
||
| public sealed class MenuItem | ||
| { | ||
| public string Category { get; init; } = string.Empty; | ||
| public string Name { get; init; } = string.Empty; | ||
| public float Price { get; init; } | ||
| public bool IsSpecial { get; init; } | ||
| } | ||
| } |
88 changes: 88 additions & 0 deletions
88
dotnet/samples/GettingStarted/Workflows/Declarative/InvokeFunctionTool/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using Azure.AI.Projects; | ||
| using Azure.AI.Projects.OpenAI; | ||
| using Azure.Identity; | ||
| using Microsoft.Extensions.AI; | ||
| using Microsoft.Extensions.Configuration; | ||
| using OpenAI.Responses; | ||
| using Shared.Foundry; | ||
| using Shared.Workflows; | ||
|
|
||
| namespace Demo.Workflows.Declarative.InvokeFunctionTool; | ||
|
|
||
| /// <summary> | ||
| /// Demonstrate a workflow that uses InvokeFunctionTool to call functions directly | ||
| /// from the workflow without going through an AI agent first. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// The InvokeFunctionTool action allows workflows to invoke function tools directly, | ||
| /// enabling pre-fetching of data or executing operations before calling an AI agent. | ||
| /// See the README.md file in the parent folder (../README.md) for detailed | ||
| /// information about the configuration required to run this sample. | ||
| /// </remarks> | ||
| internal sealed class Program | ||
| { | ||
| public static async Task Main(string[] args) | ||
| { | ||
| // Initialize configuration | ||
| IConfiguration configuration = Application.InitializeConfig(); | ||
| Uri foundryEndpoint = new(configuration.GetValue(Application.Settings.FoundryEndpoint)); | ||
|
|
||
| // Create the menu plugin with functions that can be invoked directly by the workflow | ||
| MenuPlugin menuPlugin = new(); | ||
| AIFunction[] functions = | ||
| [ | ||
| AIFunctionFactory.Create(menuPlugin.GetMenu), | ||
| AIFunctionFactory.Create(menuPlugin.GetSpecials), | ||
| AIFunctionFactory.Create(menuPlugin.GetItemPrice), | ||
| ]; | ||
|
|
||
| // Ensure sample agent exists in Foundry | ||
| await CreateAgentAsync(foundryEndpoint, configuration); | ||
|
|
||
| // Get input from command line or console | ||
| string workflowInput = Application.GetInput(args); | ||
|
|
||
| // Create the workflow factory. | ||
| WorkflowFactory workflowFactory = new("InvokeFunctionTool.yaml", foundryEndpoint); | ||
|
|
||
| // Execute the workflow | ||
| WorkflowRunner runner = new(functions) { UseJsonCheckpoints = true }; | ||
| await runner.ExecuteAsync(workflowFactory.CreateWorkflow, workflowInput); | ||
| } | ||
|
|
||
| private static async Task CreateAgentAsync(Uri foundryEndpoint, IConfiguration configuration) | ||
| { | ||
| // WARNING: DefaultAzureCredential is convenient for development but requires careful consideration in production. | ||
| AIProjectClient aiProjectClient = new(foundryEndpoint, new DefaultAzureCredential()); | ||
|
|
||
| await aiProjectClient.CreateAgentAsync( | ||
| agentName: "FunctionMenuAgent", | ||
| agentDefinition: DefineMenuAgent(configuration, []), // Create Agent with no function tool in the definition. | ||
| agentDescription: "Provides information about the restaurant menu"); | ||
| } | ||
|
|
||
| private static PromptAgentDefinition DefineMenuAgent(IConfiguration configuration, AIFunction[] functions) | ||
| { | ||
| PromptAgentDefinition agentDefinition = | ||
| new(configuration.GetValue(Application.Settings.FoundryModelMini)) | ||
| { | ||
| Instructions = | ||
| """ | ||
| Answer the users questions about the menu. | ||
| Use the information provided in the conversation history to answer questions. | ||
| If the information is already available in the conversation, use it directly. | ||
| For questions or input that do not require searching the documentation, inform the | ||
| user that you can only answer questions about what's on the menu. | ||
| """ | ||
| }; | ||
|
|
||
| foreach (AIFunction function in functions) | ||
| { | ||
| agentDefinition.Tools.Add(function.AsOpenAIResponseTool()); | ||
| } | ||
|
|
||
| return agentDefinition; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.