-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathProgram.cs
39 lines (30 loc) · 1.47 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.KernelMemory;
/* The following code shows how to create a custom handler and run it as a standalone service.
* The handler will automatically attach to a queue and listen for work to do.
* You can also add multiple handlers the same way.
*/
// Usual .NET web app builder
var appBuilder = WebApplication.CreateBuilder();
/* ... setup your handler dependencies ... */
// builder.Services.AddSingleton...
// builder.Services.AddTransient...
// Define the handlers to host
appBuilder.Services.AddHandlerAsHostedService<MyHandler>("mypipelinestep");
// builder.Services.AddHandlerAsHostedService<MyHandler2>("mypipelinestep-2");
// builder.Services.AddHandlerAsHostedService<MyHandler3>("mypipelinestep-3");
// Build the memory instance injecting its dependencies into the current app
var memory = new KernelMemoryBuilder(appBuilder.Services)
.WithoutDefaultHandlers()
.WithSimpleQueuesPipeline() // Queues are required by handlers hosted as a service
.WithOpenAIDefaults(Environment.GetEnvironmentVariable("OPENAI_API_KEY")!)
.Build();
// Console.WriteLine("Memory type: " + memory.GetType().FullName);
// Enqueue a task, just for testing
#pragma warning disable CS4014
Task.Run(() => memory.ImportTextAsync("something", steps: ["mypipelinestep"]));
// Build and run .NET web app as usual
Console.WriteLine("Starting service...");
var app = appBuilder.Build();
app.Run();
Console.WriteLine("Service stopped");