OpenAI API Nodejs GPT3 to GPT4转换

rryofs0p  于 2023-04-20  发布在  Node.js
关注(0)|答案(1)|浏览(300)

我在Node.js中有批量GPT生成器,但我想将模型更改为gpt-4。我必须在代码中更改什么?当我只尝试将text-davinci-003更改为gpt-4时,工具不起作用。

const configuration = new Configuration({
    apiKey: keys.AI_KEY,
  });
  const openai = new OpenAIApi(configuration);
  const prompt = headings
    ? `Create a product description "${title}".`
    : `Create a product description "${title}". Minimum 1000 characters. The text should be formatted to HTML, the paragraphs should be placed in <p>. Make all text on one line. Don't use semicolons.`;
  let failedRequests = 0;
  const maxFails = 4;
  const retry = async (ms) =>
    new Promise((resolve) => {
      openai.createCompletion({
          model: "text-davinci-003",
          prompt: prompt,
          temperature: 0.4,
          max_tokens: 3700,
          top_p: 0,
          frequency_penalty: headings ? 0.5 : 0.3,
          presence_penalty: headings ? 1 : 0.7,
        })
        .then((res) => resolve(res.data.choices[0].text))
        .catch((error) => {
          if (
            (error.response.status === 429 || error.response.status === 500) &&
            failedRequests < maxFails
          ) {
            setTimeout(() => {
              failedRequests++;
              console.log(
                `powtarzam: ${title} (${error.response.statusText}) ${failedRequests} / ${maxFails}`
              );
              retry(ms).then(resolve);
            }, ms);
          } else {
            resolve({
              error: true,
              statusText: error.response.statusText,
            });
          }
        });
    });

  const response = await retry(5000);
  return response;
};

我试图改变“text-davinci-003”到“gpt-4”,但工具不工作。

jxct1oxe

jxct1oxe1#

您希望将gpt-4模型与GPT-3 API端点(即/v1/completions)一起使用。这将不起作用。gpt-4模型与GPT-3.5 API端点(即/v1/chat/completions)兼容。
您需要像使用gpt-3.5-turbo模型一样编写代码。您唯一需要更改的是以下内容:model: "gpt-3.5-turbo"model: "gpt-4"
请参阅官方OpenAI documentation关于模型端点兼容性的说明。
| 终点|型号名称|
| --------------|--------------|
| /v1/chat/completions|gpt-4、gpt-4-0314、gpt-4-32k、gpt-4-32k-0314、gpt-3.5-turbo、gpt-3.5-turbo-0301|
| /v1/完成|text-davinci-003,text-davinci-002,text-curie-001,text-babbage-001,text-ada-001|
| /v1/edits|text-davinci-edit-001,代码-davinci-edit-001|
| /v1/audio/transcriptions|耳语-1|
| /v1/音频/翻译|耳语-1|
| /v1/fine-tunes|达芬奇居里巴贝奇阿达|
| /v1/嵌入|text-embedding-ada-002,text-search-ada-doc-001|
| /v1/调节|text-moderation-stable,text-moderation-latest|
试试这个:

import { Configuration, OpenAIApi } from "openai";
import { keys } from "./keys.js";
import "openai";

const createContent = async (title, headings = false) => {
  if (!headings) {
    console.log(`I'm writing: ${title}`);
  }

  const configuration = new Configuration({
    apiKey: keys.AI_KEY,
  });
  const openai = new OpenAIApi(configuration);
  const prompt = { role: "user", content: `Create product description "${title}". Minimum 1000 characters.`}
  let failedRequests = 0;
  const maxFails = 4;
  const retry = async (ms) =>
    new Promise((resolve) => {
      openai.createChatCompletion({
          model: "gpt-4",
          messages: prompt,
          temperature: 0.4,
          max_tokens: 3700,
          top_p: 0,
          frequency_penalty: headings ? 0.5 : 0.3,
          presence_penalty: headings ? 1 : 0.7,
        })
        .then((res) => resolve(res.data.choices[0].message.content))
        .catch((error) => {
          if (
            (error.response.status === 429 || error.response.status === 500) &&
            failedRequests < maxFails
          ) {
            setTimeout(() => {
              failedRequests++;
              console.log(
                `Retry: ${title} (${error.response.statusText}) ${failedRequests} / ${maxFails}`
              );
              retry(ms).then(resolve);
            }, ms);
          } else {
            resolve({
              error: true,
              statusText: error.response.statusText,
            });
          }
        });
    });

  const response = await retry(5000);
  return response;
};

相关问题