DEV Community

El Bruno for Microsoft Azure

Posted on • Originally published at elbruno.com on

How to Use #SemanticKernel with OpenAI and Azure OpenAI in C# – 1/N

Hi!

Today I’m starting a series of posts to describe an “easy way to use” AI Services with Semantic Kernel. I’m a fan of code, so I’ll focus mostly on code samples.

Semantic Kernel (via Copilot)

Semantic Kernel is a powerful tool that allows developers to integrate cutting-edge language models into their applications with ease. Semantic Kernel provides a unified interface to access various language models from different sources, such as OpenAI, AzureOpenAI, and Hugging Face. Semantic Kernel also enables developers to store and retrieve memories, create dynamic prompts, and combine functions together with planners.

With Semantic Kernel, developers can supercharge their problem-solving creativity and build AI solutions that can understand natural language, generate content, answer questions, and more. Semantic Kernel is an open-source project that can be run on any local machine or cloud environment. Semantic Kernel offers tutorials, guides, and examples to help developers get started quickly and learn how to use its features.

🌐 Hello World with OpenAI

Let’s start! First, lets create a Console Application using NET 8, and add the Semantic Kernel Nuget package. Current version is 1.0.4 and it looks like this in Visual Studio 2022.

Remeber that you can also install the package with the command:


nuget: Microsoft.SemanticKernel, 1.4.0

Enter fullscreen mode Exit fullscreen mode

_ Important: The following demo uses OpenAI APIs. You need an OpenAI Dev account. OpenAI Account page describes how to create an account._ https://platform.openai.com/docs/quickstart

Once you have your OpenAI account and keys, let’s review the main steps that we need to create a “Hello World” application:

  • Add services to the KernelBuilder, like Chat
  • Build a Kernel
  • Run a prompt with the Kernel

Those 3 simple steps can be implemented like this.

You can use your own OpenAI keys and you will be able to run the program! The output should be similar to this one:

☁ Switching to Azure OpenAI

Semantic Kernel implement an interface to main AI services like Chat and AI Completion. Out of the box, it also provides these capabilities in AI Services like OpenAI APIs and Azure OpenAI Services.

So, in order to change our chat demo from using OpenAI APIs to Azure OpenAI Services, we just need to change these lines.

  • Change the specific keys to work with Azure OpenAI.
  • Add an AzureOpenAIChatCompletion service

The code is literally the same!

Of course the test runs. However, it seems that the model in this scenario is a little picky to choose Messi as best player 😀.

Conclusion

In this article, we have explored how to use Semantic Kernel with OpenAI and Azure OpenAI in C#. We have learned how to set up the necessary tools and libraries, and how to run some examples of Semantic Kernel prompts.

By using Semantic Kernel with OpenAI and Azure OpenAI, C# programmers can leverage the power of natural language processing and artificial intelligence to work with information and solutions more efficiently and effectively.

In next post we will review other AI services, how to add specific configuration and other scenarios.

You can find the complete source here: https://aka.ms/sktutrepo

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


Top comments (0)