How to Use #SemanticKernel with OpenAI and Azure OpenAI in C# – 1/N
Tags: englishpost, azureopenai, codesample
Published: 2024-02-28 18:17:06

---
title: How to Use #SemanticKernel with OpenAI and Azure OpenAI in C# – 1/N
published: true
date: 2024-02-27 14:00:00 UTC
tags: EnglishPost,AzureOpenAI,CodeSample,EnglishPost
canonical_url: 
---

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](https://learn.microsoft.com/en-us/semantic-kernel/overview/?WT.mc_id=academic-00000-brunocapuano) (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.

![](https://brunocapuano.files.wordpress.com/2024/02/add-semantic-kernel-package.png?w=1024)

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

```

nuget: Microsoft.SemanticKernel, 1.4.0

```

_ **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_](https://platform.openai.com/docs/quickstart%20)

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.

{% gist  https://gist.github.com/elbruno/4bc24b3debf8e9e13b9b4b0fe647b4bf %}

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

![](https://brunocapuano.files.wordpress.com/2024/02/image-4.png?w=1024)

## ☁ 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

![](https://brunocapuano.files.wordpress.com/2024/02/image-5.png?w=1024)

The code is literally the same!

{% gist https://gist.github.com/elbruno/c1b4702652325d64425e58a7209b05ec %}

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

![](https://brunocapuano.files.wordpress.com/2024/02/image-6.png?w=1024)

## 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](https://aka.ms/sktutrepo)

Happy coding!

Greetings

El Bruno

More posts in my blog [ElBruno.com](https://www.elbruno.com).

More info in [https://beacons.ai/elbruno](https://beacons.ai/elbruno)

* * *