28 June, 2024

✨Building an Engaging Chat Program with Azure OpenAI and .NET Semantic Kernel 🧿

In the ever-evolving landscape of artificial intelligence and cloud computing, creating interactive and intelligent applications has become more accessible and powerful. Today, we’ll dive into the fascinating world of integrating Azure OpenAI with .NET Semantic Kernel to build a simple yet engaging chat program. This blog post will walk you through the key components of the application, highlighting the elegance and efficiency of the integration.


 

Setting the Stage: Loading Configuration

The first step in our journey is setting up the environment configuration. Using the DotNetEnv library, we can effortlessly load environment variables from a .env file. This approach keeps our sensitive information secure and makes the configuration process seamless.

using DotNetEnv;

Env.Load();

This line of code ensures that our application can access the necessary Azure OpenAI credentials and other configuration details stored in the .env file.

Initializing the Kernel: The Common Class

The heart of our chat program lies in the initialization of the Semantic Kernel. The Common class is responsible for setting up the kernel using the credentials and model details from the environment variables.

using Microsoft.SemanticKernel;

namespace AzureSemanticKernel.AI
{
public static class Common
{
private static string key = Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");
private static string model = Environment.GetEnvironmentVariable("AZURE_OPENAI_MODEL");
private static string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");

public static Kernel initializeKernel()
{
var kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.Services.AddAzureOpenAIChatCompletion(model, endpoint, key);
var kernel = kernelBuilder.Build();

return kernel;
}
}
}

In this class, we retrieve the API key, model, and endpoint from the environment variables and use them to configure the kernel. The initializeKernel method creates and returns a fully initialized kernel ready to handle chat requests.

Bringing It All Together: The Main Controller

Now that we have our configuration and kernel setup, let’s look at how we can create a simple yet powerful chat controller. The SimpleController class is where the magic happens. It receives user messages, processes them using the Semantic Kernel, and returns the responses.

using Microsoft.AspNetCore.Mvc;
using Microsoft.SemanticKernel;

namespace AzureSemanticKernel.AI.Controllers
{
public class SimpleController : Controller
{
[HttpPost]
public async Task<string> GetChatKernelResponse(string userMessage)
{
try
{
var kernel = Common.initializeKernel();

var result = await kernel.InvokePromptAsync(userMessage);

return result.ToString();
}
catch (Exception ex)
{
return ex.Message;
}
}
}
}

The GetChatKernelResponse action handles POST requests with user messages. It initializes the kernel, invokes the chat completion prompt, and returns the response. Error handling ensures that any exceptions are caught and their messages are returned to the user.

 
 
 

Conclusion

By leveraging Azure OpenAI and .NET Semantic Kernel, we’ve created a simple yet robust chat program with just few lines of code. This integration demonstrates the power and flexibility of modern AI and cloud technologies. Whether you’re a seasoned developer or just starting, this project showcases how easily you can build engaging and intelligent applications.

As AI continues to evolve, the possibilities for creating interactive and responsive applications are endless. Dive into the code, experiment with different models and prompts, and unleash the full potential of Azure OpenAI and .NET Semantic Kernel in your projects. Happy coding!

 

No comments:

Post a Comment