|
1 | 1 | using System;
|
2 |
| -using System.Collections.Generic; |
3 |
| -using System.Text.Json.Serialization; |
4 | 2 | using System.Threading.Tasks;
|
5 | 3 |
|
6 | 4 | using Elastic.Clients.Elasticsearch;
|
7 | 5 | using Elastic.Transport;
|
8 | 6 |
|
9 |
| -using Elastic.SemanticKernel.Connectors.Elasticsearch; |
10 |
| - |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Microsoft.Extensions.Hosting; |
11 | 9 | using Microsoft.Extensions.VectorData;
|
| 10 | +using Microsoft.SemanticKernel; |
| 11 | +using Microsoft.SemanticKernel.Data; |
| 12 | +using Microsoft.SemanticKernel.Embeddings; |
| 13 | +using Microsoft.SemanticKernel.PromptTemplates.Handlebars; |
12 | 14 |
|
13 | 15 | namespace Elastic.SemanticKernel.Playground;
|
14 | 16 |
|
15 |
| -internal class Program |
| 17 | +#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task |
| 18 | + |
| 19 | +internal sealed class Program |
16 | 20 | {
|
17 |
| - static async Task Main(string[] args) |
| 21 | + public static async Task Main(string[] args) |
18 | 22 | {
|
19 |
| - using var settings = new ElasticsearchClientSettings(new Uri("https://primary.es.europe-west3.gcp.cloud.es.io")) |
20 |
| - .Authentication(new BasicAuthentication("elastic", "g7wuRWPrJF2yAzytvd9w18s8")) |
| 23 | +#pragma warning disable SKEXP0010 // Some SK methods are still experimental |
| 24 | + |
| 25 | + var builder = Host.CreateApplicationBuilder(args); |
| 26 | + |
| 27 | + // Register AI services. |
| 28 | + var kernelBuilder = builder.Services.AddKernel(); |
| 29 | + kernelBuilder.AddAzureOpenAIChatCompletion("gpt-4o", "https://my-service.openai.azure.com", "my_token"); |
| 30 | + kernelBuilder.AddAzureOpenAITextEmbeddingGeneration("ada-002", "https://my-service.openai.azure.com", "my_token"); |
| 31 | + |
| 32 | + // Register text search service. |
| 33 | + kernelBuilder.AddVectorStoreTextSearch<Hotel>(); |
| 34 | + |
| 35 | + // Register Elasticsearch vector store. |
| 36 | + var elasticsearchClientSettings = new ElasticsearchClientSettings(new Uri("https://my-elasticsearch-instance.cloud")) |
| 37 | + .Authentication(new BasicAuthentication("elastic", "my_password")) |
21 | 38 | .DisableDirectStreaming()
|
22 | 39 | .EnableDebugMode(cd =>
|
23 | 40 | {
|
| 41 | + //var request = System.Text.Encoding.Default.GetString(cd.RequestBodyInBytes); |
24 | 42 | Console.WriteLine(cd.DebugInformation);
|
25 | 43 | });
|
| 44 | + kernelBuilder.AddElasticsearchVectorStoreRecordCollection<string, Hotel>("skhotels", elasticsearchClientSettings); |
26 | 45 |
|
27 |
| - var client = new ElasticsearchClient(settings); |
| 46 | + // Build the host. |
| 47 | + using var host = builder.Build(); |
28 | 48 |
|
29 |
| - var vectorStore = new ElasticsearchVectorStore(client); |
| 49 | + // For demo purposes, we access the services directly without using a DI context. |
30 | 50 |
|
31 |
| - var collection = vectorStore.GetCollection<string, MyRecord>("sk"); |
| 51 | + var kernel = host.Services.GetService<Kernel>()!; |
| 52 | + var embeddings = host.Services.GetService<ITextEmbeddingGenerationService>()!; |
| 53 | + var vectorStoreCollection = host.Services.GetService<IVectorStoreRecordCollection<string, Hotel>>()!; |
32 | 54 |
|
33 |
| - await collection.CreateCollectionIfNotExistsAsync(); |
| 55 | + // Register search plugin. |
| 56 | + var textSearch = host.Services.GetService<VectorStoreTextSearch<Hotel>>()!; |
| 57 | + kernel.Plugins.Add(textSearch.CreateWithGetTextSearchResults("SearchPlugin")); |
34 | 58 |
|
35 |
| - await collection.UpsertAsync(new MyRecord |
36 |
| - { |
37 |
| - MyKey = "40", |
38 |
| - Vec1 = new float[] { 2.1f, 2.0f, 2.2f }, |
39 |
| - Vec2 = new float[] { 1.2f, 1.1f, 1.3f }, |
40 |
| - Data1 = 1337, |
41 |
| - Data2 = DateTimeKind.Utc, |
42 |
| - Data3 = ["a", "b", "c"] |
43 |
| - }); |
| 59 | + // Crate collection and ingest a few demo records. |
| 60 | + await vectorStoreCollection.CreateCollectionIfNotExistsAsync(); |
44 | 61 |
|
45 |
| - await collection.UpsertAsync(new MyRecord |
| 62 | + await vectorStoreCollection.UpsertAsync(new Hotel |
46 | 63 | {
|
47 |
| - MyKey = "41", |
48 |
| - Vec1 = new float[] { 0.1f, 0.1f, 1000.1f }, |
49 |
| - Vec2 = new float[] { 1.2f, 1.1f, 1.3f }, |
50 |
| - Data1 = 1338, |
51 |
| - Data2 = DateTimeKind.Utc, |
52 |
| - Data3 = ["a", "abba", "c"] |
| 64 | + HotelId = "1", |
| 65 | + HotelName = "First Hotel", |
| 66 | + Description = "The blue hotel.", |
| 67 | + DescriptionEmbedding = await embeddings.GenerateEmbeddingAsync("The blue hotel."), |
| 68 | + ReferenceLink = "Global Hotel Database, Entry 1337" |
53 | 69 | });
|
54 | 70 |
|
55 |
| - var id = await collection.UpsertAsync(new MyRecord |
| 71 | + await vectorStoreCollection.UpsertAsync(new Hotel |
56 | 72 | {
|
57 |
| - MyKey = "42", |
58 |
| - Vec1 = new float[] { 2.2f, 2.1f, 2.3f, 0.0f }, |
59 |
| - Vec2 = new float[] { 1.2f, 1.1f, 1.3f }, |
60 |
| - Data1 = 1337, |
61 |
| - Data2 = DateTimeKind.Utc, |
62 |
| - Data3 = ["a", "b", "c"] |
| 73 | + HotelId = "2", |
| 74 | + HotelName = "Second Hotel", |
| 75 | + Description = "The green hotel.", |
| 76 | + DescriptionEmbedding = await embeddings.GenerateEmbeddingAsync("The green hotel."), |
| 77 | + ReferenceLink = "Global Hotel Database, Entry 4242" |
63 | 78 | });
|
64 | 79 |
|
65 |
| - var record = await collection.GetAsync(id); |
66 |
| - |
67 |
| - var search = await collection.VectorizedSearchAsync(new float[] { 2.2f, 2.1f, 2.3f }, new VectorSearchOptions |
68 |
| - { |
69 |
| - IncludeTotalCount = true, |
70 |
| - Filter = new VectorSearchFilter(new FilterClause[] |
| 80 | + // Invoke the LLM with a template that uses the search plugin to |
| 81 | + // 1. get related information to the user query from the vector store |
| 82 | + // 2. add the information to the LLM prompt. |
| 83 | + var response = await kernel.InvokePromptAsync( |
| 84 | + promptTemplate: """ |
| 85 | + Please use this information to answer the question: |
| 86 | + {{#with (SearchPlugin-GetTextSearchResults question)}} |
| 87 | + {{#each this}} |
| 88 | + Name: {{Name}} |
| 89 | + Value: {{Value}} |
| 90 | + Source: {{Link}} |
| 91 | + ----------------- |
| 92 | + {{/each}} |
| 93 | + {{/with}} |
| 94 | +
|
| 95 | + Include the source of relevant information in the response. |
| 96 | +
|
| 97 | + Question: {{question}} |
| 98 | + """, |
| 99 | + arguments: new KernelArguments |
71 | 100 | {
|
72 |
| - new AnyTagEqualToFilterClause(nameof(MyRecord.Data3), "abba"), |
73 |
| - new EqualToFilterClause(nameof(MyRecord.Data1), 1338) // TODO: Test char |
74 |
| - }) |
75 |
| - }); |
| 101 | + { "question", "What is the name of the hotel that has the same color as grass?" }, |
| 102 | + }, |
| 103 | + templateFormat: "handlebars", |
| 104 | + promptTemplateFactory: new HandlebarsPromptTemplateFactory()); |
76 | 105 |
|
77 |
| - var genericCollection = vectorStore.GetCollection<string, VectorStoreGenericDataModel<string>>("sk", new VectorStoreRecordDefinition |
78 |
| - { |
79 |
| - Properties = |
80 |
| - [ |
81 |
| - new VectorStoreRecordKeyProperty(nameof(MyRecord.MyKey), typeof(string)), |
82 |
| - new VectorStoreRecordVectorProperty(nameof(MyRecord.Vec1), typeof(ReadOnlyMemory<float>)) |
83 |
| - { |
84 |
| - StoragePropertyName = "xxx" |
85 |
| - }, |
86 |
| - new VectorStoreRecordVectorProperty(nameof(MyRecord.Vec2), typeof(ReadOnlyMemory<float>)), |
87 |
| - new VectorStoreRecordDataProperty(nameof(MyRecord.Data1), typeof(int)), |
88 |
| - new VectorStoreRecordDataProperty(nameof(MyRecord.Data2), typeof(DateTimeKind)), |
89 |
| - new VectorStoreRecordDataProperty(nameof(MyRecord.Data3), typeof(string[])) |
90 |
| - ] |
91 |
| - }); |
92 |
| - |
93 |
| - var genericRecord = await genericCollection.GetAsync(id); |
94 |
| - genericRecord.Key = null; |
95 |
| - |
96 |
| - var id2 = await genericCollection.UpsertAsync(genericRecord); |
97 |
| - var record2 = await collection.GetAsync(id2); |
98 |
| - |
99 |
| - await collection.DeleteAsync(id); |
100 |
| - await collection.DeleteAsync(id2); |
| 106 | + Console.WriteLine(response.ToString()); |
101 | 107 |
|
102 |
| - await collection.DeleteCollectionAsync(); |
| 108 | + // > The name of the hotel that has the same color as grass is "Second Hotel." |
| 109 | + // > This hotel is described as the green hotel. (Source: Global Hotel Database, Entry 4242) |
103 | 110 | }
|
104 | 111 | }
|
105 | 112 |
|
106 |
| -public sealed class MyRecord |
| 113 | +public sealed record Hotel |
107 | 114 | {
|
108 | 115 | [VectorStoreRecordKey]
|
109 |
| - public required string? MyKey { get; init; } |
110 |
| - |
111 |
| - [VectorStoreRecordVector(3)] |
112 |
| - [JsonPropertyName("xxx")] |
113 |
| - public required IReadOnlyCollection<float> Vec1 { get; init; } |
114 |
| - |
115 |
| - [VectorStoreRecordVector(3)] |
116 |
| - public required ReadOnlyMemory<float> Vec2 { get; init; } |
117 |
| - |
118 |
| - [VectorStoreRecordData(IsFilterable = true, IsFullTextSearchable = true)] |
119 |
| - public required int Data1 { get; init; } |
120 |
| - |
121 |
| - [VectorStoreRecordData(IsFilterable = true)] |
122 |
| - public required DateTimeKind Data2 { get; init; } |
| 116 | + public required string HotelId { get; set; } |
123 | 117 |
|
| 118 | + [TextSearchResultName] |
124 | 119 | [VectorStoreRecordData(IsFilterable = true)]
|
| 120 | + public required string HotelName { get; set; } |
125 | 121 |
|
126 |
| -#pragma warning disable CA1819 |
| 122 | + [TextSearchResultValue] |
| 123 | + [VectorStoreRecordData(IsFullTextSearchable = true)] |
| 124 | + public required string Description { get; set; } |
127 | 125 |
|
128 |
| - public required string[] Data3 { get; init; } |
| 126 | + [VectorStoreRecordVector(Dimensions: 1536, DistanceFunction.CosineSimilarity, IndexKind.Hnsw)] |
| 127 | + public ReadOnlyMemory<float>? DescriptionEmbedding { get; set; } |
129 | 128 |
|
130 |
| -#pragma warning restore CA1819 |
| 129 | + [TextSearchResultLink] |
| 130 | + [VectorStoreRecordData] |
| 131 | + public string? ReferenceLink { get; set; } |
131 | 132 | }
|
0 commit comments