Skip to content

Commit

Permalink
feat: activate licenseKey after used
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Mar 5, 2023
1 parent 3695bf8 commit 682d0c2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
29 changes: 4 additions & 25 deletions lib/openai/OpenAIResult.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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),
Expand Down
7 changes: 7 additions & 0 deletions lib/openai/formatResult.ts
Original file line number Diff line number Diff line change
@@ -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;
}
23 changes: 23 additions & 0 deletions lib/openai/selectApiKey.ts
Original file line number Diff line number Diff line change
@@ -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 || "";
}
10 changes: 8 additions & 2 deletions pages/api/summarize.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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();
Expand Down

1 comment on commit 682d0c2

@vercel
Copy link

@vercel vercel bot commented on 682d0c2 Mar 5, 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.