const gptResponse = await openai
.createCompletion({
model: "davinci",
prompt,
max_tokens: 60,
temperature: 0.9,
presence_penalty: 0,
frequency_penalty: 0.5,
best_of: 1,
n: 1,
stream: false,
stop: ["\n", "\n\n"]
})
.catch((err) => {
console.log(err);
return { data: { choices: [{ text: "" }] } };
});
const response = gptResponse.data.choices[0]?.text;
为什么我会收到错误“gptResponse.data.choices”可能是“undefined”.ts(18048)?
最佳答案
我做了一些调查并找到了您问题的答案:
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: "your_key",
});
const openai = new OpenAIApi(configuration);
const response = openai.createCompletion({
model: "text-davinci-002",
prompt: "#JavaScript to Python:\nJavaScript: \ndogs = [\"bill\", \"joe\", \"carl\"]\ncar = []\ndogs.forEach((dog) > temperature: 0",
max_tokens: 64,
top_p: 1.0,
frequency_penalty: 0.0,
presence_penalty: 0.0,
}).then((res) => {console.log(res.data.choices[0].text)});
只需使用.then()
关于node.js - 如何从完成响应中返回 OpenAI 的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73824894/