Next.js13とSupabase、ChatGPT、Whisperで自動要約アプリ構築で本番環境でのエラー
解決済
回答 2
2023/05/25 11:04
質問内容

実現したいこと

Vercelの本番環境で動画を要約したい

発生している問題

SyntaxError: Unexpected token 'A', "An error o"... is not valid JSON

Failed to load resource: the server responded with a status of 504 ()

Vercelのサーバーレス関数は、デフォルトで最大10秒間実行

api/chatgptの応答が10秒以上かかりエラーになる

ソースコード

import { Configuration, OpenAIApi } from 'openai'

import type { NextApiRequest, NextApiResponse } from 'next'

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

const openai = new OpenAIApi(configuration)

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  try {
    const { prompt } = req.body

    // ChatGPT
    const content = `Please prepare an agenda and summary of the following statement in Japanese:\n: ${prompt}`
    const response = await openai.createChatCompletion({
      model: 'gpt-3.5-turbo',
      messages: [{ role: 'user', content: content }],
    })

    // レスポンスを返す
    const text = response.data.choices[0].message?.content

    res.status(200).json({ text })
  } catch (error) {
    console.error(error)
    res.status(500).send('Something went wrong')
  }
}

自分で試したこと

すでに非同期処理になっているか確認したところ非同期処理になっていたが これはAPIの応答時間自体を短縮するものではないということがわかった

補足情報

Image from Gyazo

解決方法ご教授いただきたいです

回答 2
ベストアンサーを選択すると、解決済みとなります。
nodata
まだ回答がありません
回答
nodata
回答するにはログインが必要です