Easy to Use .NET OpenAI Library

I fired up Visual Studio yesterday to check out OpenAI to work on a little web app for my parenting website. Along the way I discovered the cool Betalgo .NET library which wraps the OpenAI APIs. I was able to get things up and running literally within minutes.

Just check out how easy it is to initialize the library and request the completion to a prompt:

var openAiOptions = new OpenAiOptions();

openAiOptions.ApiKey = AppSettings.Instance["MyOpenApiKey"];
var openAiService = new OpenAIService(openAiOptions);

// Calculate the tokens in the prompt
var completionCreateRequest = new CompletionCreateRequest()
{
    Prompt = "Tell me a short story",
    Model = Models.TextDavinciV3
};

var completionResult = await openAiService.Completions.CreateCompletion(completionCreateRequest);

// Message is a member of the PageModel in Razor used to display the result
if (completionResult.Successful)
{
    var result = completionResult.Choices.FirstOrDefault();
    Message = result.Text;                
}
else
{
    if (completionResult.Error == null)
    {
        Message = "Unknown Error";
    }
    Message = completionResult.Error.Message;
}

Note: The custom AppSettings class pulls values directly from the appsettings.json file.

About The Author