ChatGPT.Net - .Net Library for ChatGPT [Discord]
Other versions [NodeJS Version]
The ChatGPT.Net is a C# library for ChatGPT using official OpenAI API that allows developers to access ChatGPT, a chat-based large language model. With this API, developers can send queries to ChatGPT and receive responses in real-time, making it easy to integrate ChatGPT into their own applications.
using ChatGPT.Net;
// ChatGPT Official API
var bot = new ChatGpt("<API_KEY>");
var response = await bot.Ask("What is the weather like today?");
Console.WriteLine(response);
- Easy to use.
- Using official OpenAI API.
- Supports both free and pro accounts.
- Supports multiple accounts, and multiple conversations.
- Support response streaming, so you can get response while the model is still generating it.
To install ChatGPT.Net, run the following command in the Package Manager Console:
Install-Package ChatGPT.Net
Alternatively, you can install it using the .NET Core command-line interface:
dotnet add package ChatGPT.Net
Here is a sample code showing how to use ChatGPT.Net:
using ChatGPT.Net;
// ChatGPT Official API
var bot = new ChatGpt("<API_KEY>");
// get response
var response = await bot.Ask("What is the weather like today?");
Console.WriteLine(response);
// stream response
await bot.AskStream(response => {
Console.WriteLine(response);
}, "What is the weather like today?");
// get response for a specific conversation
var response = await bot.Ask("What is the weather like today?", "conversation name");
Console.WriteLine(response);
// stream response for a specific conversation
await bot.AskStream(response => {
Console.WriteLine(response);
}, "What is the weather like today?", "conversation name");
Here is a sample code showing how to integrate (chat.openai.com) with your applications using ChatGPT.Net:
using ChatGPT.Net;
// ChatGPT Official API
var bot = new ChatGptUnofficial("<SESSION_TOKEN>");
// get response
var response = await bot.Ask("What is the weather like today?");
Console.WriteLine(response);
// stream response
await bot.AskStream(response => {
Console.WriteLine(response);
}, "What is the weather like today?");
// get response for a specific conversation
var response = await bot.Ask("What is the weather like today?", "conversation name");
Console.WriteLine(response);
// stream response for a specific conversation
await bot.AskStream(response => {
Console.WriteLine(response);
}, "What is the weather like today?", "conversation name");
ChatGptOptions
{
string BaseUrl; // Default: https://api.openai.com
string Model; // Default: gpt-3.5-turbo
double Temperature; // Default: 0.9;
double TopP; // Default: 1.0;
long MaxTokens; // Default: 64;
string[]? Stop; // Default: null;
double PresencePenalty; // Default: 0.0;
double FrequencyPenaltyl; // Default: 0.0;
}
ChatGptUnofficialOptions
{
string BaseUrl; // Default: https://api.pawan.krd
string Model; // Default: text-davinci-002-render-sha
}
This is a simple console app that uses ChatGPT.Net to interact with ChatGPT.
using ChatGPT.Net;
// ChatGPT Official API
var bot = new ChatGpt("<API_KEY>");
var prompt = string.Empty;
while (true)
{
Console.Write("You: ");
prompt = Console.ReadLine();
if (prompt is null) break;
if (string.IsNullOrWhiteSpace(prompt)) break;
if (prompt == "exit") break;
Console.Write("ChatGPT: ");
await bot.AskStream(Console.Write, prompt, "default");
Console.WriteLine();
}
You can use a different model by passing the model name to the constructor.
var bot = new ChatGpt("<API_KEY>", new ChatGptOptions
{
Model = "text-davinci-002-render-paid"
});
you can use ChatGPT Official API by setting the base url to a free reverse proxy server like ChatGPT Free Reverse Proxy
var bot = new ChatGpt("<API_KEY>", new ChatGptOptions
{
BaseUrl = "https://api.pawan.krd"
});
This project is licensed under the MIT License - see the LICENSE file for details