-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathProgram.cs
More file actions
238 lines (195 loc) · 9.18 KB
/
Program.cs
File metadata and controls
238 lines (195 loc) · 9.18 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System;
using System.Threading.Tasks;
using DotNetEnv;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using WorkflowCore.AI.AzureFoundry.Interface;
using WorkflowCore.AI.AzureFoundry.ServiceExtensions;
using WorkflowCore.Interface;
using WorkflowCore.Sample.AzureFoundry.Tools;
using WorkflowCore.Sample.AzureFoundry.Workflows;
namespace WorkflowCore.Sample.AzureFoundry
{
public class Program
{
public static async Task Main(string[] args)
{
// Load environment variables from .env file
Env.Load();
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();
var serviceProvider = ConfigureServices(configuration);
// Register tools
var toolRegistry = serviceProvider.GetRequiredService<IToolRegistry>();
toolRegistry.Register(serviceProvider.GetRequiredService<WeatherTool>());
toolRegistry.Register(serviceProvider.GetRequiredService<CalculatorTool>());
var host = serviceProvider.GetRequiredService<IWorkflowHost>();
// Register workflows
host.RegisterWorkflow<SimpleChatWorkflow, ChatWorkflowData>();
host.RegisterWorkflow<AgentWithToolsWorkflow, AgentWorkflowData>();
host.RegisterWorkflow<HumanReviewWorkflow, ReviewWorkflowData>();
host.Start();
Console.WriteLine("=== WorkflowCore Azure AI Foundry Sample ===");
Console.WriteLine();
Console.WriteLine("Choose a workflow to run:");
Console.WriteLine("1. Simple Chat Completion");
Console.WriteLine("2. Agent with Tools (Agentic Loop)");
Console.WriteLine("3. Human-in-the-Loop Review");
Console.WriteLine("Q. Quit");
Console.WriteLine();
while (true)
{
Console.Write("Enter choice: ");
var choice = Console.ReadLine()?.Trim().ToUpper();
switch (choice)
{
case "1":
await RunSimpleChatWorkflow(host);
break;
case "2":
await RunAgentWithToolsWorkflow(host);
break;
case "3":
await RunHumanReviewWorkflow(host);
break;
case "Q":
host.Stop();
return;
default:
Console.WriteLine("Invalid choice. Try again.");
break;
}
}
}
private static async Task RunSimpleChatWorkflow(IWorkflowHost host)
{
Console.WriteLine("Type 'quit' to exit the conversation.");
Console.WriteLine();
while (true)
{
Console.Write("You: ");
var message = Console.ReadLine();
if (string.IsNullOrWhiteSpace(message) || message.Equals("quit", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Exiting chat.");
break;
}
var data = new ChatWorkflowData
{
UserMessage = message
};
var workflowId = await host.StartWorkflow("SimpleChatWorkflow", data);
// Wait a bit for the workflow to complete
await Task.Delay(5000);
var instance = await host.PersistenceStore.GetWorkflowInstance(workflowId);
var result = instance.Data as ChatWorkflowData;
Console.WriteLine($"Assistant: {result?.Response ?? "Still processing..."}");
Console.WriteLine();
}
}
private static async Task RunAgentWithToolsWorkflow(IWorkflowHost host)
{
Console.WriteLine("Available tools: weather (get weather for a city), calculator (do math)");
Console.WriteLine("Type 'quit' to exit the conversation.");
Console.WriteLine();
while (true)
{
Console.Write("You: ");
var message = Console.ReadLine();
if (string.IsNullOrWhiteSpace(message) || message.Equals("quit", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Exiting agent conversation.");
break;
}
var data = new AgentWorkflowData
{
UserRequest = message
};
var workflowId = await host.StartWorkflow("AgentWithToolsWorkflow", data);
// Wait for the agent loop to complete
await Task.Delay(15000);
var instance = await host.PersistenceStore.GetWorkflowInstance(workflowId);
var result = instance.Data as AgentWorkflowData;
Console.WriteLine($"Agent: {result?.AgentResponse ?? "Still processing..."}");
Console.WriteLine();
}
}
private static async Task RunHumanReviewWorkflow(IWorkflowHost host)
{
Console.Write("Enter content to generate and review: ");
var topic = Console.ReadLine();
var data = new ReviewWorkflowData
{
Topic = topic,
Reviewer = "demo-user"
};
var workflowId = await host.StartWorkflow("HumanReviewWorkflow", data);
Console.WriteLine($"Started workflow: {workflowId}");
// Wait for AI to generate content
await Task.Delay(5000);
var instance = await host.PersistenceStore.GetWorkflowInstance(workflowId);
var result = instance.Data as ReviewWorkflowData;
Console.WriteLine();
Console.WriteLine("=== Content Generated by AI ===");
Console.WriteLine(result?.GeneratedContent ?? "Still generating...");
Console.WriteLine("================================");
Console.WriteLine();
Console.WriteLine("To approve, publish a HumanReview event. For this demo, auto-approving...");
// In a real app, this would come from a UI or API
// For demo, we auto-approve
await host.PublishEvent(
"HumanReview",
$"{workflowId}.{GetReviewPointerId(instance)}",
new WorkflowCore.AI.AzureFoundry.Models.ReviewAction
{
Decision = WorkflowCore.AI.AzureFoundry.Models.ReviewDecision.Approved,
Reviewer = "demo-user"
});
await Task.Delay(2000);
instance = await host.PersistenceStore.GetWorkflowInstance(workflowId);
result = instance.Data as ReviewWorkflowData;
Console.WriteLine();
Console.WriteLine($"Final approved content: {result?.ApprovedContent ?? "Pending..."}");
Console.WriteLine();
}
private static string GetReviewPointerId(WorkflowCore.Models.WorkflowInstance instance)
{
string lastId = null;
foreach (var pointer in instance.ExecutionPointers)
{
if (pointer.StepName == "HumanReview")
return pointer.Id;
lastId = pointer.Id;
}
return lastId;
}
private static IServiceProvider ConfigureServices(IConfiguration configuration)
{
var services = new ServiceCollection();
services.AddLogging(builder =>
{
builder.AddConsole();
builder.SetMinimumLevel(LogLevel.Information);
});
services.AddWorkflow();
// Configure Azure AI Foundry
services.AddAzureFoundry(options =>
{
options.Endpoint = configuration["AZURE_AI_ENDPOINT"]
?? throw new InvalidOperationException("AZURE_AI_ENDPOINT not configured. Copy .env.example to .env and fill in values.");
options.ApiKey = configuration["AZURE_AI_API_KEY"];
options.ProjectName = configuration["AZURE_AI_PROJECT"] ?? "default";
options.DefaultModel = configuration["AZURE_AI_DEFAULT_MODEL"] ?? "gpt-4o";
options.DefaultEmbeddingModel = configuration["AZURE_AI_EMBEDDING_MODEL"] ?? "text-embedding-3-small";
options.SearchEndpoint = configuration["AZURE_SEARCH_ENDPOINT"];
options.SearchApiKey = configuration["AZURE_SEARCH_API_KEY"];
});
// Register tools
services.AddTransient<WeatherTool>();
services.AddTransient<CalculatorTool>();
return services.BuildServiceProvider();
}
}
}