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 2,810 7/28/2024
4.1.0-alpha 675 12/17/2023
4.0.2-alpha 458 12/5/2023
4.0.1-alpha 200 12/5/2023
4.0.0-alpha 194 12/5/2023
3.3.0 23,711 11/24/2023
3.2.0 981 11/17/2023
3.1.1 433 11/11/2023
3.1.0 1,531 11/10/2023
3.0.0 303 11/8/2023
2.9.3 235 11/8/2023
2.9.2 216 11/7/2023
2.9.1 870 11/3/2023
2.9.0 539 10/20/2023
2.8.0 5,604 7/20/2023
2.7.1 636 7/13/2023
2.7.0 483 7/2/2023
2.6.0 746 6/17/2023
2.5.0 4,319 4/28/2023
2.4.2 396 4/24/2023
2.4.1 413 4/24/2023
2.4.0 351 4/24/2023
2.3.0 442 4/20/2023
2.2.2 340 4/19/2023
2.2.0 381 4/19/2023
2.1.0 324 4/18/2023
2.0.3 323 4/18/2023
2.0.2 342 4/18/2023 2.0.2 is deprecated because it is no longer maintained.
2.0.1 308 4/18/2023 2.0.1 is deprecated because it is no longer maintained.
2.0.0 326 4/17/2023 2.0.0 is deprecated because it is no longer maintained.
1.1.0 973 3/3/2023 1.1.0 is deprecated because it is no longer maintained.
1.0.0 392 3/2/2023 1.0.0 is deprecated because it is no longer maintained.
0.0.1 393 3/2/2023 0.0.1 is deprecated because it is no longer maintained.