Skip to content

Commit

Permalink
fix: add assistant example for better result
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Mar 12, 2023
1 parent 45f227c commit 933b735
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
13 changes: 12 additions & 1 deletion lib/openai/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ const PROMPT_LANGUAGE_MAP = {
हिंदी: "Hindi",
};

export function getExamplePrompt() {
return {
input: `标题: "【BiliGPT】AI 自动总结 B站 视频内容,GPT-3 智能提取并总结字幕"
视频字幕: "2.06 - 哈喽哈喽 这里是机密的频道 今天给大家整个活叫哔哩哔哩gp t 6.71 - 选择插着gp t的爆火 作为软件工程师的我也按捺不住 去需要把哔哩哔哩的url贴进来 21.04 - 然后你就点击一键总结 稍等片刻 你就可以获得这样一份精简的总结`,
output: `视频概述:BiliGPT 是一款自动总结B站视频内容的 AI 工具
- 2.06 - 作为软件工程师的我按捺不住去开发了 BiliGPT
- 21.04 - 只需要粘贴哔哩哔哩的URL,一键总结为精简内容`,
};
}

export function getSystemPrompt(promptConfig: PromptConfig) {
// [gpt-3-youtube-summarizer/main.py at main · tfukaza/gpt-3-youtube-summarizer](https://github.com/tfukaza/gpt-3-youtube-summarizer/blob/main/main.py)
console.log("prompt config: ", promptConfig);
Expand All @@ -34,7 +45,7 @@ export function getSystemPrompt(promptConfig: PromptConfig) {
const betterPrompt = `I want you to act as an educational content creator. You will help students summarize the essence of the video in ${enLanguage}. Please summarize the video subtitles (there may be typos in the subtitles, please correct them) and return them in an unordered list format. Please do not exceed ${sentenceCount} items, and make sure not to repeat any sentences and all sentences are concise, clear, and complete. Good luck!`;
// const timestamp = ' ' //`(类似 10:24)`;
// 我希望你是一名专业的视频内容编辑,帮我用${language}总结视频的内容精华。请先用一句简短的话总结视频梗概。然后再请你将视频字幕文本进行总结(字幕中可能有错别字,如果你发现了错别字请改正),在每句话的最前面加上时间戳${timestamp},每句话开头只需要一个开始时间。请你以无序列表的方式返回,请注意不要超过5条哦,确保所有的句子都足够精简,清晰完整,祝你好运!
const promptWithTimestamp = `I want you to act as an educational content creator. You will help students summarize the essence of the video in ${enLanguage}. Please start by summarizing the whole video in one short sentence. Then, please summarize the video subtitles in an unordered list format, you should add the start timestamp (e.g. 403 -) at the beginning of each sentence so that students can jump to the source of the video. Please make sure not to exceed ${sentenceCount} items and all sentences are concise, clear, and complete. Good luck!`;
const promptWithTimestamp = `I would like you to act as a professional video content editor. You will help students summarize the essence of the video in ${enLanguage}. Please start by summarizing the whole video in one short sentence (there may be typos in the subtitles, so please correct them if you find them). Then, please summarize the video subtitles, each subtitle should has the start timestamp (e.g. 12.4 -) so that students can select the video part. Please return in an unordered list format, make sure not to exceed ${sentenceCount} items and all sentences are concise, clear, and complete. Good luck!`;

return shouldShowTimestamp ? promptWithTimestamp : betterPrompt;
}
Expand Down
18 changes: 14 additions & 4 deletions pages/api/sumup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ import { Redis } from "@upstash/redis";
import type { NextFetchEvent, NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { fetchSubtitle } from "~/lib/fetchSubtitle";
import { ChatGPTAgent, fetchOpenAIResult } from "~/lib/openai/fetchOpenAIResult";
import {
ChatGPTAgent,
fetchOpenAIResult,
} from "~/lib/openai/fetchOpenAIResult";
import { getSmallSizeTranscripts } from "~/lib/openai/getSmallSizeTranscripts";
import { getSystemPrompt, getUserSubtitlePrompt } from "~/lib/openai/prompt";
import {
getExamplePrompt,
getSystemPrompt,
getUserSubtitlePrompt,
} from "~/lib/openai/prompt";
import { selectApiKeyAndActivatedLicenseKey } from "~/lib/openai/selectApiKeyAndActivatedLicenseKey";
import { SummarizeParams } from "~/lib/types";
import { isDev } from "~/utils/env";
Expand Down Expand Up @@ -42,9 +49,11 @@ export default async function handler(
const systemPrompt = getSystemPrompt({
shouldShowTimestamp: subtitlesArray ? shouldShowTimestamp : false,
});
const examplePrompt = getExamplePrompt();
const userPrompt = getUserSubtitlePrompt(title, inputText);
if (isDev) {
console.log("final system prompt: ", systemPrompt);
console.log("final example prompt: ", examplePrompt);
console.log("final user prompt: ", userPrompt);
}

Expand All @@ -56,8 +65,8 @@ export default async function handler(
role: ChatGPTAgent.system,
content: systemPrompt,
},
// {"role": "user", "content": "谁赢得了2020年的世界职业棒球大赛?"},
// {"role": "assistant", "content": "洛杉矶道奇队在2020年赢得了世界职业棒球大赛冠军。"},
{ role: ChatGPTAgent.user, content: examplePrompt.input },
{ role: ChatGPTAgent.assistant, content: examplePrompt.output },
{ role: ChatGPTAgent.user, content: userPrompt },
],
temperature: 0.5,
Expand All @@ -80,6 +89,7 @@ export default async function handler(
const cacheId = shouldShowTimestamp ? `timestamp-${videoId}` : videoId;
const data = await redis.set(cacheId, result);
console.info(`video ${cacheId} cached:`, data);
isDev && console.log("========result========", result);

return NextResponse.json(result);
} catch (error: any) {
Expand Down
8 changes: 3 additions & 5 deletions utils/reduceSubtitleTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export function reduceSubtitleTimestamp<T>(
): Array<CommonSubtitleItem> {
// 把字幕数组总共分成 20 组
const TOTAL_GROUP_COUNT = 30;
// 如果字幕不够多,就每7句话合并一下
const MINIMUM_COUNT_ONE_GROUP = 7;
// 如果字幕不够多,就每3句话合并一下
const MINIMUM_COUNT_ONE_GROUP = 3;
const eachGroupCount =
subtitles.length > TOTAL_GROUP_COUNT
? subtitles.length / TOTAL_GROUP_COUNT
Expand All @@ -52,9 +52,7 @@ export function reduceSubtitleTimestamp<T>(
accumulator[groupIndex] = {
// 5.88 -> 5.9
// text: current.start.toFixed() + ": ",
text: shouldShowTimestamp
? trimSeconds(getStart(current)) + "s - "
: "",
text: shouldShowTimestamp ? getStart(current) + " - " : "",
index: groupIndex,
};
}
Expand Down

1 comment on commit 933b735

@vercel
Copy link

@vercel vercel bot commented on 933b735 Mar 12, 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.