• Stars
    star
    214
  • Rank 180,880 (Top 4 %)
  • Language
    C#
  • License
    MIT License
  • Created over 1 year ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A ChatGPT integration library for .NET, supporting both OpenAI and Azure OpenAI Service

ChatGPT for .NET

Lint Code Base CodeQL NuGet Nuget License: MIT

A ChatGPT integration library for .NET, supporting both OpenAI and Azure OpenAI Service.

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 =>
{
    // OpenAI.
    //options.UseOpenAI(apiKey: "", organization: "");

    // Azure OpenAI Service.
    //options.UseAzure(resourceName: "", apiKey: "", authenticationType: AzureAuthenticationType.ApiKey);

    options.DefaultModel = "my-model";
    options.MessageLimit = 16;  // Default: 10
    options.MessageExpiration = TimeSpan.FromMinutes(5);    // Default: 1 hour
});

ChatGptNet supports both OpenAI and Azure OpenAI Service, so it is necessary to set the correct configuration settings based on the chosen provider:

OpenAI (UseOpenAI)

  • ApiKey: it is available in the User settings page of the OpenAI account (required).
  • Organization: for users who belong to multiple organizations, you can also specify which organization is used. Usage from these API requests will count against the specified organization's subscription quota (optional).
Azure OpenAI Service (UseAzure)
  • ResourceName: the name of your Azure OpenAI Resource (required).
  • ApiKey: Azure OpenAI provides two methods for authentication. You can use either API Keys or Azure Active Directory (required).
  • ApiVersion: the version of the API to use (optional). Allowed values:
    • 2023-03-15-preview
    • 2023-05-15
    • 2023-06-01-preview
    • 2023-07-01-preview (default)
  • AuthenticationType: it specifies if the key is an actual API Key or an Azure Active Directory token (optional, default: "ApiKey").

DefaultModel

ChatGPT can be used with different models for chat completion, both on OpenAI and Azure OpenAI service. With the DefaultModel property, you can specify the default model that will be used, unless you pass an explicit value in the AskAsync method.

OpenAI

Currently available models are: gpt-3.5-turbo, gpt-3.5-turbo-16k, gpt-4 and gpt-4-32k. They have fixed names, available in the OpenAIChatGptModels.cs file.

Azure OpenAI Service

In Azure OpenAI Service, you're required to first deploy a model before you can make calls. When you deploy a model, you need to assign it a name, that must match the name you use with ChatGptNet.

Note
Some models are not available in all regions. You can refer to Model Summary table and region availability page to check current availabilities.

Caching, MessageLimit and MessageExpiration

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 or Azure OpenAI service, so it's up to us to retain the current state. By default, ChatGptNet 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.

If necessary, it is possibile to provide a custom Cache by implementing the IChatGptCache interface and then calling the WithCache extension method:

public class LocalMessageCache : IChatGptCache
{
    private readonly Dictionary<Guid, IEnumerable<ChatGptMessage>> localCache = new();

    public Task SetAsync(Guid conversationId, IEnumerable<ChatGptMessage> messages, TimeSpan expiration, CancellationToken cancellationToken = default)
    {
        localCache[conversationId] = messages.ToList();
        return Task.CompletedTask;
    }

    public Task<IEnumerable<ChatGptMessage>?> GetAsync(Guid conversationId, CancellationToken cancellationToken = default)
    {
        localCache.TryGetValue(conversationId, out var messages);
        return Task.FromResult(messages);
    }

    public Task RemoveAsync(Guid conversationId, CancellationToken cancellationToken = default)
    {
        localCache.Remove(conversationId);
        return Task.CompletedTask;
    }

    public Task<bool> ExistsAsync(Guid conversationId, CancellationToken cancellationToken = default)
    {
        var exists = localCache.ContainsKey(conversationId);
        return Task.FromResult(exists);
    }
}

// Registers the custom cache at application startup.
builder.Services.AddChatGpt(/* ... */).WithCache<LocalMessageCache>();

We can also set ChatGPT parameters for chat completion at startup. Check the official documentation for the list of available parameters and their meaning.

Configuration using an external source

The configuration can be automatically read from IConfiguration, using for example a ChatGPT section in the appsettings.json file:

"ChatGPT": {
    "Provider": "OpenAI",               // Optional. Allowed values: OpenAI (default) or Azure
    "ApiKey": "",                       // Required
    //"Organization": "",               // Optional, used only by OpenAI
    "ResourceName": "",                 // Required when using Azure OpenAI Service
    "ApiVersion": "2023-07-01-preview", // Optional, used only by Azure OpenAI Service (default: 2023-07-01-preview)
    "AuthenticationType": "ApiKey",     // Optional, used only by Azure OpenAI Service. Allowed values: ApiKey (default) or ActiveDirectory

    "DefaultModel": "my-model",
    "MessageLimit": 20,
    "MessageExpiration": "00:30:00",
    "ThrowExceptionOnError": true
    //"User": "UserName",
    //"DefaultParameters": {
    //    "Temperature": 0.8,
    //    "TopP": 1,
    //    "MaxTokens": 500,
    //    "PresencePenalty": 0,
    //    "FrequencyPenalty": 0
    //}
}

And then use the corresponding overload of che AddChatGpt method:

// Adds ChatGPT service using settings from IConfiguration.
builder.Services.AddChatGpt(builder.Configuration);

Configuring ChatGptNet dinamically

The AddChatGpt method has also an overload that accepts an IServiceProvider as argument. It can be used, for example, if we're in a Web API and we need to support scenarios in which every user has a different API Key that can be retrieved accessing a database via Dependency Injection:

builder.Services.AddChatGpt((services, options) =>
{
    var accountService = services.GetRequiredService<IAccountService>();

    // Dynamically gets the API Key from the service.
    var apiKey = "..."        

    options.UseOpenAI(apiKyey);
});

Configuring ChatGptNet using both IConfiguration and code

In more complex scenarios, it is possible to configure ChatGptNet using both code and IConfiguration. This can be useful if we want to set a bunch of common properties, but at the same time we need some configuration logic. For example:

builder.Services.AddChatGpt((services, options) =>
{
    // Configure common properties (message limit and expiration, default parameters, ecc.) using IConfiguration.
    options.UseConfiguration(builder.Configuration);

    var accountService = services.GetRequiredService<IAccountService>();

    // Dynamically gets the API Key from the service.
    var apiKey = "..."        

    options.UseOpenAI(apiKyey);
});

Usage

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

app.MapPost("/api/chat/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 retrieve the response message, we can call the GetMessage method:

var message = response.GetMessage();

Handling a conversation

The AskAsync and AskStreamAsync (see below) methods provides overloads that require 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 AskAsync or AskStreamAsync, so that the library automatically retrieves previous messages of the current conversation (according to MessageLimit and MessageExpiration settings) and send them to chat completion API.

This is the default behavior for all the chat interactions. If you want to exlude a particular interaction from the conversation history, you can set the addToConversationHistory argument to false:

var response = await chatGptClient.AskAsync(conversationId, message, addToConversationHistory: false);

In this way, the message will be sent to the chat completion API, but it and the corresponding answer from ChatGPT will not be added to the conversation history.

On the other hand, in some scenarios, it could be useful to manually add a chat interaction (i.e., a question followed by an answer) to the conversation history. For example, we may want to add a message that was generated by a bot. In this case, we can use the AddInteractionAsync method:

await chatGptClient.AskInteractionAsync(conversationId, question: "What is the weather like in Taggia?",
    answer: "It's Always Sunny in Taggia");

The question will be added as user message and the answer will be added as assistant message in the conversation history. As always, these new messages (respecting the MessageLimit option) will be used in subsequent invocations of AskAsync or AskStreamAsync.

Response streaming

Chat completion API supports response streaming. When using this feature, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available. ChatGptNet provides response streaming using the AskStreamAsync method:

// Requests a streaming response.
var responseStream = chatGptClient.AskStreamAsync(conversationId, message);

await foreach (var response in responseStream)
{
    Console.Write(response.GetMessage());
    await Task.Delay(80);
}

Response streaming works by returning an IAsyncEnumerable, so it can be used even in a Web API project:

app.MapGet("/api/chat/stream", (Guid? conversationId, string message, IChatGptClient chatGptClient) =>
{
    async IAsyncEnumerable<string> Stream()
    {
        // Requests a streaming response.
        var responseStream = chatGptClient.AskStreamAsync(conversationId.GetValueOrDefault(), message);

        // Uses the "AsDeltas" extension method to retrieve partial message deltas only.
        await foreach (var delta in responseStream.AsDeltas())
        {
            yield return delta;
            await Task.Delay(50);
        }
    }

    return Stream();
})
.WithOpenApi();

The library is 100% compatible also with Blazor WebAssembly applications:

Check out the Samples folder for more information about the different implementations.

Changing the assistant's behavior

ChatGPT supports messages with the system role to influence how the assistant should behave. For example, we can tell to ChatGPT something like that:

  • You are an helpful assistant
  • Answer like Shakespeare
  • Give me only wrong answers
  • Answer in rhyme

ChatGptNet provides this feature using the SetupAsync method:

var conversationId await = chatGptClient.SetupAsync("Answer in rhyme");

If we use the same conversationId when calling AskAsync, then the system message will be automatically sent along with every request, so that the assistant will know how to behave.

Note
The system message does not count for messages limit number.

Deleting a conversation

Conversation history is automatically deleted when expiration time (specified by MessageExpiration property) is reached. However, if necessary it is possible to immediately clear the history:

await chatGptClient.DeleteConversationAsync(conversationId, preserveSetup: false);

The preserveSetup argument allows to decide whether mantain also the system message that has been set with the SetupAsync method (default: false).

Function calling

With function calling, we can describe functions and have the model intelligently choose to output a JSON object containing arguments to call those functions. This is a new way to more reliably connect GPT's capabilities with external tools and APIs.

Note
Currently, on Azure OpenAI Service, function calling is supported in the following models in API version 2023-07-01-preview:

  • gpt-35-turbo-0613
  • gpt-35-turbo-16k-0613
  • gpt-4-0613
  • gpt-4-32k-0613

ChatGptNet fully supports function calling by providing an overload of the AskAsync method that allows to specify function definitions. If this parameter is supplied, then the model will decide when it is appropiate to use one the functions. For example:

var functions = new List<ChatGptFunction>
{
    new()
    {
        Name = "GetCurrentWeather",
        Description = "Get the current weather",
        Parameters = JsonDocument.Parse("""                                        
        {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and/or the zip code"
                },
                "format": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "The temperature unit to use. Infer this from the users location."
                }
            },
            "required": ["location", "format"]
        }
        """)
    },
    new()
    {
        Name = "GetWeatherForecast",
        Description = "Get an N-day weather forecast",
        Parameters = JsonDocument.Parse("""                                        
        {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and/or the zip code"
                },
                "format": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "The temperature unit to use. Infer this from the users location."
                },
                "daysNumber": {
                    "type": "integer",
                    "description": "The number of days to forecast"
                }
            },
            "required": ["location", "format", "daysNumber"]
        }
        """)
    }
};

var functionParameters = new ChatGptFunctionParameters
{
    FunctionCall = ChatGptFunctionCalls.Auto,   // This is the default if functions are present.
    Functions = functions
};

var response = await chatGptClient.AskAsync("What is the weather like in Taggia?", functionParameters);

We can pass an arbitrary number of functions, each one with a name, a description and a JSON schema describing the function parameters, following the JSON Schema references. Under the hood, functions are injected into the system message in a syntax the model has been trained on. This means functions count against the model's context limit and are billed as input tokens.

The response object returned by the AskAsync method provides a property to check if the model has selected a function call:

if (response.IsFunctionCall)
{
    Console.WriteLine("I have identified a function to call:");

    var functionCall = response.GetFunctionCall()!;

    Console.WriteLine(functionCall.Name);
    Console.WriteLine(functionCall.Arguments);
}

This code will print something like this:

I have identified a function to call:
GetCurrentWeather
{
    "location": "Taggia",
    "format": "celsius"
}

Note that the API will not actually execute any function calls. It is up to developers to execute function calls using model outputs.

After the actual execution, we need to call the AddFunctionResponseAsync method on the ChatGptClient to add the response to the conversation history, just like a standard message, so that it will be automatically used for chat completion:

// Calls the remote function API.
var functionResponse = await GetWeatherAsync(functionCall.Arguments);
await chatGptClient.AddFunctionResponseAsync(conversationId, functionCall.Name, functionResponse);

Check out the Function calling sample for a complete implementation of this workflow.

Content filtering

When using Azure OpenAI Service, we automatically get content filtering for free. For details about how it works, check out the documentation. This information is returned for all scenarios when using API version 2023-06-01-preview or later. ChatGptNet fully supports this object model by providing the corresponding properties in the ChatGptResponse and ChatGptChoice classes.

Documentation

The full technical documentation is available here.

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.

Warning
Remember to work on the develop branch, don't use the master branch directly. Create Pull Requests targeting develop.

More Repositories

1

TinyHelpers

A collection of helper methods and classes for .NET that I use every day. I have packed them in a single library to avoid code duplication.
C#
179
star
2

SimpleAuthentication

A library to easily integrate Authentication in ASP.NET Core projects.
C#
111
star
3

MinimalHelpers

A collection of helpers libraries for Minimal API projects.
C#
31
star
4

OperationResults

A set of lightweight libraries to totally decouple operation results and actual application responses.
C#
27
star
5

AlexaSkillTemplate

A Visual Studio Project Template to create Alexa Skills using an Azure Function as backend.
C#
14
star
6

IdentitySample

A sample that shows how to deal with Tokens, Users, Roles, Claims and Policy using ASP.NET Core Identity
C#
13
star
7

TinyCMS

The smallest CMS engine ever, made with ASP. NET Core and Dapper
JavaScript
13
star
8

TranslatorService

A lightweight library that uses Cognitive Translator Service for text translation and Cognitive Speech Service for text-to-speech and speech-to-text
C#
11
star
9

TotalDecoupling

A sample that shows how to write a Web API in which every Controller method is literally formed by a single line of code, yet with the ability to handle all response types, validation errors, etc.
C#
9
star
10

DesktopNet70

A couple of .NET 7.0 Windows Forms and WPF applications that show how to use Host Builder, Dependency Injection, Logging and how to access to Windows 10 APIs.
C#
8
star
11

XamarinNetCore

A sample on how to use .NET 5.0 features like HostBuilder, Dependecy Injection, Logging and HttpClientFactory with Xamarin.Forms
C#
7
star
12

MinimalHelpers.Binding

A library that provides Binding helpers for Minimal API projects.
C#
7
star
13

MyWebApiToolbox

A sample structure of a Web API project with Entity Framework Core, Mapping, Validation, Error Handling and much more.
C#
7
star
14

QueueMessaging

A library that allows to easily integrate Queue messaging with RabbitMQ and Azure Service Bus in a .NET application.
C#
7
star
15

Schrodinger

Like the famous paradox, a .NET type that can be any value until you check it.
C#
6
star
16

EntityDapperCore

An example that shows how to use both Entity Framework Core and Dapper in the same project, sharing the database configuration.
C#
6
star
17

SearchGPT

A sample that shows how to integrate ChatGPT with your own data that comes from Azure Cognitive Search
JavaScript
6
star
18

BabelFish

An app that shows how to use the Microsoft Translator Speech APIs to build a real-time voice translation app for the Universal Windows Platform, event for Windows 10 IoT Core
C#
5
star
19

marcominerva

My public profile
5
star
20

StorageProviders

A collection of Storage Providers for various destinations (e.g. file system and Azure Storage)
C#
5
star
21

WebApiUploaderSample

A sample that shows how to handle file uploads/downloads in an ASP.NET Core Web API
C#
5
star
22

RestLibrary

A lightweight library to perform common REST API calls
C#
5
star
23

AICompanion

A sample app showcasing the use of Cognitive Services within UWP & Xamarin apps: Vision, Face and Custom Vision.
C#
5
star
24

Dapper110

A collection of samples that show how to use some advanced features of Dapper
C#
5
star
25

PeopleFinder

Enrich Azure Cognitive Search index with Face Cognitive Service, Azure Maps & Exif Metadata and access all the information within a UWP client
C#
5
star
26

DallENet

A DALLยทE integration library for .NET
C#
5
star
27

WebAPI-Samples

A collection of Web APIs made with ASP.NET Core 5.0 and related clients to show their usage
C#
4
star
28

UWPCustomVisionCompanion

A sample app showcasing the use of Custom Vision in the Universal Windows Platform.
C#
4
star
29

WeatherApp

A sample that shows how to leverage .NET 5.0 features like HostBuilder, ServiceProvider, Logging and Cache on every supported platform
C#
4
star
30

MultiTenant

An ASP.NET Core application that uses wildcard domains to handle multi-tenancy
C#
4
star
31

AwesomeBackend

A sample API backend made with ASP.NET Core 5.0
C#
4
star
32

LoggingNetCore

A sample that shows how to use Logging features of .NET on every supported platforms
C#
4
star
33

ErrorHandling

Some samples that show how to correctly handle errors in Web API projects
C#
4
star
34

customvision-trainer

A small application that allows to automatically upload and tag images for Custom Vision Service
C#
4
star
35

Minimal-IoT

A sample that showcases how to use Minimal APIs to build a Web Server to control IoT devices
C#
3
star
36

LocalizationSample

A sample about how to handle Localization with ASP.NET Core WEB APIs
C#
3
star
37

FluentEmailSample

A Web API that uses FluentEmail to send e-mails, with the support for Sendinblue
C#
3
star
38

ChatGptPlayground

A ready-to-use ASP.NET Core chat application backed by a Minimal API that can be used to test ChatGPT workflows
JavaScript
3
star
39

HangfireSample

A collection of samples about using Hangfire in a Web API application
C#
3
star
40

Friendbook

A sample Web API for managing a friends' list
C#
3
star
41

MinimalApiHang

This repository shows a problem with Swagger when using WithOpenApi extension method in a Minimal API project
C#
2
star
42

AI-Samples

A collection of AI related samples made with different frameworks
C#
2
star
43

ValidationSample

A sample that shows how to use FluentValidation in .NET
C#
2
star
44

AspNetCoreWithAlpineJs

A sample that shows how to integrate Alpine.js with ASP.NET Core Razor Pages and Web API
JavaScript
2
star
45

MinimalApiSample

A sample that showcases Minimal APIs features.
C#
2
star
46

SqlDatabaseProject

SQL Server Database Project sample
TSQL
2
star
47

EntityFrameworkCore7Samples

A collection of samples about the main new features available in Entity Framework Core 7.0
C#
2
star
48

DateTimeMadness

A collection of use cases and samples that show how to manage dates and times in a backend
C#
2
star
49

DateTimeOnly

A repository that shows how to work with DateOnly and TimeOnly data types with .NET 6.0, ASP.NET Core and Entity Framework Core 6.0
C#
2
star
50

Raspberry-Jukebox

A remotely controlled Jukebox built with a Raspberry Pi 2 running Windows 10 IoT Core
JavaScript
2
star
51

SortingPagination

A sample that shows how to efficiently handle dynamic sorting and pagination of data that come from a database, using Entity Framework Core or Dapper
C#
2
star
52

AspNetCore7Samples

A collection of samples about the main new features available in ASP.NET Core 7.0
C#
2
star
53

MultilanguageChat

This project shows how to use Azure SignalR, Microsoft Translator and Speech Service to build a real-time voice translation system
C#
2
star
54

CognitiveSearch

Samples about Azure Cognitive Search with a UWP client
C#
1
star
55

Kowalski

A personal assistant made with LUIS and the Bot Framework, running on a Windows 10 IoT Core device
C#
1
star
56

RateLimiterSample

A sample that shows how to use the Rate Limiter of ASP.NET Core 7.0 in conjunction with Authentication
C#
1
star
57

GalacticProject

Sample projects that show how to not reinvent the wheel while developing
C#
1
star
58

EntityFrameworkCoreAuditing

This repository show different ways to audit database changes with Entity Framework Core
C#
1
star
59

EntityFrameworkCoreTipsTricks

A collection of code samples that can be useful when using Entity Framework Core
C#
1
star
60

Swagger

A sample that shows how to effectively integrate Swagger into an ASP.NET Core 6.0 Web API
C#
1
star
61

TheMovieDbApiClient

A small library that allows to access the API of The Movie Database (https://www.themoviedb.org)
C#
1
star
62

MultipleAuthenticationProviders

A sample that shows how to handle multiple authentication providers in an ASP.NET Core Web API project
C#
1
star
63

marcominerva.github.io

Sharp Design Web Site
HTML
1
star
64

SecondaryClock

A clock that is shown on secondary (last) Taskbar on Windows 11.
C#
1
star
65

OutputCacheIssue

This repository shows an inconsistency in Output caching behavior documentation.
C#
1
star
66

ParameterRenameIssue

This repository shows that Route, Query and Header parameter names are ignored when using WithOpenApi extension method
C#
1
star
67

RandomPhotos

Use AI to generate random photos
JavaScript
1
star
68

TranslatorGPT

Translate text using AI
JavaScript
1
star
69

DataProtectionSample

This example shows how to use the DataProtection APIs with ASP.NET Core
C#
1
star
70

PathIssue

This repository shows that Route parameters are not set as required when using WithOpenApi and Nullable Reference Types are disabled
C#
1
star
71

OpenApiParameterIssue

This repository shows a problem with OpenAPI definition of path, query and header parameters when there is a body and we're using WithOpenApi extension method
C#
1
star
72

KeyedServicesIssue

This repository shows a different behavior of FromKeyedServices attribute in Minimal APIs endpoint handler and inside a class library
C#
1
star
73

CustomCommands

A sample that shows how to integrate Custom Commands in a real system, with support for Wake Word.
C#
1
star