Skip to content

Commit

Permalink
feat: support multiple user api keys
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Mar 5, 2023
1 parent aa5f59a commit 31d96cd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
24 changes: 14 additions & 10 deletions utils/OpenAIResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ParsedEvent,
ReconnectInterval,
} from "eventsource-parser";
import { checkOpenaiApiKey } from "./3rd/openai";
import { checkOpenaiApiKey, checkOpenaiApiKeys } from "./3rd/openai";
import { sample } from "./fp";

// TODO: maybe chat with video?
Expand Down Expand Up @@ -34,21 +34,25 @@ function formatResult(result: any) {
return answer;
}

function selectApiKey(apiKey: string | undefined) {
if (apiKey && checkOpenaiApiKeys(apiKey)) {
const userApiKeys = apiKey.split(",");
return sample(userApiKeys);
}

// don't need to validate anymore, already verified in middleware?
const myApiKeyList = process.env.OPENAI_API_KEY;
const luckyApiKey = sample(myApiKeyList?.split(","));
return luckyApiKey || "";
}

export async function OpenAIResult(
payload: OpenAIStreamPayload,
apiKey?: string
) {
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const myApiKeyList = process.env.OPENAI_API_KEY;
const luckyApiKey = sample(myApiKeyList?.split(","));
const openai_api_key = checkOpenaiApiKey(apiKey || "") ? apiKey : luckyApiKey || "";

/* // don't need to validate anymore, already verified in middleware
if (!checkOpenaiApiKey(openai_api_key)) {
throw new Error("OpenAI API Key Format Error");
}
*/
const openai_api_key = selectApiKey(apiKey);

const res = await fetch("https://api.openai.com/v1/chat/completions", {
headers: {
Expand Down
4 changes: 2 additions & 2 deletions utils/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

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

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

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

1 comment on commit 31d96cd

@vercel
Copy link

@vercel vercel bot commented on 31d96cd Mar 5, 2023

Choose a reason for hiding this comment

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

Please # to comment.