CodeMonarchs.OpenAISharp.Client
0.0.1-beta
See the version list below for details.
dotnet add package CodeMonarchs.OpenAISharp.Client --version 0.0.1-beta
NuGet\Install-Package CodeMonarchs.OpenAISharp.Client -Version 0.0.1-beta
<PackageReference Include="CodeMonarchs.OpenAISharp.Client" Version="0.0.1-beta" />
paket add CodeMonarchs.OpenAISharp.Client --version 0.0.1-beta
#r "nuget: CodeMonarchs.OpenAISharp.Client, 0.0.1-beta"
// Install CodeMonarchs.OpenAISharp.Client as a Cake Addin
#addin nuget:?package=CodeMonarchs.OpenAISharp.Client&version=0.0.1-beta&prerelease
// Install CodeMonarchs.OpenAISharp.Client as a Cake Tool
#tool nuget:?package=CodeMonarchs.OpenAISharp.Client&version=0.0.1-beta&prerelease
OpenAISharp
A <b>.NET Standard 2.1</b> C# class library created to easily interface with the the Open AI API.
Supported Versions of .NET and .NET Core
.NET Core 3.0 | .NET Core 3.1 | .NET 5.0 | .NET 6.0 | .NET 7.0 |
---|
Getting Started - Web API (.NET/.NET Core)
- Install the NuGet package from <a href="#">CodeMonarchs.OpenAISharp</a>:
dotnet add package CodeMonarchs.OpenAISharp
- Open your
appsettings.json
file and add the following key/value pair:{ ...omitted for brevity "OpenAI": { "ApiKey": "<your-api-key>", "OrganizationId": "<your-organization-id>" } }
- Replace
<your-api-key>
with your <b>Open AI API Key</b> from here: - Replace
<your-organizatin-key>
with your <b>Open AI Organization Id</b> from here: - Add <b>required dependencies</b> to the
Program.cs
file:
Currently, thebuilder.Services.AddHttpClient(); builder.Services.AddScoped<IOpenAIClient, OpenAIClient>(); builder.Services.Configure<OpenAIClientOptions>(builder.Configuration.GetSection("OpenAI"));
OpenAIClient
class depends onIHttpClientFactory
so you have toAddHttpClient()
to your project for the dependencies to resolve properly. - Add any the API specifc service classes you want to utilize to the
Program.cs
file:builder.Services.AddScoped<ICompletionService, CompletionService>(); builder.Services.AddScoped<IEditService, EditService>(); builder.Services.AddScoped<IEmbeddingService, EmbeddingService>(); builder.Services.AddScoped<IFileService, FileService>(); builder.Services.AddScoped<IFineTuneService, FineTuneService>(); builder.Services.AddScoped<IImageService, ImageService>(); builder.Services.AddScoped<IModelService, ModelService>(); builder.Services.AddScoped<IModerationService, ModerationService>();
- <b>That's it!</b> Now you can consume any of the services in this repository as you normally would with regular old dependency injection.
<i><b>Note: </b>If you add the base package CodeMonarchs.OpenAISharp
it will include all of the packages listed above. If you are only interested in a particular endpoint (such as the Completion API) from Open AI then just install the package related to that endpoint you're interested in. Example: </i> dotnet add package CodeMonarchs.OpenAISharp.Completion
Usage
CompletionService
// Create Completion
var request = new CreateCompletionRequest("model-name") { Prompt = "Say this is cool" };
var response = await service.CreateCompletionAsync(request);
EditService
// Create Edit
var request = new CreateEditRequest(KnownModelNames.TextDavinciEdit001, "Translate this to Spanish") { Input = "Hey" };
var response = await service.CreateEditAsync(request);
EmbeddingService
// Create Embedding
var request = new CreateEmbeddingRequest(KnownModelNames.TextEmbeddingAda002, "The car was super fast and...");
var response = await service.CreateEmbeddingAsync(request);
FileService
// List Files
var response = await service.ListFilesAsync();
// Upload File (JsonL string in memory)
var fileContent = FileUtility.ToJsonL(new List<FilePromptAndCompletion>
{
new FilePromptAndCompletion("How old is Ryan Tunis?", "35"),
new FilePromptAndCompletion("How old is Lourdes Palacios?", "29"),
new FilePromptAndCompletion("How old is Enzo Tunis?", "That's a trick question. As of Jan 2023 he hasn't been born yet. Expeceted May 7th 2023." ),
new FilePromptAndCompletion("How old is Chris Doherty?", "49. But for a mountain, he has only begun in years." )
});
var response = await service.UploadFileAsync(new UploadFileRequest("test-file.jsonl", fileContent, false));
// Upload File (JsonL from file)
var response = await service.UploadFileAsync(new UploadFileRequest("test-file.jsonl", @"C:\path\to\file.jsonl", true));
// Retrieve File
var response = await service.RetrieveFileAsync("file-id");
// Delete File
var response = await service.DeleteFileAsync("file-id");
// Retrieve File Content
var response = await service.RetrieveFileContentAsync("file-id");
FineTuneService
// List FineTunes
var response = await service.ListFineTunesAsync();
// Create FineTunes
var request = new CreateFineTuneRequest("file-id");
var response = await service.CreateFineTuneAsync(request);
// Retrieve FineTune
var response = await service.RetrieveFineTuneAsync("fine-tune-id");
// Cancel FineTune
var response = await service.CancelFineTuneAsync("fine-tune-id");
// Delete FineTune Model
var response = await service.DeleteFineTuneModelAsync("model-id");
// List FineTune Events
var response = await service.ListFineTuneEventsAsync("fine-tune-id");
ImageService
// Create Image
var request = new CreateImageRequest("Draw Etzio from Assassin's Creed");
var response = await service.CreateImageAsync(request);
// Create Image Edit
var request = new CreateImageEditRequest("test_image_rgba.png", @"C:\path\to\image.png", "Make me something random.", true);
var response = await service.CreateImageEditAsync(request);
// Create Image Variation
var request = new CreateImageVariationRequest("test_image_rgba.png", @"C:\path\to\image.png", true);
var response = await service.CreateImageVariationAsync(request);
ModelService
// List Models
var response = await service.ListModelsAsync();
// Retrieve Model
var response = await service.RetrieveModelAsync(KnownModelNames.Ada);
ModerationService
// List Models
var request = new CreateModerationRequest("is this a bad word");
var response = await service.CreateModerationAsync(request);
Examples
- .NET/.NET Core Web API
- Blazor WebAssembly
- Blazor Server
- Azure Functions v4
NuGet Packages
CodeMonarchs.OpenAISharp
- <small>Includes all of the below packages.</small>
- <small><a href="#">NuGet Package</a></small>
CodeMonarchs.OpenAISharp.Completion
- <small>Based on the Completions API - (https://beta.openai.com/docs/api-reference/completions)</small>
- <small><a href="#">NuGet Package</a></small>
CodeMonarchs.OpenAISharp.Edit
- <small>Based on the Edit API - (https://beta.openai.com/docs/api-reference/edits)</small>
- <small><a href="#">NuGet Package</a></small>
CodeMonarchs.OpenAISharp.Embedding
- <small>Based on the Embeddings API - (https://beta.openai.com/docs/api-reference/embeddings)</small>
- <small><a href="#">NuGet Package</a></small>
CodeMonarchs.OpenAISharp.File
- <small>Based on the Files API - (https://beta.openai.com/docs/api-reference/files)</small>
- <small><a href="#">NuGet Package</a></small>
CodeMonarchs.OpenAISharp.FineTune
- <small>Based on the FineTunes API - (https://beta.openai.com/docs/api-reference/fine-tunes)</small>
- <small><a href="#">NuGet Package</a></small>
CodeMonarchs.OpenAISharp.Image
- <small>Based on the Images API - (https://beta.openai.com/docs/api-reference/images)</small>
- <small><a href="#">NuGet Package</a></small>
CodeMonarchs.OpenAISharp.Model
- <small>Based on the Models API - (https://beta.openai.com/docs/api-reference/models)</small>
- <small><a href="#">NuGet Package</a></small>
CodeMonarchs.OpenAISharp.Moderation
- <small>Based on the Moderations API - (https://beta.openai.com/docs/api-reference/moderation)</small>
- <small><a href="#">NuGet Package</a></small>
CodeMonarchs.OpenAISharp.Client
- <small>An abstraction layer specific to sending HTTP requests to the Open AI API. You don't need to include this by itself.</small>
- <small><a href="#">NuGet Package</a></small>
CodeMonarchs.OpenAISharp.Utilities
- <small>A utility library that contains an implementation of the <a href="https://beta.openai.com/tokenizer?view=bpe">Tokenizer Tool</a> for GPT-3.</small>
- <small><a href="#">NuGet Package</a></small>
Integration Testing Setup (OpenAISharp.IntegrationTests)
- Retrieve your
Apikey
andOrganizationId
from the Open AI Api. - Run the powershell script located in
OpenAISharp.IntegrationTests
to set environment variables with yourApiKey
andOrganizationId
from the Open AI API:.\set-openai-credentials.ps1 -ApiKey <your-api-key> -OrganizationId <your-organization-id>
- That's it!
<i><b>Note: </b>If you have Visual Studio open while you set these environment variables you need to restart it as Visual Studio does not detect when the environment variables change.</i>
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
- Microsoft.Extensions.Http (>= 7.0.0)
- System.Text.Json (>= 7.0.1)
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.0.2-beta | 1 | 1/27/2023 |
0.0.1-beta | 1 | 1/26/2023 |