Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

C# library for ChatGPT using official OpenAI API

License

NotificationsYou must be signed in to change notification settings

PawanOsman/ChatGPT.Net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

95 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Other versions [NodeJS Version]

Nuget PackageNuget PackageGitHub issuesGitHub forksGitHub starsGitHub licenseDiscord server

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.

usingChatGPT.Net;// ChatGPT Official APIvarbot=newChatGpt("<API_KEY>");varresponse=awaitbot.Ask("What is the weather like today?");Console.WriteLine(response);

Table of Contents

Features

  • 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.

Getting Started

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

Usage

ChatGPT Official API

Here is a sample code showing how to use ChatGPT.Net:

usingChatGPT.Net;// ChatGPT Official APIvarbot=newChatGpt("<API_KEY>");// get responsevarresponse=awaitbot.Ask("What is the weather like today?");Console.WriteLine(response);// stream responseawaitbot.AskStream(response=>{Console.WriteLine(response);},"What is the weather like today?");// get response for a specific conversationvarresponse=awaitbot.Ask("What is the weather like today?","conversation name");Console.WriteLine(response);// stream response for a specific conversationawaitbot.AskStream(response=>{Console.WriteLine(response);},"What is the weather like today?","conversation name");// Set a system messagebot.SetConversationSystemMessage("conversation name","You are a helpful assistant that provides clear and concise answers to questions.");

Some models can understand both text and images. To use this feature, you need to pass a list of content items that includes both text and images.

// To stream responses with both image and text input, create a list of content itemsvarcontentItems=newList<ChatGptMessageContentItem>();// Each content item can be either "Text" type or "Image" type. Let's add text to inquire about the imagecontentItems.Add(newChatGptMessageContentItem(){Type=ChatGptMessageContentType.TEXT,Text="what is this image about?"});// Now, create another content item of "Image" typevarcontentItemWithImage=newChatGptMessageContentItem(){Type=ChatGptMessageContentType.IMAGE};// Set image by pathcontentItemWithImage.SetImageFromFile("<path-to-file>");// Or set image by UrlcontentItemWithImage.SetImageFromUrl("https://path-to-image.com/image.png");// Or set base64 image urlcontentItemWithImage.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;varresponse=awaitbot.AskStream(response=>{Console.WriteLine(response);},contentItems,"conversation name");

ChatGPT Unofficial API

Here is a sample code showing how to integrate (chat.openai.com) with your applications using ChatGPT.Net:

usingChatGPT.Net;// ChatGPT Official APIvarbot=newChatGptUnofficial("<SESSION_TOKEN>");// get responsevarresponse=awaitbot.Ask("What is the weather like today?");Console.WriteLine(response);// stream responseawaitbot.AskStream(response=>{Console.WriteLine(response);},"What is the weather like today?");// get response for a specific conversationvarresponse=awaitbot.Ask("What is the weather like today?","conversation name");Console.WriteLine(response);// stream response for a specific conversationawaitbot.AskStream(response=>{Console.WriteLine(response);},"What is the weather like today?","conversation name");

Configuration options

ChatGPT Official API

ChatGptOptions{stringBaseUrl;// Default: https://api.openai.comstringModel;// Default: gpt-3.5-turbodoubleTemperature;// Default: 0.9;doubleTopP;// Default: 1.0;longMaxTokens;// Default: 64;string[]?Stop;// Default: null;doublePresencePenalty;// Default: 0.0;doubleFrequencyPenaltyl;// Default: 0.0;}

ChatGPT Unofficial API

ChatGptUnofficialOptions{stringBaseUrl;// Default: https://api.pawan.krdstringModel;// Default: text-davinci-002-render-sha}

Examples

ChatGPT Console App

This is a simple console app that uses ChatGPT.Net to interact with ChatGPT.

usingChatGPT.Net;// ChatGPT Official APIvarbot=newChatGpt("<API_KEY>");varprompt=string.Empty;while(true){Console.Write("You: ");prompt=Console.ReadLine();if(promptisnull)break;if(string.IsNullOrWhiteSpace(prompt))break;if(prompt=="exit")break;Console.Write("ChatGPT: ");awaitbot.AskStream(Console.Write,prompt,"default");Console.WriteLine();}

Use a different model

You can use a different model by passing the model name to the constructor.

varbot=newChatGpt("<API_KEY>",newChatGptOptions{Model="text-davinci-002-render-paid"});

Using ChatGPT Official API For Free

you can use ChatGPT Official API by setting the base url to a free reverse proxy server likeChatGPT Free Reverse Proxy

varbot=newChatGpt("<API_KEY>",newChatGptOptions{BaseUrl="https://api.pawan.krd"});

License

This project is licensed under the MIT License - see theLICENSE file for details

Releases

No releases published

Sponsor this project

    Packages

    No packages published

    Languages


    [8]ページ先頭

    ©2009-2025 Movatter.jp