chatgpt.net เป็นไลบรารี C# สำหรับ chatgpt โดยใช้ Openai API อย่างเป็นทางการที่ช่วยให้นักพัฒนาสามารถเข้าถึง 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 ให้เรียกใช้คำสั่งต่อไปนี้ในคอนโซลแพ็คเกจ Manager:
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 " ) ;
นี่คือรหัสตัวอย่างที่แสดงวิธีการรวม (chat.openai.com) กับแอปพลิเคชันของคุณโดยใช้ 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 "
} ) ;
คุณสามารถใช้ API อย่างเป็นทางการของ Chatgpt ได้โดยการตั้งค่า URL พื้นฐานเป็นพร็อกซีเซิร์ฟเวอร์ย้อนกลับฟรีเช่น Chatgpt Reverse Proxy ฟรี chatgpt
var bot = new ChatGpt ( " <API_KEY> " , new ChatGptOptions
{
BaseUrl = " https://api.pawan.krd "
} ) ;
โครงการนี้ได้รับใบอนุญาตภายใต้ใบอนุญาต MIT - ดูไฟล์ใบอนุญาตสำหรับรายละเอียด