Azure OpenAI – Agentic AI – Semantic Kernel – RAG integration

Octavian Mocanu 20 Reputation points
2024-12-24T09:08:14.9033333+00:00

I designed an ASP.NET Core WebAPI application which provides chatbot capabilities by searching for specific products through some internal documents.

I used for this application the RAG approach.

It’s working just fine.

Now I want to leverage the Azure Agentic AI capabilities by updating of this application to use the Azure Agentic AI  – Semantic Kernel approach.

Which means to have Azure Agentic AI on my own data with Semantic Kernel.

I installed the Azure AI SemanticKernel:

dotnet add package Microsoft.SemanticKernel

I created 2 plugin functions: CoveragePlugin and ProductCodePlugin to be called by the Semantic Kernel when some specific context found in searched documents.

I searched on internet/documentation/ ChatGPT but I could not find how to make the application bootstrap setup in such way that Azure OpenAI client (together with the underlying Azure Search Service) to be under the Semantic Kernel.

A version of the Program.cs section is like bellow (but this is not what I want since SearchIndexClient is not under the Semantic Kernel):

using Azure.Search.Documents.Indexes;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddSingleton((_) => new SearchIndexClient(
        new Uri("AZURE_SEARCH_SERVICE_ENDPOINT"),
        new Azure.AzureKeyCredential("AZURE_SEARCH_SERVICE_API_KEY")))
    .AddKernel()
    .AddAzureOpenAIChatCompletion(
        deploymentName: builder.Configuration.GetValue
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
40,912 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 15,011 Reputation points
    2024-12-24T10:37:04.1866667+00:00

    Hello Octavian Mocanu,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you are having challenges to integrate both Azure OpenAI and Azure Search Service under the Azure Semantic Kernel.

    There are two methods to achieve this, by leveraging necessary components (Microsoft.SemanticKernel and Microsoft.SemanticKernel.Connectors.AzureAISearch). Secondly, using the KernelBuilder for a more modular setup.

    The below narrow down based on your scenario:

    Get NuGet packages installed:

    dotnet add package Microsoft.SemanticKernel
    dotnet add package Microsoft.SemanticKernel.Connectors.AzureAISearch --prerelease
    

    Implementation in Program.cs

    using Azure;
    using Azure.Search.Documents;
    using Microsoft.SemanticKernel;
    using Microsoft.SemanticKernel.Connectors.AzureAISearch;
    using Microsoft.SemanticKernel.ChatCompletion;
    using Microsoft.SemanticKernel.Plugins;
    var builder = WebApplication.CreateBuilder(args);
    // Configure Semantic Kernel
    builder.Services.AddKernel(kernel =>
    {
        // Add Azure OpenAI Chat Completion
        kernel.Config.AddAzureChatCompletionService(
            deploymentName: builder.Configuration["AZURE_OPENAI_DEPLOYMENT_NAME"],
            endpoint: builder.Configuration["AZURE_OPENAI_ENDPOINT"],
            apiKey: builder.Configuration["AZURE_OPENAI_API_KEY"]);
        // Add Azure Search Vector Store
        kernel.Config.AddAzureAISearchVectorStore(
            searchEndpoint: new Uri(builder.Configuration["AZURE_SEARCH_SERVICE_ENDPOINT"]),
            credential: new AzureKeyCredential(builder.Configuration["AZURE_SEARCH_SERVICE_API_KEY"]));
        // Add Plugins
        kernel.Config.AddPlugin(new CoveragePlugin(), "CoveragePlugin");
        kernel.Config.AddPlugin(new ProductCodePlugin(), "ProductCodePlugin");
    });
    var app = builder.Build();
    app.Run();
    

    Each plugin should implement logic that interacts with Semantic Kernel's context. For example:

    public class CoveragePlugin : IPlugin
    {
        public string GetCoverageDetails(string query)
        {
            // Example implementation for retrieving coverage details
            return $"Coverage details for {query}.";
        }
    }
    
    public class ProductCodePlugin : IPlugin
    {
        public string GetProductCode(string productName)
        {
            // Example implementation for retrieving product codes
            return $"Product code for {productName} is ABC123.";
        }
    }
    

    In the ChatAgentService, utilize the Semantic Kernel to process queries:

    public class ChatAgentService
    {
        private readonly IKernel _kernel;
        public ChatAgentService(IKernel kernel)
        {
            _kernel = kernel;
        }
        public async Task<string> QueryAsync(string userQuery)
        {
            var context = _kernel.CreateNewContext();
            context["userQuery"] = userQuery;
            var completion = await _kernel.RunAsync(context, "CoveragePlugin.GetCoverageDetails", "ProductCodePlugin.GetProductCode");
            return completion.Result;
        }
    }
    

    Ensure Azure Search results are pre-processed and embedded into the Semantic Kernel context:

    public async Task<List<SearchResult>> SearchDocumentsAsync(string query)
    {
        var searchClient = new SearchClient(new Uri(builder.Configuration["AZURE_SEARCH_SERVICE_ENDPOINT"]),
                                            builder.Configuration["AZURE_SEARCH_INDEX_NAME"],
                                            new AzureKeyCredential(builder.Configuration["AZURE_SEARCH_SERVICE_API_KEY"]));
        var results = await searchClient.SearchAsync<SearchResult>(query);
        return results.Value.ToList();
    }
    

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.