OpenAI.Net.Client 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package OpenAI.Net.Client --version 1.0.3                
NuGet\Install-Package OpenAI.Net.Client -Version 1.0.3                
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="OpenAI.Net.Client" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OpenAI.Net.Client --version 1.0.3                
#r "nuget: OpenAI.Net.Client, 1.0.3"                
#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 OpenAI.Net.Client as a Cake Addin
#addin nuget:?package=OpenAI.Net.Client&version=1.0.3

// Install OpenAI.Net.Client as a Cake Tool
#tool nuget:?package=OpenAI.Net.Client&version=1.0.3                

.NET NuGet Badge badge Stryker Mutation testing badge license

OpenAI.Net

OpenAI library for .NET

C# .NET library for use with the OpenAI API.

This is community-maintained library.

This library supports .net core 6.0 and above.

Getting started

Install package Nuget package

Install-Package OpenAI.Net.Client

Register services using the extension method

 services.AddOpenAIServices(apiKey);
 OR
 services.AddOpenAIServices(apiKey, organizationId);
 OR
 services.AddOpenAIServices(apiKey, organizationId, apiUrl);

N.B We recommened using environment variables, configuration files or secret file for storing the API key securely. See here for further details.

Inject the service where you need it.

e.g

    public class MyAwsomeService 
    {
        private readonly IOpenAIService _openAIService;
        private readonly ILogger<MyAwsomeService> _logger;

        public MyAwsomeService(IOpenAIService openAIService,ILogger<MyAwsomeService> logger)
        {
            _openAIService = openAIService;
            _logger = logger;
        }

        public async Task<TextCompletionResponse> Search()
        {
            var response = await _openAIService.TextCompletion.Get(model, "Say this is a test",(o) => {
                o.MaxTokens = 1024;
                o.BestOf = 2;
            });

            if (response.IsSuccess)
            {
                return response.Result;
            }
            else
            {
                _logger.LogError(response.Exception, response.ErrorMessage, response.ErrorResponse);
            }

            return new TextCompletionResponse();
        }
    }


Full support of all current API's

Testing

This project has 100% code coverage with Unit tests and 100% pass rate with Stryker mutation testing.

See latest Stryker report here.

We also have Integration tests foreach service.

This should provide confidence in the library going forwards.

Examples

Completion

var response = await service.TextCompletion.Get(model, "Say this is a test",(o) => {
                o.MaxTokens = 1024;
                o.BestOf = 2;
            });

Completion Stream

await foreach(var response in service.TextCompletion.GetStream(request))
{
    Console.WriteLine(response?.Result?.Choices[0].Text);
}

Text Edit

var response = await service.TextEdit.Get("text-davinci-edit-001", "Fix the spelling mistakes", "What day of the wek is it?", (o =>{
    o.TopP = 0.1;
    o.Temperature = 100;
}));

Image Edit

Using file paths
var response = await service.Images.Edit("A cute baby sea otter", @"Images\BabyCat.png", @"Images\Mask.png", o => {
    o.N = 99;
});
Using file bytes
var response = await service.Images.Edit("A cute baby sea otter",File.ReadAllBytes(@"Images\BabyCat.png"), File.ReadAllBytes(@"Images\BabyCat.png"), o => {
    o.N = 99;
});

Image Generate

var response = await service.Images.Generate("A cute baby sea otter",2, "1024x1024");

Image Variation

Using file paths
var response = await service.Images.Variation(@"Images\BabyCat.png", o => {
    o.N = 2;
    o.Size = "1024x1024";
});
Using file bytes
 var response = await service.Images.Variation(File.ReadAllBytes(@"Images\BabyCat.png"), o => {
                o.N = 2;
                o.Size = "1024x1024";
});

Fine Tune Create

var response = await service.FineTune.Create("myfile.jsonl", o => {
    o.BatchSize = 1;
});

Fine Tune Get All

var response = await service.FineTune.Get();

Fine Tune Get By Id

var response = await service.FineTune.Get("fineTuneId");

Fine Tune Get Events

var response = await service.FineTune.GetEvents("fineTuneId");

Fine Tune Delete

var response = await service.FineTune.Delete("modelId");

Fine Tune Cancel

var response = await service.FineTune.Cancel("fineTuneId");

File Upload

Using file path
var response = await service.Files.Upload(@"Images\BabyCat.png");
Using file bytes
var response = await service.Files.Upload(bytes, "mymodel.jsonl");

File Get Content

 var response = await service.Files.GetContent("fileId");

File Get File Detail

var response = await service.Files.Get("fileId");

File Get File All

var response = await service.Files.Get();

File Delete

var response = await service.Files.Delete("1");

Emdeddings Create

var response = await service.Embeddings.Create("The food was delicious and the waiter...", "text-embedding-ada-002", "test");

Models Get All

var response = await service.Models.Get();

Models Get By Id

var response = await service.Get("babbage");

Moderation Create

var response = await service.Moderation.Create("input text", "test");

Contributions

Contributions are welcome.

Minimum requirements for any PR's.

  • MUST include Unit tests and maintain 100% coverage.

  • MUST pass Stryker mutation testing with 100%

  • SHOULD have integration tests

Product Compatible and additional computed target framework versions.
.NET 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.