Betalgo.Ranul.OpenAI 9.1.0

dotnet add package Betalgo.Ranul.OpenAI --version 9.1.0
                    
NuGet\Install-Package Betalgo.Ranul.OpenAI -Version 9.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Betalgo.Ranul.OpenAI" Version="9.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Betalgo.Ranul.OpenAI" Version="9.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Betalgo.Ranul.OpenAI" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Betalgo.Ranul.OpenAI --version 9.1.0
                    
#r "nuget: Betalgo.Ranul.OpenAI, 9.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Betalgo.Ranul.OpenAI&version=9.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Betalgo.Ranul.OpenAI&version=9.1.0
                    
Install as a Cake Tool

Betalgo Ranul OpenAI Github readme banner

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! GitHub Repo stars

  • Discord Please come and help us build the .NET AI community
  • Static Badge
  • Static Badge

Install Packages

Core Library

⚠️ We now have new PackageId and new Namespace. ⚠️
⚠️ Betalgo.OpenAI is now Betalgo.Ranul.OpenAI ⚠️

Betalgo.Ranul.OpenAI

Install-Package Betalgo.Ranul.OpenAI
Experimental Utilities Library

Betalgo.OpenAI.Utilities

Install-Package Betalgo.OpenAI.Utilities

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.1.0

Image Generation

  • Added UsageModel to include detailed token usage tracking for image generation responses.

JSON Schema Validation

  • Enhanced FunctionParameters with MultipleOf, Minimum, Maximum, and Pattern support.
  • Added SingleOrArrayToListConverter to handle single values or arrays.

Code Improvements

  • Refactored MessageContent with expression-bodied helpers and added binary file support.
  • Converted filter type definitions in ToolDefinition from static string to const string for clarity and performance.
  • Simplified filter creation methods.

Documentation and Formatting

  • Improved comments and formatting in ToolDefinition.
  • Fixed BOM character issue in FunctionParameters.

9.0.4

  • Updated Microsoft.Extensions.AI to version 9.5.0

9.0.3

  • Updated Microsoft.Extensions.AI to version 9.4.0-preview.1.25207.5
  • Added new models to the model list

9.0.2

  • Updated Microsoft.Extensions.AI to version 9.3.0-preview.1.25114.11
  • Added reasoning effort parameters
  • Added o1 and o3-mini models to the model list

9.0.1

  • Message list now accept RunId
  • Upgraded to Microsoft.Extensions.AI version 9.0.1, which resolves the "Method not found: '!!0" error when used alongside other SDKs with different versions.

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.

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 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.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on Betalgo.Ranul.OpenAI:

Package Downloads
Squidex.CLI.Core

Command Line Interface for Squidex Headless CMS

Squidex.AI

Squidex Internal Libraries

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 and .NET 9 only; Docs and broader support to follow.

JS.Abp.AI.OpenAI

Package Description

Jumoo.TranslationManager.OpenAi

Package Description

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Betalgo.Ranul.OpenAI:

Repository Stars
ganjoor/GanjoorService
وب سرویس و سایت گنجور
Version Downloads Last Updated
9.1.0 0 7/11/2025
9.0.4 19,818 5/19/2025
9.0.3 6,679 4/23/2025
9.0.2 6,539 3/21/2025
9.0.1 43,590 12/4/2024
9.0.0 4,042 11/18/2024
8.10.1 6,453 11/18/2024
8.10.0 2,217 11/13/2024
8.9.0 488 11/11/2024
8.8.0 4,081 11/1/2024