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.
$ dotnet add package OpenAI.ChatGPT.AspNetCore
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.
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.
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.
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.
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.
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.
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))
{
//...
}
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, this 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).
Since ChatGPTFactory depends on IHttClientFactory, you can easily use any of the available policies for it, like Polly.
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.
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. * The maximum number of tokens allowed for the generated answer. Defaults to ChatCompletionRequest.MaxTokensDefault (64).
ChatCompletionModels.GetMaxTokensLimitForModel method.ChatCompletionMessage.CalculateApproxTotalTokenCount methodChatCompletionResponse.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/tokenizerWhat sampling temperature to use, between 0 and 2.
ChatCompletionTemperatures.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
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}");
string text = "Who are you?";
string response = await _client.GetChatCompletions(new UserMessage(text), maxTokens: 80);
Console.WriteLine(response);
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);
}
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.