简体   繁体   English

OpenAI 和 Javascript 错误:获取“类型错误:无法在 Object 读取未定义的属性(读取‘创建’)。<anonymous> “</anonymous>

[英]OpenAI and Javascript error : Getting 'TypeError: Cannot read properties of undefined (reading 'create') at Object.<anonymous>"

I am sorry for basic question but getting no where with what seems to be a very basic piece of code.对于基本问题,我很抱歉,但似乎是一段非常基本的代码却无处可去。 I have npm installed latest version of openai.我有 npm 安装了最新版本的 openai。 I am getting a constant error in my terminal:我的终端不断出现错误:

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

Code:代码:

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);
  }
});

Would be grateful for any help!将不胜感激任何帮助!

I was expecting the terminal to give me the response to the prompt我期待终端给我对提示的回应

Based on the docs in the library it looks like you want something like this instead:根据库中的文档,您似乎想要这样的东西:

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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法读取未定义(读取“过滤器”)和未定义错误的属性 - React - TypeError: Cannot read properties of undefined (reading 'filter') and undefined error - React 我在 React 中收到“TypeError: Cannot read properties of undefined(reading: 0)” - I'm getting "TypeError: Cannot read properties of undefined(reading: 0)" in React 使用 openweather API “TypeError: Cannot read properties of undefined (reading 'temp')”做出反应时出错 - Getting an error in react using openweather API "TypeError: Cannot read properties of undefined (reading 'temp')" Angular 6 TypeError: Cannot read properties of undefined (reading 'length') error - Angular 6 TypeError: Cannot read properties of undefined (reading 'length') error 无法访问通过 API 检索到的 object 属性,并显示错误消息 TypeError: Cannot read property '' of undefined - Nextjs/JavaScript - Cant access object properties retrieved via API with error message TypeError: Cannot read property '' of undefined - Nextjs/JavaScript 错误:无法获取订单:TypeError:无法读取未定义的属性(读取“连接”) - Error: Cannot get orders: TypeError: Cannot read properties of undefined (reading 'connect') React 中的 API 调用“TypeError:无法读取未定义的属性(读取&#39;map&#39;)” - API call in React "TypeError: Cannot read properties of undefined (reading 'map')" 未捕获的类型错误:无法读取未定义的属性(读取“forEach”)-OpenWeather - Uncaught TypeError: Cannot read properties of undefined (reading 'forEach') - OpenWeather 反应:类型错误:无法读取未定义的属性(读取“地图”) - React: TypeError: Cannot read properties of undefined (reading 'map') TypeError:无法在 React 代码中读取未定义的属性(读取“总计”) - TypeError: Cannot read properties of undefined (reading 'total') in React code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM