chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1 @@
|
||||
Kernel Memory (KM) is a multi-modal AI Service specialized in the efficient indexing of datasets through custom continuous data hybrid pipelines, with support for Retrieval Augmented Generation (RAG), synthetic memory, prompt engineering, and custom semantic memory processing.
|
||||
@@ -0,0 +1 @@
|
||||
Semantic Kernel is a lightweight, open-source development kit that lets you easily build AI agents and integrate the latest AI models into your C#, Python, or Java codebase. It serves as an efficient middleware that enables rapid delivery of enterprise-grade solutions.
|
||||
@@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<NoWarn>$(NoWarn);CA2007;CS0612;VSTHRD111;SKEXP0050;SKEXP0001</NoWarn>
|
||||
<UserSecretsId>5ee045b0-aea3-4f08-8d31-32d1a6f8fed0</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\src\Connectors\Connectors.Onnx\Connectors.Onnx.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\Extensions\PromptTemplates.Handlebars\PromptTemplates.Handlebars.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\SemanticKernel.Abstractions\SemanticKernel.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\SemanticKernel.Core\SemanticKernel.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" />
|
||||
<PackageReference Include="Microsoft.SemanticKernel.Connectors.InMemory" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Update="Facts\*.txt">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,139 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.VectorData;
|
||||
using Microsoft.ML.OnnxRuntimeGenAI;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Microsoft.SemanticKernel.ChatCompletion;
|
||||
using Microsoft.SemanticKernel.Connectors.InMemory;
|
||||
using Microsoft.SemanticKernel.Connectors.Onnx;
|
||||
using Microsoft.SemanticKernel.Data;
|
||||
using Microsoft.SemanticKernel.PromptTemplates.Handlebars;
|
||||
|
||||
Console.OutputEncoding = System.Text.Encoding.UTF8;
|
||||
|
||||
// Ensure you follow the preparation steps provided in the README.md
|
||||
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
|
||||
|
||||
// Path to the folder of your downloaded ONNX PHI-3 model
|
||||
var chatModelPath = config["Onnx:ModelPath"]!;
|
||||
var chatModelId = config["Onnx:ModelId"] ?? "phi-3";
|
||||
|
||||
// Path to the file of your downloaded ONNX BGE-MICRO-V2 model
|
||||
var embeddingModelPath = config["Onnx:EmbeddingModelPath"]!;
|
||||
|
||||
// Path to the vocab file your ONNX BGE-MICRO-V2 model
|
||||
var embeddingVocabPath = config["Onnx:EmbeddingVocabPath"]!;
|
||||
|
||||
// If using Onnx GenAI 0.5.0 or later, the OgaHandle class must be used to track
|
||||
// resources used by the Onnx services, before using any of the Onnx services.
|
||||
using var ogaHandle = new OgaHandle();
|
||||
|
||||
// Load the services
|
||||
var builder = Kernel.CreateBuilder()
|
||||
.AddOnnxRuntimeGenAIChatCompletion(chatModelId, chatModelPath)
|
||||
.AddBertOnnxEmbeddingGenerator(embeddingModelPath, embeddingVocabPath);
|
||||
|
||||
// Build Kernel
|
||||
var kernel = builder.Build();
|
||||
|
||||
// Get the instances of the services
|
||||
using var chatService = kernel.GetRequiredService<IChatCompletionService>() as OnnxRuntimeGenAIChatCompletionService;
|
||||
var embeddingService = kernel.GetRequiredService<IEmbeddingGenerator<string, Embedding<float>>>();
|
||||
|
||||
// Create a vector store and a collection to store information
|
||||
var vectorStore = new InMemoryVectorStore(new() { EmbeddingGenerator = embeddingService });
|
||||
var collection = vectorStore.GetCollection<string, InformationItem>("ExampleCollection");
|
||||
await collection.EnsureCollectionExistsAsync();
|
||||
|
||||
// Save some information to the memory
|
||||
var collectionName = "ExampleCollection";
|
||||
foreach (var factTextFile in Directory.GetFiles("Facts", "*.txt"))
|
||||
{
|
||||
var factContent = File.ReadAllText(factTextFile);
|
||||
await collection.UpsertAsync(new InformationItem()
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Text = factContent
|
||||
});
|
||||
}
|
||||
|
||||
// Add a plugin to search the database with.
|
||||
var vectorStoreTextSearch = new VectorStoreTextSearch<InformationItem>(collection);
|
||||
kernel.Plugins.Add(vectorStoreTextSearch.CreateWithSearch("SearchPlugin"));
|
||||
|
||||
// Start the conversation
|
||||
while (true)
|
||||
{
|
||||
// Get user input
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.Write("User > ");
|
||||
var question = Console.ReadLine()!;
|
||||
|
||||
// Clean resources and exit the demo if the user input is null or empty
|
||||
if (question is null || string.IsNullOrWhiteSpace(question))
|
||||
{
|
||||
// To avoid any potential memory leak all disposable
|
||||
// services created by the kernel are disposed
|
||||
DisposeServices(kernel);
|
||||
return;
|
||||
}
|
||||
|
||||
// Invoke the kernel with the user input
|
||||
var response = kernel.InvokePromptStreamingAsync(
|
||||
promptTemplate: @"Question: {{input}}
|
||||
Answer the question using the memory content:
|
||||
{{#with (SearchPlugin-Search input)}}
|
||||
{{#each this}}
|
||||
{{this}}
|
||||
-----------------
|
||||
{{/each}}
|
||||
{{/with}}",
|
||||
templateFormat: "handlebars",
|
||||
promptTemplateFactory: new HandlebarsPromptTemplateFactory(),
|
||||
arguments: new KernelArguments()
|
||||
{
|
||||
{ "input", question },
|
||||
{ "collection", collectionName }
|
||||
});
|
||||
|
||||
Console.Write("\nAssistant > ");
|
||||
|
||||
await foreach (var message in response)
|
||||
{
|
||||
Console.Write(message);
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
static void DisposeServices(Kernel kernel)
|
||||
{
|
||||
foreach (var target in kernel
|
||||
.GetAllServices<IChatCompletionService>()
|
||||
.OfType<IDisposable>())
|
||||
{
|
||||
target.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Information item to represent the embedding data stored in the memory
|
||||
/// </summary>
|
||||
internal sealed class InformationItem
|
||||
{
|
||||
[VectorStoreKey]
|
||||
[TextSearchResultName]
|
||||
public string Id { get; set; } = string.Empty;
|
||||
|
||||
[VectorStoreData]
|
||||
[TextSearchResultValue]
|
||||
public string Text { get; set; } = string.Empty;
|
||||
|
||||
[VectorStoreVector(Dimensions: 384)]
|
||||
public string Embedding => this.Text;
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
# Onnx Simple RAG (Retrieval Augmented Generation) Sample
|
||||
|
||||
This sample demonstrates how you can do RAG using Semantic Kernel with the ONNX Connector that enables running Local Models straight from files.
|
||||
|
||||
In this example we setup two ONNX AI Services:
|
||||
- Chat Completion with [Microsoft's Phi-3-ONNX](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx) model
|
||||
- Text Embeddings with [Taylor's BGE Micro V2](https://huggingface.co/TaylorAI/bge-micro-v2) for embeddings to enable RAG for user queries.
|
||||
|
||||
> [!IMPORTANT]
|
||||
> You can modify to use any other combination of models enabled for ONNX runtime.
|
||||
|
||||
## Semantic Kernel used Features
|
||||
|
||||
- [Chat Completion Service](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatCompletionService.cs) - Using the Chat Completion Service from [Onnx Connector](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/Connectors/Connectors.Onnx/OnnxRuntimeGenAIChatCompletionService.cs) to generate responses from the Local Model.
|
||||
- [Text Embeddings Generation Service]() - Using the Text Embeddings Generation Service from [Onnx Connector](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/Connectors/Connectors.Onnx/BertOnnxTextEmbeddingGenerationService.cs) to generate
|
||||
- [Vector Store](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/Connectors/VectorData.Abstractions/VectorStorage/IVectorStore.cs) Using Vector Store Service with [InMemoryVectorStore](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/Connectors/Connectors.Memory.InMemory/InMemoryVectorStore.cs) to store and retrieve embeddings in memory for RAG.
|
||||
- [Semantic Text Memory](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel.Core/Memory/SemanticTextMemory.cs) to manage the embeddings in memory for RAG.
|
||||
- [Text Memory Plugin](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/Plugins/Plugins.Memory/TextMemoryPlugin.cs) to enable memory retrieval functions (Recall) to be used with Prompts for RAG.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0).
|
||||
|
||||
## 1. Configuring the sample
|
||||
|
||||
### Downloading the Models
|
||||
|
||||
For this example we chose Hugging Face as our repository for download of the local models, go to a directory of your choice where the models should be downloaded and run the following commands:
|
||||
|
||||
```powershell
|
||||
git clone https://huggingface.co/TaylorAI/bge-micro-v2
|
||||
git clone https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx
|
||||
```
|
||||
|
||||
> [!IMPORTANT]
|
||||
> Both `BGE-Micro-V2` and `Phi-3` models are too large to be downloaded by the `git clone` command alone if you don't have [git-lfs extension](https://git-lfs.com/) installed, for this you may need to download the models manually and overwrite the files in the cloned directories.
|
||||
|
||||
- Manual download [BGE-Micro-V2](https://huggingface.co/TaylorAI/bge-micro-v2/resolve/main/onnx/model.onnx?download=true) (69 MB)
|
||||
- Manual download [Phi-3-Mini-4k CPU](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx/resolve/main/cpu_and_mobile/cpu-int4-rtn-block-32/phi3-mini-4k-instruct-cpu-int4-rtn-block-32.onnx.data?download=true) (≈2.7 GB)
|
||||
|
||||
Update the `Program.cs` file lines below with the paths to the models you downloaded in the previous step.
|
||||
|
||||
```csharp
|
||||
// Path to the folder of your downloaded ONNX PHI-3 model
|
||||
var chatModelPath = @"C:\path\to\huggingface\Phi-3-mini-4k-instruct-onnx\cpu_and_mobile\cpu-int4-rtn-block-32";
|
||||
|
||||
// Path to the file of your downloaded ONNX BGE-MICRO-V2 model
|
||||
var embeddingModelPath = @"C:\path\to\huggingface\bge-micro-v2\onnx\model.onnx";
|
||||
|
||||
// Path to the vocab file your ONNX BGE-MICRO-V2 model
|
||||
var embeddingVocabPath = @"C:\path\to\huggingface\bge-micro-v2\vocab.txt";
|
||||
```
|
||||
|
||||
**Optional**: Change or add any fact text you want your AI to know about to the `facts` list in the `Program.cs` file.
|
||||
|
||||
```csharp
|
||||
foreach (var fact in new[] {
|
||||
"My fact 1.",
|
||||
"My fact 2." })
|
||||
{
|
||||
```
|
||||
|
||||
## Configuring the sample
|
||||
|
||||
The sample can be configured by using the command line with .NET [Secret Manager](https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets) to avoid the risk of leaking secrets into the repository, branches and pull requests.
|
||||
|
||||
### Using .NET [Secret Manager](https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets)
|
||||
|
||||
```powershell
|
||||
dotnet user-secrets set "Onnx:ModelId" ".. Onnx model id" (default: phi-3)
|
||||
dotnet user-secrets set "Onnx:ModelPath" ".. your Onnx model folder path .."
|
||||
dotnet user-secrets set "Onnx:EmbeddingModelPath" ".. your Onnx model file path .."
|
||||
dotnet user-secrets set "Onnx:EmbeddingVocabPath" ".. your Onnx model vocab file path .."
|
||||
```
|
||||
|
||||
## 3. Running the sample
|
||||
|
||||
After configuring the sample, to build and run the console application just hit `F5`.
|
||||
|
||||
To build and run the console application from the terminal use the following commands:
|
||||
|
||||
```powershell
|
||||
dotnet build
|
||||
dotnet run
|
||||
```
|
||||
|
||||
Ask to the Model any question you want, the AI will try to answer based on the facts you provided.
|
||||
|
||||
### Example of a conversation
|
||||
|
||||
> **User** > What is semantic kernel SDK used for?
|
||||
|
||||
> **Assistant** > The Semantic Kernel SDK is used for developing AI agents and integrating the latest AI models into your C#, Python, or Java codebase. It acts as a lightweight, open-source development kit that facilitates the creation of AI applications with enterprise-grade capabilities. By serving as an efficient middleware, it allows for the rapid development and deployment of AI solutions across various programming environments.
|
||||
|
||||
> **User** > When should I use Kernel Memory?
|
||||
|
||||
> **Assistant** > Kernel Memory is best suited for tasks that involve large-scale continuous data, require efficient indexing and processing, and benefit from the integration of retrieval-based and generative models, synthetic memory, prompt engineering, and custom semantic memory processing.
|
||||
Reference in New Issue
Block a user