OpenAI.ChatGPT 2.9.2

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

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

example gif...

ChatGPT integration for .NET

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

[NEW!] StructuredResponse module allows you to get structured responses from the API as C# object. See: StructuredResponse section.

Content

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

The easiest way to use ChatGPT service in your .NET project with DI and persistence (EF Core) supporting is to install the NuGet package OpenAI.ChatGPT.EntityFrameworkCore:

Install-Package OpenAI.ChatGPT.EntityFrameworkCore

If you don't want to use EF Core, you can install the package OpenAI.ChatGPT.AspNetCore and implement your own storage for chat history, using IChatHistoryStorage interface.

Usage

  1. Set the OpenAI API key or even host (optional) in your project user secrets, or the appsettings.json file (not safe):
{
  "OpenAICredentials": {
    "ApiKey": "your-api-key-from-openai",
    "ApiHost": "https://api.openai.com/v1/"
  }
}

Also, you can specify OpenAI API key as environment variable ASPNETCORE_OpenAICredentials:ApiKey.

  1. Add ChatGPT integration with EF to your DI container:
builder.Services.AddChatGptEntityFrameworkIntegration(
    options => options.UseSqlite("Data Source=chats.db"));

Instead of options.UseSqlite("Data Source=chats.db") use your own db and connection string.

  1. Inject ChatGPTFactory to your service and use it to create ChatGPT instance:
public class YourService
{
    private readonly ChatGPTFactory _chatGptFactory;

    public YourService(ChatGPTFactory chatGptFactory)
    {
        _chatGptFactory = chatGptFactory;
    }

    public async Task<string> GetAnswer(string text)
    {
        ChatGPT chatGpt = await _chatGptFactory.Create(userId);
        var chatService = await chatGpt.ContinueOrStartNewTopic();
        response = await _chatService.GetNextMessageResponse(_prompt);
        return response;
    }
}

See Blazor Example.

If you want to configure request parameters, you can do it in appsettings.json configuration or in ChatGPTFactory.Create or in ChatGPT.CreateChat methods.

{
  "ChatGPTConfig": {
      "InitialSystemMessage": null,
      "InitialUserMessage": null,
      "MaxTokens": null,
      "Model": null,
      "Temperature": null,
      "PassUserIdToOpenAiRequests": true
  }
}

See parameters description inside ChatGPTConfig.

Exceptions

If the server response is not a success status code, the client will throw a NotExpectedResponseException. The exception will contain the error message from the OpenAI API.
By default, requesting cancellation or ChatService.Stop() method calling will throw OperationCanceledException. If you don't want to throw it (relevant for streaming responses), you can set throwOnCancellation parameter to false:

await foreach (string chunk in chatService.StreamNextMessageResponse(text, throwOnCancellation: false))
{
    //...
}

Thread safety and async

ChatGPTFactory, ChatGPT classes thread-safety is depend on the IChatHistoryStorage implementation. If you use ChatGPTFactory with entity framework, it's NOT thread-safe. ChatService class is not thread-safe.
Anyways, these services are designed to be used safely with DI, so you don't need to worry about it.
All the methods from all the packages are designed to be used in async context and use ConfigureAwait(false) (thanks for the ConfigureAwait.Fody package).

Retries, timeouts and other policies

Since ChatGPTFactory depends on IHttpClientFactory, you can easily use any of the available policies for it, like Polly.

Modules

StructuredResponse

This module allows you to get structured responses from the API as C# object. It's useful if you want to use the API for something more than just chat.

record City(string Name, int YearOfFoundation, string Country);

var message = Dialog
    .StartAsSystem("Return requested data.")
    .ThenUser("I need info about Almaty city");
City almaty = await _client.GetStructuredResponse<City>(message);
Console.WriteLine(almaty); // Name: "Almaty", Country: "Kazakhstan", YearOfFoundation: 1854

More complex examples with arrays, nested objects and enums are available in tests: https://github.com/rodion-m/ChatGPT_API_dotnet/blob/f50d386f0b65a4ba8c1041a28bab2a1a475c2296/tests/OpenAI.ChatGpt.IntegrationTests/OpenAiClientTests/OpenAiClient_GetStructuredResponse.cs#L1

NuGet: https://www.nuget.org/packages/OpenAI.ChatGPT.Modules.StructuredResponse

Translator

This module allows you to translate messages from one language to another.

string textToTranslate = "Hello, world!";
string translatedText = await _client.TranslateText(textToTranslate, "English", "Russian");
Console.WriteLine(translatedText); // "Привет, мир!"

Also, it's possible to translate entire object in pair with StructuredResponse module:

var objectToTranslate = new Order(
    new List<Order.Item> 
    {
        new(1,"Book", 5),
        new(2,"Pen", 10),
        new(3,"Notebook", 3)
    }
);
Order translatedObject = await _client.TranslateObject(objectToTranslate, "English", "Russian");

See full example in tests: https://github.com/rodion-m/ChatGPT_API_dotnet/blob/11658b76b497b9cc4ac74621c91c5e22cd724f2e/tests/OpenAI.ChatGpt.IntegrationTests/ChatGptTranslatorServiceTests.cs#L36

NuGet: https://www.nuget.org/packages/OpenAI.ChatGPT.Modules.Translator

Examples

API Parameters

Here is a list of the main parameters that can be used in the ChatCompletions (ChatGPT) API request (OpenAI.ChatGpt/Models/ChatCompletion/ChatCompletionRequest.cs). Some of them are taken from this article: https://towardsdatascience.com/gpt-3-parameters-and-prompt-design-1a595dc5b405
Below listed parameters for ChatCompletions API.

Model

The prediction-generating AI model is specified by the engine parameter. The available models are:

  • ChatCompletionModels.Gpt3_5_Turbo (Default): Most capable GPT-3.5 model and optimized for chat at 1/10th the cost of text-davinci-003. Will be updated with OpenAI's latest model iteration.
  • ChatCompletionModels.Gpt3_5_Turbo_0301: Snapshot of gpt-3.5-turbo from March 1st 2023. Unlike gpt-3.5-turbo, this model will not receive updates, and will only be supported for a three month period ending on June 1st 2023.
  • ChatCompletionModels.Gpt4: More capable than any GPT-3.5 model, able to do more complex tasks, and optimized for chat. Will be updated with OpenAI's latest model iteration. *
  • ChatCompletionModels.Gpt4_0314: Snapshot of gpt-4 from March 14th 2023. Unlike gpt-4, this model will not receive updates, and will only be supported for a three month period ending on June 14th 2023. *
  • ChatCompletionModels.Gpt4_32k: Same capabilities as the base gpt-4 mode but with 4x the context length. Will be updated with OpenAI's latest model iteration. *
  • ChatCompletionModels.Gpt4_32k_0314: Snapshot of gpt-4-32 from March 14th 2023. Unlike gpt-4-32k, this model will not receive updates, and will only be supported for a three month period ending on June 14th 2023. *
    Note that training data for all models is up to Sep 2021.
    * These models are currently in beta and are not yet available to all users. Here is the link for joining waitlist: https://openai.com/waitlist/gpt-4-api

MaxTokens

The maximum number of tokens allowed for the generated answer. Defaults to ChatCompletionRequest.MaxTokensDefault (64).

  • This value is validated and limited with ChatCompletionModels.GetMaxTokensLimitForModel method.
  • It's possible to calculate approximately tokens count using ChatCompletionMessage.CalculateApproxTotalTokenCount method
  • The number of tokens can be retrieved from the API response: ChatCompletionResponse.Usage.TotalTokens. As a rule of thumb for English, 1 token is around 4 characters (so 100 tokens ≈ 75 words). See tokenizer from OpenAI: https://platform.openai.com/tokenizer
  • Encoding algorithm can be found here: https://github.com/latitudegames/GPT-3-Encoder

Temperature

What sampling temperature to use, between 0 and 2.

  • Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
  • Predefined values are available in ChatCompletionTemperatures.
  • Default value is: ChatCompletionTemperatures.Balanced (0.5).

Description: Before being mapped into probabilities, the model outputs unnormalized values (logits). The logits are typically used with a function such as softmax to convert them into probabilities.

But, before applying the softmax function, we can use a trick inspired by thermodynamics and scale the logits with the temperature parameter, i.e. softmax(logits/temperature).

A temperature parameter close to 1 would mean that the logits are passed through the softmax function without modification. If the temperature is close to zero, the highest probable tokens will become very likely compared to the other tokens, i.e. the model becomes more deterministic and will always output the same set of tokens after a given sequence of words.

More parameters description can be found here: Some of them are taken from this article: https://towardsdatascience.com/gpt-3-parameters-and-prompt-design-1a595dc5b405

Using raw client without DI

If you don't need DI and chat history, you can use only the NuGet package OpenAI.ChatGPT:

Install-Package OpenAI.ChatGPT

Then create an instance of OpenAIClient:

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

Simple usage of the Chat Completions API (raw client)

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:

var 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.

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.