Skip to main content
Extensions, integrations, and deployment options for AutoGen .NET applications.

Microsoft.Extensions.AI Integration

Microsoft.Extensions.AI chat client interface.
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();

// Add OpenAI chat client
services.AddChatClient(builder => builder
    .UseOpenAI("gpt-4", apiKey));

var chatClient = services.BuildServiceProvider()
    .GetRequiredService<IChatClient>();

// Use with InferenceAgent
var agent = new InferenceAgent<TaskMessage>(
    id,
    runtime,
    "Assistant",
    logger,
    chatClient
);
GetResponseAsync
Task<ChatResponse>
Generate a chat completion
messages
IList<ChatMessage>
required
Conversation messages
options
ChatOptions?
Generation options (temperature, max tokens, etc.)
cancellationToken
CancellationToken
Cancellation token
GetStreamingResponseAsync
IAsyncEnumerable<ChatResponseUpdate>
Stream chat completion updates
Configuration for chat completions.
var options = new ChatOptions
{
    Temperature = 0.7f,
    MaxTokens = 2000,
    TopP = 0.9f,
    FrequencyPenalty = 0.5f,
    PresencePenalty = 0.5f,
    StopSequences = new[] { "STOP", "END" },
    ModelId = "gpt-4"
};

var response = await chatClient.GetResponseAsync(messages, options);
Temperature
float?
Sampling temperature (0.0 to 2.0)
MaxTokens
int?
Maximum tokens in response
TopP
float?
Nucleus sampling parameter
FrequencyPenalty
float?
Frequency penalty (-2.0 to 2.0)
PresencePenalty
float?
Presence penalty (-2.0 to 2.0)
StopSequences
IList<string>?
Stop sequences
ModelId
string?
Override model identifier
Message in a conversation.
using Microsoft.Extensions.AI;

var messages = new List<ChatMessage>
{
    new ChatMessage(ChatRole.System, "You are helpful."),
    new ChatMessage(ChatRole.User, "Hello!"),
    new ChatMessage(ChatRole.Assistant, "Hi! How can I help?"),
    new ChatMessage(ChatRole.User, "Tell me a joke.")
};
Role
ChatRole
required
Message role (System, User, Assistant, Tool)
Content
string
required
Message content

Semantic Kernel Integration

Integration with Semantic Kernel.
using Microsoft.AutoGen.Extensions.SemanticKernel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;

var builder = Host.CreateApplicationBuilder();

// Add Semantic Kernel
builder.Services.AddSemanticKernel()
    .AddOpenAIChatCompletion("gpt-4", apiKey);

// Add SK-powered agents
builder.Services.AddSemanticKernelAgents();

var host = builder.Build();
Configuration for Qdrant vector database.
using Microsoft.AutoGen.Extensions.SemanticKernel.Options;

builder.Services.Configure<QdrantOptions>(options =>
{
    options.Endpoint = "http://localhost:6333";
    options.ApiKey = "your-api-key";
    options.CollectionName = "agent_memory";
    options.VectorSize = 1536;
});
Endpoint
string
required
Qdrant server endpoint
ApiKey
string?
API key for authentication
CollectionName
string
required
Collection name for vectors
VectorSize
int
required
Dimension of embedding vectors

Microsoft.Extensions.AI (MEAI)

Hosting extensions for Microsoft.Extensions.AI.
using Microsoft.AutoGen.Extensions.MEAI;
using Microsoft.Extensions.DependencyInjection;

var builder = Host.CreateApplicationBuilder();

// Configure chat completion services
builder.Services.AddChatClient(config =>
{
    config.UseOpenAI("gpt-4", apiKey);
    config.UseLogging();
    config.UseDistributedCaching();
});

var host = builder.Build();
Configuration for AI clients.
using Microsoft.AutoGen.Extensions.MEAI.Options;

builder.Services.Configure<AIClientOptions>(options =>
{
    options.Endpoint = "https://api.openai.com";
    options.ApiKey = "sk-...";
    options.DefaultModel = "gpt-4";
    options.Timeout = TimeSpan.FromSeconds(30);
    options.MaxRetries = 3;
});
Endpoint
string
required
API endpoint URL
ApiKey
string
required
API key for authentication
DefaultModel
string
required
Default model identifier
Timeout
TimeSpan
Request timeout
MaxRetries
int
Maximum retry attempts
Extension methods for adding chat completion services.
services.AddChatCompletionClient<OpenAIChatClient>(config =>
{
    config.ApiKey = "sk-...";
    config.Model = "gpt-4";
});

Aspire Integration

.NET Aspire integration for cloud-native deployment.
using Microsoft.AutoGen.Extensions.Aspire;

var builder = DistributedApplication.CreateBuilder(args);

// Add AutoGen runtime
var runtime = builder.AddAutoGenRuntime("runtime")
    .WithReplicas(3);

// Add agent services
builder.AddProject<Projects.WorkerAgents>("workers")
    .WithReference(runtime);

builder.AddProject<Projects.AgentGateway>("gateway")
    .WithReference(runtime)
    .WithExternalHttpEndpoints();

var app = builder.Build();
await app.RunAsync();
AddAutoGenRuntime
method
Add AutoGen runtime to the application model
name
string
required
Runtime service name
WithReplicas
method
Configure number of runtime replicas
count
int
required
Number of replicas

gRPC Runtime

Distributed runtime using gRPC for inter-process communication.
using Microsoft.AutoGen.Core.Grpc;

// Server side
var builder = WebApplication.CreateBuilder();
builder.Services.AddGrpc();
builder.Services.AddAutoGenGrpcRuntime();

var app = builder.Build();
app.MapGrpcService<GrpcGatewayService>();
await app.RunAsync();

// Client side
var channel = GrpcChannel.ForAddress("http://localhost:50051");
var runtime = new GrpcAgentRuntime(channel);

await runtime.SendMessageAsync(
    message: "Task",
    recipient: new AgentId("worker", "instance1")
);
Gateway service for gRPC-based agent communication.
using Microsoft.AutoGen.RuntimeGateway.Grpc;

var builder = WebApplication.CreateBuilder();
builder.Services.AddGrpcGateway();

var app = builder.Build();
app.MapGrpcGateway();
await app.RunAsync();
Extension methods for agent runtime.
using Microsoft.AutoGen.Core.Grpc;

// Send and wait for response
var result = await runtime.SendAndAwaitResponseAsync(
    message: request,
    recipient: agentId,
    timeout: TimeSpan.FromSeconds(30)
);

// Broadcast to all agents of a type
await runtime.BroadcastAsync(
    message: notification,
    agentType: "worker"
);

Orleans Integration

Microsoft Orleans integration for distributed agents.
using Microsoft.AutoGen.RuntimeGateway.Grpc.Services.Orleans;

var builder = WebApplication.CreateBuilder();

builder.Services.AddOrleansAgentRuntime(siloBuilder =>
{
    siloBuilder.UseLocalhostClustering();
    siloBuilder.AddMemoryGrainStorage("PubSubStore");
});

var app = builder.Build();
await app.RunAsync();
Orleans grain for agent registry.
// Automatic registration via Orleans
// Tracks all active agents in the cluster

Serialization

Protocol Buffers serialization for messages.
using Microsoft.AutoGen.Core.Grpc;

var serializer = new ProtobufMessageSerializer();

// Serialize
var bytes = serializer.Serialize(message);

// Deserialize
var message = serializer.Deserialize<TaskMessage>(bytes);
CloudEvents format support.
using Microsoft.AutoGen.Core.Grpc;

var cloudEvent = message.ToCloudEvent(
    source: "agent/worker",
    type: "autogen.task"
);

var message = cloudEvent.ToMessage<TaskMessage>();

Example: Complete gRPC Setup

using Microsoft.AutoGen.Core;
using Microsoft.AutoGen.Core.Grpc;
using Microsoft.AutoGen.Contracts;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Grpc.Net.Client;

// Server (Gateway)
var serverBuilder = WebApplication.CreateBuilder();
serverBuilder.Services.AddGrpc();
serverBuilder.Services.AddAutoGenGrpcRuntime();

var server = serverBuilder.Build();
server.MapGrpcService<GrpcGatewayService>();
await server.StartAsync();

// Worker (Agent Host)
var workerBuilder = Host.CreateApplicationBuilder();
var channel = GrpcChannel.ForAddress("http://localhost:50051");
var runtime = new GrpcAgentRuntime(channel);

workerBuilder.Services.AddSingleton<IAgentRuntime>(runtime);
workerBuilder.Services.AddTransient<WorkerAgent>();

var worker = workerBuilder.Build();

await runtime.RegisterAgentFactoryAsync(
    new AgentType("worker"),
    async (id, rt) => new WorkerAgent(id, rt)
);

await worker.RunAsync();

See Also

Build docs developers (and LLMs) love