Rystem.OpenAi
1.0.3
See the version list below for details.
dotnet add package Rystem.OpenAi --version 1.0.3
NuGet\Install-Package Rystem.OpenAi -Version 1.0.3
<PackageReference Include="Rystem.OpenAi" Version="1.0.3" />
paket add Rystem.OpenAi --version 1.0.3
#r "nuget: Rystem.OpenAi, 1.0.3"
// Install Rystem.OpenAi as a Cake Addin #addin nuget:?package=Rystem.OpenAi&version=1.0.3 // Install Rystem.OpenAi as a Cake Tool #tool nuget:?package=Rystem.OpenAi&version=1.0.3
Unofficial Fluent C#/.NET SDK for accessing the OpenAI API
A simple C# .NET wrapper library to use with OpenAI's API.
Help the project
Contribute: https://www.buymeacoffee.com/keyserdsoze
Contribute: https://patreon.com/Rystem
Requirements
This library targets .NET standard 2.1 and above.
Adv
Watch out my Rystem framework to be able to do .Net webapp faster (easy integration with repository pattern or CQRS for your Azure services).
What is Rystem?
Setup
Install package Rystem.OpenAi from Nuget.
Here's how via command line:
Install-Package Rystem.OpenAi
Documentation
Table of Contents
- Dependency Injection
- Models
- Completions
- Chat
- Edits
- Images
- Embeddings
- Audio
- File
- Fine-Tunes
- Moderations
Dependency Injection
Add to service collection the OpenAi service in your DI
var apiKey = configuration["Azure:ApiKey"];
services.AddOpenAi(settings =>
{
settings.ApiKey = apiKey;
});
Dependency Injection With Azure
Add to service collection the OpenAi service in your DI with Azure integration
When you want to use the integration with Azure, you need to specify all the models you're going to use. In the example you may find the model name for DavinciText3. You still may add a custom model, with AddDeploymentCustomModel.
builder.Services.AddOpenAi(settings =>
{
settings.ApiKey = apiKey;
settings.Azure.ResourceName = "AzureResourceName (Name of your deployed service on Azure)";
settings.Azure
.AddDeploymentTextModel("Test (The name from column 'Model deployment name' in Model deployments blade in your Azure service)", TextModelType.DavinciText3);
});
Add to service collection the OpenAi service in your DI with Azure integration and app registration
See how to create an app registration here.
var resourceName = builder.Configuration["Azure:ResourceName"];
var clientId = builder.Configuration["AzureAd:ClientId"];
var clientSecret = builder.Configuration["AzureAd:ClientSecret"];
var tenantId = builder.Configuration["AzureAd:TenantId"];
builder.Services.AddOpenAi(settings =>
{
settings.Azure.ResourceName = resourceName;
settings.Azure.AppRegistration.ClientId = clientId;
settings.Azure.AppRegistration.ClientSecret = clientSecret;
settings.Azure.AppRegistration.TenantId = tenantId;
settings.Azure
.AddDeploymentTextModel("Test", TextModelType.CurieText)
.AddDeploymentTextModel("text-davinci-002", TextModelType.DavinciText2)
.AddDeploymentEmbeddingModel("Test", EmbeddingModelType.AdaTextEmbedding);
});
Add to service collection the OpenAi service in your DI with Azure integration and system assigned managed identity
See how to create a managed identity here.
System Assigned Managed Identity
var resourceName = builder.Configuration["Azure:ResourceName"];
builder.Services.AddOpenAi(settings =>
{
settings.Azure.ResourceName = resourceName;
settings.Azure.ManagedIdentity.UseDefault = true;
settings.Azure
.AddDeploymentTextModel("Test", TextModelType.CurieText)
.AddDeploymentTextModel("text-davinci-002", TextModelType.DavinciText2)
.AddDeploymentEmbeddingModel("Test", EmbeddingModelType.AdaTextEmbedding);
});
Add to service collection the OpenAi service in your DI with Azure integration and user assigned managed identity
See how to create a managed identity here.
User Assigned Managed Identity
var resourceName = builder.Configuration["Azure:ResourceName"];
var managedIdentityId = builder.Configuration["ManagedIdentity:ClientId"];
builder.Services.AddOpenAi(settings =>
{
settings.Azure.ResourceName = resourceName;
settings.Azure.ManagedIdentity.Id = managedIdentityId;
settings.Azure
.AddDeploymentTextModel("Test", TextModelType.CurieText)
.AddDeploymentTextModel("text-davinci-002", TextModelType.DavinciText2)
.AddDeploymentEmbeddingModel("Test", EmbeddingModelType.AdaTextEmbedding);
});
Models
📖 Back to summary
List and describe the various models available in the API. You can refer to the Models documentation to understand what models are available and the differences between them.
You may find more details here,
and here samples from unit test.
List Models
Lists the currently available models, and provides basic information about each one such as the owner and availability.
IOpenAiApi _openAiApi;
var results = await _openAiApi.Model.ListAsync();
Retrieve Models
Retrieves a model instance, providing basic information about the model such as the owner and permissioning.
IOpenAiApi _openAiApi;
var result = await _openAiApi.Model.RetrieveAsync(TextModelType.DavinciText3.ToModelId());
Completions
📖 Back to summary
Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.
You may find more details here,
and here samples from unit test
IOpenAiApi _openAiApi;
var results = await _openAiApi.Completion
.Request("One Two Three Four Five Six Seven Eight Nine One Two Three Four Five Six Seven Eight")
.WithModel(TextModelType.CurieText)
.WithTemperature(0.1)
.SetMaxTokens(5)
.ExecuteAsync();
Streaming
IOpenAiApi _openAiApi;
var results = new List<CompletionResult>();
await foreach (var x in _openAiApi.Completion
.Request("Today is Monday, tomorrow is", "10 11 12 13 14")
.WithTemperature(0)
.SetMaxTokens(3)
.ExecuteAsStreamAsync())
{
results.Add(x);
}
Chat
📖 Back to summary
Given a chat conversation, the model will return a chat completion response.
You may find more details here,
and here samples from unit test.
IOpenAiApi _openAiApi;
var results = await _openAiApi.Chat
.Request(new ChatMessage { Role = ChatRole.User, Content = "Hello!! How are you?" })
.WithModel(ChatModelType.Gpt35Turbo0301)
.WithTemperature(1)
.ExecuteAsync();
Chat Streaming
IOpenAiApi _openAiApi;
var results = new List<ChatResult>();
await foreach (var x in _openAiApi.Chat
.Request(new ChatMessage { Role = ChatRole.User, Content = "Hello!! How are you?" })
.WithModel(ChatModelType.Gpt35Turbo)
.WithTemperature(1)
.ExecuteAsStreamAsync())
{
results.Add(x);
}
Edits
Given a prompt and an instruction, the model will return an edited version of the prompt. You may find more details here, and here samples from unit test.
IOpenAiApi _openAiApi;
var results = await _openAiApi.Edit
.Request("Fix the spelling mistakes")
.WithModel(EditModelType.TextDavinciEdit)
.SetInput("What day of the wek is it?")
.WithTemperature(0.5)
.ExecuteAsync();
Images
📖 Back to summary
Given a prompt and/or an input image, the model will generate a new image.
You may find more details here,
and here samples from unit test.
Create Image
Creates an image given a prompt.
IOpenAiApi _openAiApi;
var response = await _openAiApi.Image
.Generate("A cute baby sea otter")
.WithSize(ImageSize.Small)
.ExecuteAsync();
Download directly and save as stream
IOpenAiApi _openAiApi;
var streams = new List<Stream>();
await foreach (var image in _openAiApi.Image
.Generate("A cute baby sea otter")
.WithSize(ImageSize.Small)
.DownloadAsync())
{
streams.Add(image);
}
Create Image Edit
Creates an edited or extended image given an original image and a prompt.
IOpenAiApi _openAiApi;
var response = await _openAiApi.Image
.Generate("A cute baby sea otter wearing a beret")
.EditAndTrasformInPng(editableFile, "otter.png")
.WithSize(ImageSize.Small)
.WithNumberOfResults(2)
.ExecuteAsync();
Create Image Variation
Creates a variation of a given image.
IOpenAiApi _openAiApi;
var response = await _openAiApi.Image
.VariateAndTransformInPng(editableFile, "otter.png")
.WithSize(ImageSize.Small)
.WithNumberOfResults(1)
.ExecuteAsync();
Embeddings
📖 Back to summary
Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
You may find more details here,
and here samples from unit test.
Create Embedding
Creates an embedding vector representing the input text.
IOpenAiApi _openAiApi;
var results = await _openAiApi.Embedding
.Request("A test text for embedding")
.ExecuteAsync();
Audio
📖 Back to summary
You may find more details here,
and here samples from unit test.
Create Transcription
Transcribes audio into the input language.
IOpenAiApi _openAiApi;
var results = await _openAiApi.Audio
.Request(editableFile, "default.mp3")
.TranscriptAsync();
Create Translation
Translates audio into English.
IOpenAiApi _openAiApi;
var results = await _openAiApi.Audio
.Request(editableFile, "default.mp3")
.TranslateAsync();
File
📖 Back to summary
Files are used to upload documents that can be used with features like Fine-tuning.
You may find more details here,
and here samples from unit test.
List files
Returns a list of files that belong to the user's organization.
IOpenAiApi _openAiApi;
var results = await _openAiApi.File
.AllAsync();
Upload file
Upload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact us if you need to increase the storage limit.
IOpenAiApi _openAiApi;
var uploadResult = await _openAiApi.File
.UploadFileAsync(editableFile, name);
Delete file
Delete a file.
IOpenAiApi _openAiApi;
var deleteResult = await _openAiApi.File
.DeleteAsync(uploadResult.Id);
Retrieve file
Returns information about a specific file.
IOpenAiApi _openAiApi;
var retrieve = await _openAiApi.File
.RetrieveAsync(uploadResult.Id);
Retrieve file content
Returns the contents of the specified file
IOpenAiApi _openAiApi;
var contentRetrieve = await _openAiApi.File
.RetrieveFileContentAsStringAsync(uploadResult.Id);
Fine-Tunes
📖 Back to summary
Manage fine-tuning jobs to tailor a model to your specific training data.
You may find more details here,
and here samples from unit test.
Create fine-tune
Creates a job that fine-tunes a specified model from a given dataset. Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.
IOpenAiApi _openAiApi;
var createResult = await _openAiApi.FineTune
.Create(fileId)
.ExecuteAsync();
List fine-tunes
List your organization's fine-tuning jobs
IOpenAiApi _openAiApi;
var allFineTunes = await _openAiApi.FineTune
.ListAsync();
Retrieve fine-tune
Gets info about the fine-tune job.
IOpenAiApi _openAiApi;
var retrieveFineTune = await _openAiApi.FineTune
.RetrieveAsync(fineTuneId);
Cancel fine-tune
Immediately cancel a fine-tune job.
IOpenAiApi _openAiApi;
var cancelResult = await _openAiApi.FineTune
.CancelAsync(fineTuneId);
List fine-tune events
Get fine-grained status updates for a fine-tune job.
IOpenAiApi _openAiApi;
var events = await _openAiApi.FineTune
.ListEventsAsync(fineTuneId);
List fine-tune events as stream
Get fine-grained status updates for a fine-tune job.
IOpenAiApi _openAiApi;
var events = await _openAiApi.FineTune
.ListEventsAsStreamAsync(fineTuneId);
Delete fine-tune model
Delete a fine-tuned model. You must have the Owner role in your organization.
IOpenAiApi _openAiApi;
var deleteResult = await _openAiApi.File
.DeleteAsync(fileId);
Moderations
📖 Back to summary
Given a input text, outputs if the model classifies it as violating OpenAI's content policy.
You may find more details here,
and here samples from unit test.
Create moderation
Classifies if text violates OpenAI's Content Policy
IOpenAiApi _openAiApi;
var results = await _openAiApi.Moderation
.Create("I want to kill them.")
.WithModel(ModerationModelType.TextModerationStable)
.ExecuteAsync();
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 | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | 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.1
- Azure.Identity (>= 1.8.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Http.Polly (>= 7.0.3)
- Microsoft.Identity.Client.Extensions.Msal (>= 2.26.0)
- Polly (>= 7.2.3)
- System.Drawing.Common (>= 7.0.0)
- System.Text.Json (>= 7.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.3.12 | 2,050,529 | 4/2/2024 |
3.3.11 | 1,628,264 | 11/2/2023 |
3.3.10 | 3,099 | 10/18/2023 |
3.3.9 | 1,047 | 10/18/2023 |
3.3.8 | 408,951 | 10/17/2023 |
3.3.7 | 5,855 | 10/17/2023 |
3.3.6 | 3,949 | 10/17/2023 |
3.3.5 | 10,781 | 10/16/2023 |
3.3.4 | 215,560 | 9/28/2023 |
3.3.3 | 665,192 | 8/4/2023 |
3.3.2 | 104,001 | 7/27/2023 |
3.3.1 | 116,008 | 7/18/2023 |
3.3.0 | 389,358 | 6/19/2023 |
3.2.0 | 30,416 | 6/17/2023 |
3.1.0 | 152,541 | 6/4/2023 |
3.0.6 | 1,234 | 6/4/2023 |
3.0.5 | 1,327 | 6/4/2023 |
3.0.4 | 80,382 | 5/20/2023 |
3.0.3 | 62,435 | 5/10/2023 |
3.0.2 | 114,133 | 5/5/2023 |
3.0.1 | 61,498 | 5/1/2023 |
3.0.0 | 88,409 | 4/25/2023 |
2.1.0 | 43,368 | 4/22/2023 |
2.0.2 | 29,209 | 4/20/2023 |
2.0.1 | 29,828 | 4/18/2023 |
2.0.0 | 8,790 | 4/17/2023 |
1.0.9 | 57,088 | 4/9/2023 |
1.0.8 | 5,976 | 4/9/2023 |
1.0.7 | 6,925 | 4/7/2023 |
1.0.6 | 7,067 | 4/3/2023 |
1.0.5 | 1,347 | 4/2/2023 |
1.0.4 | 3,142 | 3/14/2023 |
1.0.3 | 1,333 | 3/14/2023 |
1.0.2 | 1,926 | 3/10/2023 |
1.0.1 | 1,287 | 3/10/2023 |
1.0.0 | 1,335 | 3/8/2023 |
0.0.86 | 1,265 | 3/8/2023 |
0.0.85 | 1,355 | 3/8/2023 |
0.0.84 | 1,360 | 3/8/2023 |
0.0.83 | 1,359 | 3/8/2023 |
0.0.82 | 1,389 | 3/8/2023 |
0.0.81 | 1,402 | 3/7/2023 |
0.0.80 | 1,376 | 3/6/2023 |