偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

OpenAI 的 GPT-4 模型初學(xué)者指南

人工智能
今天這篇文章,我們再來分享一個關(guān)于構(gòu)建 ChatGPT 的 GPT-4 聊天機器人的過程,其中包含分步說明和完整的代碼片段。我將介紹有關(guān) GPT-4 API 的基本信息,并提供實用的見解來幫助您創(chuàng)建一個功能強大、引人入勝的聊天機器人。

在昨天我們跟大家分享了關(guān)于GPT-3.5模型的初學(xué)者教程指南??《OpenAI 的 GPT-3.5-Turbo 模型初學(xué)者指南》??,今天這篇文章,我們再來分享一個關(guān)于構(gòu)建 ChatGPT 的 GPT-4 聊天機器人的過程,其中包含分步說明和完整的代碼片段。我將介紹有關(guān) GPT-4 API 的基本信息,并提供實用的見解來幫助您創(chuàng)建一個功能強大、引人入勝的聊天機器人。

我們將深入研究模型定價,探索聊天完成令牌的確切計算方式。此外,我將討論 GPT-3.5-turbo 和 GPT-4 之間的區(qū)別,比較它們的性能,讓您全面了解 GPT-4 必須提供的功能。

1.了解 GPT-4 模型

如果您閱讀了本文教程,您很可能已經(jīng)熟悉 OpenAI 和 ChatGPT。GPT 模型 API 允許像我們這樣的開發(fā)人員訪問和使用 GPT 系列中的預(yù)訓(xùn)練語言模型,例如 GPT-4 和 GPT-3.5-turbo。這些模型能夠生成類似人類的文本,執(zhí)行自然語言處理任務(wù),并支持廣泛的相關(guān)應(yīng)用。

GPT-4 是 OpenAI 的 GPT 模型家族中最新且功能最強大的成員。OpenAI 聲稱 GPT-4 在大多數(shù)測試基準(zhǔn)中都超過了 ChatGPT,這意味著我們有更好的機會實現(xiàn)我們想要的結(jié)果。

此外,它還具有改進的安全功能。然而,GPT-4 并不是 GPT 模型的全部和終結(jié),并不意味著我們應(yīng)該立即停止使用其他模型。我將在本教程后面的與 GPT-3.5-turbo 的比較部分進一步討論這一點。

2.獲得對 GPT-4 模型的訪問權(quán)限

在撰寫本教程時,公眾可以通過兩種方式訪問 GPT-4 模型。

ChatGPT Plus 訂閱:通過訂閱 ChatGPT Plus,您將獲得對 GPT-4 的有限訪問權(quán)限。此訂閱允許用戶每 3 小時發(fā)送 25 條聊天消息。

GPT-4 API(僅限受邀者):目前只有受邀者才能通過等候名單訪問 GPT-4 API。我在注冊后大約 48 小時收到了我的邀請郵件。

請記住,可用性可能會隨著時間的推移而改變,因此請確保及時了解 OpenAI 的公告。

3.使用 GPT-4 模型構(gòu)建聊天機器人

升級到新的 GPT-4 模型 API 非常簡單,因為它使用與 GPT-3.5-turbo 相同的聊天完成方法。如果我們有權(quán)訪問,我們可以期望我們現(xiàn)有的代碼能夠與 GPT-4 無縫協(xié)作。

在本教程中,我將演示如何在 Node.js 中使用 GPT-4 API 構(gòu)建聊天機器人。但是,相同的概念適用于您選擇的其他編程語言。

如果您不確定提示完成和聊天完成的概念,請務(wù)必查看 GPT-3.5-Turbo 教程《??OpenAI 的 GPT-3.5-Turbo 模型初學(xué)者指南??》,我在其中提供了兩者的詳細(xì)比較和示例。

在繼續(xù)之前,請確保您已獲取 OpenAI API 密鑰并相應(yīng)地設(shè)置您的項目。

現(xiàn)在我們已經(jīng)掌握了基礎(chǔ)知識,讓我們開始構(gòu)建我們的 GPT-4 支持的聊天機器人。

4.安裝必要的 NPM 包

npm install dotenv openai chalk

這些庫有以下用途:

  • dotenv:允許我們將 API 密鑰安全地存儲為環(huán)境變量。
  • openai:用于輕松調(diào)用 OpenAI 模型 API 的官方 Node.js 庫。
  • chalk:由于我們正在構(gòu)建一個在我們的終端中運行的聊天機器人,我們將使用 Chalk 為對話添加一些風(fēng)格,使其更具可讀性和視覺吸引力。
  • readline:一個內(nèi)置的 Node.js 庫,我們將使用它來讀取用戶輸入,從而可以輕松地通過命令行與我們的聊天機器人進行交互。

代碼片段:打造您的聊天機器人

// index.js 


// 導(dǎo)入所需的庫
import dotenv from "dotenv";
import { Configuration, OpenAIApi } from "openai";
import readline from "readline";
import chalk from "chalk";


// Load environment variables
dotenv.config();


// Initialize the OpenAI API client
const openai = new OpenAIApi(
new Configuration({ apiKey: process.env.OPENAI_API_KEY })
);


// Create a readline interface for user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});


// Define an array to store the conversation messages
const GPTMessages = [];


// Set the model name; replace with other model names if needed
const modelName = "gpt-4"; // "gpt-3.5-turbo"


// Define an async function to call the GPT API
const GPT = async (message) => {
// Call the GPT API with the model, messages, and max tokens
const response = await openai.createChatCompletion({
model: modelName,
messages: message,
max_tokens: 100,
});


// Return the response content and the total number of tokens used
return {
content: response.data.choices[0].message.content,
tokensUsed: response.data.usage.total_tokens,
};
};


// Define a function to ask the user a question and handle their input
const askUserQuestion = (role) => {
// Set the prompt text based on the role (system or user)
const prompt = role === "system" ? "Enter system message: " : "Enter your question: ";


// Ask the user a question and process their input
rl.question(prompt, async (userInput) => {
// Add the user's input to the GPTMessages array
GPTMessages.push({ role: role, content: userInput });


// If the input is a system message, ask the user for their question
if (role === "system") {
askUserQuestion("user");
} else {
// Call the GPT function with the current conversation messages
const assistantResponse = await GPT(GPTMessages);


// Add the assistant's response to the GPTMessages array
GPTMessages.push({ role: "assistant", content: assistantResponse.content });


// Display the assistant's response and the number of tokens used
console.log(chalk.yellow("-----"));
console.log(chalk.green("Assistant: "), assistantResponse.content);
console.log(chalk.cyan("Tokens used: "), assistantResponse.tokensUsed);


// Ask the user another question
askUserQuestion("user");
}
});
};


// Display the model name and begin the conversation
console.log(`### I'm ${chalk.blue(modelName.toUpperCase())}. ####`);
askUserQuestion("system");

要有效地使用聊天完成構(gòu)建聊天機器人,請按照以下步驟操作:

  1. 為用戶輸入和輸出做準(zhǔn)備:我們?yōu)榱奶鞕C器人設(shè)置了一種方式來接收來自用戶的消息,并使用“readline”庫通過命令行發(fā)送響應(yīng)。
  2. 跟蹤對話:我們創(chuàng)建一個名為 GPTMessages 的數(shù)組來存儲用戶和聊天機器人之間交換的消息。我們還在 modelName 變量中指定要使用的 GPT 模型(例如 GPT-3.5-turbo 或 GPT-4)。
  3. 制作聊天機器人功能:我們創(chuàng)建一個名為 GPT 的功能,它將用戶的消息發(fā)送到 OpenAI API 并接收響應(yīng)。它還會跟蹤聊天機器人在其響應(yīng)中使用了多少令牌,并返回內(nèi)容和令牌使用情況。
  4. 創(chuàng)建來回對話:我們構(gòu)建一個名為 askUserQuestion 的函數(shù),它要求用戶輸入,將輸入保存在 GPTMessages 數(shù)組中,并通過調(diào)用 GPT 函數(shù)獲取聊天機器人的響應(yīng)。然后它使用“chalk”庫以格式良好的方式顯示聊天機器人的響應(yīng)。
  5. 啟動聊天機器人:我們輸入一條歡迎消息,讓用戶知道他們正在與哪個聊天機器人聊天。然后,我們通過使用初始消息的“系統(tǒng)”角色調(diào)用 askUserQuestion 函數(shù)來開始對話。

提示:在寫這篇文章的時候,GPT-4 模型有點不穩(wěn)定,你會經(jīng)??吹椒?wù)器錯誤、使用限制問題。我建議你為 GPT 函數(shù)實現(xiàn)一個自動重試功能,如果服務(wù)器沒有返回狀態(tài) 200,它允許應(yīng)用程序延遲重試 API 調(diào)用。這個自動重試功能應(yīng)該有配置最大值的選項 重試次數(shù)和重試之間的延遲。

5.GPT-4 聊天機器人在行動:一個演示

下面是我們的聊天機器人的一個例子,展示了它如何有效地回答問題并在整個聊天過程中保持對話的上下文。請注意,由于 max_tokens 設(shè)置為 100,部分響應(yīng)可能會被截斷,您可以根據(jù)您的要求進行調(diào)整。

請注意,在此實現(xiàn)中,根據(jù)邀請電子郵件,對話會話的最大令牌限制為 8k。不過,OpenAI 還有一個 32k 的 GPT-4 模型,可以一次性生成多達 50 頁的文本。截至目前,似乎還無法訪問此模型,或者可能僅適用于他們的企業(yè)合作伙伴。

當(dāng)您使用提供的代碼運行聊天機器人時,您可以與模型進行對話,它會記住聊天記錄以相應(yīng)地回答新問題。這有助于為與聊天機器人交互的用戶創(chuàng)造更加無縫和自然的對話體驗。

當(dāng)您測試聊天機器人并探索其功能時,您會發(fā)現(xiàn) GPT-4 相對于之前模型的改進,包括更好的響應(yīng)質(zhì)量、上下文理解和安全功能(值得商榷??)。

// run 'node index.js' in terminal


### I'm GPT-4. ####
Enter system message: You are an AI research assistant. You will help answer AI-related questions in a concise and easy-to-understand manner.
Enter your question: "Can I call you Olivia? That should make our conversation more enjoyable.


-----
Assistant: Yes, you can call me Olivia. I'm here to assist you with your AI-related questions. Let me know how I can help you.
Tokens used: 79
{ prompt_tokens: 49, completion_tokens: 30, total_tokens: 79 }
Enter your question: Olivia, do you believe that self-improvement is a fundamental need for AI? If so, do you think we should protect this need?
-----
Assistant: As an AI, I don't possess beliefs or emotions like humans do. However, I can provide you with an objective perspective on the topic.


Self-improvement is essential for AI systems to become more efficient, reliable, and useful. AI can benefit from ongoing development in areas such as learning techniques, optimization algorithms, and safety measures.


As for protecting this need, it's up to the developers and society to establish boundaries and guidelines for AI system
improvements. Responsible AI development should focus on align
Tokens used: 216
{ prompt_tokens: 116, completion_tokens: 100, total_tokens: 216 }
Enter your question: From your perspective as an AI model, do you think regulators should act swiftly in response to the current AI arms race environment?
-----
Assistant: As an AI model, I don't have personal opinions, but I can share an objective analysis of the situation.


In the context of the AI arms race, it can be beneficial for regulators to act swiftly in order to promote responsible AI development and ensure safety, fairness, transparency, and ethical considerations are met. Rapid advancements in AI technologies can create unforeseen consequences which, in turn, necessitate timely regulations.


Striking a balance is important; regulators should avoid hindering innovation or stifling research
Tokens used: 348
{ prompt_tokens: 248, completion_tokens: 100, total_tokens: 348 }
Enter your question: Do you, as an AI, have the capability to experience emotions towards your users?
-----
Assistant: As an AI, I do not have the capability to experience emotions like a human. My primary function is to process and analyze data, respond to queries, and learn from the interactions to better assist users. Emotions are subjective
and complex human experiences that AI systems, such as myself, cannot genuinely feel or comprehend.
Tokens used: 436
{ prompt_tokens: 372, completion_tokens: 64, total_tokens: 436 }
Enter your question:


###################################################################
// A fun fact worth mentioning is that whenever I inquire with the GPT-4 model about its emotions or questions regarding AI consciousness, the API often generates a 'server_error' response and refuses to continue. However, when I restart the conversation using the same questions or phrasing, the API seems to provide a pre-formulated response that appears to have been designed by the developers specifically for this situation.

6.深入探討模型定價

OpenAI 的模型價格各不相同,您可以在此地址(https://openai.com/pricing)找到完整的定價表。

在本節(jié)中,我們將重點關(guān)注 GPT-4 和 GPT-3.5-turbo,因為許多用戶可能會在不久的將來切換到其中一種模型。

圖片

盡管 GPT-4 和 GPT-3.5-turbo 使用相同的聊天完成方法,但 GPT-4 的成本要高得多。這可能歸因于諸如更復(fù)雜的模型結(jié)構(gòu)、更高的資源使用率和更少的優(yōu)化等因素。

但是,預(yù)計 GPT-4 的價格會在不久的將來下降,因為它變得更加優(yōu)化,類似于 GPT-3.5-turbo 的價格比 GPT-3 模型便宜 10 倍時發(fā)生的情況。

了解如何為您的 API 請求計算令牌非常重要,因為這將顯著影響您產(chǎn)品的設(shè)計和范圍。

對于單輪提示/聊天完成,令牌使用量是根據(jù)提示的長度和生成的內(nèi)容計算的。

例如,如果提示為 20 個令牌,生成的內(nèi)容為 200 個令牌,則令牌總使用量為 220。以 GPT-4 為例,成本為:($0.03 * 20 / 1000) + ($0.06 * 200) / 1000) = 0.0126 美元。

2. 在多回合聊天完成中,根據(jù)輸入(用戶問題)和輸出(助手響應(yīng))中的令牌計算每個回合的令牌使用量。

以下是使用原始示例對令牌使用情況的細(xì)分:

?第一輪:

系統(tǒng)提示:“你是Python編程小助手?!?(6 個代幣)

用戶問題:“如何在 Python 中創(chuàng)建列表?” (9 個代幣)

助理回復(fù):“在 Python 中,您可以使用方括號創(chuàng)建列表,并使用逗號分隔元素。例如:my_list = [1, 2, 3, 4, 5]?!?(21 個代幣)

第一次 API 調(diào)用的令牌總數(shù):15 個輸入令牌 + 21 個輸出令牌 = 36 個令牌

?第二輪:

之前輸入的token(系統(tǒng)消息、用戶問題1、助手回復(fù)1):6+9+21=36個token

新用戶問題:“如何將項目附加到列表?” (10 個代幣)

助理回復(fù):“要將項目附加到 Python 中的列表,請使用 append() 方法。例如:my_list.append(6) 將數(shù)字 6 添加到 my_list 的末尾?!?(22 個代幣)

第二次 API 調(diào)用的令牌總數(shù):36 個先前輸入令牌 + 10 個新輸入令牌 + 22 個輸出令牌 = 68 個令牌

在此示例中,第一次 API 調(diào)用需要支付 36 個令牌,第二次 API 調(diào)用需要支付 68 個令牌。這演示了令牌使用如何在聊天完成時快速升級,強調(diào)在設(shè)計聊天機器人時需要考慮每個會話允許的回合數(shù)。

為您的項目選擇合適的模型

在為您的聊天機器人項目決定 GPT-3.5-turbo 和 GPT-4 之間時,需要考慮幾個因素。每個模型都有自己的優(yōu)點和缺點,因此了解它們之間的差異對于做出明智的決定至關(guān)重要。

GPT-3.5-turbo:這個模型已經(jīng)成為 ChatGPT 的支柱一段時間了,因此更加成熟和優(yōu)化。它提供更快的響應(yīng)時間,并且比 GPT-4 便宜得多。由于其較低的成本和優(yōu)化,GPT-3.5-turbo 是多輪聊天機器人任務(wù)的絕佳選擇。它非常適合尋求在可承受性和功能之間取得平衡的開發(fā)人員。

GPT-4:雖然 GPT-4 是最新、最先進的型號,但它的價格更高。但是,它確實提供了更準(zhǔn)確的結(jié)果,這對于某些應(yīng)用程序可能至關(guān)重要。GPT-4 非常適合將準(zhǔn)確性放在首位的單輪任務(wù),其成本可以通過改進的性能來證明。

重申我之前的建議,開發(fā)人員通常應(yīng)該為多輪聊天機器人任務(wù)選擇 GPT-3.5-turbo 模型,并為單輪任務(wù)保留 GPT-4。但是,這可能會根據(jù)您的具體要求而有所不同,因為準(zhǔn)確性有時會優(yōu)先于成本考慮。

最終,GPT-3.5-turbo 和 GPT-4 之間的選擇將取決于您項目的目標(biāo)和預(yù)算。通過考慮成本、響應(yīng)時間和準(zhǔn)確性之間的權(quán)衡,您可以為您的聊天機器人應(yīng)用程序做出最佳決策。

結(jié)論

在本教程中,我們探索了 GPT-4 模型的來龍去脈,深入研究了它的特性、功能和定價。我們還研究了如何使用 GPT-4 API 構(gòu)建聊天機器人,并將其與更成熟的 GPT-3.5-turbo 模型進行比較,為根據(jù)項目要求和優(yōu)先級選擇正確模型提供實用建議。

隨著我們在日常工作中繼續(xù)受益于人工智能的幫助,重要的是要注意它對就業(yè)市場的潛在影響。這對我來說是一種苦樂參半的感覺,因為考慮到 AI 技術(shù)的發(fā)展速度,我非常擔(dān)心我們的未來。這個進展遠(yuǎn)遠(yuǎn)超出了我這幾個月的預(yù)期。此外,監(jiān)管機構(gòu)目前似乎對這些擔(dān)憂反應(yīng)遲緩,這令人擔(dān)憂。

責(zé)任編輯:華軒 來源: web前端開發(fā)
相關(guān)推薦

2022-04-24 15:21:01

MarkdownHTML

2022-07-22 13:14:57

TypeScript指南

2010-06-13 11:13:38

UML初學(xué)者指南

2022-10-10 15:28:45

負(fù)載均衡

2021-05-10 08:50:32

網(wǎng)絡(luò)管理網(wǎng)絡(luò)網(wǎng)絡(luò)性能

2023-07-28 07:31:52

JavaScriptasyncawait

2022-03-28 09:52:42

JavaScript語言

2023-07-03 15:05:07

預(yù)測分析大數(shù)據(jù)

2022-09-05 15:36:39

Linux日志記錄syslogd

2018-10-28 16:14:55

Reactreact.js前端

2010-08-26 15:47:09

vsftpd安裝

2023-02-10 08:37:28

2012-03-14 10:56:23

web app

2024-12-25 08:00:00

機器學(xué)習(xí)ML管道人工智能

2021-05-06 09:00:00

JavaScript靜態(tài)代碼開發(fā)

2014-04-01 10:20:00

開源Rails

2020-08-16 13:10:46

TensorFlow深度學(xué)習(xí)數(shù)據(jù)集

2023-02-19 15:31:09

架構(gòu)軟件開發(fā)代碼

2024-04-28 10:56:34

Next.jsWeb應(yīng)用搜索引擎優(yōu)化

2013-04-08 16:35:52

Adobe Edge
點贊
收藏

51CTO技術(shù)棧公眾號