|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using Microsoft.KernelMemory; |
| 4 | +using Microsoft.KernelMemory.ContentStorage.DevTools; |
| 5 | +using Microsoft.KernelMemory.Sources.DiscordBot; |
| 6 | + |
| 7 | +namespace Microsoft.Discord.TestApplication; |
| 8 | + |
| 9 | +/* Example: Listen for new messages in Discord, and save them in a table in Postgres. |
| 10 | + * |
| 11 | + * Use ASP.NET hosted services to host a Discord Bot. The discord bot logic is based |
| 12 | + * on DiscordConnector class. |
| 13 | + * |
| 14 | + * While the Discord bot is running, every time there is a new message, DiscordConnector |
| 15 | + * invokes KM.ImportDocument API, uploading a JSON file that contains details about the |
| 16 | + * Discord message, including server ID, channel ID, author ID, message content, etc. |
| 17 | + * |
| 18 | + * The call to KM.ImportDocument API asks to process the JSON file uploaded using |
| 19 | + * DiscordMessageHandler, included in this project. No other handlers are used. |
| 20 | + * |
| 21 | + * DiscordMessageHandler, loads the uploaded file, deserializes its content, and |
| 22 | + * save each Discord message into a table in Postgres, using Entity Framework. |
| 23 | + */ |
| 24 | + |
| 25 | +internal static class Program |
| 26 | +{ |
| 27 | + public static void Main(string[] args) |
| 28 | + { |
| 29 | + WebApplicationBuilder appBuilder = WebApplication.CreateBuilder(); |
| 30 | + |
| 31 | + appBuilder.Configuration |
| 32 | + .AddJsonFile("appsettings.json") |
| 33 | + .AddJsonFile("appsettings.Development.json", optional: true) |
| 34 | + .AddEnvironmentVariables() |
| 35 | + .AddCommandLine(args); |
| 36 | + |
| 37 | + // Discord setup |
| 38 | + // Use DiscordConnector to connect to Discord and listen for messages. |
| 39 | + // The Discord connection can listen from multiple servers and channels. |
| 40 | + // For each message, DiscordConnector will send a file to Kernel Memory to process. |
| 41 | + // Files sent to Kernel Memory are processed by DiscordMessageHandler (in this project) |
| 42 | + var discordCfg = appBuilder.Configuration.GetSection("Discord").Get<DiscordConnectorConfig>(); |
| 43 | + ArgumentNullExceptionEx.ThrowIfNull(discordCfg, nameof(discordCfg), "Discord config is NULL"); |
| 44 | + appBuilder.Services.AddSingleton<DiscordConnectorConfig>(discordCfg); |
| 45 | + appBuilder.Services.AddHostedService<DiscordConnector>(); |
| 46 | + |
| 47 | + // Postgres with Entity Framework |
| 48 | + // DiscordMessageHandler reads files received by Kernel Memory and store each message in a table in Postgres. |
| 49 | + // See DiscordDbMessage for the table schema. |
| 50 | + appBuilder.AddNpgsqlDbContext<DiscordDbContext>("postgresDb"); |
| 51 | + |
| 52 | + // Run Kernel Memory and DiscordMessageHandler |
| 53 | + // var kmApp = BuildAsynchronousKernelMemoryApp(appBuilder, discordConfig); |
| 54 | + var kmApp = BuildSynchronousKernelMemoryApp(appBuilder, discordCfg); |
| 55 | + |
| 56 | + Console.WriteLine("Starting KM application...\n"); |
| 57 | + kmApp.Run(); |
| 58 | + Console.WriteLine("\n... KM application stopped."); |
| 59 | + } |
| 60 | + |
| 61 | + private static WebApplication BuildSynchronousKernelMemoryApp(WebApplicationBuilder appBuilder, DiscordConnectorConfig discordConfig) |
| 62 | + { |
| 63 | + appBuilder.AddKernelMemory(kmb => |
| 64 | + { |
| 65 | + // Note: there's no queue system, so the memory instance will be synchronous (ie MemoryServerless) |
| 66 | + |
| 67 | + // Store files on disk |
| 68 | + kmb.WithSimpleFileStorage(SimpleFileStorageConfig.Persistent); |
| 69 | + |
| 70 | + // Disable AI, not needed for this example |
| 71 | + kmb.WithoutEmbeddingGenerator(); |
| 72 | + kmb.WithoutTextGenerator(); |
| 73 | + }); |
| 74 | + |
| 75 | + WebApplication app = appBuilder.Build(); |
| 76 | + |
| 77 | + // In synchronous apps, handlers are added to the serverless memory orchestrator |
| 78 | + (app.Services.GetService<IKernelMemory>() as MemoryServerless)! |
| 79 | + .Orchestrator |
| 80 | + .AddHandler<DiscordMessageHandler>(discordConfig.Steps[0]); |
| 81 | + |
| 82 | + return app; |
| 83 | + } |
| 84 | + |
| 85 | + private static WebApplication BuildAsynchronousKernelMemoryApp(WebApplicationBuilder appBuilder, DiscordConnectorConfig discordConfig) |
| 86 | + { |
| 87 | + appBuilder.Services.AddHandlerAsHostedService<DiscordMessageHandler>(discordConfig.Steps[0]); |
| 88 | + appBuilder.AddKernelMemory(kmb => |
| 89 | + { |
| 90 | + // Note: because of this the memory instance will be asynchronous (ie MemoryService) |
| 91 | + kmb.WithSimpleQueuesPipeline(); |
| 92 | + |
| 93 | + // Store files on disk |
| 94 | + kmb.WithSimpleFileStorage(SimpleFileStorageConfig.Persistent); |
| 95 | + |
| 96 | + // Disable AI, not needed for this example |
| 97 | + kmb.WithoutEmbeddingGenerator(); |
| 98 | + kmb.WithoutTextGenerator(); |
| 99 | + }); |
| 100 | + |
| 101 | + return appBuilder.Build(); |
| 102 | + } |
| 103 | +} |
0 commit comments