【问题标题】:OpenAI and Javascript error : Getting 'TypeError: Cannot read properties of undefined (reading 'create') at Object.<anonymous>"OpenAI 和 Javascript 错误:获取 \'TypeError:无法读取未定义的属性(读取 \'create\')在 Object.<anonymous>\"
【发布时间】:2023-01-02 14:20:07
【问题描述】:

对于基本问题,我很抱歉,但似乎是一段非常基本的代码却无处可去。我已经 npm 安装了最新版本的 openai。我的终端不断出现错误:

TypeError: Cannot read properties of undefined (reading 'create')
    at Object.<anonymous> (/Users/michalchojnacki/Desktop/Coding/OpenAi2/code.js:9:20)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

代码:

const openai = require('openai');

openai.apiKey = "my API here";

const prompt = "What is the capital of France?";

const model = "davinci";

openai.completions.create({
  engine: model,
  prompt: prompt,
  max_tokens: 2048,
  n: 1,
  stop: '.',
  temperature: 0.5,
}, (error, response) => {
  if (error) {
    console.log(error);
  } else {
    console.log(response.choices[0].text);
  }
});

将不胜感激任何帮助!

我期待终端给我对提示的回应

【问题讨论】:

标签: api openai gpt-3


【解决方案1】:

根据库中的文档,您似乎想要这样的东西:

const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
  apiKey: "YOUR_API_KEY",
});

async function getAiResponse(topic) {
  const openai = new OpenAIApi(configuration);
  const completion = await openai.createCompletion({
    model: "text-davinci-003",
    prompt: topic,
    max_tokens: 1024,
    n: 1,
    stop: null,
    temperature: 0.7
  });
  console.log(completion.data.choices[0].text);
}
getAiResponse("Your Prompt here");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-24
    • 2023-02-26
    • 2022-07-06
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多