diff --git a/pages/[...slug].tsx b/pages/[...slug].tsx index 8ea6586a..33841d13 100644 --- a/pages/[...slug].tsx +++ b/pages/[...slug].tsx @@ -8,7 +8,6 @@ import { useLocalStorage } from "react-use"; import Footer from "../components/Footer"; import Header from "../components/Header"; import SquigglyLines from "../components/SquigglyLines"; -import { fetchWithTimeout } from "../utils/fetchWithTimeout"; export const Home: NextPage = () => { const router = useRouter(); @@ -50,7 +49,7 @@ export const Home: NextPage = () => { router.replace(curUrl); } setLoading(true); - const response = await fetchWithTimeout("/api/summarize", { + const response = await fetch("/api/summarize", { method: "POST", headers: { "Content-Type": "application/json", @@ -59,7 +58,7 @@ export const Home: NextPage = () => { }); if (!response.ok) { - console.log("error", response.statusText); + console.log("error", response); if (response.status === 501) { toast.error("啊叻?视频字幕不见了?!"); } else { diff --git a/pages/api/summarize.ts b/pages/api/summarize.ts index 3f36665e..0e6dd55a 100644 --- a/pages/api/summarize.ts +++ b/pages/api/summarize.ts @@ -1,4 +1,3 @@ -import { omitBy } from "lodash"; import { OpenAIStream } from "../../utils/OpenAIStream"; import { getChunckedTranscripts, getSummaryPrompt } from "../../utils/prompt"; @@ -10,7 +9,7 @@ if (!process.env.OPENAI_API_KEY) { throw new Error("Missing env var from OpenAI"); } -export default async function handler(req: Request) { +export default async function handler(req: Request, res: Response) { const { url, apiKey } = (await req.json()) as { url?: string; apiKey?: string; @@ -37,6 +36,8 @@ export default async function handler(req: Request) { // @ts-ignore const title = res.data?.title; const subtitleUrl = res.data?.subtitle?.list?.[0]?.subtitle_url; + apiKey && console.log("========use user key========"); + console.log("bvid_url", url); console.log("subtitle_url", subtitleUrl); if (!subtitleUrl) { return new Response("No subtitle in the video", { status: 501 }); @@ -57,13 +58,13 @@ export default async function handler(req: Request) { const prompt = getSummaryPrompt(title, text); const payload = { - model: "text-davinci-003", + model: apiKey ? "text-davinci-003" : "text-curie-001", prompt, temperature: 0.5, top_p: 1, frequency_penalty: 0, presence_penalty: 0, - max_tokens: 400, + max_tokens: 300, stream: true, n: 1, }; @@ -72,6 +73,6 @@ export default async function handler(req: Request) { return new Response(stream); } catch (error: any) { console.log(error); - return new Response(error, { status: 500 }); + return new Response(error); } } diff --git a/utils/OpenAIStream.ts b/utils/OpenAIStream.ts index 7ef439ef..e933772e 100644 --- a/utils/OpenAIStream.ts +++ b/utils/OpenAIStream.ts @@ -17,18 +17,21 @@ export interface OpenAIStreamPayload { n: number; } -function checkApiKey(str :string) { +function checkApiKey(str: string) { var pattern = /^sk-[A-Za-z0-9]{48}$/; return pattern.test(str); } -export async function OpenAIStream(payload: OpenAIStreamPayload, apiKey?: string) { +export async function OpenAIStream( + payload: OpenAIStreamPayload, + apiKey?: string +) { const encoder = new TextEncoder(); const decoder = new TextDecoder(); - const openai_api_key = apiKey || process.env.OPENAI_API_KEY || ''; + const openai_api_key = apiKey || process.env.OPENAI_API_KEY || ""; - if(!checkApiKey(openai_api_key)) { - throw new Error('OpenAI API Key Format Error') + if (!checkApiKey(openai_api_key)) { + throw new Error("OpenAI API Key Format Error"); } const res = await fetch("https://api.openai.com/v1/completions", { @@ -40,6 +43,10 @@ export async function OpenAIStream(payload: OpenAIStreamPayload, apiKey?: string body: JSON.stringify(payload), }); + if (res.status !== 200) { + throw new Error("OpenAI API " + res.statusText); + } + let counter = 0; const stream = new ReadableStream({