Przejdź do głównej zawartości

Unleash the power of ChatGPT in your C# applications with NGpt - C# OpenAI GPT client

Meet NGpt - OpenAI GPT C# client library that will make your conversations with ChatGPT a breeze! Have you ever been struggling building your own smart AI application in C#? Well, now you can use ChatGpt inside your application! Introducing NGpt, the powerful .NET library that lets you integrate ChatGPT seamlessly into your C# applications. No more nigts spent on building your own AI solitions, no more convoluted logic and building complex business rules - just pure AI bliss. In this blog post, we'll dive into the wonderful world of NGpt and show you how easy it is to use. You might even find a few laughs along the way.

The Magic of NGpt

NGpt is a transient fault-tolerant .NET 6 OpenAI client that simplifies ChatGPT integration for C# developers. It's like your friendly neighborhood Spider-Man - always there to help you out when you need it most. With just your OpenAI API key, you can start coding AI applications in C# faster than you can say "Peter Parker." Let's start. Like any good superhero, NGpt - OpenAI GPT C# client library comes with its own set of amazing features: - Install one nuget package called NGpt and you're ready to code AI solutions - Simplicity of use. We created easy to use classes like Chat or Edit. You can use them instead of struggling with old-school manual HTTP calls. - Supports retry policies like 350-million-downloads Polly retry library. Quite popular one! - You can generate multiple completion options in a single response - it's all configurable in your C# code! - Exceed the request limit? No worries, NGpt has your back! It uses retry in such cases with exponential backoff strategy. Installing NGpt Installing NGpt is as easy as pie, and we all know how much Scott Hanselman loves pie. Head over to NuGet and run Install-Package NGpt. That's it! You're now ready to rock and roll with ChatGPT. The Simplest Example: To get started, let's create a basic chat using NGpt. All you need is to initialize the client with your API key and ask ChatGPT to say hello:
using NGpt;

// Initialize the client with your API key.
var chat = new Chat("");

// Get a response from ChatGPT
var responseFromOpenAI = chat.Complete("Say hello");
Console.WriteLine(responseFromOpenAI);
Run the code and watch in awe as ChatGPT says hello to you through your C# application. Congratulations! You've just created OpenAI supported C# application!

More Complex Example

NGpt - OpenAI GPT C# client library is perfect for the beginners, but you can achieve much more. Now let's ramp it up a notch with a more complex example, where we can tweak some configuration settings:
using NGpt;

var chat = new Chat("");

var completionRequest = new ChatRequest()
{
    Messages = new ChatMessage[]
    {
        new ChatMessage()
        {
            Role = Role.User,
            Content = "Say this is a test!",
        }
    },
    Temperature = 0.7f,
    Model = ChatModel.GPT3_5Turbo
};

var response = chat.Complete(completionRequest);
var content = response.Choices[0].Message.Content;

Console.WriteLine(content);
Notice how we've added a bit more configuration? We've set the temperature to 0.7 for added creativity and specified the model to use GPT-3.5 Turbo. The possibilities are endless! For example, you can write a science-fiction book with temperature equal to maximum level - 2. Or you your app can be cold as ice business analyst when you choose temperature at the minimum level of 0. As a software developer, you choose your application temper:)

Generate Multiple Response Options

Let's say you're feeling indecisive and want ChatGPT to come up with three different ways to write a C# program that checks if a given number is a prime number. NGpt can do that too. Just use N parameter. My quess is that N stands for Number of options to generate.
using NGpt.ChatCompletion;
using NGpt;

var chat = new Chat("");

var completionRequest = new ChatRequest()
{
    Messages = new ChatMessage[]
    {
        new ChatMessage()
        {
            Role = Role.User,
            Content = "Create C# program that checks if a given number is prime number",
        },
    },
    Temperature = 0f,
    Model = ChatModel.GPT3_5Turbo,
    N = 3
};

var response = chat.Complete(completionRequest);

foreach(var choice in response.Choices)
{
    var content = choice.Message.Content;
    Console.WriteLine(content);
}
Just like that, you have three different ways to check for prime numbers! Never had so many choices? Me too. It's like having a team of AI developers working for you.

Editing text or code with ChatGPT (and of course NGpt)

NGpt's Edit class allows you to modify text or code easily. (Note: OpenAI GPT has special feature to write and refactor code. OpenAI must love software developers!) For example, let's say you want ChatGPT to fix a spelling error in the phrase "To bee or not to be. You can do it easily:
using NGpt.Domain.Edit;

var edit = new Edit("", "");
var response = edit.EditText("To bee or not to be.", "fix spelling");

Console.WriteLine(response); // "To be or not to be.\n"
Can you imagine potential business cases where you can use it? Text editors, Spelling prompt in email clients, software to prepare documents free of mistakes. In such a tool in your hands, your software development climb up to Mont Everest (8849 m). Hard to breath at this level? Keep reading! But the most important to coders. How to improve our code quality or maybe make automatic code review suggestions? I believe Visual Studio Copilot could do it (When Microsoft publish it. At the moment Copilot X does not exist in real life - you can see it in Copilot X Scott Hanselman youtube video) So, the
using NGpt.Domain.Edit;

var edit = new Edit("", "");
var response = edit.EditCode("Console.WriteLine(\"Test\");", "Change it to display text: \"aaa\"");

Console.WriteLine(response); // "Console.WriteLine(\"aaa\");\n"
It's like having a spellchecker and a code reviewer all in one!

Conclusion

In my opinion, the best part of NGpt - OpenAI GPT C# client library it are the endless possibilities. You can use this technology to enrich your applications and to the next level that you haven't imagined of 1 year ago. NGpt is the .NET library that will make your AI dreams come true. With just a few lines of code, you can easily integrate ChatGPT into your C# applications and enjoy all the benefits it brings. So, what are you waiting for? Give NGpt a try today and unlock the full potential of ChatGPT just in your C# applications! And remember, with great power comes great responsibility - or in this case, great AI assistance. Have a great coding! And remember: - share your comments below if you find this article useful - share the link to this article with your friends Thanks!

Komentarze

Popularne posty z tego bloga

Specflow czyli BDD w praktyce

Dzisiaj pokażę w praktyce w jaki sposób zbieram wymagania korzystając z narzędzia Specflow. Na początku przypomnę nasze historie użtkownika. Potem skonfigurujemy dodatek Specflow Visual Studio. Na końcu pokażę jak wygląda język Gherkin i zaimplementujemy pierwszą historię użytkownika US1. Oto lista funkcjonalności, które będę implementował w aplikacji Perfect Life: US1: Jako użytkownik aplikacji mobilnej chcę ekran logowania aby mieć możliwość zapisywania swoich postępów US2: Jako użytkownik aplikacji mobilnej chcę mieć kalkulator wskaźnika BMI aby móc mierzyć poziom swojej kondycji US3: Jako użytkownik aplikacji mobilnej chcę widzieć wykresy aby móc wizualnie interpretować swoje postępy US4: Jako użytkownik aplikacji mobilnej chcę mieć możliwość rozwiązania quizu, żeby zbadać wskaźnik Body Age US5: Jako użytkownik aplikacji mobilnej chcę pobierać pliki PDF aby mieć dostęp do planów dietetycznych US6: Jako administrator aplikacji chcę zapisywać dane telemetryczne aby móc an

How to call webAPI service in Xamarin?

In the last few weeks I worked on the web service written in ASP.NET Core. I had a plan to connect the Xamarin application to this web service. Today I'll show you how you can connect your Xamarin app to web service located anywhere in the cloud. First we need to have a web service deployed, which is our server-side part of the system. I describe how to create a web service in ASP.NET COre in my previous post: . Today I'll demonstrate how it works from client perspective, in our case it's Xamarin.Forms application but web application or desktop application can use exactly the same code. This is the great benefit of using C# language - we can use the same code on multiple platforms. OK, I bet you're interested in the code. Let's jump to the code. This is the final version of the client class that I can use to save and retrieve data from web service: The class design is straightforward. It has 2 public methods which encapsulate 2 functionalities: adding s