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.