OpenAI模块导出错误:Configuration不存在的修复求助
问题解决:OpenAI SDK v4适配修改
你遇到的错误是因为OpenAI Node.js SDK从v3升级到v4后,API结构发生了变化,旧版的Configuration和OpenAIApi导出被移除了。下面是适配v4 SDK的完整修改代码:
import OpenAI from "openai" import { encode, decode } from "gpt-3-encoder" export default defineComponent({ props: { openai: { type: "app", app: "openai", } }, async run({steps, $}) { // 导入上一步的转录内容 const transcript = steps.create_transcription.$return_value.transcription // 设置输入token的最大数量 const maxTokens = 2000 // 初始化OpenAI - 适配v4 SDK const openai = new OpenAI({ apiKey: this.openai.$auth.api_key, }); // 根据GPT token限制,将转录内容分割为较短的字符串 function splitTranscript(encodedTranscript, maxTokens) { const stringsArray = [] let currentIndex = 0 while (currentIndex < encodedTranscript.length) { let endIndex = Math.min(currentIndex + maxTokens, encodedTranscript.length) // 找到下一个句号 while (endIndex < encodedTranscript.length && decode([encodedTranscript[endIndex]]) !== ".") { endIndex++ } // 将句号包含进当前字符串 if (endIndex < encodedTranscript.length) { endIndex++ } // 将当前片段加入数组 const chunk = encodedTranscript.slice(currentIndex, endIndex) stringsArray.push(decode(chunk)) currentIndex = endIndex } return stringsArray } const encoded = encode(transcript) const stringsArray = splitTranscript(encoded, maxTokens) const result = await sendToChat(stringsArray) return result // 将转录字符串发送到Chat API的函数 async function sendToChat (stringsArray) { const resultsArray = [] for (let arr of stringsArray) { // 定义提示词 const prompt = `Analyze the transcript provided below, then provide the following: Key "title:" - add a title. Key "summary" - create a summary. Key "main_points" - add an array of the main points. Limit each item to 100 words, and limit the list to 10 items. Key "action_items:" - add an array of action items. Limit each item to 100 words, and limit the list to 5 items. Key "follow_up:" - add an array of follow-up questions. Limit each item to 100 words, and limit the list to 5 items. Key "stories:" - add an array of an stories, examples, or cited works found in the transcript. Limit each item to 200 words, and limit the list to 5 items. Key "arguments:" - add an array of potential arguments against the transcript. Limit each item to 100 words, and limit the list to 5 items. Key "related_topics:" - add an array of topics related to the transcript. Limit each item to 100 words, and limit the list to 5 items. Key "sentiment" - add a sentiment analysis Ensure that the final element of any array within the JSON object is not followed by a comma. Write all responses in Dutch. Transcript: ${arr}` let retries = 3 while (retries > 0) { try { // 适配v4 SDK的Chat Completion调用方式 const completion = await openai.chat.completions.create({ model: "gpt-3.5-turbo", messages: [{role: "user", content: prompt}, {role: "system", content: `You are an assistant that only speaks Dutch, formatted in Markdown. Do not write text that is not formatted as markdown. Do not write in any language other than Dutch. Example formatting: { "title": "Notion Buttons", "summary": "A collection of buttons for Notion", "action_items": [ "item 1", "item 2", "item 3" ], "follow_up": [ "item 1", "item 2", "item 3" ], "arguments": [ "item 1", "item 2", "item 3" ], "related_topics": [ "item 1", "item 2", "item 3" ] "sentiment": "positive" } `}], temperature: 0.2 }); resultsArray.push(completion) break } catch (error) { if(error.response && error.response.status === 500) { retries-- if (retries == 0) { throw new Error("Failed to get a response from OpenAI Chat API after 3 attempts.") } console.log("OpenAI Chat API returned a 500 error. Retrying...") } else { throw error } } } } return resultsArray } }, })
关键修改点:
- 导入方式:移除旧版的
Configuration和OpenAIApi,直接导入OpenAI类 - 客户端初始化:直接通过
new OpenAI({ apiKey: ... })创建客户端,无需单独的配置对象 - Chat Completion调用:将
openai.createChatCompletion改为openai.chat.completions.create,这是v4 SDK的标准调用方式
内容的提问来源于stack exchange,提问作者Roni Mejia
相关产品推荐
相关产品推荐

