33#pragma warning disable SKEXP0070
44#pragma warning disable SKEXP0050
55#pragma warning disable SKEXP0001
6+ #pragma warning disable SKEXP0020
67
78using System ;
89using System . IO ;
910using Microsoft . Extensions . Configuration ;
11+ using Microsoft . Extensions . VectorData ;
1012using Microsoft . SemanticKernel ;
1113using Microsoft . SemanticKernel . ChatCompletion ;
14+ using Microsoft . SemanticKernel . Connectors . InMemory ;
15+ using Microsoft . SemanticKernel . Data ;
1216using Microsoft . SemanticKernel . Embeddings ;
13- using Microsoft . SemanticKernel . Memory ;
14- using Microsoft . SemanticKernel . Plugins . Memory ;
17+ using Microsoft . SemanticKernel . PromptTemplates . Handlebars ;
1518
1619// Ensure you follow the preparation steps provided in the README.md
1720var config = new ConfigurationBuilder ( ) . AddUserSecrets < Program > ( ) . Build ( ) ;
3841var chatService = kernel . GetRequiredService < IChatCompletionService > ( ) ;
3942var embeddingService = kernel . GetRequiredService < ITextEmbeddingGenerationService > ( ) ;
4043
41- // Create a memory store and a semantic text memory
42- var memoryStore = new VolatileMemoryStore ( ) ;
43- var semanticTextMemory = new SemanticTextMemory ( memoryStore , embeddingService ) ;
44-
45- // Loading it for Save, Recall and other methods
46- kernel . ImportPluginFromObject ( new TextMemoryPlugin ( semanticTextMemory ) ) ;
44+ // Create a vector store and a collection to store information
45+ var vectorStore = new InMemoryVectorStore ( ) ;
46+ var collection = vectorStore . GetCollection < string , InformationItem > ( "ExampleCollection" ) ;
47+ await collection . CreateCollectionIfNotExistsAsync ( ) ;
4748
4849// Save some information to the memory
4950var collectionName = "ExampleCollection" ;
5051foreach ( var factTextFile in Directory . GetFiles ( "Facts" , "*.txt" ) )
5152{
5253 var factContent = File . ReadAllText ( factTextFile ) ;
53- await semanticTextMemory . SaveInformationAsync (
54- collection : collectionName ,
55- text : factContent ,
56- id : Guid . NewGuid ( ) . ToString ( ) ) ;
54+ await collection . UpsertAsync ( new ( )
55+ {
56+ Id = Guid . NewGuid ( ) . ToString ( ) ,
57+ Text = factContent ,
58+ Embedding = await embeddingService . GenerateEmbeddingAsync ( factContent )
59+ } ) ;
5760}
5861
62+ // Add a plugin to search the database with.
63+ var vectorStoreTextSearch = new VectorStoreTextSearch < InformationItem > ( collection , embeddingService ) ;
64+ kernel . Plugins . Add ( vectorStoreTextSearch . CreateWithSearch ( "SearchPlugin" ) ) ;
65+
5966// Start the conversation
6067while ( true )
6168{
@@ -72,8 +79,16 @@ await semanticTextMemory.SaveInformationAsync(
7279
7380 // Invoke the kernel with the user input
7481 var response = kernel . InvokePromptStreamingAsync (
75- promptTemplate : @"Question: {{$input}}
76- Answer the question using the memory content: {{Recall}}" ,
82+ promptTemplate : @"Question: {{input}}
83+ Answer the question using the memory content:
84+ {{#with (SearchPlugin-Search input)}}
85+ {{#each this}}
86+ {{this}}
87+ -----------------
88+ {{/each}}
89+ {{/with}}" ,
90+ templateFormat : "handlebars" ,
91+ promptTemplateFactory : new HandlebarsPromptTemplateFactory ( ) ,
7792 arguments : new KernelArguments ( )
7893 {
7994 { "input" , question } ,
@@ -89,3 +104,17 @@ await semanticTextMemory.SaveInformationAsync(
89104
90105 Console . WriteLine ( ) ;
91106}
107+
108+ internal sealed class InformationItem
109+ {
110+ [ VectorStoreRecordKey ]
111+ [ TextSearchResultName ]
112+ public string Id { get ; set ; } = string . Empty ;
113+
114+ [ VectorStoreRecordData ]
115+ [ TextSearchResultValue ]
116+ public string Text { get ; set ; } = string . Empty ;
117+
118+ [ VectorStoreRecordVector ( Dimensions : 384 ) ]
119+ public ReadOnlyMemory < float > Embedding { get ; set ; }
120+ }
0 commit comments