Nextjs - 将index.html页面添加到静态导出

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

我有一个用于复制 AI 模型的 API 的代码。 问题是有些模块需要很长时间才能从API得到答案,我最长的是200s。 但当然,请求在得到答案之前就崩溃了。

代码中哪里可以实现较长的api超时?

感谢您的帮助! :)

import Replicate from "replicate";
import { auth } from "@clerk/nextjs";
import { NextResponse } from "next/server";

import { incrementApiLimit, checkApiLimit } from "@/lib/api-limit";
import { checkSubscription } from "@/lib/subscription";

const replicate = new Replicate({
 auth: process.env.REPLICATE_API_TOKEN!,
});

    export async function POST(
      req: Request
   ) {
     try {
   const { userId } = auth();
  const body = await req.json();
  const { prompt  } = body;

if (!userId) {
  return new NextResponse("Unauthorized", { status: 401 });
}

if (!prompt) {
  return new NextResponse("Prompt is required", { status: 400 });
}

const freeTrial = await checkApiLimit();
const isPro = await checkSubscription();

if (!freeTrial && !isPro) {
  return new NextResponse("Free trial has expired. Please upgrade to pro.", { status: 403 });
}

const response = await replicate.run(
  "riffusion/riffusion:8cf61ea6c56afd61d8f5b9ffd14d7c216c0a93844ce2d82ac1c9ecc9c7f24e05",
  {
    input: {
      prompt_a: prompt
    }
  }
);

if (!isPro) {
  await incrementApiLimit();
}

return NextResponse.json(response);
 } catch (error) {
    console.log('[MUSIC_ERROR]', error);
  return new NextResponse("Internal Error", { status: 500 });
}
};
typescript api next.js settimeout replicate
1个回答
0
投票

要在代码中为 API 请求实现更长的超时,您可以使用 Replicate 库提供的超时选项。此选项允许您指定 API 请求在超时之前应等待响应的最长时间(以毫秒为单位)。您可以设置更长的超时值以适应需要很长时间才能完成的请求。以下是您可以修改代码以设置更长超时的方法:

javascript 复制代码

import Replicate from "replicate";
import { auth } from "@clerk/nextjs";
import { NextResponse } from "next/server";

import { incrementApiLimit, checkApiLimit } from "@/lib/api-limit";
import { checkSubscription } from "@/lib/subscription";

const replicate = new Replicate({
  auth: process.env.REPLICATE_API_TOKEN!,
  timeout: 300000, // Set a longer timeout (300 seconds = 5 minutes)
});

export async function POST(req: Request) {
  try {
    const { userId } = auth();
    const body = await req.json();
    const { prompt } = body;

    if (!userId) {
      return new NextResponse("Unauthorized", { status: 401 });
    }

    if (!prompt) {
      return new NextResponse("Prompt is required", { status: 400 });
    }

    const freeTrial = await checkApiLimit();
    const isPro = await checkSubscription();

    if (!freeTrial && !isPro) {
      return new NextResponse("Free trial has expired. Please upgrade to pro.", { status: 403 });
    }

    // Make the API request with the specified timeout
    const response = await replicate.generate({ prompt });

    // Process the API response here
    // ...

    return new NextResponse(/* Your response here */);
  } catch (error) {
    console.error("API request failed:", error);
    return new NextResponse("Internal Server Error", { status: 500 });
  }
}

在上面的代码中,我将超时设置为 300,000 毫秒(5 分钟)。您可以根据需要调整此值,以适应 API 的最长预期响应时间。这应该可以防止请求过早超时,从而允许它等待 API 响应成功完成。

© www.soinside.com 2019 - 2024. All rights reserved.