Skip to content

Commit

Permalink
feat: add flag for better prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Mar 2, 2023
1 parent 4b45048 commit aef9f3a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
37 changes: 28 additions & 9 deletions pages/api/summarize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextResponse } from "next/server";
import { Redis } from "@upstash/redis";
import type { NextFetchEvent, NextRequest } from "next/server";
import { NextResponse } from "next/server";
import pRetry from "p-retry";
import { isDev } from "../../utils/env";
import { OpenAIResult } from "../../utils/OpenAIResult";
import { getChunckedTranscripts, getSummaryPrompt } from "../../utils/prompt";
Expand All @@ -13,6 +14,21 @@ if (!process.env.OPENAI_API_KEY) {
throw new Error("Missing env var from OpenAI");
}

const run = async (bvId: string) => {
const requestUrl = `https://api.bilibili.com/x/web-interface/view?bvid=${bvId}`;
console.log(`fetch`, requestUrl);
const response = await fetch(requestUrl, {
method: "GET",
});
const json = await response.json();
const subtitleList = json.data?.subtitle?.list;
if (!subtitleList || subtitleList?.length < 1) {
throw new Error(response.statusText);
}

return json;
};

export default async function handler(
req: NextRequest,
context: NextFetchEvent
Expand All @@ -25,16 +41,19 @@ export default async function handler(
if (!bvId) {
return new Response("No bvid in the request", { status: 500 });
}
const requestUrl = `https://api.bilibili.com/x/web-interface/view?bvid=${bvId}`;
console.log(`fetch`, requestUrl);
const response = await fetch(requestUrl, {
method: "GET",
const res = await pRetry(() => run(bvId), {
onFailedAttempt: (error) => {
console.log(
`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`
);
},
retries: 3,
});
const res = await response.json();
// @ts-ignore
const title = res.data?.title;
const subtitleList = res.data?.subtitle?.list;
if (!subtitleList || subtitleList?.length === 0) {
if (!subtitleList || subtitleList?.length < 1) {
console.error("No subtitle in the video: ", bvId);
return new Response("No subtitle in the video", { status: 501 });
}
const betterSubtitle =
Expand All @@ -54,10 +73,10 @@ export default async function handler(
});
// console.log("========transcripts========", transcripts);
const text = getChunckedTranscripts(transcripts, transcripts);
const prompt = getSummaryPrompt(title, text);
const prompt = getSummaryPrompt(title, text, true);

try {
apiKey && console.log("========use user key========");
apiKey && console.log("========use user apiKey========");
isDev && console.log("prompt", prompt);
const payload = {
model: "gpt-3.5-turbo",
Expand Down
15 changes: 9 additions & 6 deletions utils/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

export function getSummaryPrompt(title: string,transcript: any) {
return `标题: "${title
.replace(/\n+/g, " ")
.trim()}"\n视频字幕: "${truncateTranscript(transcript)
.replace(/\n+/g, " ")
.trim()}"\n我希望你是一名专业的视频内容编辑,帮我总结视频的内容精华。请先用一句简短的话总结视频梗概。然后再请你将视频字幕文本进行总结,在每句话的最前面加上时间戳,然后以无序列表的方式返回,不要超过5条。记得不要重复句子,确保所有的句子都足够精简,清晰完整,祝你好运!`;
export function getSummaryPrompt(title: string, transcript: any, hasNoTimestamp?: boolean) {
const betterPrompt = `我希望你是一名专业的视频内容编辑,帮我总结视频的内容精华。请你将视频字幕文本进行总结,然后以无序列表的方式返回,不要超过5条。记得不要重复句子,确保所有的句子都足够精简,清晰完整,祝你好运!`
const promptWithTimestamp = `我希望你是一名专业的视频内容编辑,帮我总结视频的内容精华。请先用一句简短的话总结视频梗概。然后再请你将视频字幕文本进行总结,在每句话的最前面加上开始的时间戳,然后以无序列表的方式返回。请注意不要超过5条哦,确保所有的句子都足够精简,清晰完整,祝你好运!`;

return `标题: "${title
.replace(/\n+/g, " ")
.trim()}"\n视频字幕: "${truncateTranscript(transcript)
.replace(/\n+/g, " ")
.trim()}"\n${hasNoTimestamp ? betterPrompt : promptWithTimestamp}`;
}

// Seems like 15,000 bytes is the limit for the prompt
Expand Down

1 comment on commit aef9f3a

@vercel
Copy link

@vercel vercel bot commented on aef9f3a Mar 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.