Open Menu dzosoft
Close Menu dzosoft



Hiding your face is not a solution!

Publish perfectly-optimized content in 1-click


Using ChatGPT in C#


In this article I'll be showing you how to cool open AI from your c-sharp code,we'll be using the Azure open AI client Library
for .net which integrates open ai's capabilities into the .net environment as your open AI is a managed service
that lets developers deploy tune and generate content from open AI models on Azure resources the Azure open AI client library for .net is an adaptation of openai's rest apis
Using ChatGPT in C#
It provides an idiomatic interface and an excellent integration with the Azure SDK ecosystem, you can connect to both Azure open AI resources but also to non-azure open AI endpoints such as the one on so that means that you can use this
package whether you're working in Azure or not now before we start using the package there are a couple of prerequisites if you'd like to use an Azure open AI resource you need an Azure subscription and as your openai access so that will
allow you to create Azure open AI resources get both connection URL and API keys now
Using ChatGPT in C#
In this article I'm not going to be using Azure I'm just going to be using an API key that I have on
Using ChatGPT in C#
But you can do both using this package so to get started let's create a new console application and we'll use this in .net framework six because it's got long term supports and then when that's created we're going to be adding this as your dot AI dot
open AI nuget package
Using ChatGPT in C#
Using ChatGPT in C#
Using ChatGPT in C#
Using ChatGPT in C#
Now you'll notice this is a pre-release version hopefully soon there'll be a full production release of this but we can do everything we need to do with the pre-release so if we open up the nuget packages on the solution Explorer and click this include pre-release flag here
that will last find the Azure dot AI dot openai package so go ahead and install that and we just install all these prerequisites as well brilliant now we can go into our program.cs class and we can start using the open AI package
so you get this open AI client type here and the open AI client type will take in an API key now remember at the start of this article I said that you have the option of using either of the Azure services or just directly talking to I'm going
to be talking directly to and so to do that I need an API key on so I'm going to go to on the web and click on my face in the top right
hand corner and select view API Keys now this gives you the option to create a new API key in your openai account so go ahead and do that now and we'll just call this my c-sharp project create that secret key and copy it to the clipboard then if we go back into the code we can
paste our API key into the Constructor of the opener client
using Azure.AI.OpenAI;


and we've now got a client instance that we can use to talk to open AI so first up let's make a completion call to open AI so this is a standard gpt3 call where we send it a prompt
    CompletionsOptions requestOptions = new CompletionsOptions();
    requestOptions.DeploymentName = "text-davinci-002";
    requestOptions.Prompts.Add("Tell me about ChatGPT ? ");
    var openAIResponse = await  openAiClient.GetCompletionsAsync(requestOptions);
    foreach (var choice in openAIResponse.Value.Choices)

and we get a response so we create new open AI response and then await openAiClient.getcompletions async now in here we need to put in the name of the gpt3 model that we want to use with openai so if you go over to and you can go into the playground you can see on the right hand side here by doing this drop down there's a bunch of gpt3 models that we can use so all we
Using ChatGPT in C#
need to do is remember the name of one of these models so for example let's just use text-davinci-002 back in our code we can put text-davinci-002 inside the DeploymentName property and then we can put a prompt in there and our prompt is just going to tell me about ChatGPT ? that's all we need to do

You could download the whole project here   Download this project

It might also interest you

C# Get Started

Quick overview of the differences between C# and Visual Basic .NET

How to create a local MySQL connection in C#

How to execute a .NET Assembly inside a C# Program

Leave comment

Save nickname and email in this browser for the next time.

close publicity
Publish perfectly-optimized content in 1-click