フルスタックチャンネル
サインアップサインアップ
ログインログイン
利用規約プライバシーポリシーお問い合わせ
Copyright © All rights reserved | FullStackChannel
解決済
Next.js13とSupabase、ChatGPT、Whisperで自動要約アプリ構築で本番環境でのエラー
Next.js
API
個人開発
mumei
2023/07/05 12:00

実現したいこと

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件
login
回答するにはログインが必要です
はる@講師
約2年前

Vercelの無料版は10秒でタイムアウトしてしまうので、ChatGPTへの応答が10秒を超えるとエラーになってしまいますね。

有料にすると60秒まで伸びます。

https://vercel.com/docs/concepts/limits/overview

即座にレスポンスを返してキューで実行できれば解決はしそうです。

mumei
約2年前

ありがとうございます!
やってみます!

色々な講座いつもありがとうございます!
今までの学習で一番勉強になっています。

1