|
Este artigo apresenta o Microsoft Agent Framework (MAF) para a construção de sistemas multiagentes coordenados e escaláveis na plataforma .NET. |

Microsoft.Extensions.AI, que se tornou a camada de abstração recomendada para comunicação com modelos de linguagem.
Aplicação .NET
│
Microsoft Agent Framework
│
Microsoft.Extensions.AI
│
OpenAI | Azure OpenAI | Ollama | GitHub Models | outros provedores
|
Microsoft.Extensions.AI fornece abstrações como:Microsoft.Extensions.AI juntamente com o Microsoft Agent Framework.
Planner
│
┌─────────┼─────────┐
│ │ │
Financeiro Jurídico Pesquisa
└─────────┼─────────┘
│
Reviewer
│
Response
|
MultiAgentes │ ├── Agents │ PlannerAgent.cs │ CreditAgent.cs │ ComplianceAgent.cs │ ReviewerAgent.cs │ ResponseAgent.cs │ ├── Tools │ CreditTool.cs │ ComplianceTool.cs │ ├── Models │ CreditAnalysisRequest.cs │ CreditAnalysisResult.cs │ ├── Workflows │ CreditAnalysisWorkflow.cs │ ├── Configuration │ AiConfiguration.cs │ ├── appsettings.json │ └── Program.cs |
using Microsoft.Extensions.DependencyInjection; var builder = Host.CreateApplicationBuilder(args); // Configura o ChatClient builder.Services.AddChatClient( new OpenAIChatClient( model: "gpt-4.1", apiKey)); // Registra as Tools builder.Services.AddSingleton<CreditTool>(); builder.Services.AddSingleton<ComplianceTool>(); // Registra os Agents builder.Services.AddSingleton<PlannerAgent>(); builder.Services.AddSingleton<CreditAgent>(); builder.Services.AddSingleton<ComplianceAgent>(); builder.Services.AddSingleton<ReviewerAgent>(); builder.Services.AddSingleton<ResponseAgent>(); // Registra o Workflow builder.Services.AddSingleton<CreditWorkflow>(); var app = builder.Build(); var workflow = app.Services.GetRequiredService<CreditWorkflow>(); var resultado = await workflow.RunAsync( "Avalie o pedido de crédito do cliente João."); Console.WriteLine(resultado); |
public class PlannerAgent { public ChatClientAgent Create(IChatClient chatClient) { return new ChatClientAgent( chatClient, instructions: """ Analise a solicitação e determine quais verificações serão necessárias. """); } } |
public class CreditAgent { private readonly CreditTool _tool; public CreditAgent(CreditTool tool) { _tool = tool; } public ChatClientAgent Create(IChatClient chatClient) { return new ChatClientAgent( chatClient, instructions: """ Consulte o histórico financeiro e o score do cliente. """, tools: [_tool]); } } |
public class ComplianceAgent { private readonly ComplianceTool _tool; public ComplianceAgent(ComplianceTool tool) { _tool = tool; } public ChatClientAgent Create(IChatClient chatClient) { return new ChatClientAgent( chatClient, instructions: """ Verifique políticas internas e regras de compliance. """, tools: [_tool]); } } |
public class ReviewerAgent { public ChatClientAgent Create(IChatClient chatClient) { return new ChatClientAgent( chatClient, instructions: """ Revise todas as respostas recebidas dos demais agentes. """); } } |
public class ResponseAgent { public ChatClientAgent Create(IChatClient chatClient) { return new ChatClientAgent( chatClient, instructions: """ Gere uma resposta final para o usuário. """); } } |
public class CreditTool { public string GetCreditScore(string cliente) { return $"Cliente {cliente} possui score 820."; } } |
public class ComplianceTool { public string CheckCompliance(string cliente) { return "Nenhuma restrição encontrada."; } } |
public class CreditWorkflow { private readonly IChatClient _chatClient; private readonly PlannerAgent _planner; private readonly CreditAgent _credit; private readonly ComplianceAgent _compliance; private readonly ReviewerAgent _reviewer; private readonly ResponseAgent _response; public CreditWorkflow( IChatClient chatClient, PlannerAgent planner, CreditAgent credit, ComplianceAgent compliance, ReviewerAgent reviewer, ResponseAgent response) { _chatClient = chatClient; _planner = planner; _credit = credit; _compliance = compliance; _reviewer = reviewer; _response = response; } public async Task<string> RunAsync(string prompt) { var planner = _planner.Create(_chatClient); var credit = _credit.Create(_chatClient);
var compliance = _compliance.Create(_chatClient);
var reviewer = _reviewer.Create(_chatClient);
var response = _response.Create(_chatClient);
var workflow = new WorkflowBuilder(planner)
.AddParallelEdges( planner, credit, compliance) .Join(credit, compliance)
.AddEdge(reviewer, response)
.Build();
return await workflow.RunAsync(prompt); } } |
public class AIConfiguration { public const string SectionName = "OpenAI"; public string Model { get; set; } = "gpt-4.1";
public string ApiKey => Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException( "A variável de ambiente OPENAI_API_KEY não foi encontrada."); } |
Microsoft.Extensions.AI passou a ser a principal camada de abstração para comunicação com modelos de linguagem, enquanto o Semantic Kernel deixou de ser um componente obrigatório e passou a atuar como uma biblioteca complementar, útil em cenários específicos que exigem recursos adicionais como plugins, memória ou planejamento.
E estamos conversados..![]()
"Porque pela graça sois salvos, por meio da fé; e
isto não vem de vós, é dom de Deus. Não vem das obras, para que ninguém se
glorie;"
Efésios 2:8,9
Referências: