Creating a copilot using OpenAI and/or Azure OpenAI

Let’s create our own copilot, which simulates William Shakespeare by using OpenAI or Azure OpenAI

Sebastian Jensen
medialesson
3 min readFeb 21, 2024

--

Introduction

You can easily make HTTP requests to get answers either from OpenAI or Azure OpenAI. But there is also a NuGet package available (currently in preview), which allows to make all the needed requests against the APIs. In this blog post we will create a simple .NET console application, in which the user first selects the host (OpenAI or Azure OpenAI) and then is able to chat with William Shakespeare.

Let’s code

First we open Visual Studio and create a new .NET console application using .NET8. We will add the Spectre.Console NuGet package and the Azure.AI.OpenAI NuGet package. Make sure to allow prerelease versions, because as I’m writing this blog post, the Azure.AI.OpenAI NuGet package is only available as preview version (1.0.0-beta.13).

We will create a new folder, called Utils in our solution. Within this folder we will create a class, called Statics.cs. This file stores two string variables to identify the host.

Next we will create another file in our Utils folder, called ConsoleHelper.cs. This file contains helpful methods to work with Spectre.Console. For example the used header is created here or the logic to select the host or getting the needed properties to initialize all services accordingly.

Now we can open the Program.cs file to start implementing the real logic of our Shakespeare copilot. The following code snippet contains the whole code of the file, but don’t worry, because I will explain it afterwards.

First we need to ask the user for the desired host. Currently OpenAI and Azure OpenAI are the available options. Depending on the selection we need either ask just for the OpenAI key or we need to ask for the Azure OpenAI endpoint, the Azure OpenAI key and the name of the chat model deployment.

In the next step we create the ChatCompletionsOptions by setting the amount of tokens, the temperature and the deployment name. We also define a ChatRequestMessage by sending the instructions to our copilot. For sure it is currently a very simple system promt, which can be improved for future releases. Next we send a ChatRequestUserMessage by asking the copilot to introduce himself.

Finally we start a while-true-loop to simulate the chat. We are using the GetChatCompletionsStreamingAsync method of our OpenAIClient to stream the answer of our copilot.

If the reponse is complete, we save the message to our ChatCompletionsOptions and ask the user for the next question, which will also be added to the ChatCompletionsOptions and we start again.

Screenshots

The first screenshot shows the host selection.

Depending on your selection, you will be asked for the required parameters.

Finally you can start chatting with William Shakespeare.

Conclusion

In this blog post I’ve used the Azure.AI.OpenAI NuGet package in my .NET console application to work either with OpenAI but also with Azure OpenAI. So instead of doing the requests by hand, we can easily use this package to write our own copilot.

You can find the complete code in my GitHub repository.

--

--

Sebastian Jensen
medialesson

Senior Software Developer & Team Lead @ medialesson GmbH