Betalgo.OpenAI 8.2.2

dotnet add package Betalgo.OpenAI --version 8.2.2
NuGet\Install-Package Betalgo.OpenAI -Version 8.2.2
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.OpenAI" Version="8.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Betalgo.OpenAI --version 8.2.2
#r "nuget: Betalgo.OpenAI, 8.2.2"
#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.
// Install Betalgo.OpenAI as a Cake Addin
#addin nuget:?package=Betalgo.OpenAI&version=8.2.2

// Install Betalgo.OpenAI as a Cake Tool
#tool nuget:?package=Betalgo.OpenAI&version=8.2.2

.NET SDK for OpenAI

Overview

A .NET SDK for accessing OpenAI's API, provided as a community library. This SDK allows you to integrate OpenAI's capabilities into your .NET applications with ease.

Install Packages

Core Library

Betalgo.OpenAI

Install-Package Betalgo.OpenAI
Experimental Utilities Library

Betalgo.OpenAI.Utilities

Install-Package Betalgo.OpenAI.Utilities

API Documentation

  • Betalgo.OpenAI: Static Badge
  • Betalgo.OpenAI.Utilities: Static Badge

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

Library Renaming

This library was previously known as Betalgo.OpenAI.GPT3, and now it has a new package ID: Betalgo.OpenAI.

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.

I initially developed this SDK for my personal use and decided to share it with the community. As I have not maintained open-source projects before, any assistance or feedback would be greatly appreciated. Feel free to reach out with your suggestions.

Please be aware that future releases may frequently include breaking changes. Consider this before deciding to use the library. I cannot accept responsibility for any damage caused by using the library. You are free to explore alternative libraries or the OpenAI Web-API if you feel this is not suitable for your purposes.

Changelog

8.2.2

  • Assistant (Beta) feature is now available in the main package. Be aware there might still be bugs due to the beta status of the feature and the SDK itself. Please report any issues you encounter.
  • Use "UseBeta": true in your config file or serviceCollection.AddOpenAIService(r => r.UseBeta = true); or new OpenAiOptions { UseBeta = true } in your service registration to enable Assistant features.
  • Expect more frequent breaking changes around the assistant API due to its beta nature.
  • All Assistant endpoints are implemented except for streaming functionality, which will be added soon.
  • The Playground has samples for every endpoint usage, but lacks a complete implementation for the Assistant APIs. Refer to Assistants overview - OpenAI API for more details.
  • Special thanks to all contributors for making this version possible!
Other Changes:
  • Fixed a bug with multiple tools calling in stream mode.
  • Added error handling for streaming.
  • Added usage information for streaming (use StreamOptions = new(){IncludeUsage = true,} to get usage information).
  • Added timestamp_granularities[] for Create transcription to provide the timestamp of every word.

More Change Logs


For any issues, contributions, or feedback, feel free to reach out or submit a pull request.

Product 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 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. 
.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 (16)

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

Package Downloads
Soenneker.OpenAI.Client The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

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 (4)

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

Repository Stars
betalgo/openai
OpenAI .NET sdk - Azure OpenAI, ChatGPT, Whisper, and DALL-E
cmu-sei/GHOSTS
GHOSTS is a realistic user simulation framework for cyber simulation, training, and exercise
FireCubeStudios/Clippy
Bring back Clippy on Windows 10/11!
FireCubeStudios/Run
WinUI 3 Run
Version Downloads Last updated
8.2.2 2,714 5/18/2024
8.1.1 13,226 4/20/2024
8.1.0 6,878 4/16/2024
8.0.1 4,610 4/11/2024
8.0.0 2,604 4/9/2024
7.4.7 2,727 4/6/2024
7.4.6 89,608 2/7/2024
7.4.5 6,292 2/1/2024
7.4.4 1,878 1/30/2024
7.4.3 88,385 12/11/2023
7.4.2 6,844 12/6/2023
7.4.1 41,507 11/15/2023
7.4.0 14,154 11/10/2023
7.3.1 22,947 10/19/2023
7.2.0 26,811 10/6/2023
7.1.5 132,841 8/6/2023
7.1.3 8,582 7/30/2023
7.0.0 162,112 5/31/2023
6.8.6 46,949 5/11/2023
6.8.5 38,717 4/19/2023
6.8.2 939 3/28/2023