Dapr

O bloco modular Dapr Conversation encaminha a inferência do modelo através de um sidecar do Dapr e expõe um IChatClient que pode servir de base a um agente .NET do Agent Framework. O fornecedor do modelo e as credenciais são configurados no componente Dapr Conversation, em vez de diretamente no processo do agente.

Pré-requisitos

  • .NET 10 ou posterior.
  • Docker e a CLI do Dapr.
  • Um componente Dapr Conversation configurado, como um componente suportado por Ollama.

Instale os pacotes

dotnet add package Dapr.AI.Microsoft.Extensions
dotnet add package Microsoft.Agents.AI --prerelease

Configuration

DAPR_GRPC_ENDPOINT="http://localhost:3501"

DAPR_GRPC_ENDPOINT é opcional e o padrão é http://localhost:3501. Defina ConversationComponentName no código da aplicação para o nome do componente Dapr Conversation, como ollama.

Criar um agente apoiado pela Dapr

Configure o ponto final do sidecar do Dapr e o componente Conversation através da injeção de dependências, resolva o IChatClient e converta-o num agente.

// the sidecar (see this sample's README). Override it with the DAPR_GRPC_ENDPOINT environment
// variable if you run the sidecar on a different port.
var daprGrpcEndpoint = Environment.GetEnvironmentVariable("DAPR_GRPC_ENDPOINT") ?? "http://localhost:3501";

// Register the Dapr Conversation client with dependency injection.
var app = Host.CreateDefaultBuilder()
    .ConfigureServices(services =>
    {
        // Configure the gRPC endpoint for the Dapr sidecar.
        services.AddDaprConversationClient((_, builder) => builder.UseGrpcEndpoint(daprGrpcEndpoint));
        // Provide the name of the Conversation component loaded in the sidecar to use.
        services.AddDaprChatClient(opt => opt.ConversationComponentName = "ollama");
    }).Build();

// Get an instance of the Dapr chat client from the dependency injection container.
using var scope = app.Services.CreateScope();
var daprChatClient = scope.ServiceProvider.GetRequiredService<IChatClient>();

// Use this chat client to construct an AIAgent.
AIAgent agent = daprChatClient.AsAIAgent(instructions: "You are good at telling jokes.", name: "Joker");

// Invoke the agent and output the text result.
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate."));

As capacidades dos fornecedores dependem do componente Dapr Conversation e do modelo por trás dele.

Tools

O suporte para ferramentas é herdado do componente e do modelo Dapr Conversation configurados.

Tool Situação Observações
Ferramentas Funcionais Variações Requer suporte para chamadas de funções a partir do componente e modelo configurados.
Aprovação de Ferramentas Variações Disponível quando o modelo produz chamadas de ferramenta funcional.
Ferramentas alojadas pelo fornecedor O Dapr não adiciona uma interface separada para ferramentas alojadas no Agent Framework.
Ferramentas MCP Locais É executado no processo da aplicação.

Passos seguintes