Forum Discussion

Bhavin163884's avatar
Bhavin163884
Copper Contributor
Jun 27, 2024

Kernel Memory - Retrieval Augmented Generation (RAG) using Azure Open AI

Hello Community,
I am seeking for guidance here, Looking for Kernel Memory - Retrieval Augmented Generation (RAG) using Azure Open AI which can read file in kernel memory. I can ask question and based on memory it can answer my questions. I want to use .NetCore here for implementation.

I have referred below article but i did not found configuration related to Azure Open AI.

https://github.com/microsoft/kernel-memory/tree/main

  • ConstantineV's avatar
    ConstantineV
    Copper Contributor

    Bhavin163884 

    // Program.cs
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using YourNamespace.Services;
    
    namespace YourNamespace
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                CreateHostBuilder(args).Build().Run();
            }
    
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureAppConfiguration((hostingContext, config) =>
                    {
                        config.AddEnvironmentVariables();
                        config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                        config.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
                    })
                    .ConfigureServices((hostContext, services) =>
                    {
                        services.AddSingleton<IParametersAzureService, ParametersAzureService>();
                        services.AddSingleton<IKernelService, KernelService>();
                        services.AddHostedService<Worker>();
                    });
        }
    }
    
    // ParametersAzureService.cs
    using Microsoft.Extensions.Configuration;
    
    namespace YourNamespace.Services
    {
        public interface IParametersAzureService
        {
            string KernelMemoryServiceEndpoint { get; }
            string KernelMemoryServiceApiKey { get; }
        }
    
        public class ParametersAzureService : IParametersAzureService
        {
            private readonly IConfiguration _configuration;
    
            public ParametersAzureService(IConfiguration configuration)
            {
                _configuration = configuration;
            }
    
            public string KernelMemoryServiceEndpoint => _configuration["KernelMemoryServiceEndpoint"];
            public string KernelMemoryServiceApiKey => _configuration["KernelMemoryServiceApiKey"];
        }
    }
    
    // KernelService.cs
    using Microsoft.KernelMemory;
    
    namespace YourNamespace.Services
    {
        public interface IKernelService
        {
            IKernelMemory GetMemoryClient();
        }
    
        public class KernelService : IKernelService
        {
            private readonly IParametersAzureService _parametersAzureService;
    
            public KernelService(IParametersAzureService parametersAzureService)
            {
                _parametersAzureService = parametersAzureService;
            }
    
            public IKernelMemory GetMemoryClient()
            {
                string endpoint = _parametersAzureService.KernelMemoryServiceEndpoint;
                string apiKey = _parametersAzureService.KernelMemoryServiceApiKey;
    
                if (string.IsNullOrEmpty(endpoint) || string.IsNullOrEmpty(apiKey))
                {
                    throw new InvalidOperationException("KernelMemoryService endpoint or API key is not configured.");
                }
    
                return new MemoryWebClient(endpoint, apiKey: apiKey);
            }
        }
    }
    
    // Worker.cs
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    using System;
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.KernelMemory;
    
    namespace YourNamespace
    {
        public class Worker : BackgroundService
        {
            private readonly ILogger<Worker> _logger;
            private readonly IKernelService _kernelService;
    
            public Worker(ILogger<Worker> logger, IKernelService kernelService)
            {
                _logger = logger;
                _kernelService = kernelService;
            }
    
            protected override async Task ExecuteAsync(CancellationToken stoppingToken)
            {
                while (!stoppingToken.IsCancellationRequested)
                {
                    _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
    
                    try
                    {
                        var memory = _kernelService.GetMemoryClient();
    
                        // Example usage of importing a document
                        string content = "This is a sample document content.";
                        string memoryKey = Guid.NewGuid().ToString();
                        string fileName = "sample.txt";
                        string indexName = "default";
                        var tagsCollection = new TagCollection { { "category", "sample" } };
    
                        using (var memoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(content)))
                        {
                            var docId = await memory.ImportDocumentAsync(
                                content: memoryStream,
                                documentId: memoryKey,
                                fileName: fileName,
                                index: indexName,
                                steps: Constants.PipelineWithSummary,
                                tags: tagsCollection);
    
                            _logger.LogInformation($"Document imported with ID: {docId}");
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "An error occurred while processing");
                    }
    
                    await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
                }
            }
        }
    }
    To use this improved version:
    
    Create an appsettings.json file in your project root:
    {
      "KernelMemoryServiceEndpoint": "https://your-endpoint-here",
      "KernelMemoryServiceApiKey": "your-api-key-here"
    }

     

Resources