Compartilhar via


Configurar o contexto do agente em agentes do Azure

Observação

O bot do Copilot Studio é renomeado como o agente do Copilot (agente ou agente de IA). O agente humano agora é renomeado como representante do serviço de atendimento ao consumidor (representante do serviço ou representante). Você pode encontrar referências cruzadas com termos novos e anteriores, ao mesmo tempo em que atualizamos a interface de usuário do produto, a documentação e o conteúdo de treinamento.

Para agentes do Azure, você deve instalar o SDK do agente e instanciar o middleware Omnichannel antes de configurar o contexto do agente.

Instalar o SDK do bot em seu projeto

  1. Para abrir o Gerenciador de Pacotes NuGet, clique com o botão direito do mouse em seu projeto e selecione Gerenciar Pacotes NuGet.

  2. No Gerenciador de Pacotes NuGet, selecione a origem do pacote como nuget.org e procure "Microsoft.Dynamics.AgentsSDK.Middleware". Selecione o pacote e selecione Instalar. Saiba mais na página Nuget.

    Como alternativa, você pode usar o comando a seguir na CLI do NuGet.

    Install-Package Microsoft.Dynamics.AgentsSDK.Middleware
    

O SDK do agente agora está instalado e o middleware Omnichannel está disponível em seu projeto.

Use o middleware Omnichannel no seu código de agente

  1. Abra o arquivo AdapterWithErrorHandler.cs .

  2. Adicione a instrução de importação e instancie o middleware Omnichannel.

    using Microsoft.Dynamics.AgentsSDK.Middleware.Core; 
    Use(new OmnichannelMiddleware()); 
    
    using System.Globalization;
    using System.Text;
    using Microsoft.Agents.Connector;
    using Microsoft.Agents.Core;
    using Microsoft.Agents.Core.Errors;
    using Microsoft.Extensions.Logging;
    using Microsoft.Dynamics.AgentsSDK.Middleware.Core;
    
    namespace Microsoft.CCaaS.MessagingRuntime.TestAgent;
    
    public class AdapterWithErrorHandler : CloudAdapter
    {
        public AdapterWithErrorHandler(
            IChannelServiceClientFactory channelServiceClientFactory,
            IActivityTaskQueue activityTaskQueue,
            ILogger<CloudAdapter> logger)
            : base(channelServiceClientFactory, activityTaskQueue, logger)
        {
            // OmnichannelMiddleware has special handling for OC event messages
            Use(new OmnichannelMiddleware());
    
            OnTurnError = async (turnContext, exception) =>
            {
                var exceptionInfo = GetExceptionInfo(exception);
                logger.LogAppException(exceptionInfo, exception);
    
                // Send a message to the user
                await turnContext.SendActivityAsync($"The bot encountered an error or bug.{Environment.NewLine}{exceptionInfo}");
                await turnContext.SendActivityAsync("To continue to run this bot, please fix the bot source code.");
    
                // Send a trace activity, which will be displayed in the Bot Framework Emulator
                await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError");
            };
        }
    
        private static string GetExceptionInfo(Exception exception)
        {
            var sb = new StringBuilder();
    
            // Pull some well known info from ErrorResponse.Exception if available.
            if (exception is ErrorResponseException responseException)
            {
                sb.AppendLine(CultureInfo.InvariantCulture, $"Error code: {responseException.Body?.Error?.Code ?? "NA"}");
                sb.AppendLine(CultureInfo.InvariantCulture, $"Error message: {responseException.Body?.Error?.Message ?? "NA"}");
            }
    
            sb.AppendLine(CultureInfo.InvariantCulture, $"Exception message: {exception.Message}");
            sb.AppendLine();
            sb.AppendLine(exception.ToString());
    
            var exceptionInfo = sb.ToString();
            return exceptionInfo;
        }
    }
    

Próximas etapas

Analisar a atividade JSON para obter o contexto do agente

Enviar contexto personalizado
setContextProvider
Integrar um agente do Azure