Betalgo.OpenAI
7.0.0
See the version list below for details.
dotnet add package Betalgo.OpenAI --version 7.0.0
NuGet\Install-Package Betalgo.OpenAI -Version 7.0.0
<PackageReference Include="Betalgo.OpenAI" Version="7.0.0" />
paket add Betalgo.OpenAI --version 7.0.0
#r "nuget: Betalgo.OpenAI, 7.0.0"
// Install Betalgo.OpenAI as a Cake Addin #addin nuget:?package=Betalgo.OpenAI&version=7.0.0 // Install Betalgo.OpenAI as a Cake Tool #tool nuget:?package=Betalgo.OpenAI&version=7.0.0
Dotnet SDK for OpenAI ChatGPT, Whisper, GPT-4 and DALL·E
Install-Package Betalgo.OpenAI
Dotnet SDK for OpenAI Chat GPT, Whisper, GPT-4 ,GPT-3 and DALL·E
Unofficial.
OpenAI doesn't have any official .Net SDK.
This library used to known as Betalgo.OpenAI.GPT3
, now it has a new package Id Betalgo.OpenAI
.
Checkout the wiki page:
https://github.com/betalgo/openai/wiki
Features
- Plugins (coming soon)
- Chat GPT
- Chat GPT-4 (models are supported, Image analyze API not released yet by OpenAI)
- Azure OpenAI
- Image DALL·E
- Models
- Completions
- Edit
- Embeddings
- Files
- Fine-tunes
- Moderation
- Tokenizer-GPT3
- Tokenizer
- Whisper
- Rate limit
- Proxy
For changelogs please go to end of the document.
Sample Usages
The repository contains a sample project named OpenAI.Playground that you can refer to for a better understanding of how the library works. However, please exercise caution while experimenting with it, as some of the test methods may result in unintended consequences such as file deletion or fine tuning.
!! It is highly recommended that you use a separate account instead of your primary account while using the playground. This is because some test methods may add or delete your files and models, which could potentially cause unwanted issues. !!
Your API Key comes from here --> https://platform.openai.com/account/api-keys
Your Organization ID comes from here --> https://platform.openai.com/account/org-settings
Without using dependency injection:
var openAiService = new OpenAIService(new OpenAiOptions()
{
ApiKey = Environment.GetEnvironmentVariable("MY_OPEN_AI_API_KEY")
});
Using dependency injection:
secrets.json:
"OpenAIServiceOptions": {
//"ApiKey":"Your api key goes here"
//,"Organization": "Your Organization Id goes here (optional)"
},
(How to use user secret ?
Right click your project name in "solution explorer" then click "Manage User Secret", it is a good way to keep your api keys)
Program.cs
serviceCollection.AddOpenAIService();
OR
Use it like below but do NOT put your API key directly to your source code.
Program.cs
serviceCollection.AddOpenAIService(settings => { settings.ApiKey = Environment.GetEnvironmentVariable("MY_OPEN_AI_API_KEY"); });
After injecting your service you will be able to get it from service provider
var openAiService = serviceProvider.GetRequiredService<IOpenAIService>();
You can set default model(optional):
openAiService.SetDefaultModelId(Models.Davinci);
Chat Gpt Sample
var completionResult = await openAiService.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest
{
Messages = new List<ChatMessage>
{
ChatMessage.FromSystem("You are a helpful assistant."),
ChatMessage.FromUser("Who won the world series in 2020?"),
ChatMessage.FromAssistant("The Los Angeles Dodgers won the World Series in 2020."),
ChatMessage.FromUser("Where was it played?")
},
Model = Models.ChatGpt3_5Turbo,
MaxTokens = 50//optional
});
if (completionResult.Successful)
{
Console.WriteLine(completionResult.Choices.First().Message.Content);
}
Completions Sample
var completionResult = await openAiService.Completions.CreateCompletion(new CompletionCreateRequest()
{
Prompt = "Once upon a time",
Model = Models.TextDavinciV3
});
if (completionResult.Successful)
{
Console.WriteLine(completionResult.Choices.FirstOrDefault());
}
else
{
if (completionResult.Error == null)
{
throw new Exception("Unknown Error");
}
Console.WriteLine($"{completionResult.Error.Code}: {completionResult.Error.Message}");
}
Completions Stream Sample
var completionResult = openAiService.Completions.CreateCompletionAsStream(new CompletionCreateRequest()
{
Prompt = "Once upon a time",
MaxTokens = 50
}, Models.Davinci);
await foreach (var completion in completionResult)
{
if (completion.Successful)
{
Console.Write(completion.Choices.FirstOrDefault()?.Text);
}
else
{
if (completion.Error == null)
{
throw new Exception("Unknown Error");
}
Console.WriteLine($"{completion.Error.Code}: {completion.Error.Message}");
}
}
Console.WriteLine("Complete");
DALL·E Sample
var imageResult = await openAiService.Image.CreateImage(new ImageCreateRequest
{
Prompt = "Laser cat eyes",
N = 2,
Size = StaticValues.ImageStatics.Size.Size256,
ResponseFormat = StaticValues.ImageStatics.ResponseFormat.Url,
User = "TestUser"
});
if (imageResult.Successful)
{
Console.WriteLine(string.Join("\n", imageResult.Results.Select(r => r.Url)));
}
Notes:
Please note that due to time constraints, I was unable to thoroughly test all of the methods or fully document the library. If you encounter any issues, please do not hesitate to report them or submit a pull request - your contributions are always appreciated.
I initially developed this SDK for my personal use and later decided to share it with the community. As I have not maintained any open-source projects before, any assistance or feedback would be greatly appreciated. If you would like to contribute in any way, please feel free to reach out to me with your suggestions.
I will always be using the latest libraries, and future releases will frequently include breaking changes. Please take this into consideration before deciding to use the library. I want to make it clear that I cannot accept any responsibility for any damage caused by using the library. If you feel that this is not suitable for your purposes, you are free to explore alternative libraries or the OpenAI Web-API.
I am incredibly busy. If I forgot your name, please accept my apologies and let me know so I can add it to the list.
Changelog
7.0.0
- The code now supports .NET 7.0. Big cheers to @BroMarduk for making this happen.
- The library now automatically disposes of the Httpclient when it's created by the constructor. This feature is thanks to @BroMarduk.
- New support has been added for using more than one instance at the same time. Check out this link for more details. Thanks to @remixtedi for bringing this to my attention.
- A lot of small improvements have been done by @BroMarduk.
- Breaking Changes 😢
- I've removed 'GPT3' from the namespace, so you might need to modify some aspects of your project. But don't worry, it's pretty simple! For instance, instead of writing
using OpenAI.GPT3.Interfaces
, you'll now writeusing OpenAI.Interfaces
. - The order of the OpenAI constructor parameters has changed. It now takes 'options' first, then 'httpclient'.
//Before var openAiService = new OpenAIService(httpClient, options); //Now var openAiService = new OpenAIService(options, httpClient);
- I've removed 'GPT3' from the namespace, so you might need to modify some aspects of your project. But don't worry, it's pretty simple! For instance, instead of writing
6.8.6
- Updated Azure OpenAI default API version to the preview version to support ChatGPT. thanks to all issue reporters
- Added support for an optional chat
name
field. thanks to @shanepowell - Breaking Change
FineTuneCreateRequest.PromptLossWeight
converto to float thanks to @JohnJ0808
6.8.5
- Mostly bug fixes
- Fixed Moderation functions. https://github.com/betalgo/openai/issues/214 thanks to @scolmarg @AbdelAzizMohamedMousa @digitalvir
- Added File Stream support for Whisper, Thanks to @Swimburger
- Fixed Whisper default response type, Thanks to @Swimburger
- Performance improvements and code clean up,again Thanks to @Swimburger 👏
- Code clenaup, Thanks to @WeihanLi
6.8.4
- Released update message about nuget Package ID change
6.8.3
- Breaking Changes:
I am going to update library namespace fromReverted namespace change, maybe next time.Betalgo.OpenAI.GPT3
toOpenAI.GPT3
. This is the first time I am trying to update my nuget packageId. If something broken, please be patient. I will be fixing it soon.Small Typo change on model name
Model.GPT4
to Model.GPT_4
ServiceCollection.AddOpenAIService();
now returnsIHttpClientBuilder
which means it allows you to play with httpclient object. Thanks for all the reporters and @LGinC. Here is a little sample
ServiceCollection.AddOpenAIService()
.ConfigurePrimaryHttpMessageHandler((s => new HttpClientHandler
{
Proxy = new WebProxy("1.1.1.1:1010"),
});
6.8.1
- Breaking Changes: Typo fixed in Content Moderation CategoryScores, changing
Sexualminors
toSexualMinors
. Thanks to @HowToDoThis. - Tokenizer changes thanks to @IS4Code.
- Performance improvement
- Introduced a new method
TokenCount
that returns the number of tokens instead of a list. - Breaking Changes: Removed overridden methods that were basically string conversions. I think these methods were not used much and it is fairly easy to do these conversions outside of the method. If you disagree, let me know and I can consider adding them back.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.Http (>= 6.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 6.0.0)
- System.Net.Http.Json (>= 7.0.1)
-
net6.0
- Microsoft.Extensions.Http (>= 6.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 6.0.0)
-
net7.0
- Microsoft.Extensions.Http (>= 7.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 7.0.0)
NuGet packages (19)
Showing the top 5 NuGet packages that depend on Betalgo.OpenAI:
Package | Downloads |
---|---|
Soenneker.OpenAI.Client
An async thread-safe singleton for the OpenAI client by Betalgo |
|
Betalgo.OpenAI.Utilities
Utility tools for Betalgo.OpenAI - Dotnet SDK for OpenAI ChatGPT, Whisper, GPT-4 and DALL·E |
|
Squidex.CLI.Core
Command Line Interface for Squidex Headless CMS |
|
OuroborosAI.Core
Powerful layer on top of OpenAI supporting chaining and recursion scenarios. Includes fluent SDK for chaining, templates, and text processing. Still very early an .NET 7 only; Docs and broader support to follow. |
|
Squidex.AI
Squidex Internal Libraries |
GitHub repositories (5)
Showing the top 5 popular GitHub repositories that depend on Betalgo.OpenAI:
Repository | Stars |
---|---|
betalgo/openai
.NET library for the OpenAI service API by Betalgo Ranul
|
|
cmu-sei/GHOSTS
GHOSTS is a realistic user simulation framework for cyber experimentation, simulation, training, and exercise
|
|
FireCubeStudios/Clippy
Bring back Clippy on Windows 10/11!
|
|
MayDay-wpf/AIBotPublic
AIBot PRO 是一个基于.NET 6 的 AI聚合客户端 to C 弱 to B 可以集成众多AI产品(ChatGPT,Gemini,Claude,文心一言,通义千问,讯飞星火),无感切换对话,支持知识库、插件开发、AI流程引擎(workflow)、以及开放平台对外输出定制化的特色AI API
|
|
FireCubeStudios/Run
WinUI 3 Run
|
Version | Downloads | Last updated | |
---|---|---|---|
8.7.2 | 10,620 | 10/10/2024 | |
8.7.1 | 12,342 | 9/24/2024 | |
8.7.0 | 887 | 9/23/2024 | |
8.6.2 | 28,263 | 8/26/2024 | |
8.6.1 | 39,053 | 7/18/2024 | |
8.6.0 | 10,979 | 7/11/2024 | |
8.5.1 | 31,874 | 6/18/2024 | |
8.5.0 | 6,201 | 6/15/2024 | |
8.4.0 | 6,715 | 6/11/2024 | |
8.3.0 | 11,107 | 6/3/2024 | |
8.2.2 | 33,446 | 5/18/2024 | |
8.1.1 | 27,097 | 4/20/2024 | |
8.1.0 | 15,023 | 4/16/2024 | |
8.0.1 | 6,055 | 4/11/2024 | |
8.0.0 | 6,026 | 4/9/2024 | |
7.4.7 | 5,935 | 4/6/2024 | |
7.4.6 | 136,456 | 2/7/2024 | |
7.4.5 | 8,299 | 2/1/2024 | |
7.4.4 | 2,535 | 1/30/2024 | |
7.4.3 | 108,937 | 12/11/2023 | |
7.4.2 | 7,283 | 12/6/2023 | |
7.4.1 | 47,950 | 11/15/2023 | |
7.4.0 | 15,854 | 11/10/2023 | |
7.3.1 | 30,354 | 10/19/2023 | |
7.2.0 | 36,252 | 10/6/2023 | |
7.1.5 | 174,191 | 8/6/2023 | |
7.1.3 | 9,111 | 7/30/2023 | |
7.0.0 | 228,633 | 5/31/2023 | |
6.8.6 | 58,887 | 5/11/2023 | |
6.8.5 | 42,123 | 4/19/2023 | |
6.8.2 | 1,062 | 3/28/2023 |