forked from microsoft/Generative-AI-for-beginners-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
137 lines (118 loc) · 5.65 KB
/
Copy pathProgram.cs
File metadata and controls
137 lines (118 loc) · 5.65 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
using MAF_MultiAgents;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Workflows;
using Microsoft.Extensions.AI;
using OpenTelemetry;
using OpenTelemetry.Trace;
// ================================================================
// CONFIGURATION REQUIREMENTS
// ================================================================
// This demo requires the following user secrets or environment variables:
//
// REQUIRED - Microsoft Foundry Persistent Agent (Agent 1 - Researcher):
// "AZURE_FOUNDRY_PROJECT_ENDPOINT": "https://<your-project>.services.ai.azure.com/"
//
// REQUIRED - Azure OpenAI Model Deployment:
// "AzureOpenAI:Deployment": "your-model-deployment-name" (default: "gpt-5-mini")
//
// REQUIRED - Agent 2 (Writer) - One of the following options:
//
// Option A - Azure OpenAI with API Key:
// "endpoint": "https://<your-resource>.cognitiveservices.azure.com"
// "apikey": "your-azure-openai-api-key"
// "AzureOpenAI:Deployment": "your-deployment-name" (e.g., "gpt-5-mini")
//
// Option B - Azure OpenAI with Default Credentials (fallback):
// "endpoint": "https://<your-resource>.cognitiveservices.azure.com"
// "AzureOpenAI:Deployment": "your-deployment-name"
// Note: Requires Azure CLI login (az login) or managed identity
//
// REQUIRED - Ollama (Agent 3 - Reviewer):
// Ollama must be running locally on http://localhost:11434/
// with the 'llama3.2' model downloaded and available.
//
// Configuration Priority for Agent 2:
// 1. Azure OpenAI with API Key (if apikey is set)
// 2. Azure OpenAI with Default Credentials (fallback)
//
// To set user secrets, run:
// dotnet user-secrets set "AZURE_FOUNDRY_PROJECT_ENDPOINT" "https://your-project.services.ai.azure.com/"
// dotnet user-secrets set "AzureOpenAI:Deployment" "gpt-5-mini"
// dotnet user-secrets set "AzureOpenAI:Endpoint" "https://your-resource.openai.azure.com/"
// dotnet user-secrets set "AzureOpenAI:ApiKey" "your-api-key" // Optional: omit to use DefaultAzureCredential
//
// To set up Ollama:
// 1. Install Ollama from https://ollama.ai/
// 2. Run: ollama pull llama3.2
// 3. Ensure Ollama service is running
// ================================================================
Console.WriteLine("=== Microsoft Agent Framework - Multi-Model Orchestration Demo ===");
Console.WriteLine("This demo showcases 3 agents working together:");
Console.WriteLine(" 1. Researcher (Microsoft Foundry Agent) - Researches topics");
Console.WriteLine(" 2. Writer (Azure OpenAI) - Writes content based on research");
Console.WriteLine(" 3. Reviewer (Ollama - llama3.2) - Reviews and provides feedback");
Console.WriteLine();
// ===== OpenTelemetry Trace Provider ====
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("agent-telemetry-source")
.AddConsoleExporter()
.Build();
// ===== Agent 1: Researcher using Azure OpenAI =====
Console.WriteLine("Setting up Agent 1: Researcher (Microsoft Foundry Agent)...");
AIAgent researcher = AIFoundryAgentsProvider.CreateAIAgent(
name: "Researcher",
instructions: "You are a research expert. Your job is to gather key facts and interesting points about the given topic. Be concise and focus on the most important information.")
.AsBuilder()
.UseOpenTelemetry(sourceName: "agent-telemetry-source")
.Build();
// ===== Agent 2: Writer using Azure OpenAI =====
Console.WriteLine("Setting up Agent 2: Writer (Azure OpenAI)...");
IChatClient azureChatClient = ChatClientProvider.GetChatClient();
AIAgent writer = azureChatClient.AsAIAgent(
name: "Writer",
instructions: "You are a creative writer. Take the research provided and write an engaging, well-structured article. Make it informative yet entertaining.")
.AsBuilder()
.UseOpenTelemetry(sourceName: "agent-telemetry-source")
.Build();
// ===== Agent 3: Reviewer using Ollama =====
Console.WriteLine("Setting up Agent 3: Reviewer (Ollama)...");
IChatClient ollamaChatClient = ChatClientProvider.GetChatClientOllama();
AIAgent reviewer = ollamaChatClient.AsAIAgent(
name: "Reviewer",
instructions: "You are an editor and reviewer. Analyze the article provided, give constructive feedback, and suggest improvements for clarity, grammar, and engagement.")
.AsBuilder()
.UseOpenTelemetry(sourceName: "agent-telemetry-source")
.Build();
// ===== Create Sequential Workflow =====
Console.WriteLine("Creating workflow: Researcher -> Writer -> Reviewer");
Console.WriteLine();
Workflow workflow =
AgentWorkflowBuilder
.BuildSequential(researcher, writer, reviewer);
AIAgent workflowAgent = workflow.AsAIAgent();
// ===== Execute the Workflow =====
var topic = "artificial intelligence in healthcare";
Console.WriteLine($"Starting workflow with topic: '{topic}'");
Console.WriteLine(new string('=', 80));
Console.WriteLine();
AgentResponse workflowResponse =
await workflowAgent.RunAsync($"Research and write an article about: {topic}");
Console.WriteLine("=== Final Output ===");
Console.WriteLine(workflowResponse.Text);
Console.WriteLine();
Console.WriteLine(new string('=', 80));
Console.WriteLine("Workflow completed successfully!");
Console.WriteLine("=== Clean Up ===");
Console.WriteLine("Do you want to delete the Researcher agent in Microsoft Foundry? (yes/no)");
string deleteResponse = Console.ReadLine()?.Trim().ToLower() ?? "no";
if (deleteResponse == "yes" || deleteResponse == "y")
{
Console.WriteLine("Deleting Researcher agent in Microsoft Foundry...");
AIFoundryAgentsProvider.DeleteAIAgentInAIFoundry(researcher);
Console.WriteLine("Researcher agent deleted successfully.");
}
else
{
Console.WriteLine("Researcher agent not deleted.");
}
Console.WriteLine();