diff --git a/lib/openai/OpenAIResult.ts b/lib/openai/OpenAIResult.ts index 3f9db214..f7dfaa69 100644 --- a/lib/openai/OpenAIResult.ts +++ b/lib/openai/OpenAIResult.ts @@ -1,6 +1,6 @@ import { createParser, ParsedEvent, ReconnectInterval } from "eventsource-parser"; -import { checkOpenaiApiKeys } from "~/lib/openai/openai"; -import { sample } from "../../utils/fp"; +import { formatResult } from "~/lib/openai/formatResult"; +import { selectApiKey } from "~/lib/openai/selectApiKey"; // TODO: maybe chat with video? export type ChatGPTAgent = "user" | "system" | "assistant"; @@ -22,38 +22,17 @@ export interface OpenAIStreamPayload { n: number; } -function formatResult(result: any) { - const answer = result.choices[0].message?.content || ""; - if (answer.startsWith("\n\n")) { - return answer.substring(2); - } - 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 + apiKey: string ) { const encoder = new TextEncoder(); const decoder = new TextDecoder(); - const openai_api_key = selectApiKey(apiKey); const res = await fetch("https://api.openai.com/v1/chat/completions", { headers: { "Content-Type": "application/json", - Authorization: `Bearer ${openai_api_key ?? ""}`, + Authorization: `Bearer ${apiKey ?? ""}`, }, method: "POST", body: JSON.stringify(payload), diff --git a/lib/openai/formatResult.ts b/lib/openai/formatResult.ts new file mode 100644 index 00000000..904d9fc0 --- /dev/null +++ b/lib/openai/formatResult.ts @@ -0,0 +1,7 @@ +export function formatResult(result: any) { + const answer = result.choices[0].message?.content || ""; + if (answer.startsWith("\n\n")) { + return answer.substring(2); + } + return answer; +} diff --git a/lib/openai/selectApiKey.ts b/lib/openai/selectApiKey.ts new file mode 100644 index 00000000..379e9cdb --- /dev/null +++ b/lib/openai/selectApiKey.ts @@ -0,0 +1,23 @@ +import { activateLicenseKey } from "~/lib/lemon"; +import { checkOpenaiApiKeys } from "~/lib/openai/openai"; +import { sample } from "~/utils/fp"; + +export async function selectApiKey(apiKey: string | undefined) { + if (apiKey) { + if (checkOpenaiApiKeys(apiKey)) { + const userApiKeys = apiKey.split(","); + return sample(userApiKeys); + } + + // user is using validated licenseKey + const activated = await activateLicenseKey(apiKey); + if (!activated) { + throw new Error("licenseKey is not validated!"); + } + } + + // don't need to validate anymore, already verified in middleware? + const myApiKeyList = process.env.OPENAI_API_KEY; + const luckyApiKey = sample(myApiKeyList?.split(",")); + return luckyApiKey || ""; +} diff --git a/pages/api/summarize.ts b/pages/api/summarize.ts index caa0de7f..3bf06e3d 100644 --- a/pages/api/summarize.ts +++ b/pages/api/summarize.ts @@ -1,10 +1,15 @@ import { Redis } from "@upstash/redis"; import type { NextFetchEvent, NextRequest } from "next/server"; import { NextResponse } from "next/server"; +import { activateLicenseKey } from "~/lib/lemon"; +import { selectApiKey } from "~/lib/openai/selectApiKey"; import { fetchSubtitle } from "../../lib/bilibili"; import { isDev } from "../../utils/env"; import { OpenAIResult } from "../../lib/openai/OpenAIResult"; -import { getChunckedTranscripts, getSummaryPrompt } from "../../lib/openai/prompt"; +import { + getChunckedTranscripts, + getSummaryPrompt, +} from "../../lib/openai/prompt"; export const config = { runtime: "edge", @@ -57,7 +62,8 @@ export default async function handler( n: 1, }; - const result = await OpenAIResult(payload, apiKey); + const openaiApiKey = await selectApiKey(apiKey); + const result = await OpenAIResult(payload, openaiApiKey); // TODO: add better logging when dev or prod console.log("result", result); const redis = Redis.fromEnv();