Whetstone.ChatGPT
1.8.2
dotnet add package Whetstone.ChatGPT --version 1.8.2
NuGet\Install-Package Whetstone.ChatGPT -Version 1.8.2
<PackageReference Include="Whetstone.ChatGPT" Version="1.8.2" />
paket add Whetstone.ChatGPT --version 1.8.2
#r "nuget: Whetstone.ChatGPT, 1.8.2"
// Install Whetstone.ChatGPT as a Cake Addin #addin nuget:?package=Whetstone.ChatGPT&version=1.8.2 // Install Whetstone.ChatGPT as a Cake Tool #tool nuget:?package=Whetstone.ChatGPT&version=1.8.2
Whetstone.ChatGPT
A simple light-weight library that wraps the Open AI API with support for dependency injection.
Supported features include:
- GPT 4, GPT 3.5 Turbo and Chat Completions
- Audio Transcription and Translation (Whisper API)
- Chat Completions
- Vision Completions
- Files
- Fine Tunes
- Images
- Embeddings
- Moderations
- Response streaming
For a video walkthrough of a Blazor web app built on this library, please see:
This is deployed to github pages and available at: Open AI UI. Source for the Blazor web app is at Whetstone.ChatGPT.Blazor.App.
Examples include:
Dependency Injection Quickstart
services.Configure<ChatGPTCredentials>(options =>
{
options.ApiKey = "YOURAPIKEY";
options.Organization = "YOURORGANIZATIONID";
});
Use:
services.AddHttpClient();
or:
services.AddHttpClient<IChatGPTClient, ChatGPTClient>();
Configure IChatGPTClient
service:
services.AddScoped<IChatGPTClient, ChatGPTClient>();
Chat Completions
Chat completions are a special type of completion that are optimized for chat. They are designed to be used in a conversational context.
Chat Completion Quickstart
This shows a usage of the GPT-3.5 Turbo model.
using Whetstone.ChatGPT;
using Whetstone.ChatGPT.Models;
. . .
var gptRequest = new ChatGPTChatCompletionRequest
{
Model = ChatGPT35Models.Turbo,
Messages = new List<ChatGPTChatCompletionMessage>()
{
new ChatGPTChatCompletionMessage(ChatGPTMessageRoles.System, "You are a helpful assistant."),
new ChatGPTChatCompletionMessage(ChatGPTMessageRoles.User, "Who won the world series in 2020?"),
new ChatGPTChatCompletionMessage(ChatGPTMessageRoles.Assistant, "The Los Angeles Dodgers won the World Series in 2020."),
new ChatGPTChatCompletionMessage(ChatGPTMessageRoles.User, "Where was it played?")
},
Temperature = 0.9f,
MaxTokens = 100
};
using IChatGPTClient client = new ChatGPTClient("YOURAPIKEY");
var response = await client.CreateChatCompletionAsync(gptRequest);
Console.WriteLine(response?.GetCompletionText());
GPT-4 models can also be used provided your account has been granted access to the limited beta.
Completion
Completions use models to answer a wide variety of tasks, including but not limited to classification, sentiment analysis, answering questions, etc.
Completion Quickstart
This shows a direct useage of the gpt-3.5-turbo-instruct model without any prompts.
Please note, CreateCompletionAsync is obsolete. Use ChatGPTChatCompletionRequest, ChatGPTChatCompletionResponse, and the CreateChatCompletionAsync method instead.
using Whetstone.ChatGPT;
using Whetstone.ChatGPT.Models;
. . .
var gptRequest = new ChatGPTCompletionRequest
{
Model = ChatGPT35Models.Gpt35TurboInstruct,
Prompt = "How is the weather?"
};
using IChatGPTClient client = new ChatGPTClient("YOURAPIKEY");
var response = await client.CreateCompletionAsync(gptRequest);
Console.WriteLine(response.GetCompletionText());
GPT-3.5 is not deterministic. One of the test runs of the sample above returned:
The weather can vary greatly depending on location. In general, you can expect temperatures to be moderate and climate to be comfortable, but it is always best to check the forecast for your specific area.
Completion Code Sample
A C# console application that uses completions is available at:
Whetstone.ChatGPT.CommandLineBot (chatgpt-marv)
This sample includes:
- Authentication
- Created a completion request using a prompt
- Processing completion responses
File Quickstart
How to create a upload a new fine tuning file.
List<ChatGPTTurboFineTuneLine> tuningInput = new()
{
new ChatGPTTurboFineTuneLine()
{
Messages = new List<ChatGPTTurboFineTuneLineMessage>()
{
new(ChatGPTMessageRoles.System, "Marv is a factual chatbot that is also sarcastic."),
new(ChatGPTMessageRoles.User, "What's the capital of France?"),
new(ChatGPTMessageRoles.Assistant, "Paris, as if everyone doesn't know that already.")
},
},
new ChatGPTTurboFineTuneLine()
{
Messages = new List<ChatGPTTurboFineTuneLineMessage>()
{
new(ChatGPTMessageRoles.System, "Marv is a factual chatbot that is also sarcastic."),
new(ChatGPTMessageRoles.User, "Who wrote 'Romeo and Juliet'?"),
new(ChatGPTMessageRoles.Assistant, "Oh, just some guy named William Shakespeare. Ever heard of him?")
},
},
. . .
};
byte[] tuningText = tuningInput.ToJsonLBinary();
string fileName = "marvin.jsonl";
ChatGPTUploadFileRequest? uploadRequest = new ChatGPTUploadFileRequest
{
File = new ChatGPTFileContent
{
FileName = fileName,
Content = tuningText
}
};
ChatGPTFileInfo? newTurboTestFile;
using (IChatGPTClient client = new ChatGPTClient("YOURAPIKEY"))
{
newTurboTestFile = await client.UploadFileAsync(uploadRequest);
}
Fine Tuning Quickstart
Once the file has been created, get the fileId, and reference it when creating a new fine tuning.
IChatGPTClient client = new ChatGPTClient("YOURAPIKEY");
uploadedFileInfo = await client.UploadFileAsync(uploadRequest);
var fileList = await client.ListFilesAsync();
var foundFile = fileList?.Data?.First(x => x.Filename.Equals("marvin.jsonl"));
ChatGPTCreateFineTuneRequest tuningRequest = new ChatGPTCreateFineTuneRequest
{
TrainingFileId = foundFile?.Id,
Model = "gpt-3.5-turbo-1106"
};
ChatGPTFineTuneJob? tuneResponse = await client.CreateFineTuneAsync(tuningRequest);
string? fineTuneId = tuneResponse?.Id;
Processing the fine tuning request will take some time. Once it finishes, the Status will report "succeeded" and it's ready to be used in a completion request.
using IChatGPTClient client = new ChatGPTClient("YOURAPIKEY");
ChatGPTFineTuneJob? tuneResponse = await client.RetrieveFineTuneAsync("FINETUNEID");
if (tuneResponse.Status.Equals("succeeded"))
{
var gptRequest = new ChatGPTChatCompletionRequest
{
Model = "FINETUNEID",
Messages = new List<ChatGPTChatCompletionMessage>()
{
new ChatGPTChatCompletionMessage(ChatGPTMessageRoles.System, "You are a helpful assistant."),
new ChatGPTChatCompletionMessage(ChatGPTMessageRoles.User, "Who won the world series in 2020?"),
new ChatGPTChatCompletionMessage(ChatGPTMessageRoles.Assistant, "The Los Angeles Dodgers won the World Series in 2020."),
new ChatGPTChatCompletionMessage(ChatGPTMessageRoles.User, "Where was it played?")
},
Temperature = 0.9f,
MaxTokens = 100
};
var response = await client.CreateChatCompletionAsync(gptRequest);
Console.WriteLine(response?.GetCompletionText());
Image Quickstart
Here's an example that generates a 1024x1024 image.
ChatGPTCreateImageRequest imageRequest = new()
{
Prompt = "A sail boat",
Size = CreatedImageSize.Size1024,
ResponseFormat = CreatedImageFormat.Base64
};
using IChatGPTClient client = new ChatGPTClient("YOURAPIKEY");
ChatGPTImageResponse? imageResponse = await client.CreateImageAsync(imageRequest);
var imageData = imageResponse?.Data?[0];
if (imageData != null)
{
byte[]? imageBytes = await client.DownloadImageAsync(imageData);
}
Audio Transcription Quickstart
Her's an example that transcribes an audio file using the Whisper.
string audioFile = @"audiofiles\transcriptiontest.mp3";
byte[] fileContents = File.ReadAllBytes(audioFile);
ChatGPTFileContent gptFile = new ChatGPTFileContent
{
FileName = audioFile,
Content = fileContents
};
ChatGPTAudioTranscriptionRequest? transcriptionRequest = new ChatGPTAudioTranscriptionRequest
{
File = gptFile
};
using IChatGPTClient client = new ChatGPTClient("YOURAPIKEY");
ChatGPTAudioResponse? audioResponse = await client.CreateTranscriptionAsync(transcriptionRequest, true);
Console.WriteLine(audioResponse?.Text);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. 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 was computed. 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 is compatible. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.Options (>= 8.0.2)
- System.Text.Json (>= 8.0.5)
-
.NETStandard 2.1
- Microsoft.Extensions.Options (>= 8.0.2)
- System.Text.Json (>= 8.0.5)
-
net6.0
- Microsoft.Extensions.Options (>= 8.0.2)
- System.Text.Json (>= 8.0.5)
-
net8.0
- Microsoft.Extensions.Options (>= 8.0.2)
- System.Text.Json (>= 8.0.5)
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 |
---|---|---|
1.8.2 | 125 | 10/15/2024 |
1.8.1 | 4,743 | 2/17/2024 |
1.8.0 | 164 | 1/21/2024 |
1.7.1 | 185 | 1/8/2024 |
1.7.0 | 782 | 12/17/2023 |
1.6.2 | 766 | 10/8/2023 |
1.6.1 | 1,170 | 7/8/2023 |
1.6.0 | 2,390 | 3/18/2023 |
1.5.0 | 300 | 3/12/2023 |
1.4.0 | 4,085 | 2/11/2023 |
1.3.0 | 633 | 1/21/2023 |
1.2.0 | 431 | 1/8/2023 |
1.1.0 | 327 | 1/7/2023 |
1.0.0 | 350 | 1/4/2023 |
# 1.8.2
- Upgrated to safe version of System.Text.Json
- Added GPT-4o constant
- Marked ChatGPT35Models.Turbo0613 as Obsolete
# 1.8.1
- Added a constructor to ChatGPTChatCompletionMessage
- Added a constructor to ChatGPTTurboFineTuneLineMessage
- Removed unsupported examples (Twitter and simple command line client)
# 1.8.0
- Include Breaking changes
- Removed deprecated edit endpoint
- Updated fine tune endpoints and models
- Refactored Model namespaces
- Marked legacy completions as obsolete
# 1.7.1
- Replaced deprecated models with gpt-3.5-turbo-instruct
- Added vision completion
# 1.7.0
- Updated to target .NET 8.
- Added deserialization error checks.
# 1.6.2
- Updated dependencies
- Updated roles for GPT-3.5 and GPT-4.0 messages to accept strings instead of an enumeration.
# 1.6.1
- Updated package dependencies to latest versions
- Adjusted GPT model constants to match latest API
# 1.6.0
- Added support for GPT-3.5
- Added support for audio transcription and tranlation
# 1.5.0
- Break Change: Updated the StreamCompletionAsync method to return the ChatGPTCompletionStreamResponse which does not include Usage. Stream responses do not include token usage.
# 1.4.0
- Added support for .NET Standard 2.0
- Built a working Blazor sample project
# 1.3.0
- Added set-only Credentials property to IChatGPTClient so that credentials can be changed after instantiation.
- Moved Credentials validation from constructor to API invocation pre-flight checks.
- Started Blazor sample project.
# 1.2.0
- Added support for dependency injection
- Some ChatGPTClient constructors were removed
# 1.1.0
- Added IChatGPTClient interface method for:
- StreamFineTuneEventsAsync
- DownloadImageAsync
- Small updates from code analysis suggestions