OpenAI 的 GPT⑷ 模型初学者指南

npm install dotenv openai chalk这些库有以下用处:
- dotenv:允许我们将 API 密钥安全地存储为环境变量。 
- openai:用于轻松调用 OpenAI 模型 API 的官方 Node.js 库。 
- chalk:由于我们正在构建一个在我们的终端中运行的聊天机器人,我们将使用 Chalk 为对话添加一些风格,使其更具可读性和视觉吸引力。 
- readline:一个内置的 Node.js 库,我们将使用它来读取用户输入,从而可以轻松地通过命令行与我们的聊天机器人进行交互。 
代码片断:打造您的聊天机器人
// index.js// 导入所需的库import dotenv from "dotenv";import { Configuration, OpenAIApi } from "openai";import readline from "readline";import chalk from "chalk";// Load environment variablesdotenv.config();// Initialize the OpenAI API clientconst openai = new OpenAIApi(new Configuration({ apiKey: process.env.OPENAI_API_KEY }));// Create a readline interface for user inputconst rl = readline.createInterface({input: process.stdin,output: process.stdout});// Define an array to store the conversation messagesconst GPTMessages = [];// Set the model name; replace with other model names if neededconst modelName = "gpt⑷"; // "gpt⑶.5-turbo"// Define an async function to call the GPT APIconst GPT = async (message) => {// Call the GPT API with the model, messages, and max tokensconst response = await openai.createChatCompletion({model: modelName,messages: message,max_tokens: 100,});// Return the response content and the total number of tokens usedreturn {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 inputconst 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 inputrl.question(prompt, async (userInput) => {// Add the user's input to the GPTMessages arrayGPTMessages.push({ role: role, content: userInput });// If the input is a system message, ask the user for their questionif (role === "system") {askUserQuestion("user");} else {// Call the GPT function with the current conversation messagesconst assistantResponse = await GPT(GPTMessages);// Add the assistant's response to the GPTMessages arrayGPTMessages.push({ role: "assistant", content: assistantResponse.content });// Display the assistant's response and the number of tokens usedconsole.log(chalk.yellow("-----"));console.log(chalk.green("Assistant: "), assistantResponse.content);console.log(chalk.cyan("Tokens used: "), assistantResponse.tokensUsed);// Ask the user another questionaskUserQuestion("user");}});};// Display the model name and begin the conversationconsole.log(`### I'm ${chalk.blue(modelName.toUpperCase())}. ####`);askUserQuestion("system");
要有效地使用聊天完成构建聊天机器人,请依照以下步骤操作:
- 为用户输入和输出做准备:我们为聊天机器人设置了一种方式来接收来自用户的消息,并使用“readline”库通过命令行发送响应。 
- 跟踪对话:我们创建一个名为 GPTMessages 的数组来存储用户和聊天机器人之间交换的消息。我们还在 modelName 变量中指定要使用的 GPT 模型(例如 GPT⑶.5-turbo 或 GPT⑷)。 
- 制作聊天机器人功能:我们创建一个名为 GPT 的功能,它将用户的消息发送到 OpenAI API 并接收响应。它还会跟踪聊天机器人在其响应中使用了多少令牌,并返回内容和令牌使用情况。 
- 创建来回对话:我们构建一个名为 askUserQuestion 的函数,它要求用户输入,将输入保存在 GPTMessages 数组中,并通过调用 GPT 函数获得聊天机器人的响应。然后它使用“chalk”库以格式良好的方式显示聊天机器人的响应。 
- 启动聊天机器人:我们输入一条欢迎消息,让用户知道他们正在与哪一个聊天机器人聊天。然后,我们通过使用初始消息的“系统”角色调用 askUserQuestion 函数来开始对话。 
提示:在写这篇文章的时候,GPT⑷ 模型有点不稳定,你会常常看到服务器毛病、使用限制问题。我建议你为 GPT 函数实现一个自动重试功能,如果服务器没有返回状态 200,它允许利用程序延迟重试 API 调用。这个自动重试功能应当有配置最大值的选项 重试次数和重试之间的延迟。
05、GPT⑷ 聊天机器人在行动:一个演示
下面是我们的聊天机器人的一个例子,展现了它如何有效地回答问题并在全部聊天进程中保持对话的上下文。请注意,由于 max_tokens 设置为 100,部份响应可能会被截断,您可以根据您的要求进行调剂。
请注意,在此实现中,根据约请电子邮件,对话会话的最大令牌限制为 8k。不过,OpenAI 还有一个 32k 的 GPT⑷ 模型,可以一次性生成多达 50 页的文本。截至目前,仿佛还没法访问此模型,或可能仅适用于他们的企业合作火伴。
当您使用提供的代码运行聊天机器人时,您可以与模型进行对话,它会记住聊天记录以相应地回答新问题。这有助于为与聊天机器人交互的用户创造更加无缝和自然的对话体验。
当您测试聊天机器人并探索其功能时,您会发现 GPT⑷ 相对之前模型的改进,包括更好的响应质量、上下文理解和安全功能(值得商议
         TikTok千粉号购买平台:https://tiktokusername.com/

