forked from microsoft/agent-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
125 lines (100 loc) · 4.38 KB
/
Copy pathProgram.cs
File metadata and controls
125 lines (100 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) Microsoft. All rights reserved.
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Workflows;
using Microsoft.Extensions.AI;
string endpoint = Environment.GetEnvironmentVariable("AZURE_AI_PROJECT_ENDPOINT")
?? throw new InvalidOperationException("AZURE_AI_PROJECT_ENDPOINT is not set.");
string deploymentName = Environment.GetEnvironmentVariable("AZURE_AI_MODEL_DEPLOYMENT_NAME") ?? "gpt-5.4-mini";
// WARNING: DefaultAzureCredential is convenient for development but requires careful consideration in production.
// In production, consider using a specific credential (e.g., ManagedIdentityCredential) to avoid
// latency issues, unintended credential probing, and potential security risks from fallback mechanisms.
AIProjectClient projectClient = new(new Uri(endpoint), new DefaultAzureCredential());
IChatClient chatClient = projectClient.ProjectOpenAIClient
.GetChatClient(deploymentName)
.AsIChatClient();
Workflow workflow = CreateWorkflow(chatClient);
await RunWorkflowAsync(workflow).ConfigureAwait(false);
static Workflow CreateWorkflow(IChatClient chatClient)
{
AgentRegistry agents = new(chatClient);
HandoffWorkflowBuilder handoffBuilder = AgentWorkflowBuilder.CreateHandoffBuilderWith(agents.IntakeAgent);
// Add a handoff to each of the experts from every agent in the registry (experts + Intake)
foreach (AIAgent expert in agents.Experts)
{
handoffBuilder.WithHandoffs(agents.All.Except([expert]), expert);
}
// Let agents request more user information and return to the asking agent (rather than going back to the intake agent)
handoffBuilder.EnableReturnToPrevious();
return handoffBuilder.Build();
}
static async Task RunWorkflowAsync(Workflow workflow)
{
using CancellationTokenSource cts = CreateConsoleCancelKeySource();
await using StreamingRun run = await InProcessExecution.OpenStreamingAsync(workflow, cancellationToken: cts.Token)
.ConfigureAwait(false);
bool hadError = false;
do
{
Console.Write("> ");
string userInput = Console.ReadLine() ?? string.Empty;
if (userInput.Equals("exit", StringComparison.OrdinalIgnoreCase))
{
break;
}
await run.TrySendMessageAsync(userInput);
string? speakingAgent = null;
await foreach (WorkflowEvent evt in run.WatchStreamAsync(cts.Token))
{
switch (evt)
{
case AgentResponseUpdateEvent update:
{
if (speakingAgent == null || speakingAgent != update.Update.AuthorName)
{
speakingAgent = update.Update.AuthorName;
Console.Write($"\n{speakingAgent}: ");
}
Console.Write(update.Update.Text);
break;
}
case WorkflowErrorEvent workflowError:
{
Console.ForegroundColor = ConsoleColor.Red;
if (workflowError.Exception != null)
{
Console.WriteLine($"\nWorkflow error: {workflowError.Exception}");
}
else
{
Console.WriteLine("\nUnknown workflow error occurred.");
}
Console.ResetColor();
hadError = true;
break;
}
case WorkflowWarningEvent workflowWarning when workflowWarning.Data is string message:
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(message);
Console.ResetColor();
break;
}
}
}
} while (!hadError);
}
static CancellationTokenSource CreateConsoleCancelKeySource()
{
CancellationTokenSource cts = new();
// Normally, support a way to detach events, but in this case this is a termination signal, so cleanup will happen
// as part of application shutdown.
Console.CancelKeyPress += (s, args) =>
{
cts.Cancel();
// We handle cleanup + termination ourselves
args.Cancel = true;
};
return cts;
}