-
Notifications
You must be signed in to change notification settings - Fork 1.6k
.NET: Add declarative HttpRequestAction sample #5572
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
10 commits
Select commit
Hold shift + click to select a range
9b16616
Add declarative HttpRequestAction support to workflows
peibekwe ed9a9e0
Clean up response body for diagnostics and fix tests.
peibekwe c9ff588
Merge branch 'main' into peibekwe/declarative-openapi-tool
peibekwe 1d659e2
Fix merge with main.
peibekwe c0e8dca
Merge branch 'main' into peibekwe/declarative-openapi-tool
peibekwe 3a3da73
Remove redundant fallback for request content headers.
peibekwe c2df1c0
Merge branch 'main' into peibekwe/declarative-openapi-tool
peibekwe 6b43ca1
Add declarative InvokeHttpRequest sample
peibekwe 64a2a5b
Fix solution file and update sample yaml comments
peibekwe 1e2d008
Add final newline to sample class to fix formatting failure
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
38 changes: 38 additions & 0 deletions
38
dotnet/samples/03-workflows/Declarative/InvokeHttpRequest/InvokeHttpRequest.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.Foundry\Microsoft.Agents.AI.Workflows.Declarative.Foundry.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Include="InvokeHttpRequest.yaml"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
76 changes: 76 additions & 0 deletions
76
dotnet/samples/03-workflows/Declarative/InvokeHttpRequest/InvokeHttpRequest.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,76 @@ | ||
| # | ||
| # This workflow demonstrates using HttpRequestAction to call a REST API directly | ||
| # from the workflow without going through an AI agent first. | ||
| # | ||
| # HttpRequestAction allows workflows to: | ||
| # - Fetch data from external HTTP endpoints | ||
| # - Store the parsed response in workflow variables for later use | ||
| # - Add the response body to the conversation so a downstream agent can | ||
| # answer questions based on it | ||
| # | ||
| # This sample fetches public metadata for the dotnet/runtime repository from | ||
| # the GitHub REST API (no authentication required) and uses an agent to | ||
| # answer follow-up questions about it. | ||
| # | ||
| # Example input: | ||
| # How many subscribers does the repository have? | ||
| # | ||
| kind: Workflow | ||
| trigger: | ||
|
|
||
| kind: OnConversationStart | ||
| id: workflow_invoke_http_request_demo | ||
| actions: | ||
|
|
||
| # Capture the original user message for input to the follow-up agent. | ||
| - kind: SetVariable | ||
| id: set_user_message | ||
| variable: Local.InputMessage | ||
| value: =System.LastMessage | ||
|
|
||
| # Set the repository org/name used to form the request URL. | ||
| - kind: SetVariable | ||
| id: set_repo_name | ||
| variable: Local.RepoName | ||
| value: microsoft/agent-framework | ||
|
|
||
| # Invoke the GitHub repo API. The response body is parsed into Local.RepoInfo | ||
| # and also added to the conversation (via conversationId) so the agent below | ||
| # can answer questions based on it. | ||
| - kind: HttpRequestAction | ||
| id: fetch_repo_info | ||
| conversationId: =System.ConversationId | ||
| method: GET | ||
| url: =Concatenate("https://api.github.com/repos/", Local.RepoName) | ||
| headers: | ||
| Accept: application/vnd.github+json | ||
| User-Agent: agent-framework-sample | ||
| response: Local.RepoInfo | ||
|
|
||
| # Display a confirmation message showing key fields from the parsed response. | ||
| - kind: SendMessage | ||
| id: show_repo_summary | ||
| message: "Fetched repo: visibility={Local.RepoInfo.visibility}, description={Local.RepoInfo.description}" | ||
|
|
||
| # Use the agent to summarize the repo using the conversation context. | ||
| - kind: InvokeAzureAgent | ||
| id: summarize_repo | ||
| conversationId: =System.ConversationId | ||
| agent: | ||
| name: GitHubRepoInfoAgent | ||
| input: | ||
| messages: =UserMessage("Please provide a brief summary of this GitHub repository based on the data already in the conversation.") | ||
| output: | ||
| autoSend: true | ||
| messages: Local.AgentResponse | ||
|
|
||
| # Allow the user to ask follow-up questions about the repo in a loop. | ||
| - kind: InvokeAzureAgent | ||
| id: invoke_followup | ||
| conversationId: =System.ConversationId | ||
| agent: | ||
| name: GitHubRepoInfoAgent | ||
| input: | ||
| messages: =Local.InputMessage | ||
| externalLoop: | ||
| when: =Upper(System.LastMessage.Text) <> "EXIT" | ||
95 changes: 95 additions & 0 deletions
95
dotnet/samples/03-workflows/Declarative/InvokeHttpRequest/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,95 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using Azure.AI.Projects; | ||
| using Azure.AI.Projects.Agents; | ||
| using Azure.Identity; | ||
| using Microsoft.Agents.AI.Workflows.Declarative; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Shared.Foundry; | ||
| using Shared.Workflows; | ||
|
|
||
| namespace Demo.Workflows.Declarative.InvokeHttpRequest; | ||
|
|
||
| /// <summary> | ||
| /// Demonstrates a workflow that uses HttpRequestAction to call a REST API | ||
| /// directly from the workflow. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// The HttpRequestAction allows workflows to issue HTTP requests and: | ||
| /// </para> | ||
| /// <list type="bullet"> | ||
| /// <item>Fetch data from external REST endpoints</item> | ||
| /// <item>Store the parsed response in workflow variables</item> | ||
| /// <item>Add the response body to the conversation so an agent can answer | ||
| /// questions based on it</item> | ||
| /// </list> | ||
| /// <para> | ||
| /// This sample fetches public metadata for the dotnet/runtime repository from | ||
| /// the GitHub REST API (no authentication required) and uses a Foundry agent | ||
| /// to answer follow-up questions about it. Type "EXIT" to end the conversation. | ||
| /// </para> | ||
| /// <para> | ||
| /// See the README.md file in the parent folder (../README.md) for detailed | ||
| /// information about the configuration required to run this sample. | ||
| /// </para> | ||
| /// </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)); | ||
|
|
||
| // Ensure sample agent exists in Foundry. The agent has no tools - it answers | ||
| // questions about the GitHub repository using only the JSON data that the | ||
| // HttpRequestAction adds to the conversation. | ||
| await CreateAgentAsync(foundryEndpoint, configuration); | ||
|
peibekwe marked this conversation as resolved.
|
||
|
|
||
| // Get input from command line or console | ||
| string workflowInput = Application.GetInput(args); | ||
|
|
||
| // The default HttpRequestHandler is sufficient for this sample because the | ||
| // GitHub REST endpoint used here does not require authentication. For | ||
| // authenticated endpoints, supply a custom Func<HttpRequestInfo, ..., HttpClient?> | ||
| // to DefaultHttpRequestHandler so each request can be routed through a | ||
| // pre-configured (cached) HttpClient with the appropriate credentials. | ||
| await using DefaultHttpRequestHandler httpRequestHandler = new(); | ||
|
|
||
| // Create the workflow factory with the HTTP request handler | ||
| WorkflowFactory workflowFactory = new("InvokeHttpRequest.yaml", foundryEndpoint) | ||
| { | ||
| HttpRequestHandler = httpRequestHandler | ||
| }; | ||
|
|
||
| // Execute the workflow | ||
| WorkflowRunner runner = new() { 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: "GitHubRepoInfoAgent", | ||
| agentDefinition: DefineAgent(configuration), | ||
| agentDescription: "Answers questions about a GitHub repository using HTTP response data in the conversation"); | ||
|
peibekwe marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private static DeclarativeAgentDefinition DefineAgent(IConfiguration configuration) | ||
| { | ||
| return new DeclarativeAgentDefinition(configuration.GetValue(Application.Settings.FoundryModel)) | ||
| { | ||
| Instructions = | ||
| """ | ||
| Answer the user's questions about the GitHub repository using only the | ||
| JSON data already present in the conversation history. | ||
| If the answer is not contained in the conversation, say so plainly | ||
| rather than guessing. Be concise and helpful. | ||
| """ | ||
| }; | ||
| } | ||
| } | ||
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
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
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.