Betalgo.Ranul.OpenAI
9.0.0
dotnet add package Betalgo.Ranul.OpenAI --version 9.0.0
NuGet\Install-Package Betalgo.Ranul.OpenAI -Version 9.0.0
<PackageReference Include="Betalgo.Ranul.OpenAI" Version="9.0.0" />
paket add Betalgo.Ranul.OpenAI --version 9.0.0
#r "nuget: Betalgo.Ranul.OpenAI, 9.0.0"
// Install Betalgo.Ranul.OpenAI as a Cake Addin #addin nuget:?package=Betalgo.Ranul.OpenAI&version=9.0.0 // Install Betalgo.Ranul.OpenAI as a Cake Tool #tool nuget:?package=Betalgo.Ranul.OpenAI&version=9.0.0
Overview
A .NET Library for accessing OpenAI's API, provided as a community library. This Library allows you to integrate OpenAI's capabilities into your .NET applications with ease.
⭐ We appreciate your star, it helps!
Community Links
Install Packages
Core Library
⚠️ We now have new PackageId and new Namespace. ⚠️
⚠️ Betalgo.OpenAI
is now Betalgo.Ranul.OpenAI
⚠️
Install-Package Betalgo.Ranul.OpenAI
Experimental Utilities Library
Install-Package Betalgo.OpenAI.Utilities
Documentation and Links
- Realtime API ✨NEW
- Wiki Page
- Feature Availability Table
- Change Logs
- Migration Guide for Breaking Changes
Acknowledgements
Maintenance of this project is made possible by all the bug reporters, contributors, and sponsors.
💖 Sponsors:
@betalgo, Laser Cat Eyes
@tylerje, @oferavnery, @MayDay-wpf, @AnukarOP, @Removable, @Scar11
Sample Usage
The repository contains a sample project named OpenAI.Playground to help you understand how the library works. However, please exercise caution while experimenting, as some 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. Some test methods may add or delete your files and models, potentially causing unwanted issues. !!
Your API Key can be obtained from here: https://platform.openai.com/account/api-keys
Your Organization ID can be found 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)",
"UseBeta": "true/false (optional)"
}
(To use user secrets: Right-click your project name in "Solution Explorer", then click "Manage User Secrets". This is a good way to keep your API keys secure.)
Program.cs
serviceCollection.AddOpenAIService();
OR
serviceCollection.AddOpenAIService(settings => { settings.ApiKey = Environment.GetEnvironmentVariable("MY_OPEN_AI_API_KEY"); });
After injecting your service, you can retrieve it from the service provider:
var openAiService = serviceProvider.GetRequiredService<IOpenAIService>();
You can set a default model (optional):
openAiService.SetDefaultModelId(Models.Gpt_4o);
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.Gpt_4o,
});
if (completionResult.Successful)
{
Console.WriteLine(completionResult.Choices.First().Message.Content);
}
Notes
Due to time constraints, not all methods have been thoroughly tested or fully documented. If you encounter any issues, please report them or submit a pull request. Your contributions are always appreciated.
Needless to say, I cannot accept responsibility for any damage caused by using the library.
Changelog
9.0.0
- .NET 9 support added.
- ⚠️ Support for .NET 6 and .NET 7 has ended.
- Fixed utility library issues and synced with latest version.
8.10.1
- Fixed an issue with the
Store
parameter being included in requests by default, causing errors with Azure OpenAI models. The parameter is now optional and excluded from serialization unless explicitly set.
8.10.0
Added support for
Microsoft.Extensions.AI
IChatClient
andIEmbeddingGenerator
(more information will be coming soon to the Wiki).Added missing
Temperature
andTopP
parameters toAssistantResponse
.Added missing
Store
parameter toChatCompletionCreateRequest
.Breaking Changes:
- ⚠️
CreatedAt
parameter renamed toCreatedAtUnix
and converted tolong
instead ofint
. AddedCreatedAt
parameter asDateTimeOffset
type, which will automatically convert Unix time toDateTime
.
- ⚠️
8.9.0
- Realtime API implementation is completed. As usual this is the first version and it may contain bugs. Please report any issues you encounter.
- Realtime Sample
8.8.0
Compatibility Enhancement: You can now use this library alongside the official OpenAI library and/or Semantic Kernel within the same project. The name changes in this update support this feature.
Namespace and Package ID Update: The namespace and PackageId have been changed from
Betalgo.OpenAI
toBetalgo.Ranul.OpenAI
.OpenAI Naming Consistency: We've standardized the use of "OpenAI" throughout the library, replacing any instances of "OpenAi" or other variations.
Migration Instructions: Intellisense should assist you in updating your code. If it doesn't, please make the following changes manually:
- Switch to the new NuGet package:
Betalgo.Ranul.OpenAI
instead ofBetalgo.OpenAI
. - Update all namespaces from
OpenAI
toBetalgo.Ranul.OpenAI
. - Replace all occurrences of "OpenAi", "Openai", or any other variations with "OpenAI".
- Switch to the new NuGet package:
Need Help?: If you encounter any issues, feel free to reach out via our Discord channel, Reddit channel, or GitHub discussions. We're happy to assist.
Feedback Welcomed: If you notice any mistakes or missing name changes, please create an issue to let us know.
Utilities Library Status: Please note that the Utilities library might remain broken for a while. I will focus on fixing it after completing the real-time API implementation.
More Change Logs
For any issues, contributions, or feedback, feel free to reach out or submit a pull request.
Betalgo X: Betalgo (@Betalgo) / X (twitter.com)
Betalgo Linkedin: Betalgo | LinkedIn
Tolga X: Tolga Kayhan (@kayhantolga) / X (twitter.com)
Tolga Linkedin: Tolga Kayhan | LinkedIn
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 is compatible. 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. net9.0 is compatible. |
.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.Bcl.AsyncInterfaces (>= 9.0.0)
- Microsoft.Extensions.AI.Abstractions (>= 9.0.0-preview.9.24525.1)
- Microsoft.Extensions.Http (>= 9.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Net.Http.Json (>= 9.0.0)
- System.Text.Json (>= 9.0.0)
-
net8.0
- Microsoft.Bcl.AsyncInterfaces (>= 8.0.0)
- Microsoft.Extensions.AI.Abstractions (>= 9.0.0-preview.9.24525.1)
- Microsoft.Extensions.Http (>= 8.0.1)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Text.Json (>= 8.0.5)
-
net9.0
- Microsoft.Bcl.AsyncInterfaces (>= 9.0.0)
- Microsoft.Extensions.AI.Abstractions (>= 9.0.0-preview.9.24525.1)
- Microsoft.Extensions.Http (>= 9.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Text.Json (>= 9.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Betalgo.Ranul.OpenAI:
Package | Downloads |
---|---|
Squidex.AI
Squidex Internal Libraries |
|
OBotService
OBase Framework |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Betalgo.Ranul.OpenAI:
Repository | Stars |
---|---|
betalgo/openai
.NET library for the OpenAI service API by Betalgo Ranul
|