Some exciting news for us .Net Devs. OpenAI has just released its library for .NET, making our lives so much easier and our development process faster. In this post, we explore using this library and compare it to the older method of individual HTTP requests, which we discussed in this post.
Getting Started
First things first, let’s start a new Console App. I’m using Visual Studio 2022 for this demonstration, so all the instructions and screenshots will reflect that workflow. However, you can follow along with VS Code or any other editor you prefer, as long as you know the corresponding commands.
In our fresh console app, we need to get the OpenAI library. Head to the NuGet management window and search for “OpenAI.” Make sure to check the “Include pre-release” checkbox to find it, as it’s still in beta. As of now, the version is 2.0.0-beta.7
, and it’s a dynamically evolving library. If you’re using a later version, you might notice some slight changes to the object model.
Setting Up the OpenAI Client
Once we have the correct library reference, the first step is to instantiate a client to access OpenAI. OpenAI offers various clients for its major products, and some of the notable ones include:
ChatClient
– for chating with GPTImageClient
– for accessing DALL-E to generate imagesAudioClient
– for accessing OpenAI’s speech-to-text product
For this tutorial, we’ll be instantiating a ChatClient to tap into the power of language models (LLMs). We specify the particular LLM we want to use and provide our API key. Here’s how you do it:
using OpenAI.Chat;
namespace TestOpenAILibrary
{
internal class Program
{
static void Main(string[] args)
{
ChatClient client = new ChatClient(model: "gpt-4o", Environment.GetEnvironmentVariable("MY_OPENAI_KEY"));
}
}
}
The call to Environment.GetEnvironmentVariable("MY_OPENAI_KEY")
means the key is being read from your local machine’s environment variables. If you’re on Windows, you can add your key by clicking the Windows button, typing “environment variables,” and then navigating to the System Properties dialog box. Click the Environment Variables button near the bottom and add your key there.
Sending a Query to ChatGPT
Now that we have our client set up, it’s incredibly easy to send a query to ChatGPT-4. Just add two lines to the main()
function:
static void Main(string[] args)
{
ChatClient client = new ChatClient(model: "gpt-4o", Environment.GetEnvironmentVariable("MY_OPENAI_KEY"));
var response = client.CompleteChat("How many NFL teams are currently active?");
Console.WriteLine("Response = " + response.Value);
}
There you have it! Running this should give you something like below:
Comparing to the Old Code
When you compare this new method to the old code, you’ll notice that many of the cumbersome details are now hidden and neatly tucked away. Plus, there’s a cohesive object model that makes accessing OpenAI’s products a breeze.
Feel free to tinker with this code, have longer conversations, and even try out cool features like image generation.
Until next time, happy coding! 🚀