OpenAI 1.1.0
Prefix ReservedOpenAI SDK package versions prior to v1.3 will not work with the current OpenAI API due to breaking changes with the OpenAI API. Please update to at least v1.3 of the SDK to restore functionality.
See the version list below for details.
dotnet add package OpenAI --version 1.1.0
NuGet\Install-Package OpenAI -Version 1.1.0
<PackageReference Include="OpenAI" Version="1.1.0" />
paket add OpenAI --version 1.1.0
#r "nuget: OpenAI, 1.1.0"
// Install OpenAI as a Cake Addin #addin nuget:?package=OpenAI&version=1.1.0 // Install OpenAI as a Cake Tool #tool nuget:?package=OpenAI&version=1.1.0
C#/.NET SDK for accessing the OpenAI GPT-3 API
A simple C# .NET wrapper library to use with OpenAI's GPT-3 API. More context on my blog.
Examples
var api = new OpenAI_API.OpenAIAPI(engine: Engine.Davinci);
var result = await api.Completions.CreateCompletionAsync("One Two Three One Two", temperature: 0.1);
Console.WriteLine(result.ToString());
// should print something starting with "Three"
var api = new OpenAI_API.OpenAIAPI("sk-mysecretkeyhere"););
var result = await api.Search.GetBestMatchAsync("Washington DC", "Canada", "China", "USA", "Spain");
Console.WriteLine(result);
// should print "USA"
Requirements
This library is based on .NET Standard 2.0, so it should work across .NET Framework >=4.7.2 and .NET Core >= 3.0. It should work across console apps, winforms, wpf, asp.net, etc (although I have not yet tested with asp.net). It should work across Windows, Linux, and Mac, although I have only tested on Windows so far.
Getting started
Install from NuGet
Install package OpenAI
from Nuget. Here's how via commandline:
Install-Package OpenAI
Authentication
There are 3 ways to provide your API keys, in order of precedence:
- Pass keys directly to
APIAuthentication(string key)
constructor - Set environment vars for OPENAI_KEY and/or OPENAI_SECRET_KEY
- Include a config file in the local directory or in your user directory named
.openai
and containing one or both lines:
OPENAI_KEY=pk-aaaabbbbbccccddddd
OPENAI_SECRET_KEY=sk-aaaabbbbbccccddddd
You use the APIAuthentication
when you initialize the API as shown:
// for example
OpenAIAPI api = new OpenAIAPI("sk-mysecretkeyhere"); // shorthand
// or
OpenAIAPI api = new OpenAIAPI(new APIAuthentication("pk-publishkey","sk-secretkey")); // specify manually
// or
OpenAIAPI api = new OpenAIAPI(APIAuthentication LoadFromEnv()); // use env vars
// or
OpenAIAPI api = new OpenAIAPI(APIAuthentication LoadFromPath()); // use config file (can optionally specify where to look)
// or
OpenAIAPI api = new OpenAIAPI(); // uses default, env, or config file
Completions
The Completion API is accessed via OpenAIAPI.Completions
:
CreateCompletionAsync(CompletionRequest request)
// for example
var result = await api.Completions.CreateCompletionAsync(new CompletionRequest("One Two Three One Two", temperature: 0.1));
// or
var result = await api.Completions.CreateCompletionAsync("One Two Three One Two", temperature: 0.1);
// or other convenience overloads
You can create your CompletionRequest
ahead of time or use one of the helper overloads for convenience. It returns a CompletionResult
which is mostly metadata, so use its .ToString()
method to get the text if all you want is the completion.
Streaming
Streaming allows you to get results are they are generated, which can help your application feel more responsive, especially on slow models like Davinci.
Using the new C# 8.0 async interators:
IAsyncEnumerable<CompletionResult> StreamCompletionEnumerableAsync(CompletionRequest request)
// for example
await foreach (var token in api.Completions.StreamCompletionEnumerableAsync(new CompletionRequest("My name is Roger and I am a principal software engineer at Salesforce. This is my resume:", 200, 0.5, presencePenalty: 0.1, frequencyPenalty: 0.1)))
{
Console.Write(token);
}
Or if using .NET framework or C# <8.0:
StreamCompletionAsync(CompletionRequest request, Action<CompletionResult> resultHandler)
// for example
await api.Completions.StreamCompletionAsync(
new CompletionRequest("My name is Roger and I am a principal software engineer at Salesforce. This is my resume:", 200, 0.5, presencePenalty: 0.1, frequencyPenalty: 0.1),
res => ResumeTextbox.Text += res.ToString());
Document Search
The Search API is accessed via OpenAIAPI.Search
:
You can get all results as a dictionary using
GetSearchResultsAsync(SearchRequest request)
// for example
var request = new SearchRequest()
{
Query = "Washington DC",
Documents = new List<string> { "Canada", "China", "USA", "Spain" }
};
var result = await api.Search.GetSearchResultsAsync(request);
// result["USA"] == 294.22
// result["Spain"] == 73.81
The returned dictionary maps documents to scores. You can create your SearchRequest
ahead of time or use one of the helper overloads for convenience, such as
GetSearchResultsAsync(string query, params string[] documents)
// for example
var result = await api.Search.GetSearchResultsAsync("Washington DC", "Canada", "China", "USA", "Spain");
You can get only the best match using
GetBestMatchAsync(request)
And if you only want the best match but still want to know the score, use
GetBestMatchWithScoreAsync(request)
Each of those methods has similar convenience overloads to specify the request inline.
Finetuning
I don't yet have access to finetuning, but once I do I will add it to this SDK. Subscribe to this repo if you want to be alerted.
Documentation
Every single class, method, and property has extensive XML documentation, so it should show up automatically in IntelliSense. That combined with the official OpenAI documentation should be enough to get started. Feel free to ping me on Twitter @OkGoDoIt if you have any questions. Better documentation may come later.
License
This library is licensed CC-0, in the public domain. You can use it for whatever you want, publicly or privately, without worrying about permission or licensing or whatever. It's just a wrapper around the OpenAI API, so you still need to get access to OpenAI from them directly. I am not affiliated with OpenAI and this library is not endorsed by them, I just have beta access and wanted to make a C# library to access it more easily. Hopefully others find this useful as well. Feel free to open a PR if there's anything you want to contribute.
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 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.Bcl.AsyncInterfaces (>= 1.1.1)
- Newtonsoft.Json (>= 12.0.3)
NuGet packages (51)
Showing the top 5 NuGet packages that depend on OpenAI:
Package | Downloads |
---|---|
Azure.AI.OpenAI
Azure OpenAI's official extension package for using OpenAI's .NET library with the Azure OpenAI Service. |
|
Microsoft.SemanticKernel.Connectors.OpenAI
Semantic Kernel connectors for OpenAI. Contains clients for chat completion, embedding and DALL-E text to image. |
|
Newguys.Sport
Package Description |
|
Aspire.Azure.AI.OpenAI
A client for Azure OpenAI that integrates with Aspire, including logging and telemetry. |
|
Rystem.PlayFramework
Rystem.PlayFramework helps you to use concepts like multi agent and openai. |
GitHub repositories (11)
Showing the top 5 popular GitHub repositories that depend on OpenAI:
Repository | Stars |
---|---|
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
|
|
Azure/azure-sdk-for-net
This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
|
|
SciSharp/BotSharp
AI Multi-Agent Framework in .NET
|
|
OfficeDev/Microsoft-Teams-Samples
Welcome to the Microsoft Teams samples repository. Here you will find task-focused samples in C#, JavaScript and TypeScript to help you get started with the Microsoft Teams App!
|
|
VladislavAntonyuk/MauiSamples
.NET MAUI Samples
|
Version | Downloads | Last updated | |
---|---|---|---|
2.1.0-beta.2 | 34,815 | 11/4/2024 | |
2.1.0-beta.1 | 261,401 | 10/1/2024 | |
2.0.0 | 235,301 | 9/30/2024 | |
2.0.0-beta.13 | 3,170 | 9/27/2024 | |
2.0.0-beta.12 | 30,461 | 9/20/2024 | |
2.0.0-beta.11 | 200,933 | 9/3/2024 | |
2.0.0-beta.10 | 109,935 | 8/26/2024 | |
2.0.0-beta.9 | 31,212 | 8/24/2024 | |
2.0.0-beta.8 | 36,460 | 7/31/2024 | |
2.0.0-beta.7 | 115,437 | 6/24/2024 | |
2.0.0-beta.6 | 2,223 | 6/21/2024 | |
2.0.0-beta.5 | 259,419 | 6/14/2024 | |
2.0.0-beta.4 | 9,364 | 6/10/2024 | |
2.0.0-beta.3 | 35,142 | 6/7/2024 | |
2.0.0-beta.2 | 3,494 | 6/6/2024 | |
2.0.0-beta.1 | 2,136 | 6/6/2024 | |
1.11.0 | 413,907 | 3/13/2024 | |
1.10.0 | 208,615 | 12/14/2023 | |
1.9.0 | 20,382 | 12/12/2023 | |
1.8.0 | 15,531 | 12/6/2023 | |
1.7.2 | 596,354 | 4/2/2023 | |
1.7.1 | 1,348 | 4/2/2023 | |
1.7.0 | 1,225 | 4/2/2023 | |
1.6.0 | 56,359 | 3/9/2023 | |
1.5.0 | 22,335 | 2/16/2023 | |
1.4.0 | 12,070 | 2/3/2023 | |
1.3.0 | 13,882 | 1/12/2023 | |
1.2.0 | 41,939 | 12/22/2020 | |
1.1.0 | 1,768 | 7/28/2020 | |
1.0.0 | 4,402 | 7/23/2020 |
Added additional API debugging metadata and improved the readme. An OpenAI API account is required (still in closed beta)