mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
7ebe00ec3d
* Updates to async run loop. * fix: Workflow Onwership can be release by nonowner * fix: Incorrect handling of blockOnPending in StreamingRun Depending on whether we are running in streaming on non-streaming mode, we may be using the StreamingRun in different ways. Unfortunately, the only place we can really know what is the actual state of execution is in the RunEventStream implementations. This resulted in blocking where blocking was unneeded and occasionally not-blocking when blocking was needed. The fix is to move the logic of handling this blocking into RunEventStream implementations. * fix: Fix cleanup on error and end run This ensures we clean up the background resources correctly. * fix: Ensure we let the run loop proceed when shutting down * fix: Add timeout for Input Waiting * fix: Make the samples properly clean up `Run`s and `StreamingRun`s * fix: Simplify Declarative Workflow Run disposal pattern * Also fixes missing .Disposal() in Integration tests --------- Co-authored-by: Ben Thomas <ben.thomas@microsoft.com>
80 lines
3.6 KiB
C#
80 lines
3.6 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using Azure.AI.Agents.Persistent;
|
|
using Azure.Identity;
|
|
using Microsoft.Agents.AI;
|
|
using Microsoft.Agents.AI.Workflows;
|
|
using Microsoft.Extensions.AI;
|
|
|
|
namespace WorkflowFoundryAgentSample;
|
|
|
|
/// <summary>
|
|
/// This sample shows how to use Azure Foundry Agents within a workflow.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Pre-requisites:
|
|
/// - Foundational samples should be completed first.
|
|
/// - An Azure Foundry project endpoint and model id.
|
|
/// </remarks>
|
|
public static class Program
|
|
{
|
|
private static async Task Main()
|
|
{
|
|
// Set up the Azure OpenAI client
|
|
var endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT")
|
|
?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");
|
|
var deploymentName = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
|
|
var persistentAgentsClient = new PersistentAgentsClient(endpoint, new AzureCliCredential());
|
|
|
|
// Create agents
|
|
AIAgent frenchAgent = await GetTranslationAgentAsync("French", persistentAgentsClient, deploymentName);
|
|
AIAgent spanishAgent = await GetTranslationAgentAsync("Spanish", persistentAgentsClient, deploymentName);
|
|
AIAgent englishAgent = await GetTranslationAgentAsync("English", persistentAgentsClient, deploymentName);
|
|
|
|
// Build the workflow by adding executors and connecting them
|
|
var workflow = new WorkflowBuilder(frenchAgent)
|
|
.AddEdge(frenchAgent, spanishAgent)
|
|
.AddEdge(spanishAgent, englishAgent)
|
|
.Build();
|
|
|
|
// Execute the workflow
|
|
await using StreamingRun run = await InProcessExecution.StreamAsync(workflow, new ChatMessage(ChatRole.User, "Hello World!"));
|
|
// Must send the turn token to trigger the agents.
|
|
// The agents are wrapped as executors. When they receive messages,
|
|
// they will cache the messages and only start processing when they receive a TurnToken.
|
|
await run.TrySendMessageAsync(new TurnToken(emitEvents: true));
|
|
await foreach (WorkflowEvent evt in run.WatchStreamAsync().ConfigureAwait(false))
|
|
{
|
|
if (evt is AgentRunUpdateEvent executorComplete)
|
|
{
|
|
Console.WriteLine($"{executorComplete.ExecutorId}: {executorComplete.Data}");
|
|
}
|
|
}
|
|
|
|
// Cleanup the agents created for the sample.
|
|
await persistentAgentsClient.Administration.DeleteAgentAsync(frenchAgent.Id);
|
|
await persistentAgentsClient.Administration.DeleteAgentAsync(spanishAgent.Id);
|
|
await persistentAgentsClient.Administration.DeleteAgentAsync(englishAgent.Id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a translation agent for the specified target language.
|
|
/// </summary>
|
|
/// <param name="targetLanguage">The target language for translation</param>
|
|
/// <param name="persistentAgentsClient">The PersistentAgentsClient to create the agent</param>
|
|
/// <param name="model">The model to use for the agent</param>
|
|
/// <returns>A ChatClientAgent configured for the specified language</returns>
|
|
private static async Task<ChatClientAgent> GetTranslationAgentAsync(
|
|
string targetLanguage,
|
|
PersistentAgentsClient persistentAgentsClient,
|
|
string model)
|
|
{
|
|
var agentMetadata = await persistentAgentsClient.Administration.CreateAgentAsync(
|
|
model: model,
|
|
name: $"{targetLanguage} Translator",
|
|
instructions: $"You are a translation assistant that translates the provided text to {targetLanguage}.");
|
|
|
|
return await persistentAgentsClient.GetAIAgentAsync(agentMetadata.Value.Id);
|
|
}
|
|
}
|