-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
88 lines (79 loc) · 3.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using KernelMemory.Extensions.Cohere;
using KernelMemory.Extensions.ConsoleTest.Helper;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.MemoryStorage;
using SemanticMemory.Samples;
using Spectre.Console;
using System.Text.Json;
namespace KernelMemory.Extensions.ConsoleTest;
public static class Program
{
static async Task Main(string[] args)
{
var services = new ServiceCollection();
services.AddSingleton<SimpleBookIndexingWithTextCleaning>();
services.AddSingleton<SBertSample>();
services.AddSingleton<BasicSample>();
services.AddSingleton<TextCleanerHandler>();
services.AddSingleton<CustomSearchPipelineBase>();
services.AddSingleton<AnthropicSample>();
services.AddSingleton<ContextualRetrievalSample>();
services.AddHttpClient();
var serviceProvider = services.BuildServiceProvider();
// Ask for the user's favorite fruits
var choices = new Dictionary<string, Type?>
{
["Basic Sample"] = typeof(BasicSample),
["Custom Pipeline (text cleaner)"] = typeof(SimpleBookIndexingWithTextCleaning),
["SBert in action"] = typeof(SBertSample),
["Custom Search pipeline (Basic)"] = typeof(CustomSearchPipelineBase),
["Anthropic"] = typeof(AnthropicSample),
["Contextual retrieval"] = typeof(ContextualRetrievalSample),
["Exit"] = null
};
Type? sampleType;
do
{
var sample = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("Choose the option to run?")
.PageSize(10)
.MoreChoicesText("[grey](Move up and down to select the example)[/]")
.AddChoices(choices.Keys.ToArray()));
sampleType = choices[sample];
if (sampleType != null)
{
var sampleInstance = serviceProvider.GetRequiredService(sampleType);
if (sampleInstance is ISample2 sampleInstance2)
{
await sampleInstance2.RunSample2();
}
else if (sampleInstance is ISample sampleInstance1)
{
var book = AnsiConsole.Prompt(new SelectionPrompt<string>()
.Title("Select the [green]book[/] to index")
.AddChoices([
@"c:\temp\advancedapisecurity.pdf",
@"S:\OneDrive\B19553_11.pdf",
@"c:\temp\blackhatpython.pdf",
@"/Users/gianmariaricci/Downloads/llchaindata/blackhatpython.pdf"]));
await sampleInstance1.RunSample(book);
}
}
} while (sampleType != null);
}
private static MemoryRecord CreateMemoryRecord(string documentId, string fileId, int partitionNumber, string textPartition)
{
var mr = new MemoryRecord();
mr.Payload = new Dictionary<string, object>();
mr.Payload["text"] = textPartition;
mr.Tags = new TagCollection
{
{ Constants.ReservedDocumentIdTag, documentId },
{ Constants.ReservedFileIdTag, fileId },
{ Constants.ReservedFilePartitionNumberTag, partitionNumber.ToString() }
};
return mr;
}
}