使用 OpenAI api 运行 Firebase 函数时出错

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

我目前正在开发一个应用程序,该应用程序应该每天使用 OpenAI api 给我带来挑战。当我尝试部署该函数时,出现错误:“openai.OpenAIApi 不是构造函数”

这是整个功能代码

import * as functions from 'firebase-functions'
import admin from 'firebase-admin'
import openai from 'openai'

admin.initializeApp()

const openAIKey = functions.config().openai.key
if (!openAIKey) {
  console.error('Failed to load OpenAI API key from Firebase config.')
  throw new Error('OpenAI API key not found')
}

const configuration = new openai.Configuration({
  apiKey: openAIKey
})
const openaiClient = new openai.OpenAIApi(configuration)

export const generateDailyChallenge = functions.pubsub.schedule('* * * * *')
  .timeZone('Europe/Prague')
  .onRun(async (context) => {
    const currentDate = new Date()
    const challengeId = 'D' + currentDate.toISOString().split('T')[0].replace(/-/g, '').slice(2)

    try {
      const response = await openai.createCompletion({
        model: 'text-davinci-003',
        prompt: "You are an AI model powering a social media app based around challenges. The app works in a similar way to BeReal, meaning that once a day it generates a challenge for the users. The challenge needs to be simple to complete and require just a single photo taken from both cameras, just keep this in mind, but don't mention it as the users are used to it. The challenge is supposed to be fun, but easy to complete so users don't have to spend too much time on it. The goal is to connect with your online friends in a fun and engaging way. Mainly to just see what they were up to at that specific moment. Give me just the challenge name and a brieif description of the challenge as if you were challenging a user to do it",
        max_tokens: 500
      })

      const challengeText = response.data.choices[0].text.trim()
      const [title, description] = challengeText.split(' - ')

      const db = admin.database()
      await db.ref(`challenges/${challengeId}`).set({
        name: title,
        description
      })
    } catch (error) {
      console.error('Error during function execution:', error)
    }
  })

整个错误日志都在这里

{
  "textPayload": "Error during function execution: (just test) TypeError: openai.OpenAIApi is not a constructor",
  "insertId": "65a2fcdc0009502443121192",
  "resource": {
    "type": "cloud_function",
    "labels": {
      "region": "us-central1",
      "project_id": "nocena-dea56",
      "function_name": "generateDailyChallenge"
    }
  },
  "timestamp": "2024-01-13T21:13:00.610340Z",
  "labels": {
    "execution_id": "wc1wmg43etjw",
    "instance_id": "0087599d42c50142db1c9d36d9193328a9221f12fe0d0dd943f72f97b2cb7e525f5e52ae47deaf4b933f5efa24a228608101da4965c6d34f4bfef254291fc77be876",
    "runtime_version": "nodejs16_20231231_16_20_2_RC00"
  },
  "logName": "projects/nocena-dea56/logs/cloudfunctions.googleapis.com%2Fcloud-functions",
  "trace": "projects/nocena-dea56/traces/a2387447f0517c6aa55698b1bece52f8",
  "receiveTimestamp": "2024-01-13T21:13:00.944674302Z"
}

有谁知道可能是什么问题,我已经坚持了好几天了,进行更改并阅读文档以尝试解决它

javascript firebase google-cloud-functions openai-api
1个回答
0
投票

您的初始化不正确。

如果您有 OpenAI Node.js SDK

v3
请按如下方式进行初始化:

import { Configuration, OpenAIApi } from "openai";

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

const openai = new OpenAIApi(configuration);

如果您有 OpenAI Node.js SDK

v4
请按如下方式进行初始化:

/* You need to implement this and the error will disappear */
import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});
© www.soinside.com 2019 - 2024. All rights reserved.