Partilhar via


Configurar o contexto do agente nos agentes do Azure

Observação

O bot do Copilot Studio é renomeado como agente do Copilot (agente ou agente de IA). O agente humano agora é renomeado como representante de suporte ao cliente (representante de serviço ou representante). Poderá deparar-se com referências aos termos antigos e novos enquanto atualizamos a IU do produto, a documentação e o conteúdo de preparação.

Para agentes Azure, tens de instalar o SDK do agente e instanciar o middleware Omnichannel antes de configurares o contexto do agente.

Instale 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 Gestor de Pacotes NuGet, selecione a fonte do pacote como nuget.org e navegue por "Microsoft.Dynamics.AgentsSDK.Middleware". Seleciona o pacote e depois seleciona Instalar. Saiba mais na página Nuget.

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

    Install-Package Microsoft.Dynamics.AgentsSDK.Middleware
    

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

Usa o middleware Omnichannel no código do teu agente

  1. Abra o arquivo AdapterWithErrorHandler.cs .

  2. Adicione a instrução import 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óximos passos

Analisar a atividade JSON para obter o contexto do agente

Enviar contexto personalizado
setContextProvider
Integrar um agente do Azure