ManagedCode.OpenAI 0.0.6-alpha

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

// Install ManagedCode.OpenAI as a Cake Tool
#tool nuget:?package=ManagedCode.OpenAI&version=0.0.6-alpha&prerelease                

OpenAI

This is an unofficial C# library for the OpenAI API. As there are no official libraries available, we have created our own to help C# developers interact with the API easily.

.NET Coverage Status nuget CodeQL NuGet Package

Installation

To install the ManagedCode.OpenAI library from NuGet, you can use the following methods:

Package Manager

Open the Package Manager Console in Visual Studio and run the following command:

Install-Package ManagedCode.OpenAI

.NET CLI

You can also use the .NET CLI to install the package. Open a terminal/command prompt and run the following command:

dotnet add package ManagedCode.OpenAI

Usage

Initializing the client You can initialize the client in two ways:

var client1 = GptClient.Builder("#API_KEY#")
    .WithOrganization("#ORGANIZATION#")
    .Build();
var client2 = GptClient.Builder("#API_KEY#")
    .WithOrganization("#ORGANIZATION#")
    .Configure(x => x.SetDefaultModel(GptModel.Ada))
    .Build();

or using DI

builder.Services.AddOpenAI("#API_KEY#");

public class MyClass
{
    public MyClass(IGptClient client)
    {
        var chat = client.OpenChat();
    }
}

Ask chat gpt

var client = GptClient.Builder("#API_KEY#")
    .WithOrganization("#ORGANIZATION#")
    .Build();
var chat = client.OpenChat();
var answer = await chat.AskAsync("2+2?");
Console.WriteLine($"Answer: {answer.Data.Content}");

Generating an image URL

var client = new GptClient("#API_KEY#");
var img = await client.ImageClient
    .GenerateImage("Big man")
    .AsUrl().ExecuteAsync();

var url = img.Content;
Console.WriteLine(url);

Generating an image URL with editing

var client = new GptClient("#API_KEY#");
var imgBytes = new byte[] { };
var maskBase64 = "#CONTENT#";

var img = await client.ImageClient
    .EditImage("Change color to red", x => x.FromBytes(imgBytes))
    .SetImageMask(x => x.FromBase64(maskBase64))
    .AsUrl().ExecuteAsync();

// Edited img URL
Console.WriteLine(img.Content);

Editing an image using a mask

var client = new GptClient("#API_KEY#");
var imgBytes = new byte[] { };
var imgCollection = await client.ImageClient
    .VariationImage(x => x.FromBytes(imgBytes))
    .AsBase64String()
    .ExecuteMultipleAsync(5);

foreach (var imageBase64 in imgCollection.Content)
    Console.WriteLine(imageBase64);

Generating multiple image variations as base64 strings

Create multiple variations of an image in base64 string format with 5 results:

var client = new GptClient("#API_KEY#");
var imgBytes = new byte[] { };
var imgCollection = await client.ImageClient
    .VariationImage(x => x.FromBytes(imgBytes))
    .AsBase64String()
    .ExecuteMultipleAsync(5);

foreach (var imageBase64 in imgCollection.Content)
    Console.WriteLine(imageBase64);

Contributing

We welcome contributions to this project. Please submit a pull request or create an issue if you'd like to help improve this library.

Product Compatible and additional computed target framework versions.
.NET 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 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 (1)

Showing the top 1 NuGet packages that depend on ManagedCode.OpenAI:

Package Downloads
ManagedCode.OpenAI.Azure

OpenAI

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.11-alpha 532 10/3/2023
0.0.10-alpha 437 7/24/2023
0.0.9-alpha 153 7/24/2023
0.0.8-alpha 103 7/23/2023
0.0.7-alpha 99 7/21/2023
0.0.6-alpha 91 7/21/2023
0.0.5 487 7/7/2023
0.0.4 326 4/6/2023
0.0.3 196 4/6/2023
0.0.2 258 4/3/2023
0.0.1 229 3/19/2023