自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

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

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

1.了解 GPT-4 模型

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

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

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

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

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

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

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

請(qǐng)記住,可用性可能會(huì)隨著時(shí)間的推移而改變,因此請(qǐng)確保及時(shí)了解 OpenAI 的公告。

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

升級(jí)到新的 GPT-4 模型 API 非常簡(jiǎn)單,因?yàn)樗褂门c GPT-3.5-turbo 相同的聊天完成方法。如果我們有權(quán)訪問,我們可以期望我們現(xiàn)有的代碼能夠與 GPT-4 無(wú)縫協(xié)作。

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

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

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

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

4.安裝必要的 NPM 包

npm install dotenv openai chalk

這些庫(kù)有以下用途:

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

代碼片段:打造您的聊天機(jī)器人

// index.js 


// 導(dǎo)入所需的庫(kù)
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)建聊天機(jī)器人,請(qǐng)按照以下步驟操作:

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

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

5.GPT-4 聊天機(jī)器人在行動(dòng):一個(gè)演示

下面是我們的聊天機(jī)器人的一個(gè)例子,展示了它如何有效地回答問題并在整個(gè)聊天過程中保持對(duì)話的上下文。請(qǐng)注意,由于 max_tokens 設(shè)置為 100,部分響應(yīng)可能會(huì)被截?cái)啵梢愿鶕?jù)您的要求進(jìn)行調(diào)整。

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

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

當(dāng)您測(cè)試聊天機(jī)器人并探索其功能時(shí),您會(huì)發(fā)現(xiàn) GPT-4 相對(duì)于之前模型的改進(jìn),包括更好的響應(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.深入探討模型定價(jià)

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

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

圖片

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

但是,預(yù)計(jì) GPT-4 的價(jià)格會(huì)在不久的將來下降,因?yàn)樗兊酶觾?yōu)化,類似于 GPT-3.5-turbo 的價(jià)格比 GPT-3 模型便宜 10 倍時(shí)發(fā)生的情況。

了解如何為您的 API 請(qǐng)求計(jì)算令牌非常重要,因?yàn)檫@將顯著影響您產(chǎn)品的設(shè)計(jì)和范圍。

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

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

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

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

?第一輪:

系統(tǒng)提示:“你是Python編程小助手。” (6 個(gè)代幣)

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

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

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

?第二輪:

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

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

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

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

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

為您的項(xiàng)目選擇合適的模型

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

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

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

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

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

結(jié)論

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

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

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

2022-04-24 15:21:01

MarkdownHTML

2022-10-10 15:28:45

負(fù)載均衡

2010-06-13 11:13:38

UML初學(xué)者指南

2022-07-22 13:14:57

TypeScript指南

2021-05-10 08:50:32

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

2022-03-28 09:52:42

JavaScript語(yǔ)言

2023-07-28 07:31:52

JavaScriptasyncawait

2023-07-03 15:05:07

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

2010-08-26 15:47:09

vsftpd安裝

2018-10-28 16:14:55

Reactreact.js前端

2012-03-14 10:56:23

web app

2023-02-10 08:37:28

2022-09-05 15:36:39

Linux日志記錄syslogd

2024-12-25 08:00:00

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

2021-05-06 09:00:00

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

2013-03-06 10:40:58

Adobe Edge HTML5

2013-04-08 16:35:52

Adobe Edge

2011-03-02 10:57:27

vsFTPd

2023-02-19 15:31:09

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

2014-04-01 10:20:00

開源Rails
點(diǎn)贊
收藏

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