Skip to content

Commit 8f81caf

Browse files
authored
.Net: Update ONNX Rag sample to use vector store. (#9546)
### Motivation and Context #9377 ### Description Update ONNX Rag sample to use vector store. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
1 parent 113cea0 commit 8f81caf

2 files changed

Lines changed: 45 additions & 15 deletions

File tree

‎dotnet/samples/Demos/OnnxSimpleRAG/OnnxSimpleRAG.csproj‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<ProjectReference Include="..\..\..\src\Connectors\Connectors.Memory.InMemory\Connectors.Memory.InMemory.csproj" />
1112
<ProjectReference Include="..\..\..\src\Connectors\Connectors.Onnx\Connectors.Onnx.csproj" />
12-
<ProjectReference Include="..\..\..\src\Plugins\Plugins.Memory\Plugins.Memory.csproj" />
13+
<ProjectReference Include="..\..\..\src\Extensions\PromptTemplates.Handlebars\PromptTemplates.Handlebars.csproj" />
1314
<ProjectReference Include="..\..\..\src\SemanticKernel.Abstractions\SemanticKernel.Abstractions.csproj" />
1415
<ProjectReference Include="..\..\..\src\SemanticKernel.Core\SemanticKernel.Core.csproj" />
1516
</ItemGroup>

‎dotnet/samples/Demos/OnnxSimpleRAG/Program.cs‎

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
#pragma warning disable SKEXP0070
44
#pragma warning disable SKEXP0050
55
#pragma warning disable SKEXP0001
6+
#pragma warning disable SKEXP0020
67

78
using System;
89
using System.IO;
910
using Microsoft.Extensions.Configuration;
11+
using Microsoft.Extensions.VectorData;
1012
using Microsoft.SemanticKernel;
1113
using Microsoft.SemanticKernel.ChatCompletion;
14+
using Microsoft.SemanticKernel.Connectors.InMemory;
15+
using Microsoft.SemanticKernel.Data;
1216
using 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
1720
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
@@ -38,24 +41,28 @@
3841
var chatService = kernel.GetRequiredService<IChatCompletionService>();
3942
var 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
4950
var collectionName = "ExampleCollection";
5051
foreach (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
6067
while (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

Comments
 (0)