ChatGptNet 0.1.8

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

// Install ChatGptNet as a Cake Tool
#tool nuget:?package=ChatGptNet&version=0.1.8                

ChatGPT for .NET

Lint Code Base CodeQL NuGet Nuget License: MIT

A ChatGPT integration library for .NET

Installation

The library is available on NuGet. Just search for ChatGptNet in the Package Manager GUI or run the following command in the .NET CLI:

dotnet add package ChatGptNet

Configuration

Register ChatGPT service at application startup:

builder.Services.AddChatGpt(options =>
{
    options.ApiKey = "";
    options.MessageLimit = 16;  // Default: 10
    options.MessageExpiration = TimeSpan.FromMinutes(5);    // Default: 1 hour
});

The API Key can be obtained in the User settings page of your OpenAI account.

ChatGPT is aimed to support conversational scenarios: user can talk to ChatGPT without specifying the full context for every interaction. However, conversation history isn't managed by OpenAI, so it's up to us to retain the current state. This library handles this requirement using a MemoryCache that stores messages for each conversation. The behavior can be set using the following properties:

  • MessageLimit: specifies how many messages for each conversation must be saved. When this limit is reached, oldest messages are automatically removed.
  • MessageExpiration: specifies the time interval used to maintain messages in cache, regardless their count.

Usage

The library can be used in any .NET application built with .NET 6.0 and later. For example, we can create a Minimal API in this way:

app.MapPost("/api/ask", async (Request request, IChatGptClient chatGptClient) =>
{
    var response = await chatGptClient.AskAsync(request.ConversationId, request.Message);
    return TypedResults.Ok(response);
})
.WithOpenApi();

// ...

public record class Request(Guid ConversationId, string Message);

If we just want to get the response message, we can call the GetMessage method on the response object:

var message = response.GetMessage();

Handling a conversation

The GetAsk method has an overload (the one shown in the example above) that requires a ConversationId parameter. If we pass an empty value, a random one is generated and returned. We can pass this value in subsequent invocations of GetAsk so that the library automatically retrieves previous messages of the current conversation (according to MessageLimit and MessageExpiration settings) and send them to ChatGPT.

Contribute

The project is constantly evolving. Contributions are welcome. Feel free to file issues and pull requests on the repo and we'll address them as we can.

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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.3.9 95 11/6/2024
3.3.7 74 11/5/2024
3.3.5 160 10/15/2024
3.3.4 413 9/11/2024
3.3.2 131 9/5/2024
3.2.18 268 7/23/2024
3.2.17 464 6/26/2024
3.2.15 1,276 5/17/2024
3.2.14 218 5/3/2024
3.2.10 1,144 3/13/2024
3.2.9 154 3/7/2024
3.2.2 382 1/29/2024
3.1.2 268 1/17/2024
3.0.7 242 1/8/2024
3.0.6 818 12/11/2023
2.6.3 1,520 10/16/2023
2.5.4 463 9/19/2023
2.4.7 268 9/18/2023
2.2.15 332 9/7/2023
2.2.14 512 8/11/2023
2.2.12 367 8/3/2023
2.2.11 270 7/27/2023
2.2.8 188 7/21/2023
2.2.4 172 7/20/2023
2.1.9 240 7/12/2023
2.1.6 256 6/26/2023
2.1.3 257 6/19/2023
2.0.16 218 6/14/2023
2.0.14 262 6/7/2023
2.0.11 194 6/5/2023
2.0.9 199 5/30/2023
2.0.3 297 5/16/2023
1.4.2 436 4/17/2023
1.3.8 250 4/10/2023
1.3.6 232 4/5/2023
1.3.3 254 3/29/2023
1.2.3 567 3/24/2023
1.1.6 258 3/22/2023
1.1.4 265 3/21/2023
1.0.5 264 3/19/2023
0.1.12 265 3/12/2023
0.1.8 242 3/11/2023