オープンAI APIを依存噴射をサポートして包む単純な軽量ライブラリ。
サポートされている機能は次のとおりです。
このライブラリに構築されたBlazor Webアプリのビデオウォークスルーについては、以下を参照してください。
これはGitHubページに展開され、Open AI UIで利用可能です。 Blazor Webアプリのソースは、whetstone.chatgpt.blazor.appにあります。
例は次のとおりです。
services . Configure < ChatGPTCredentials > ( options =>
{
options . ApiKey = " YOURAPIKEY " ;
options . Organization = " YOURORGANIZATIONID " ;
} ) ;使用:
services . AddHttpClient ( ) ;または:
services . AddHttpClient < IChatGPTClient , ChatGPTClient > ( ) ; IChatGPTClientサービスの構成:
services . AddScoped < IChatGPTClient , ChatGPTClient > ( ) ; チャットの完了は、チャット用に最適化された特別なタイプの完了です。これらは、会話のコンテキストで使用されるように設計されています。
これは、GPT-3.5ターボモデルの使用を示しています。
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モデルを使用することもできます。
完了モデルを使用して、分類、感情分析、質問への回答などを含むがこれらに限定されないさまざまなタスクに答えます。
これは、プロンプトなしのGPT-3.5ターボin腸内モデルの直接的な使用を示しています。
CreateCompletionAsyncは時代遅れです。代わりに、 chatgptchatcompletionRequest 、 chatgptchatcompletionResponse 、およびcreatechatcompletionAsyncメソッドを使用してください。
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は決定論的ではありません。上記のサンプルのテスト実行の1つが返されました。
天気は場所によって大きく異なります。一般に、気温は中程度で気候が快適であると予想できますが、特定の領域の予測を確認することが常に最適です。
完了を使用するAC#コンソールアプリケーションは、以下で利用できます。
whetstone.chatgpt.commandlinebot(chatgpt-marv)
このサンプルには次のものが含まれます。
新しいファインチューニングファイルをアップロードする方法。
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 ) ;
} ファイルが作成されたら、FileIDを取得し、新しい微調整を作成するときに参照してください。
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 ;微調整リクエストの処理には時間がかかります。終了すると、ステータスは「成功」を報告し、完了リクエストで使用する準備ができています。
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 ( ) ) ; 1024x1024画像を生成する例を次に示します。
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 ) ;
} 彼女は、ささやきを使用してオーディオファイルを転写する例です。
string audioFile = @"audiofilestranscriptiontest.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 ) ;