Mengonfigurasi konteks agen di agen Azure

Note

Copilot Studio bot diganti namanya menjadi agen Copilot (agen atau agen AI). Agen manusia sekarang berganti nama menjadi perwakilan layanan pelanggan (perwakilan atau perwakilan layanan). Anda mungkin menemukan referensi ke istilah lama dan baru saat kami memperbarui UI produk, dokumentasi, dan konten pelatihan.

Untuk agen Azure, Anda harus menginstal SDK agen dan membuat instance middleware Multisaluran sebelum mengonfigurasi konteks agen.

Menginstal bot SDK di project Anda

  1. Untuk membuka Package Manager NuGet, klik kanan proyek Anda, lalu pilih Kelola Paket NuGet.

  2. Di Package Manager NuGet, pilih sumber paket sebagai nuget.org dan telusuri "Microsoft.Dynamics. AgentsSDK.Middleware". Pilih paket, lalu pilih Instal. Pelajari lebih lanjut di Nuget page.

    Atau, Anda dapat menggunakan perintah berikut di NuGet CLI.

    Install-Package Microsoft.Dynamics.AgentsSDK.Middleware
    

SDK agen sekarang diinstal dan middleware Multisaluran tersedia di proyek Anda.

Gunakan middleware Multisaluran dalam kode agen Anda

  1. Buka file AdapterWithErrorHandler.cs .

  2. Tambahkan pernyataan impor dan buat instance middleware Multisaluran.

    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;
        }
    }
    

Langkah berikutnya

Mengurai JSON aktivitas untuk mendapatkan konteks agen

Mengirim konteks kustom
setContextProvider
Mengintegrasikan agen Azure