ChatGPT.Net
1.0.0
Chatgpt.net은 공식 OpenAI API를 사용하여 ChatGpt를위한 C# 라이브러리로, 개발자가 Chat 기반 대형 언어 모델 인 Chatgpt에 액세스 할 수 있도록합니다. 이 API를 사용하면 개발자가 ChatGpt에 쿼리를 보내고 실시간으로 응답을받을 수 있으므로 Chatgpt를 자신의 응용 프로그램에 쉽게 통합 할 수 있습니다.
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 ) ;
Chatgpt.net을 설치하려면 패키지 관리자 콘솔에서 다음 명령을 실행하십시오.
Install-Package ChatGPT.Net
또는 .NET Core 명령 줄 인터페이스를 사용하여 설치할 수 있습니다.
dotnet add package ChatGPT.Net
다음은 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. " ) ;
일부 모델은 텍스트와 이미지를 모두 이해할 수 있습니다. 이 기능을 사용하려면 텍스트와 이미지가 모두 포함 된 콘텐츠 항목 목록을 전달해야합니다.
// 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 " ) ;
다음은 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
}
Chatgpt.net을 사용하여 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 ( ) ;
}
모델 이름을 생성자에게 전달하여 다른 모델을 사용할 수 있습니다.
var bot = new ChatGpt ( " <API_KEY> " , new ChatGptOptions
{
Model = " text-davinci-002-render-paid "
} ) ;
기본 URL을 Chatgpt 무료 리버스 프록시와 같은 무료 리버스 프록시 서버로 설정하여 Chatgpt 공식 API를 사용할 수 있습니다.
var bot = new ChatGpt ( " <API_KEY> " , new ChatGptOptions
{
BaseUrl = " https://api.pawan.krd "
} ) ;
이 프로젝트는 MIT 라이센스에 따라 라이센스가 부여됩니다. 자세한 내용은 라이센스 파일을 참조하십시오.