Chatgpt.net adalah perpustakaan C# untuk chatgpt menggunakan Openai API resmi yang memungkinkan pengembang untuk mengakses ChatGPT, model bahasa besar berbasis obrolan. Dengan API ini, pengembang dapat mengirim pertanyaan ke chatgpt dan menerima tanggapan secara real-time, membuatnya mudah untuk mengintegrasikan chatgpt ke dalam aplikasi mereka sendiri.
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 ) ;
Untuk menginstal chatgpt.net, jalankan perintah berikut di konsol manajer paket:
Install-Package ChatGPT.Net
Atau, Anda dapat menginstalnya menggunakan antarmuka baris perintah .NET Core:
dotnet add package ChatGPT.Net
Berikut adalah kode sampel yang menunjukkan cara menggunakan 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 " ) ;
// Set a system message
bot . SetConversationSystemMessage ( " conversation name " , " You are a helpful assistant that provides clear and concise answers to questions. " ) ;
Beberapa model dapat memahami teks dan gambar. Untuk menggunakan fitur ini, Anda perlu melewati daftar item konten yang mencakup teks dan gambar.
// To stream responses with both image and text input, create a list of content items
var contentItems = new List < ChatGptMessageContentItem > ( ) ;
// Each content item can be either "Text" type or "Image" type. Let's add text to inquire about the image
contentItems . Add ( new ChatGptMessageContentItem ( )
{
Type = ChatGptMessageContentType . TEXT ,
Text = " what is this image about? "
} ) ;
// Now, create another content item of "Image" type
var contentItemWithImage = new ChatGptMessageContentItem ( )
{
Type = ChatGptMessageContentType . IMAGE
} ;
// Set image by path
contentItemWithImage . SetImageFromFile ( " <path-to-file> " ) ;
// Or set image by Url
contentItemWithImage . SetImageFromUrl ( " https://path-to-image.com/image.png " ) ;
// Or set base64 image url
contentItemWithImage . SetImageFromUrl ( " data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA..... " ) ;
contentItems . Add ( contentItemWithImage ) ;
// Only specific models understand image input. Therefore, if you are using 'gpt-3.5-turbo' or similar model, switch or override the model before calling 'AskStream'. More details about `ChatGptOptions` (config) are covered in the next section.
config . Model = ChatGptModels . GPT_4_Vision_Preview ;
var response = await bot . AskStream ( response => {
Console . WriteLine ( response ) ;
} , contentItems , " conversation name " ) ;
Berikut adalah kode sampel yang menunjukkan cara mengintegrasikan (chat.openai.com) dengan aplikasi Anda menggunakan 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
}
Ini adalah aplikasi konsol sederhana yang menggunakan chatgpt.net untuk berinteraksi dengan 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 ( ) ;
}
Anda dapat menggunakan model yang berbeda dengan meneruskan nama model ke konstruktor.
var bot = new ChatGpt ( " <API_KEY> " , new ChatGptOptions
{
Model = " text-davinci-002-render-paid "
} ) ;
Anda dapat menggunakan API resmi chatgpt dengan mengatur URL dasar ke server proxy terbalik gratis seperti chatgpt gratis proxy terbalik
var bot = new ChatGpt ( " <API_KEY> " , new ChatGptOptions
{
BaseUrl = " https://api.pawan.krd "
} ) ;
Proyek ini dilisensikan di bawah lisensi MIT - lihat file lisensi untuk detailnya