OpenAI.ChatGPT 1.1.0

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

// Install OpenAI.ChatGPT as a Cake Tool
#tool nuget:?package=OpenAI.ChatGPT&version=1.1.0

OpenAI Chat Completions (ChatGPT) client for .NET

This is a .NET client for the OpenAI Chat Completions API (ChatGPT). It allows you to use the API in your .NET applications. Also, the client supports streaming responses (like ChatGPT) via async streams.

Preparation

First, you need to create an OpenAI account and get an API key. You can do this at https://platform.openai.com/account/api-keys.

Installation

You can install the package via NuGet:

Install-Package OpenAI.ChatGPT

Then create an instance of OpenAIClient:

_client = new OpenAiClient("{YOUR_OPENAI_API_KEY}");

Simple usage of the Chat Completions API

string text = "Who are you?";
string response = await _client.GetChatCompletions(new UserMessage(text), maxTokens: 80);
Console.WriteLine(response);

Streaming response with async streams (like ChatGPT)

var text = "Write the world top 3 songs of Soul genre";
await foreach (string chunk in _client.StreamChatCompletions(new UserMessage(text), maxTokens: 80))
{
    Console.Write(chunk);
}

Continue dialog with ChatGPT (message history)

Use ThenAssistant and ThenUser methods to create a dialog:

ChatCompletionDialog dialog = 
    Dialog.StartAsUser("How many meters are in a kilometer? Write just the number.") //the message from user
          .ThenAssistant("1000") // response from the assistant
          .ThenUser("Convert it to hex. Write just the number."); // the next message from user

await foreach (var chunk in _client.StreamChatCompletions(dialog, maxTokens: 80))
{
    Console.Write(chunk);
}

Or just send message history as a collection.

OpenAI Images API (text-to-image)

Generate image bytes

byte[] image = await _client.GenerateImageBytes("bicycle", "test", OpenAiImageSize._256);

Generate images uris

Uri[] uris = await _client.GenerateImagesUris("bicycle", "test", OpenAiImageSize._256, count: 2);

More examples see in the 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 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.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on OpenAI.ChatGPT:

Package Downloads
OpenAI.ChatGPT.AspNetCore

OpenAI Chat Completions API (ChatGPT) integration with easy DI supporting (Microsoft.Extensions.DependencyInjection). It allows you to use the API in your .NET applications. Also, the client supports streaming responses (like ChatGPT) via async streams.

OpenAI.ChatGPT.Modules.StructuredResponse

The module for OpenAI ChatGPT that allows to retrive a structured response from ChatGPT.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.1.0-alpha 470 12/17/2023
4.0.2-alpha 381 12/5/2023
4.0.1-alpha 142 12/5/2023
4.0.0-alpha 132 12/5/2023
3.3.0 4,932 11/24/2023
3.2.0 801 11/17/2023
3.1.1 262 11/11/2023
3.1.0 726 11/10/2023
3.0.0 238 11/8/2023
2.9.3 175 11/8/2023
2.9.2 154 11/7/2023
2.9.1 718 11/3/2023
2.9.0 476 10/20/2023
2.8.0 5,428 7/20/2023
2.7.1 568 7/13/2023
2.7.0 421 7/2/2023
2.6.0 681 6/17/2023
2.5.0 3,740 4/28/2023
2.4.2 349 4/24/2023
2.4.1 369 4/24/2023
2.4.0 304 4/24/2023
2.3.0 347 4/20/2023
2.2.2 295 4/19/2023
2.2.0 274 4/19/2023
2.1.0 278 4/18/2023
2.0.3 284 4/18/2023
2.0.2 298 4/18/2023
2.0.1 259 4/18/2023
2.0.0 279 4/17/2023
1.1.0 921 3/3/2023
1.0.0 357 3/2/2023
0.0.1 325 3/2/2023