If you go online and research ChatGPT you will quickly find countless articles gushing about how amazing it is. If ChatGPT is promising to revolutionize the world, why not take advantage of this API within your own applications? Instead of reinventing the wheel, it will be quicker and easier for you to make use of a ready-made package.
Within the Nuget marketplace, there are a number of ChatGPT plugins. If you are anything like me you will want to use a package that allows you to write clean, testable code that is also easy to master. Within this article, you will learn which of the existing ChatGPT Nuget packages is the best one to power your C# powered projects. If this sounds good to you, read on 🔥🔥🔥
Before we get started you can see a working code example of all these packages from a repo I created called 7-nuget-gems-for-net7 over at one of my GitHub accounts.
When it comes to implementation, integrating ChatGPT within your codebase is not actually that complex. Even though the ChatGPT integration is not that complex, it is still never a good idea to re-invent the wheel when you do not need to. Currently, the three most popular ChatGPT packages on Nuget are:
All of these packages have been downloaded over 45k times each. As well as being popular, each package provides you with all the capabilities to talk to ChatGTP, as well as handle all the authentication and setup configuration for you. All the packages work with both the v3 and v4 versions of the ChatGTP API. In essence, using one of these packages will be much quicker and faster compared to creating something from scratch yourself.
One thing to mention when you are considering this as an option is the fees. The ChatGPT service is not free. I have taken the current pricing charges from this blog post:
The ChatGPT API costs $0.002 per 1,000 tokens, which are the sequences of messages with metadata that the model consumes. The Whisper large-V2 model is priced at $0.006 per minute.
When reviewing each package, a key metric to consider is based on how much of the OpenAI capabilities the package supports. OpenAI does not simply provide a single ChatGPT end-point. OpenAI offers several ways of asking ChatGPT for help. Before we compare the packages it will be handy to summarize these end-points so you can appreciate what each package offers. The endpoints include:
Completions: You input some text as a prompt, and the model will generate a text completion that attempts to match whatever context or pattern you gave it.
Conversations: This API takes a series of messages as input and uses that context in order to return an answer
Embeddings: The Embedding API allows you to measure the relatedness between search terms. If you need to create a search, a recommendation engine, or build some form of anomaly detection you can use this end-point.
Moderations: This API allows you to check if a search query complies with OpenAI's fair usage policies.
Fine-tuning: Allows you to get more granularity in your results to hopefully improve the quality by providing training.
We will start by looking at OpenAI by OkGoDoIt. This is the most popular Nuget package at the time of writing, downloaded over 65k times. The first thing I want to mention about this package is that the documentation is really good. If you go to the Nuget homepage, you can find code samples and installation instructions. All the endpoints are well documented.
The OpenAI package includes wrappers that will allow you to integrate with the conversations, completions, embeddings, moderations, files, and the image API also known as DALL-E.
To install OpenAI you can use this command:
To integrate this package within your code base, you can implement it like this:
One of the things that I really like about this extension is the declarative nature of its API. The OpenAI package API adheres to a more fluent in-style syntax. This means you can read the code using this package as sentences rather than a bunch of code. It is a common fact that developers spend more time reading code than writing it, so using a package that will help make your code more understandable is a big plus point in my opinion!
Another added benefit is that the package is hosted within a public Guthub so you can see how it works pretty easily. This repo also contains a unit test project, so if you are struggling to implement the code, I recommend heading over to that project so you can see how it needs to be implemented!
In conclusion, I give this 5/5
The Betalgo Nuget package was created by BetalgoUp and it has been downloaded over 57 thousand times. This package is currently the second most downloaded extension found within the Nuget marketplace.
The Betalgo plug-in gives you access to the completions, embeddings, files, fine-tuning, moderation, and the image API DALL-E endpoints. Additionally, it also offers streaming support as well as model editing capabilities.
The documentation for this package is pretty good, although it is not quite as comprehensive as the OpenAI package.
The coding syntax of this API follows a more imperative style. This is understandable as it mirrors the actual OpenAI endpoint syntax. To install this package you can use this command:
The code to implement it then looks like this:
One thing that caught me a few times can be found in this snippet:
If you do not include
Prompt the code will simply blow up when run. As the package does not force you to add
Prompt as a constructor param., it is a little annoying encountering an exception when you forget about it.
In conclusion, this is a solid package, and I give this 4/5
Forge.OpenAI created by JZO has been downloaded over 45k times. This is the third most popular extension in the marketplace. Just like the other packages Forge.OpenAI provides wrappers for all the standard stuff like completion and the image API. fine-tuning, files, moderations, and embedding.
The added benefit of this package is that it also supports the transcription and translation APIs. This is the only package that offers these services!
Again, the documentation for this package is solid. I would say the connection and installation instructions are more comprehensive here, however, the implementation steps are slightly less comprehensive compared to the OpenAI version.
It took me a little longer to figure out how to use this API compared to the other versions. In essence, you need to use the correct model to structure your query. This is different from the OpenAI model where you can just chain methods together, which I found a little easier to work with!
You can install this package with this command:
The code to implement the chat API then looks like this:
This package is more imperative in style compared to the OpenAI version. I'd say overall this package is 4/5, however, as it has those two additional end-points I give it 4.5/5
Out of the packages reviewed today, the OpenAI package is my winner. Good documentation, I like the API syntax and it was easy to use. In all honestly all the packages were pretty good, but fluent API made this package a winner for me.
Implementing ChatGPT is one of those things that might give your project a competitive advantage. As this article has shown, getting started in C# is easy so it's definitely an idea that is worth considering! Happy Coding 🤘