-
Notifications
You must be signed in to change notification settings - Fork 260
/
Program.cs
80 lines (65 loc) · 3.25 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
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.KernelMemory;
public static class Program
{
private const string IndexName = "acronyms";
public static async Task Main()
{
var azureOpenAITextConfig = new AzureOpenAIConfig();
var azureOpenAIEmbeddingConfig = new AzureOpenAIConfig();
var azureAISearchConfigWithHybridSearch = new AzureAISearchConfig();
var azureAISearchConfigWithoutHybridSearch = new AzureAISearchConfig();
new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile("appsettings.Development.json", optional: true)
.Build()
.BindSection("KernelMemory:Services:AzureOpenAIText", azureOpenAITextConfig)
.BindSection("KernelMemory:Services:AzureOpenAIEmbedding", azureOpenAIEmbeddingConfig)
.BindSection("KernelMemory:Services:AzureAISearch", azureAISearchConfigWithHybridSearch)
.BindSection("KernelMemory:Services:AzureAISearch", azureAISearchConfigWithoutHybridSearch);
azureAISearchConfigWithHybridSearch.UseHybridSearch = true;
azureAISearchConfigWithoutHybridSearch.UseHybridSearch = false;
var memoryNoHybridSearch = new KernelMemoryBuilder()
.WithAzureOpenAITextGeneration(azureOpenAITextConfig)
.WithAzureOpenAITextEmbeddingGeneration(azureOpenAIEmbeddingConfig)
.WithAzureAISearchMemoryDb(azureAISearchConfigWithoutHybridSearch)
.WithSearchClientConfig(new SearchClientConfig { MaxMatchesCount = 2, Temperature = 0, TopP = 0 })
.Build<MemoryServerless>();
var memoryWithHybridSearch = new KernelMemoryBuilder()
.WithAzureOpenAITextGeneration(azureOpenAITextConfig)
.WithAzureOpenAITextEmbeddingGeneration(azureOpenAIEmbeddingConfig)
.WithAzureAISearchMemoryDb(azureAISearchConfigWithHybridSearch)
.WithSearchClientConfig(new SearchClientConfig { MaxMatchesCount = 2, Temperature = 0, TopP = 0 })
.Build<MemoryServerless>();
await CreateIndexAndImportData(memoryWithHybridSearch);
const string Question = "abc";
Console.WriteLine("Answer without hybrid search:");
await AskQuestion(memoryNoHybridSearch, Question);
// Output: INFO NOT FOUND
Console.WriteLine("Answer using hybrid search:");
await AskQuestion(memoryWithHybridSearch, Question);
// Output: 'Aliens Brewing Coffee'
}
private static async Task AskQuestion(IKernelMemory memory, string question)
{
var answer = await memory.AskAsync(question, index: IndexName);
Console.WriteLine(answer.Result);
}
private static async Task CreateIndexAndImportData(IKernelMemory memory)
{
await memory.DeleteIndexAsync(IndexName);
var data = """
aaa bbb ccc 000000000
C B A .......
ai bee cee Something else
XY. abc means 'Aliens Brewing Coffee'
abeec abecedario
A B C D first 4 letters
""";
var rows = data.Split("\n");
foreach (var acronym in rows)
{
await memory.ImportTextAsync(acronym, index: IndexName);
}
}
}