我正在使用 OpenAI API,但本地主机服务器未运行

问题描述 投票:0回答:1

我正在使用 OpenAI API,并且我正在尝试运行本地主机服务器。但是,它似乎不起作用。这是为什么?

这是我的 JavaScript 代码:

const express = require('express');
require("dotenv").config();
const { Configuration, OpenAIApi } = require("openai");
const app = express();

app.use(express.json());

const configuration = new Configuration({
apiKey: process.env.OPEN_AI_KEY,
});
const openai = new OpenAIApi(configuration);

app.post("/find-complexity", async (req, res) => {
try {
return res.status(200).json({
message: "Working",
});
} catch (error) {}
});

const port = process.env.PORT || 3000;

app.listen(port, () => console.log("Server listening on port 3000"));

我通过终端和邮递员运行它,但都不起作用。当我通过终端运行

npm run dev
时,出现错误,指出:

const configuration = new Configuration({
                      ^

TypeError: Configuration is not a constructor

当我通过邮递员运行服务器时,输入为

localhost:3000/find-complexity
,出现以下错误:
Error: connect ECONNREFUSED 127.0.0.1:3000

node.js express localhost artificial-intelligence openai-api
1个回答
0
投票

此 SDK 的早期版本使用

Configuration
类。请参阅 v3 到 v4 迁移指南。

// Old
import { Configuration, OpenAIApi } from "openai";

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

// New
import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY // This is also the default, can be omitted
});
© www.soinside.com 2019 - 2024. All rights reserved.